Commit acacf9fb by 杨树贤

折扣价逻辑独立出来

parent 54e5adb5
......@@ -194,12 +194,13 @@ func (ls *LyService) GetExtendFee(supplierId int64, canal string) interface{} {
}
}
// 获取系数
// 获取系数和总体价格
func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
if len(sku.LadderPrice) == 0 {
//sku.Original = nil
return sku
}
priceService := PriceService{}
flag := 0
var data []model.LadderPrice
var originalPrice []model.OriginPrice
......@@ -210,8 +211,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
//如果没有成本价字段,就要去生成阶梯价格
if len(ladderPrice) > 0 {
if ladderPrice[0].PriceCostUs == 0 && ladderPrice[0].PriceCostCn == 0 {
var priceService PriceService
generatedLadderPrice, priceRatio := priceService.GenerateCostLadderPrice(sku)
generatedLadderPrice, priceRatio := priceService.GenerateLadderPrice(sku)
ladderPrice = generatedLadderPrice
sku.PriceRatio = priceRatio
sku.PriceRatioSort = -1
......@@ -259,79 +259,13 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
//先去读取成本折扣系数
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultDiscoutRatio := false
discountRatio, _ := redis.String(redisCon.Do("HGET", "magic_cube_channel_discount_daigou", sku.SupplierId))
checkNullRation := gjson.Get(discountRatio, "ration").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if discountRatio == "" || checkNullRation == "{}" {
isDefaultDiscoutRatio = true
discountRatio, _ = redis.String(redisCon.Do("GET", "magic_cube_channel_discount_default_daigou"))
}
var cnDiscountRatio float64
var usDiscountRatio float64
//是否找到对应的折扣系数,找不到就去找默认
findedDiscountRatio := false
//如果有默认系数,那么就去找默认系数
if isDefaultDiscoutRatio {
} else {
//拿到系数以后,就要去计算
//拿出里面的所有排序,以人民币系数为准
ration := gjson.Get(discountRatio, "ration").Map()
var sortNumbers []int
for sortNumberString, _ := range ration {
sortNumber, _ := strconv.Atoi(sortNumberString)
sortNumbers = append(sortNumbers, sortNumber)
}
//然后确定排序
sortNumbers = sorter.IntSliceSortDesc(sortNumbers)
//确定排序以后,就可以进行按排序(从大到小)取系数
for _, sortNumber := range sortNumbers {
sortString := strconv.Itoa(sortNumber)
cnDiscountRatio = gjson.Get(discountRatio, "ration."+sortString).Float()
usDiscountRatio = gjson.Get(discountRatio, "ration_usd."+sortString).Float()
var hasSpecialCheck = false
//判断是否有符合的商品名称
goodsNames := gjson.Get(discountRatio, "goods_name."+sortString).String()
if goodsNames != "" {
hasSpecialCheck = true
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
findedDiscountRatio = true
break
}
}
/**
//判断是否有符合的品牌名称
brandIds := gjson.Get(discountRatio, "brand."+sortString).String()
if brandIds != "" {
hasSpecialCheck = true
standardBrandIdList := strings.Split(brandIds, ",")
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
findedDiscountRatio = true
break
}
}
//如果没有设置品牌和商品,那么这个优先级高的就会覆盖下面的了,不需要再去判断品牌和型号了
if hasSpecialCheck {
continue
}
findedDiscountRatio = true
break
}
}
//如果上面都没找到折扣系数,那么就去找全局的
if !findedDiscountRatio {
discountRatio, _ = redis.String(redisCon.Do("GET", "magic_cube_channel_discount_default_daigou"))
cnDiscountRatio = gjson.Get(discountRatio, "ration").Float()
usDiscountRatio = gjson.Get(discountRatio, "ration_usd").Float()
}
sku.DiscountRatio.Ratio = cnDiscountRatio
sku.DiscountRatio.RatioUsd = usDiscountRatio
**/
sku = priceService.GetDiscountRatio(sku)
/**
再去找售价组系数
......@@ -340,7 +274,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultPriceRatio := false
priceRatioCache, _ := redis.String(redisCon.Do("HGET", "magic_cube_price_rule_channel", sku.SupplierId))
checkNullRation = gjson.Get(priceRatioCache, "ladder_price").String()
checkNullRation := gjson.Get(priceRatioCache, "ladder_price").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if priceRatioCache == "" || checkNullRation == "{}" {
isDefaultPriceRatio = true
......@@ -350,7 +284,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
//这个就是最终要获取到的价格系数
var priceRatioList []model.PriceRatio
//是否找到系数的标志位
ratioFinded := false
foundRatio := false
//如果只有默认系数,那么就去找默认系数
if isDefaultPriceRatio {
} else {
......@@ -387,7 +321,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
ratioFinded = true
foundRatio = true
break
}
}
......@@ -400,7 +334,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
ratioFinded = true
foundRatio = true
break
}
}
......@@ -423,27 +357,27 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
if hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.Contains(sku.Eccn, eccn) {
ratioFinded = true
foundRatio = true
break outerLoop
}
}
if hasPrefix && !hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasSuffix(sku.Eccn, eccn) {
ratioFinded = true
foundRatio = true
break outerLoop
}
}
if !hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasPrefix(sku.Eccn, eccn) {
ratioFinded = true
foundRatio = true
break outerLoop
}
}
} else {
if sku.Eccn == eccn {
ratioFinded = true
foundRatio = true
break outerLoop
}
}
......@@ -453,12 +387,12 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
if hasSpecialCheck {
continue
}
ratioFinded = true
foundRatio = true
break
}
}
//找不到特定的系数,那就去找全局的
if !ratioFinded {
if !foundRatio {
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_channel_default"))
priceRatioArr := gjson.Get(priceRatioCache, "ladder_price").Array()
priceRatioList = nil
......@@ -529,7 +463,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
for key, price := range sku.LadderPrice {
//这里有个前置条件处理美金价,因为element(6)存到美金字段里面的是港币,rs(21)存到美金字段里的是人民币,buerklin(1676)是欧元
//所以要全部先转成正确的美金价才能显示,目前先写死汇率,因为目前没有地方能获取实时的各种转美金的汇率
price.PriceUs = ls.TransformSpecialSupplierPrice(sku.SupplierId, price.PriceUs, coefficient.Ratio)
price.PriceUs = priceService.TransformSpecialSupplierPrice(sku.SupplierId, price.PriceUs, coefficient.Ratio)
originalPrice = append(originalPrice, model.OriginPrice{
PriceUs: price.PriceUs,
Purchases: price.Purchases,
......@@ -562,7 +496,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
}
}
//美金成本价
priceCostUs := c.MulFloat(price.PriceUs, usDiscountRatio)
priceCostUs := c.MulFloat(price.PriceUs, sku.DiscountRatio.RatioUsd)
priceCostUs = c.MyRound(priceCostUs, 4)
//美金售价
priceUs := c.MulFloat(priceCostUs, priceRatio.RatioUsd)
......@@ -573,7 +507,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
} else {
priceCostCn = c.MulFloat(price.PriceUs, coefficient.Ratio, tax)
}
priceCostCn = c.MulFloat(c.MyRound(priceCostCn, 4), cnDiscountRatio)
priceCostCn = c.MulFloat(c.MyRound(priceCostCn, 4), sku.DiscountRatio.Ratio)
priceCostCn = c.MyRound(priceCostCn, 4)
//人民币售价
priceCn := c.MulFloat(priceCostCn, priceRatio.Ratio)
......
......@@ -2,12 +2,15 @@ package service
import (
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"go_sku_server/model"
c "go_sku_server/pkg/common"
"go_sku_server/pkg/gredis"
"go_sku_server/service/sorter"
"sort"
"strconv"
"strings"
)
type PriceService struct {
......@@ -15,7 +18,7 @@ type PriceService struct {
// 这里有个前置条件处理美金价,因为element(6)存到美金字段里面的是港币,rs(21)存到美金字段里的是人民币,buerklin(1676)是欧元
// 所以要全部先转成正确的美金价才能显示
func (ls *LyService) TransformSpecialSupplierPrice(supplierId int64, priceUs float64, usRatio float64) float64 {
func (ps *PriceService) TransformSpecialSupplierPrice(supplierId int64, priceUs float64, usRatio float64) float64 {
//去redis获取价格
redisCon := gredis.Conn("default_r")
defer func() {
......@@ -34,7 +37,7 @@ func (ls *LyService) TransformSpecialSupplierPrice(supplierId int64, priceUs flo
}
// 构建专营的阶梯价,现在专营只会存一个简单的成本价,阶梯数量是1,所以我这边要根据专营的阶梯系数去构建具体的阶梯价
func (ps *PriceService) GenerateCostLadderPrice(sku model.LySku) (generatedLadderPrice []model.LadderPrice, showPriceRatio []model.PriceRatio) {
func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPrice []model.LadderPrice, showPriceRatio []model.PriceRatio) {
//先直接获取成本价原始值,判断第一个阶梯的阶梯数量是否为0,如果是0,那么代表是要走成本价生成,如果不是0,那么就要走阶梯价生成
firstLadderPurchases := sku.LadderPrice[0].Purchases
isCostPrice := bool(firstLadderPurchases == 0)
......@@ -250,3 +253,92 @@ func (ps *PriceService) GenerateCostLadderPrice(sku model.LySku) (generatedLadde
return generatedLadderPrice, showPriceRatio
}
}
// 获取折扣系数
func (ps *PriceService) GetDiscountRatio(sku model.LySku) model.LySku {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
discountRatioRedisKey := "magic_cube_channel_discount_daigou"
discountRatioDefaultRedisKey := "magic_cube_channel_discount_default_daigou"
if sku.SupplierId == 17 {
discountRatioRedisKey = "magic_cube_channel_discount_zhuanying"
discountRatioDefaultRedisKey = "magic_cube_channel_discount_default_zhuanying"
}
//先去读取成本折扣系数
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultDiscountRatio := false
discountRatio, _ := redis.String(redisCon.Do("HGET", discountRatioRedisKey, sku.SupplierId))
checkNullRation := gjson.Get(discountRatio, "ration").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if discountRatio == "" || checkNullRation == "{}" {
isDefaultDiscountRatio = true
discountRatio, _ = redis.String(redisCon.Do("GET", discountRatioDefaultRedisKey))
}
var cnDiscountRatio float64
var usDiscountRatio float64
//是否找到对应的折扣系数,找不到就去找默认
foundDiscountRatio := false
//如果有默认系数,那么就去找默认系数
if isDefaultDiscountRatio {
} else {
//拿到系数以后,就要去计算
//拿出里面的所有排序,以人民币系数为准
ration := gjson.Get(discountRatio, "ration").Map()
var sortNumbers []int
for sortNumberString, _ := range ration {
sortNumber, _ := strconv.Atoi(sortNumberString)
sortNumbers = append(sortNumbers, sortNumber)
}
//然后确定排序
sortNumbers = sorter.IntSliceSortDesc(sortNumbers)
//确定排序以后,就可以进行按排序(从大到小)取系数
for _, sortNumber := range sortNumbers {
sortString := strconv.Itoa(sortNumber)
cnDiscountRatio = gjson.Get(discountRatio, "ration."+sortString).Float()
usDiscountRatio = gjson.Get(discountRatio, "ration_usd."+sortString).Float()
var hasSpecialCheck = false
//判断是否有符合的商品名称
goodsNames := gjson.Get(discountRatio, "goods_name."+sortString).String()
if goodsNames != "" {
hasSpecialCheck = true
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
foundDiscountRatio = true
break
}
}
//判断是否有符合的品牌名称
brandIds := gjson.Get(discountRatio, "brand."+sortString).String()
if brandIds != "" {
hasSpecialCheck = true
standardBrandIdList := strings.Split(brandIds, ",")
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
foundDiscountRatio = true
break
}
}
//如果没有设置品牌和商品,那么这个优先级高的就会覆盖下面的了,不需要再去判断品牌和型号了
if hasSpecialCheck {
continue
}
foundDiscountRatio = true
break
}
}
//如果上面都没找到折扣系数,那么就去找全局的
if !foundDiscountRatio {
discountRatio, _ = redis.String(redisCon.Do("GET", discountRatioDefaultRedisKey))
cnDiscountRatio = gjson.Get(discountRatio, "ration").Float()
usDiscountRatio = gjson.Get(discountRatio, "ration_usd").Float()
}
sku.DiscountRatio.Ratio = cnDiscountRatio
sku.DiscountRatio.RatioUsd = usDiscountRatio
return sku
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment