feat: add shipping template management and update favicon
This commit is contained in:
@@ -300,14 +300,80 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
break
|
||||
}
|
||||
|
||||
var shippingFeeFirstWeight, shippingFeePerGram, serviceFeeRate, taxRate, channelFeeRate float64
|
||||
// 获取收货地址省份
|
||||
var shippingAddress models.Address
|
||||
var province string
|
||||
if err := utils.DB.First(&shippingAddress, req.ShippingAddressID).Error; err == nil {
|
||||
province = shippingAddress.Province
|
||||
}
|
||||
|
||||
// 使用运费模板计算运费
|
||||
shippingFee := 0.0
|
||||
|
||||
// 按运费模板分组计算
|
||||
type TemplateGroup struct {
|
||||
TemplateID *uint
|
||||
Weight float64
|
||||
Quantity int
|
||||
Subtotal float64
|
||||
}
|
||||
templateGroups := make(map[uint]*TemplateGroup)
|
||||
noTemplateGroup := &TemplateGroup{Subtotal: 0, Weight: 0, Quantity: 0}
|
||||
|
||||
for _, cart := range carts {
|
||||
var templateID uint
|
||||
if cart.Product.ShippingTemplateID != nil {
|
||||
templateID = *cart.Product.ShippingTemplateID
|
||||
}
|
||||
|
||||
if templateID == 0 {
|
||||
// 没有运费模板,累加到无模板组
|
||||
noTemplateGroup.Weight += cart.Product.Weight * float64(cart.Quantity)
|
||||
noTemplateGroup.Quantity += cart.Quantity
|
||||
noTemplateGroup.Subtotal += cart.Product.Price * float64(cart.Quantity)
|
||||
} else {
|
||||
if _, ok := templateGroups[templateID]; !ok {
|
||||
templateGroups[templateID] = &TemplateGroup{
|
||||
TemplateID: &templateID,
|
||||
Weight: 0,
|
||||
Quantity: 0,
|
||||
Subtotal: 0,
|
||||
}
|
||||
}
|
||||
templateGroups[templateID].Weight += cart.Product.Weight * float64(cart.Quantity)
|
||||
templateGroups[templateID].Quantity += cart.Quantity
|
||||
templateGroups[templateID].Subtotal += cart.Product.Price * float64(cart.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
// 计算有模板的运费
|
||||
for _, group := range templateGroups {
|
||||
fee, _ := CalculateShippingFee(group.TemplateID, group.Weight, group.Quantity, group.Subtotal, province)
|
||||
shippingFee += fee
|
||||
}
|
||||
|
||||
// 计算无模板的运费(使用旧的系统设置)
|
||||
if noTemplateGroup.Subtotal > 0 {
|
||||
var shippingFeeFirstWeight, shippingFeePerGram float64
|
||||
var setting models.SystemSetting
|
||||
if err := utils.DB.Where("`key` = ?", "shipping_fee_first_weight").First(&setting).Error; err == nil {
|
||||
shippingFeeFirstWeight, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
if err := utils.DB.Where("`key` = ?", "shipping_fee_per_gram").First(&setting).Error; err == nil {
|
||||
shippingFeePerGram, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
if noTemplateGroup.Subtotal < 99 && shippingFeeFirstWeight > 0 {
|
||||
fee := shippingFeeFirstWeight
|
||||
if noTemplateGroup.Weight > 500 {
|
||||
fee += shippingFeePerGram * (noTemplateGroup.Weight - 500)
|
||||
}
|
||||
shippingFee += fee
|
||||
}
|
||||
}
|
||||
|
||||
// 获取其他费率
|
||||
var serviceFeeRate, taxRate, channelFeeRate float64
|
||||
var setting models.SystemSetting
|
||||
if err := utils.DB.Where("`key` = ?", "shipping_fee_first_weight").First(&setting).Error; err == nil {
|
||||
shippingFeeFirstWeight, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
if err := utils.DB.Where("`key` = ?", "shipping_fee_per_gram").First(&setting).Error; err == nil {
|
||||
shippingFeePerGram, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
if err := utils.DB.Where("`key` = ?", "service_fee_rate").First(&setting).Error; err == nil {
|
||||
serviceFeeRate, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
@@ -318,15 +384,6 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
channelFeeRate, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
|
||||
// 根据商品重量计算运费
|
||||
shippingFee := 0.0
|
||||
if subtotal < 99 && shippingFeeFirstWeight > 0 {
|
||||
shippingFee = shippingFeeFirstWeight
|
||||
if totalWeight > 500 {
|
||||
shippingFee += shippingFeePerGram * (totalWeight - 500)
|
||||
}
|
||||
}
|
||||
|
||||
serviceFee := subtotal * serviceFeeRate / 100
|
||||
tax := subtotal * taxRate / 100
|
||||
channelFee := subtotal * channelFeeRate / 100
|
||||
|
||||
Reference in New Issue
Block a user