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.
243 lines
5.9 KiB
243 lines
5.9 KiB
package freight
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/freight"
|
|
"recook/tools"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type addFreightParam struct {
|
|
addFreeShippingParam
|
|
Provinces []deliveryProvince `json:"provinces" validate:"required"`
|
|
}
|
|
|
|
type deliveryProvince struct {
|
|
freight.Price
|
|
Children []string `json:"children"`
|
|
}
|
|
|
|
type addFreeShippingParam struct {
|
|
Name string `json:"name" validate:"required"`
|
|
ShippingAddr string `json:"shippingAddr" validate:"required"`
|
|
NoProvinces []string `json:"noProvinces"`
|
|
GysId int `json:"gys_id"`
|
|
}
|
|
|
|
func CreateNumber(c *gin.Context) {
|
|
gys_token := c.Request.Header.Get("X-Recook-GYSToken")
|
|
gysId, _ := dbc.Rds.Get(gys_token).Result()
|
|
myGysId, _ := strconv.Atoi(gysId)
|
|
|
|
var p addFreightParam
|
|
err := tools.Params(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
err = addFreight(p, 0, 0, myGysId)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", nil)
|
|
}
|
|
|
|
func CreateWeight(c *gin.Context) {
|
|
gys_token := c.Request.Header.Get("X-Recook-GYSToken")
|
|
gysId, _ := dbc.Rds.Get(gys_token).Result()
|
|
myGysId, _ := strconv.Atoi(gysId)
|
|
|
|
var p addFreightParam
|
|
err := tools.Params(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
err = addFreight(p, 0, 1, myGysId)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
back.Suc(c, "操作成功", nil)
|
|
}
|
|
|
|
// @Style 全国包邮模版
|
|
func AddFreeShipping(c *gin.Context) {
|
|
gys_token := c.Request.Header.Get("X-Recook-GYSToken")
|
|
gysId, _ := dbc.Rds.Get(gys_token).Result()
|
|
myGysId, _ := strconv.Atoi(gysId)
|
|
p := addFreightParam{}
|
|
err := tools.Params(&p.addFreeShippingParam, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
err = addFreight(p, 1, 0, myGysId)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", nil)
|
|
return
|
|
}
|
|
|
|
func checkParam(p *addFreightParam) (bool, string) {
|
|
|
|
return true, ""
|
|
}
|
|
|
|
// @Style 添加模版
|
|
// @Param isFee uint true "是否包运费"
|
|
// @Param priceType uint true "运费计算类型 0=按件 1=按重"
|
|
func addFreight(p addFreightParam, isFee, priceType uint, myGysId int) (err error) {
|
|
noProvinces := map[string]int{}
|
|
noCitys := []freight.NoCitys{}
|
|
for _, noProvince := range p.NoProvinces {
|
|
split := strings.Split(noProvince, "-")
|
|
splitLen := len(split)
|
|
if splitLen == 1 {
|
|
noProvinces[split[0]] = 1
|
|
} else if splitLen == 2 {
|
|
noCitys = append(noCitys, freight.NoCitys{ProvinceName: split[0], CityName: split[1]})
|
|
}
|
|
}
|
|
|
|
tx := dbc.DB.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
} else if err != nil {
|
|
tx.Rollback()
|
|
} else {
|
|
tx.Commit()
|
|
}
|
|
}()
|
|
{
|
|
|
|
info := freight.Information{
|
|
Name: p.Name,
|
|
AllFree: isFee,
|
|
ShippingAddr: p.ShippingAddr,
|
|
GysId: myGysId,
|
|
}
|
|
err = tx.Create(&info).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
if len(p.NoProvinces) > 0 {
|
|
// 不发货数据处理
|
|
if len(noProvinces) > 0 {
|
|
valueStr := ""
|
|
values := []interface{}{}
|
|
for province, _ := range noProvinces {
|
|
valueStr += ",(?,?)"
|
|
values = append(values, info.ID, province)
|
|
}
|
|
err = tx.Exec("INSERT INTO recook_freight_no_provinces(freight_id,province_name) VALUES "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
if len(noCitys) > 0 {
|
|
valueStr := ""
|
|
values := []interface{}{}
|
|
for _, city := range noCitys {
|
|
if _, ok := noProvinces[city.ProvinceName]; ok {
|
|
continue
|
|
}
|
|
valueStr += ",(?,?,?)"
|
|
values = append(values, info.ID, city.ProvinceName, city.CityName)
|
|
}
|
|
if len(values) > 0 {
|
|
err = tx.Exec("INSERT INTO recook_freight_no_citys(freight_id,province_name,city_name) VALUES "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 发货数据处理
|
|
for _, v := range p.Provinces {
|
|
if len(v.Children) == 0 {
|
|
return errors.New("发货省份存在空数值")
|
|
}
|
|
price := freight.Price{}
|
|
if priceType == 0 {
|
|
price = freight.Price{
|
|
FreightID: info.ID,
|
|
Type: 0,
|
|
FirstNumber: v.FirstNumber,
|
|
FirstNumberFee: v.FirstNumberFee,
|
|
AdditionalNumber: v.AdditionalNumber,
|
|
AdditionalNumberFee: v.AdditionalNumberFee,
|
|
FreeNumberThreshold: v.FreeNumberThreshold,
|
|
}
|
|
} else {
|
|
price = freight.Price{
|
|
FreightID: info.ID,
|
|
Type: 1,
|
|
FirstWeight: v.FirstWeight,
|
|
FirstWeightFee: v.FirstWeightFee,
|
|
AdditionalWeight: v.AdditionalWeight,
|
|
AdditionalWeightFee: v.AdditionalWeightFee,
|
|
FreeNumberThreshold: v.FreeNumberThreshold,
|
|
}
|
|
}
|
|
err = tx.Create(&price).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
valueStr := ""
|
|
values := []interface{}{}
|
|
|
|
// 数据处理
|
|
provinces := map[string]int{}
|
|
citys := []freight.Citys{}
|
|
for _, province := range v.Children {
|
|
split := strings.Split(province, "-")
|
|
splitLen := len(split)
|
|
if splitLen == 1 {
|
|
provinces[province] = 1
|
|
|
|
valueStr += ",(?,?,?)"
|
|
values = append(values, info.ID, price.ID, province)
|
|
} else if splitLen == 2 {
|
|
citys = append(citys, freight.Citys{ProvinceName: split[0], CityName: split[1]})
|
|
}
|
|
}
|
|
if len(values) > 0 {
|
|
err = tx.Exec("INSERT INTO recook_freight_provinces(freight_id,price_detail_id,province_name) VALUES "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
if len(citys) > 0 {
|
|
valueStr := ""
|
|
values := []interface{}{}
|
|
for _, city := range citys {
|
|
if _, ok := provinces[city.ProvinceName]; ok {
|
|
continue
|
|
}
|
|
valueStr += ",(?,?,?,?)"
|
|
values = append(values, info.ID, price.ID, city.ProvinceName, city.CityName)
|
|
}
|
|
if len(values) > 0 {
|
|
err = tx.Exec("INSERT INTO recook_freight_citys(freight_id,price_detail_id,province_name,city_name) VALUES "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|