|
- package svc
-
- import (
- commDb "applet/app/db"
- "applet/app/svc"
- "applet/app/utils"
- "applet/app/utils/logx"
- "applet/supply/db"
- "applet/supply/db/model"
- "applet/supply/md"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- "github.com/shopspring/decimal"
- "strings"
- )
-
- func SaveMallShippingTemplate(c *gin.Context, args md.MallShippingTemplateReq) (*model.MallShippingTemplate, error) {
- decimal.DivisionPrecision = 2
- var (
- shippingTemplate model.MallShippingTemplate
- err error
- )
- eg := svc.MasterDb(c)
- copier.Copy(&shippingTemplate, &args)
- // 区域校验是否出现重复区域
- if args.Data != nil && len(args.Data) > 0 {
- var regions = make(map[string]string)
- var provinces = make(map[string]string)
- var city = make(map[string]string)
- for _, item := range args.Data {
- // 判断首件/首重不能为0
- if item.FirstAmount == "" || item.FirstAmount == "0" {
- return nil, errors.New("首件/重不能小于0")
- }
- // 判断价格是否合法
- _, err := decimal.NewFromString(item.AdditionalFee)
- if err != nil {
- return nil, err
- }
- _, err = decimal.NewFromString(item.FirstFee)
- if err != nil {
- return nil, err
- }
- for _, itm := range item.Regions {
- // 判断是否重复出现
- if itm == "110000000000" {
- fmt.Println(itm)
- }
- if _, ok := regions[itm]; ok {
- return nil, errors.New("不能重复选择区域")
- } else {
- regions[itm] = "1"
- }
- // 判断是否是省,如果选择省的话表示省下的所有城市及区都应为使用同一条运费价格计算
- if len(itm) < 2 {
- continue
- }
- if itm[2:] == "0000000000" {
- if _, ok := provinces[itm[:2]]; ok {
- return nil, errors.New("不能重复选择区域")
- } else {
- provinces[itm[:2]] = "1"
- }
- }
- // 判断是否是市,如果选择市的话表示省下的所有城市及区都应为使用同一条运费价格计算
- if itm[2:4] != "00" && itm[4:] == "00000000" {
- if _, ok := city[itm[:4]]; ok {
- return nil, errors.New("不能重复选择区域")
- } else {
- city[itm[:4]] = "1"
- }
- }
- }
- }
- regions = nil
- provinces = nil
- city = nil
- }
- shippingTemplate.Data = utils.SerializeStr(args.Data)
- if args.Id == 0 {
- err = db.InsertOneMallShippingTemplate(eg, &shippingTemplate)
- } else {
- err = db.UpdateMallShippingTemplateById(eg, &shippingTemplate, "name", "data", "calculate_type")
- }
- if err != nil {
- return nil, err
- }
- return &shippingTemplate, nil
- }
-
- func CloudChainGoodsShippingTemChangeMallShippingTem(c *gin.Context, shippingInfo map[string]interface{}, id string, merchantId int) (*model.MallShippingTemplate, error) {
- if shippingInfo == nil {
- return nil, logx.Error("错误的数据")
- }
- var mallShippingTem model.MallShippingTemplate
- type RateDTO struct {
- FirstUnit int `json:"firstUnit" label:"首重(单位:克)或首件(单位:件)"` //首重(单位:克)或首件(单位:件)
- FirstUnitFee int `json:"firstUnitFee" label:"首重或首件的价格(单位:分)"` // 首重或首件的价格(单位:分)
- NextUnit int `json:"nextUnit" label:"续重件单位"` //续重件单位
- NextUnitFee int `json:"nextUnitFee" label:"重件价格(单位:分)"` //重件价格(单位:分)
- }
- type SubTemplateDTO struct {
- ChargeType int `json:"chargeType" label:"计件类型。0:重量 1:件数 2:体积"` //计件类型。0:重量 1:件数 2:体积
- IsSysTemplate bool `json:"isSysTemplate" label:"是否系统模板"` //是否系统模板
- ServiceType int `json:"serviceType" label:"服务类型。0:快递 1:货运 2:货到付款"` //服务类型。0:快递 1:货运 2:货到付款
- Type int `json:"type" label:"子模板类型 0基准 1增值。默认0。"` //子模板类型 0基准 1增值。默认0。
- }
- type Rate struct {
- IsSysRate bool `json:"isSysRate" label:"是否系统模板"` //是否系统模板
- RateDTO RateDTO `json:"rateDTO" label:"普通子模板费率"` //普通子模板费率
- ToAreaCodeText string `json:"toAreaCodeText" label:"地址编码文本,用顿号隔开。例如:上海、福建省、广东省"` //地址编码文本,用顿号隔开。例如:上海、福建省、广东省
- }
- type ExpressSubTemplate struct {
- RateList []Rate `json:"rateList" label:"费率"` //费率
- SubTemplateDTO SubTemplateDTO `json:"subTemplateDTO" label:"子模板"` //子模板
- }
- type FreightTemplate struct {
- AddressCodeText string `json:"addressCodeText" label:"地址区域编码对应的文本(包括省市区,用空格隔开)"` //地址区域编码对应的文本(包括省市区,用空格隔开)
- ExpressSubTemplate ExpressSubTemplate `json:"expressSubTemplate" label:"快递子模版"` //快递子模版
- FromAreaCode string `json:"fromAreaCode" label:"发货地址地区码"` //发货地址地区码
- ID int `json:"id" label:"运费模板ID"` //运费模板ID
- }
- type ShippingInfo struct {
- FreightTemplate []FreightTemplate `json:"freightTemplate" label:"商品运费费率"` //商品运费费率
- FreightTemplateID int `json:"freightTemplateID" label:"运费模板ID,0表示运费说明,1表示卖家承担运费,其他值表示使用运费模版。此参数可调用运费模板相关API获取"` //运费模板ID,0表示运费说明,1表示卖家承担运费,其他值表示使用运费模版。此参数可调用运费模板相关API获取
- SendGoodsAddressID int `json:"sendGoodsAddressId" label:"发货地址ID"` //发货地址ID
- SendGoodsAddressText string `json:"sendGoodsAddressText" label:"发货地描述"` //发货地描述
- UnitWeight float64 `json:"unitWeight" label:"重量/毛重"` //重量/毛重
- }
- var ShippingInfoStruct ShippingInfo
- utils.Unserialize([]byte(utils.SerializeStr(shippingInfo)), &ShippingInfoStruct)
- if ShippingInfoStruct.FreightTemplateID > 1 {
- mallShippingTem.CloudChainTemId = utils.AnyToString(ShippingInfoStruct.FreightTemplateID)
- mallShippingTem.MerchantId = merchantId
- get, err := mallShippingTem.GetV2(svc.MasterDb(c))
- if err != nil {
- return nil, err
- }
- if get {
- return &mallShippingTem, nil
- }
- for _, template1688 := range ShippingInfoStruct.FreightTemplate {
- var (
- req md.MallShippingTemplateReq
- unitRate int
- )
- if template1688.ExpressSubTemplate.SubTemplateDTO.ChargeType == 0 {
- req.CalculateType = 1
- unitRate = 1000
- } else if template1688.ExpressSubTemplate.SubTemplateDTO.ChargeType == 1 {
- req.CalculateType = 2
- unitRate = 1
- }
- req.Name = utils.AnyToString(req.MerchantId) + "云链产品" + id + "运费模版" + utils.AnyToString(template1688.ID)
- req.CloudChainTemId = utils.AnyToString(template1688.ID)
- req.MerchantId = merchantId
- toAreaCode := make(map[string]string)
- for _, rate := range template1688.ExpressSubTemplate.RateList {
- var mallShippingTemplateData md.MallShippingTemplateData
- mallShippingTemplateData.FirstAmount = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.FirstUnit) / utils.AnyToFloat64(unitRate))
- mallShippingTemplateData.FirstFee = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.FirstUnitFee) / 100)
- mallShippingTemplateData.AdditionalAmount = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.NextUnit) / utils.AnyToFloat64(unitRate))
- mallShippingTemplateData.AdditionalFee = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.NextUnitFee) / 100)
- if rate.ToAreaCodeText == "" || rate.ToAreaCodeText == " " {
- continue
- }
- ToAreaCodes := strings.Split(rate.ToAreaCodeText, "、")
- for _, code := range ToAreaCodes {
- if code == "" || code == " " {
- continue
- }
- if _, ok := toAreaCode[code]; ok {
- continue
- } else {
- toAreaCode[code] = "1"
- }
- provinceId, err := GetProvinceByName(code)
- if err != nil {
- logx.Error(err)
- continue
- }
- mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, provinceId)
- //市
- cities, err := GetCitiesByProvinceId(provinceId)
- if err != nil {
- logx.Error(err)
- continue
- }
- mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, cities...)
- //县
- counties, err := GetCountyByCitiesIds(cities)
- if err != nil {
- logx.Error(err)
- continue
- }
- mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, counties...)
- }
- if len(mallShippingTemplateData.Regions) == 0 {
- continue
- }
- req.Data = append(req.Data, mallShippingTemplateData)
- }
- template, err := SaveMallShippingTemplate(c, req)
- if err != nil || template == nil {
- logx.Error("同步1688运费模版保存出错", err)
- return nil, err
- }
- if ShippingInfoStruct.FreightTemplateID == template1688.ID {
- mallShippingTem = *template
- }
- }
- } else if ShippingInfoStruct.FreightTemplateID == 1 {
- mallShippingTem.Id = 1
- mallShippingTem.CloudChainTemId = "1"
- } else if ShippingInfoStruct.FreightTemplateID == 0 {
- mallShippingTem.Id = 0
- mallShippingTem.CloudChainTemId = "1"
- }
- return &mallShippingTem, nil
- }
-
- func GetProvinceByName(name string) (string, error) {
- id := make([]string, 0)
- err := commDb.Db.Table("province").Select("id").Where("name LIKE ?", name+"%").Find(&id)
- if len(id) > 0 {
- if id[0] == "110000000000" {
- fmt.Println(name)
- }
- return id[0], err
- }
-
- return "", err
- }
-
- func GetCitiesByProvinceId(provinceId string) ([]string, error) {
- id := make([]string, 0)
- err := commDb.Db.Table("city").Select("id").Where("province_id = ?", provinceId).Find(&id)
- return id, err
- }
- func GetCountyByCitiesIds(citiesIds []string) ([]string, error) {
- id := make([]string, 0)
- err := commDb.Db.Table("county").Select("id").In("city_id", citiesIds).Find(&id)
- return id, err
- }
|