智慧食堂-队列消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_shipping_template.go 10 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package svc
  2. import (
  3. commDb "applet/app/db"
  4. "applet/app/svc"
  5. "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/supply/db"
  8. "applet/supply/db/model"
  9. "applet/supply/md"
  10. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/jinzhu/copier"
  14. "github.com/shopspring/decimal"
  15. "strings"
  16. )
  17. func SaveMallShippingTemplate(c *gin.Context, args md.MallShippingTemplateReq) (*model.MallShippingTemplate, error) {
  18. decimal.DivisionPrecision = 2
  19. var (
  20. shippingTemplate model.MallShippingTemplate
  21. err error
  22. )
  23. eg := svc.MasterDb(c)
  24. copier.Copy(&shippingTemplate, &args)
  25. // 区域校验是否出现重复区域
  26. if args.Data != nil && len(args.Data) > 0 {
  27. var regions = make(map[string]string)
  28. var provinces = make(map[string]string)
  29. var city = make(map[string]string)
  30. for _, item := range args.Data {
  31. // 判断首件/首重不能为0
  32. if item.FirstAmount == "" || item.FirstAmount == "0" {
  33. return nil, errors.New("首件/重不能小于0")
  34. }
  35. // 判断价格是否合法
  36. _, err := decimal.NewFromString(item.AdditionalFee)
  37. if err != nil {
  38. return nil, err
  39. }
  40. _, err = decimal.NewFromString(item.FirstFee)
  41. if err != nil {
  42. return nil, err
  43. }
  44. for _, itm := range item.Regions {
  45. // 判断是否重复出现
  46. if itm == "110000000000" {
  47. fmt.Println(itm)
  48. }
  49. if _, ok := regions[itm]; ok {
  50. return nil, errors.New("不能重复选择区域")
  51. } else {
  52. regions[itm] = "1"
  53. }
  54. // 判断是否是省,如果选择省的话表示省下的所有城市及区都应为使用同一条运费价格计算
  55. if len(itm) < 2 {
  56. continue
  57. }
  58. if itm[2:] == "0000000000" {
  59. if _, ok := provinces[itm[:2]]; ok {
  60. return nil, errors.New("不能重复选择区域")
  61. } else {
  62. provinces[itm[:2]] = "1"
  63. }
  64. }
  65. // 判断是否是市,如果选择市的话表示省下的所有城市及区都应为使用同一条运费价格计算
  66. if itm[2:4] != "00" && itm[4:] == "00000000" {
  67. if _, ok := city[itm[:4]]; ok {
  68. return nil, errors.New("不能重复选择区域")
  69. } else {
  70. city[itm[:4]] = "1"
  71. }
  72. }
  73. }
  74. }
  75. regions = nil
  76. provinces = nil
  77. city = nil
  78. }
  79. shippingTemplate.Data = utils.SerializeStr(args.Data)
  80. if args.Id == 0 {
  81. err = db.InsertOneMallShippingTemplate(eg, &shippingTemplate)
  82. } else {
  83. err = db.UpdateMallShippingTemplateById(eg, &shippingTemplate, "name", "data", "calculate_type")
  84. }
  85. if err != nil {
  86. return nil, err
  87. }
  88. return &shippingTemplate, nil
  89. }
  90. func CloudChainGoodsShippingTemChangeMallShippingTem(c *gin.Context, shippingInfo map[string]interface{}, id string, merchantId int) (*model.MallShippingTemplate, error) {
  91. if shippingInfo == nil {
  92. return nil, logx.Error("错误的数据")
  93. }
  94. var mallShippingTem model.MallShippingTemplate
  95. type RateDTO struct {
  96. FirstUnit int `json:"firstUnit" label:"首重(单位:克)或首件(单位:件)"` //首重(单位:克)或首件(单位:件)
  97. FirstUnitFee int `json:"firstUnitFee" label:"首重或首件的价格(单位:分)"` // 首重或首件的价格(单位:分)
  98. NextUnit int `json:"nextUnit" label:"续重件单位"` //续重件单位
  99. NextUnitFee int `json:"nextUnitFee" label:"重件价格(单位:分)"` //重件价格(单位:分)
  100. }
  101. type SubTemplateDTO struct {
  102. ChargeType int `json:"chargeType" label:"计件类型。0:重量 1:件数 2:体积"` //计件类型。0:重量 1:件数 2:体积
  103. IsSysTemplate bool `json:"isSysTemplate" label:"是否系统模板"` //是否系统模板
  104. ServiceType int `json:"serviceType" label:"服务类型。0:快递 1:货运 2:货到付款"` //服务类型。0:快递 1:货运 2:货到付款
  105. Type int `json:"type" label:"子模板类型 0基准 1增值。默认0。"` //子模板类型 0基准 1增值。默认0。
  106. }
  107. type Rate struct {
  108. IsSysRate bool `json:"isSysRate" label:"是否系统模板"` //是否系统模板
  109. RateDTO RateDTO `json:"rateDTO" label:"普通子模板费率"` //普通子模板费率
  110. ToAreaCodeText string `json:"toAreaCodeText" label:"地址编码文本,用顿号隔开。例如:上海、福建省、广东省"` //地址编码文本,用顿号隔开。例如:上海、福建省、广东省
  111. }
  112. type ExpressSubTemplate struct {
  113. RateList []Rate `json:"rateList" label:"费率"` //费率
  114. SubTemplateDTO SubTemplateDTO `json:"subTemplateDTO" label:"子模板"` //子模板
  115. }
  116. type FreightTemplate struct {
  117. AddressCodeText string `json:"addressCodeText" label:"地址区域编码对应的文本(包括省市区,用空格隔开)"` //地址区域编码对应的文本(包括省市区,用空格隔开)
  118. ExpressSubTemplate ExpressSubTemplate `json:"expressSubTemplate" label:"快递子模版"` //快递子模版
  119. FromAreaCode string `json:"fromAreaCode" label:"发货地址地区码"` //发货地址地区码
  120. ID int `json:"id" label:"运费模板ID"` //运费模板ID
  121. }
  122. type ShippingInfo struct {
  123. FreightTemplate []FreightTemplate `json:"freightTemplate" label:"商品运费费率"` //商品运费费率
  124. FreightTemplateID int `json:"freightTemplateID" label:"运费模板ID,0表示运费说明,1表示卖家承担运费,其他值表示使用运费模版。此参数可调用运费模板相关API获取"` //运费模板ID,0表示运费说明,1表示卖家承担运费,其他值表示使用运费模版。此参数可调用运费模板相关API获取
  125. SendGoodsAddressID int `json:"sendGoodsAddressId" label:"发货地址ID"` //发货地址ID
  126. SendGoodsAddressText string `json:"sendGoodsAddressText" label:"发货地描述"` //发货地描述
  127. UnitWeight float64 `json:"unitWeight" label:"重量/毛重"` //重量/毛重
  128. }
  129. var ShippingInfoStruct ShippingInfo
  130. utils.Unserialize([]byte(utils.SerializeStr(shippingInfo)), &ShippingInfoStruct)
  131. if ShippingInfoStruct.FreightTemplateID > 1 {
  132. mallShippingTem.CloudChainTemId = utils.AnyToString(ShippingInfoStruct.FreightTemplateID)
  133. mallShippingTem.MerchantId = merchantId
  134. get, err := mallShippingTem.GetV2(svc.MasterDb(c))
  135. if err != nil {
  136. return nil, err
  137. }
  138. if get {
  139. return &mallShippingTem, nil
  140. }
  141. for _, template1688 := range ShippingInfoStruct.FreightTemplate {
  142. var (
  143. req md.MallShippingTemplateReq
  144. unitRate int
  145. )
  146. if template1688.ExpressSubTemplate.SubTemplateDTO.ChargeType == 0 {
  147. req.CalculateType = 1
  148. unitRate = 1000
  149. } else if template1688.ExpressSubTemplate.SubTemplateDTO.ChargeType == 1 {
  150. req.CalculateType = 2
  151. unitRate = 1
  152. }
  153. req.Name = utils.AnyToString(req.MerchantId) + "云链产品" + id + "运费模版" + utils.AnyToString(template1688.ID)
  154. req.CloudChainTemId = utils.AnyToString(template1688.ID)
  155. req.MerchantId = merchantId
  156. toAreaCode := make(map[string]string)
  157. for _, rate := range template1688.ExpressSubTemplate.RateList {
  158. var mallShippingTemplateData md.MallShippingTemplateData
  159. mallShippingTemplateData.FirstAmount = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.FirstUnit) / utils.AnyToFloat64(unitRate))
  160. mallShippingTemplateData.FirstFee = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.FirstUnitFee) / 100)
  161. mallShippingTemplateData.AdditionalAmount = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.NextUnit) / utils.AnyToFloat64(unitRate))
  162. mallShippingTemplateData.AdditionalFee = utils.AnyToString(utils.AnyToFloat64(rate.RateDTO.NextUnitFee) / 100)
  163. if rate.ToAreaCodeText == "" || rate.ToAreaCodeText == " " {
  164. continue
  165. }
  166. ToAreaCodes := strings.Split(rate.ToAreaCodeText, "、")
  167. for _, code := range ToAreaCodes {
  168. if code == "" || code == " " {
  169. continue
  170. }
  171. if _, ok := toAreaCode[code]; ok {
  172. continue
  173. } else {
  174. toAreaCode[code] = "1"
  175. }
  176. provinceId, err := GetProvinceByName(code)
  177. if err != nil {
  178. logx.Error(err)
  179. continue
  180. }
  181. mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, provinceId)
  182. //市
  183. cities, err := GetCitiesByProvinceId(provinceId)
  184. if err != nil {
  185. logx.Error(err)
  186. continue
  187. }
  188. mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, cities...)
  189. //县
  190. counties, err := GetCountyByCitiesIds(cities)
  191. if err != nil {
  192. logx.Error(err)
  193. continue
  194. }
  195. mallShippingTemplateData.Regions = append(mallShippingTemplateData.Regions, counties...)
  196. }
  197. if len(mallShippingTemplateData.Regions) == 0 {
  198. continue
  199. }
  200. req.Data = append(req.Data, mallShippingTemplateData)
  201. }
  202. template, err := SaveMallShippingTemplate(c, req)
  203. if err != nil || template == nil {
  204. logx.Error("同步1688运费模版保存出错", err)
  205. return nil, err
  206. }
  207. if ShippingInfoStruct.FreightTemplateID == template1688.ID {
  208. mallShippingTem = *template
  209. }
  210. }
  211. } else if ShippingInfoStruct.FreightTemplateID == 1 {
  212. mallShippingTem.Id = 1
  213. mallShippingTem.CloudChainTemId = "1"
  214. } else if ShippingInfoStruct.FreightTemplateID == 0 {
  215. mallShippingTem.Id = 0
  216. mallShippingTem.CloudChainTemId = "1"
  217. }
  218. return &mallShippingTem, nil
  219. }
  220. func GetProvinceByName(name string) (string, error) {
  221. id := make([]string, 0)
  222. err := commDb.Db.Table("province").Select("id").Where("name LIKE ?", name+"%").Find(&id)
  223. if len(id) > 0 {
  224. if id[0] == "110000000000" {
  225. fmt.Println(name)
  226. }
  227. return id[0], err
  228. }
  229. return "", err
  230. }
  231. func GetCitiesByProvinceId(provinceId string) ([]string, error) {
  232. id := make([]string, 0)
  233. err := commDb.Db.Table("city").Select("id").Where("province_id = ?", provinceId).Find(&id)
  234. return id, err
  235. }
  236. func GetCountyByCitiesIds(citiesIds []string) ([]string, error) {
  237. id := make([]string, 0)
  238. err := commDb.Db.Table("county").Select("id").In("city_id", citiesIds).Find(&id)
  239. return id, err
  240. }