Commit e24ada75 by 杨树贤

Merge branch 'ysx-价格体系修改-20230815' into dev

# Conflicts:
#	service/service_ly_common.go
#	service/service_price.go
parents 6779c613 dcf5b8be
......@@ -77,6 +77,7 @@ type LySku struct {
DiscountRatio DiscountRatio `json:"discount_ratio"`
PriceRatioSort int `json:"price_ratio_sort"`
PriceRatio []PriceRatio `json:"price_ratio"`
Source int `json:"source"`
}
type DiscountRatio struct {
......@@ -85,8 +86,9 @@ type DiscountRatio struct {
}
type PriceRatio struct {
Ratio float64 `json:"ratio"`
RatioUsd float64 `json:"ratio_usd"`
Ratio float64 `json:"ratio"`
RatioUsd float64 `json:"ratio_usd"`
Purchases int64 `json:"purchases,omitempty"`
}
type PriceActivity struct {
......@@ -134,6 +136,10 @@ type StockInfo struct {
// 为什么不直接映射到结构,而要用gjson,因为redis存的数据结构不一定正常,可能类型不一致
func InitSkuData(sku string) (data LySku) {
source := gjson.Get(sku, "source").Int()
data.Source = int(source)
goodsSn := gjson.Get(sku, "goods_sn").String()
data.GoodsSn = goodsSn
......
......@@ -5,7 +5,6 @@ import (
"go_sku_server/pkg/gredis"
"go_sku_server/service/sorter"
"sort"
"strings"
"sync"
"github.com/gin-gonic/gin"
......@@ -65,16 +64,7 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, ch chan
sku.Packing = gjson.Get(packing, "pack").String()
}
sku = ls.GetGoodsImages(sku, spu)
//pdf
pdf := gjson.Get(spu, "pdf").String()
if pdf != "" {
pdf = strings.Replace(pdf, "http://img.ichunt.com", "https://img.ichunt.com", 1)
//还要针对如果是自己的上传服务的pdf文件,还要补上类型用于预览
if (strings.Contains(pdf, "files.ichunt.net") || strings.Contains(pdf, "file.liexindev.net")) && !strings.Contains(pdf, "fileType=pdf") {
pdf += "?fileType=pdf"
}
sku.Pdf = pdf
}
sku = ls.GetPdf(sku, spu)
//获取商品名称
//1688就是mro的sku spuName和GoodsName不是一个东西,不能公用
......
......@@ -2,21 +2,17 @@ package service
import (
"encoding/json"
"fmt"
"github.com/gomodule/redigo/redis"
_ "github.com/iancoleman/orderedmap"
"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/pkg/logger"
_ "go_sku_server/pkg/mongo"
"go_sku_server/service/sorter"
"strconv"
"strings"
"github.com/gomodule/redigo/redis"
_ "github.com/iancoleman/orderedmap"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
_ "gopkg.in/mgo.v2/bson"
"strings"
)
// 获取图片信息
......@@ -40,6 +36,20 @@ func (ls *LyService) GetGoodsImages(sku model.LySku, spu string) model.LySku {
return sku
}
// 获取PDF信息
func (ls *LyService) GetPdf(sku model.LySku, spu string) model.LySku {
pdf := gjson.Get(spu, "pdf").String()
if pdf != "" {
pdf = strings.Replace(pdf, "http://img.ichunt.com", "https://img.ichunt.com", 1)
//还要针对如果是自己的上传服务的pdf文件,还要补上类型用于预览
if (strings.Contains(pdf, "files.ichunt.net") || strings.Contains(pdf, "file.liexindev.net")) && !strings.Contains(pdf, "fileType=pdf") {
pdf += "?fileType=pdf"
}
sku.Pdf = pdf
}
return sku
}
// 获取分类信息
func (ls *LyService) GetGoodsClass(sku model.LySku, spu string) model.LySku {
//仅提供价格和库存
......@@ -181,12 +191,79 @@ func (ls *LyService) GetExtendFee(supplierId int64, canal string) interface{} {
}
}
// 获取系数
// 获取供应商货期
func (ls *LyService) GetDelivery(supplierId int64, canal string) (delivery map[string]string) {
delivery = make(map[string]string)
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
if canal != "" {
supplierRatio, _ := redis.String(redisCon.Do("HGET", "supp_ratio", canal))
if supplierRatio != "" {
cnDeliveryTime := gjson.Get(supplierRatio, "cn_delivery_time").String()
usDeliveryTime := gjson.Get(supplierRatio, "us_delivery_time").String()
//为了兼容供应商修改的问题
if cnDeliveryTime != "周" && cnDeliveryTime != "天" {
delivery["cn_delivery"] = gjson.Get(supplierRatio, "cn_delivery_time").String()
} else {
delivery["cn_delivery"] = ""
}
if usDeliveryTime != "周" && usDeliveryTime != "天" {
delivery["hk_delivery"] = gjson.Get(supplierRatio, "us_delivery_time").String()
} else {
delivery["hk_delivery"] = ""
}
}
return
}
info, _ := redis.String(redisCon.Do("HGET", "SUPPLIER_REDIS_INFO_", supplierId))
cnDelivery := gjson.Get(info, "cn_delivery").String()
hkDelivery := gjson.Get(info, "hk_delivery").String()
if cnDelivery != "" || hkDelivery != "" {
delivery["cn_delivery"] = cnDelivery
delivery["hk_delivery"] = hkDelivery
return
}
return
}
// 判断能否购买
func (ls *LyService) GetIsBuy(sku model.LySku) (isBuy int) {
if sku.GoodsStatus != 1 {
return
}
if sku.Moq > sku.Stock {
return
}
if len(sku.LadderPrice) == 0 {
return
}
if sku.Stock == 0 {
return
}
return 1
}
// 合并spu的信息
func (ls *LyService) CombineSup(sku model.LySku, spuStr string) model.LySku {
sku.UpdateTime = gjson.Get(spuStr, "update_time").Int()
sku.ClassID1 = int(gjson.Get(spuStr, "class_id1").Int())
sku.ClassID2 = int(gjson.Get(spuStr, "class_id2").Int())
sku.SpuName = gjson.Get(spuStr, "spu_name").String()
sku.SpuBrief = gjson.Get(spuStr, "spu_brief").String()
sku.SpuDetail = gjson.Get(spuStr, "spu_detail").String()
sku.Status = int(gjson.Get(spuStr, "status").Int())
sku.Encap = gjson.Get(spuStr, "encap").String()
return sku
}
// 获取系数和总体价格,生成和处理价格的方法,很重要
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
......@@ -197,27 +274,29 @@ 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.GenerateLadderPrice(sku)
ladderPrice = generatedLadderPrice
//sku.Original = ladderPrice
sku.PriceRatio = priceRatio
sku.PriceRatioSort = -1
} else {
fmt.Println("不走成本价生成")
sku = priceService.GenerateLadderPrice(sku)
ladderPrice = sku.LadderPrice
}
}
//获取折扣系数
sku = priceService.GetDiscountRatio(sku)
data = make([]model.LadderPrice, len(ladderPrice))
for key, price := range ladderPrice {
if price.Purchases == 0 {
continue
}
data[key].Purchases = price.Purchases
//下面的价格,还要乘于折扣系数
if price.PriceUs != 0 {
data[key].PriceUs = c.MyRound(price.PriceUs, 4)
priceUs := c.MyRound(price.PriceUs, 4)
priceUs = c.MyRound(c.MulFloat(price.PriceUs, sku.DiscountRatio.RatioUsd), 4)
data[key].PriceUs = priceUs
}
if price.PriceCn != 0 {
data[key].PriceCn = c.MyRound(price.PriceCn, 4)
priceCn := c.MyRound(price.PriceCn, 4)
priceCn = c.MyRound(c.MulFloat(price.PriceCn, sku.DiscountRatio.Ratio), 4)
data[key].PriceCn = priceCn
}
//专卖成本价
......@@ -225,7 +304,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
data[key].PriceCostCn = price.PriceCostCn
//联营或者专卖 同时 存在活动价格
if (sku.GoodsType == 1 || sku.GoodsType == 2) && sku.AcType > 1 && sku.Ratio > 0 {
if (sku.GoodsType == 1 || sku.GoodsType == 2 || sku.GoodsType == 6) && sku.AcType > 1 && sku.Ratio > 0 {
tempAcPrice := c.MyRound(c.MulFloat(price.PriceCn, sku.Ratio/100), 4)
data[key].PriceAc = tempAcPrice
data[key].PriceAcUs = c.MyRound(c.MulFloat(price.PriceUs, sku.RatioUs/100), 4)
......@@ -249,220 +328,14 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
//先去读取成本折扣系数
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultDiscountRatio := false
discountRatio, _ := redis.String(redisCon.Do("HGET", "magic_cube_channel_discount_daigou", sku.SupplierId))
checkNullRation := gjson.Get(discountRatio, "ration").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if discountRatio == "" || checkNullRation == "{}" {
isDefaultDiscountRatio = true
discountRatio, _ = redis.String(redisCon.Do("GET", "magic_cube_channel_discount_default_daigou"))
}
var cnDiscountRatio float64
var usDiscountRatio float64
//是否找到对应的折扣系数,找不到就去找默认
findedDiscountRatio := 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) {
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)
/**
再去找售价组系数
**/
sku, priceRatioList := priceService.GetPriceRatio(sku)
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultPriceRatio := false
priceRatioCache, _ := redis.String(redisCon.Do("HGET", "magic_cube_price_rule_channel", sku.SupplierId))
checkNullRation = gjson.Get(priceRatioCache, "ladder_price").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if priceRatioCache == "" || checkNullRation == "{}" {
isDefaultPriceRatio = true
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_channel_default"))
}
var priceRatioSort int
//这个就是最终要获取到的价格系数
var priceRatioList []model.PriceRatio
//是否找到系数的标志位
findedRatio := false
//如果只有默认系数,那么就去找默认系数
if isDefaultPriceRatio {
} else {
//拿到系数以后,就要去计算
//拿出里面的所有排序
priceRatioSortMap := gjson.Get(priceRatioCache, "ladder_price").Map()
var sortNumbers []int
for sortNumberString, _ := range priceRatioSortMap {
sortNumber, _ := strconv.Atoi(sortNumberString)
sortNumbers = append(sortNumbers, sortNumber)
}
//然后确定排序
sortNumbers = sorter.IntSliceSortDesc(sortNumbers)
//确定排序以后,就可以进行按排序(从大到小)取系数
outerLoop:
for _, sortNumber := range sortNumbers {
priceRatioSort = sortNumber
priceRatioList = nil
sortString := strconv.Itoa(sortNumber)
priceRatioArr := gjson.Get(priceRatioCache, "ladder_price."+sortString).Array()
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "ratio").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "ratio_usd").Float()
priceRatioList = append(priceRatioList, priceRatio)
}
var hasSpecialCheck = false
//判断是否有符合的商品名称
goodsNames := gjson.Get(priceRatioCache, "goods_name."+sortString).String()
if goodsNames != "" {
hasSpecialCheck = true
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
findedRatio = true
break
}
}
//判断是否有符合的标准品牌ID
brandIds := gjson.Get(priceRatioCache, "brand."+sortString).String()
if brandIds != "" {
hasSpecialCheck = true
standardBrandIdList := strings.Split(brandIds, ",")
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
findedRatio = true
break
}
}
//判断是否有符合的eccn
eccns := gjson.Get(priceRatioCache, "eccn."+sortString).String()
if eccns != "" {
hasSpecialCheck = true
eccnList := strings.Split(eccns, ",")
//找到有对应的eccn,那么优先级肯定是最高的了
for _, eccn := range eccnList {
//判断是否有百分号匹配
//如果是纯%,那就是不对的,要跳过
if strings.Replace(eccn, "%", "", 10) == "" {
continue
}
if strings.Contains(eccn, "%") {
hasPrefix := strings.HasPrefix(eccn, "%")
hasSuffix := strings.HasSuffix(eccn, "%")
if hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.Contains(sku.Eccn, eccn) {
findedRatio = true
break outerLoop
}
}
if hasPrefix && !hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasSuffix(sku.Eccn, eccn) {
findedRatio = true
break outerLoop
}
}
if !hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasPrefix(sku.Eccn, eccn) {
findedRatio = true
break outerLoop
}
}
} else {
if sku.Eccn == eccn {
findedRatio = true
break outerLoop
}
}
}
}
//如果没有设置品牌和商品,那么这个优先级高的就会覆盖下面的了,不需要再去判断品牌和型号了
if hasSpecialCheck {
continue
}
findedRatio = true
break
}
}
//找不到特定的系数,那就去找全局的
if !findedRatio {
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_channel_default"))
priceRatioArr := gjson.Get(priceRatioCache, "ladder_price").Array()
priceRatioList = nil
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "ratio").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "ratio_usd").Float()
priceRatioList = append(priceRatioList, priceRatio)
}
priceRatioSort = -1
}
sku.PriceRatio = priceRatioList
sku.PriceRatioSort = priceRatioSort
//这里是供应商系数,先保留这块逻辑
/** 这里是供应商系数,先保留这块逻辑 **/
ratio, _ := redis.String(redisCon.Do("HGET", "pool_supplier_ratio", sku.SupplierId))
if ratio == "" {
logger.Select("sku_query").Error("系数获取异常,供应商:" + c.ToString(sku.SupplierId))
......@@ -512,15 +385,15 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
if !hasCoefficient {
coefficient = defaultCoefficient
}
//下面是计算价格
//价格计算文档 https://docs.qq.com/doc/DR3RJcnNPeUNkWHRk
// 为何是固定的1.13,关税基本不会变,有变的话跟产品沟通手动修改即可
//$tax = config('website.tax');
tax := 1.13
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,
......@@ -530,7 +403,7 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
continue
}
data[key].Purchases = price.Purchases
//找出对应的阶梯,从$priceRatioList找到对应的售价组系数
//找出对应的阶梯,从priceRatioList找到对应的售价组系数
//这个是为了怕后台存的数据格式不对导致无法获取到对应的系数
//这里去取对应的售价利润阶梯的时候,是往下匹配的,比如 阶梯价是 1,2,3 售价组利润是 1,2,3,4,5,那么123对应的是售价组利润的345
//而且当原始阶梯价为10个的时候,超过了售价利润组的9个,就要去取第9个售价利润组
......@@ -553,18 +426,18 @@ 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)
//人民币成本价,mro只有人民币,所以人民币价格不是从美金来的,而且人民币是含税的.就它要特殊处理
var priceCostCn float64
if sku.SupplierId == 1688 {
priceCostCn = c.MulFloat(price.PriceCn, coefficient.Ratio)
priceCostCn = c.MulFloat(price.PriceCn, 1)
} 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)
......@@ -620,69 +493,3 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
}
return sku
}
// 获取供应商货期
func (ls *LyService) GetDelivery(supplierId int64, canal string) (delivery map[string]string) {
delivery = make(map[string]string)
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
if canal != "" {
supplierRatio, _ := redis.String(redisCon.Do("HGET", "supp_ratio", canal))
if supplierRatio != "" {
cnDeliveryTime := gjson.Get(supplierRatio, "cn_delivery_time").String()
usDeliveryTime := gjson.Get(supplierRatio, "us_delivery_time").String()
//为了兼容供应商修改的问题
if cnDeliveryTime != "周" && cnDeliveryTime != "天" {
delivery["cn_delivery"] = gjson.Get(supplierRatio, "cn_delivery_time").String()
} else {
delivery["cn_delivery"] = ""
}
if usDeliveryTime != "周" && usDeliveryTime != "天" {
delivery["hk_delivery"] = gjson.Get(supplierRatio, "us_delivery_time").String()
} else {
delivery["hk_delivery"] = ""
}
}
return
}
info, _ := redis.String(redisCon.Do("HGET", "SUPPLIER_REDIS_INFO_", supplierId))
cnDelivery := gjson.Get(info, "cn_delivery").String()
hkDelivery := gjson.Get(info, "hk_delivery").String()
if cnDelivery != "" || hkDelivery != "" {
delivery["cn_delivery"] = cnDelivery
delivery["hk_delivery"] = hkDelivery
return
}
return
}
// 判断能否购买
func (ls *LyService) GetIsBuy(sku model.LySku) (isBuy int) {
if sku.GoodsStatus != 1 {
return
}
if sku.Moq > sku.Stock {
return
}
if len(sku.LadderPrice) == 0 {
return
}
if sku.Stock == 0 {
return
}
return 1
}
// 合并spu的信息
func (ls *LyService) CombineSup(sku model.LySku, spuStr string) model.LySku {
sku.UpdateTime = gjson.Get(spuStr, "update_time").Int()
sku.ClassID1 = int(gjson.Get(spuStr, "class_id1").Int())
sku.ClassID2 = int(gjson.Get(spuStr, "class_id2").Int())
sku.SpuName = gjson.Get(spuStr, "spu_name").String()
sku.SpuBrief = gjson.Get(spuStr, "spu_brief").String()
sku.SpuDetail = gjson.Get(spuStr, "spu_detail").String()
sku.Status = int(gjson.Get(spuStr, "status").Int())
sku.Encap = gjson.Get(spuStr, "encap").String()
return sku
}
package service
import (
"fmt"
"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 {
}
//这里有个前置条件处理美金价,因为element(6)存到美金字段里面的是港币,rs(21)存到美金字段里的是人民币,buerklin(1676)是欧元
//所以要全部先转成正确的美金价才能显示,目前先写死汇率,因为目前没有地方能获取实时的各种转美金的汇率
func (ls *LyService) TransformSpecialSupplierPrice(supplierId int64, priceUs float64, usRatio float64) float64 {
switch supplierId {
case 6:
customRate := 7.85 //港币转美金
priceUs = c.MyRound(c.DivFloat(priceUs, customRate), 4)
break
case 21:
customRate := usRatio //人民币转美金
priceUs = c.MyRound(c.DivFloat(priceUs, customRate), 4)
priceUs = c.MyRound(c.DivFloat(priceUs, 1.13), 4)
break
case 1676:
customRate := 0.93 //欧元转美金
priceUs = c.MyRound(c.DivFloat(priceUs, customRate), 4)
break
// case 1673:
// customRate := 0.93 //欧元转美金
// priceUs = c.MyRound(c.DivFloat(priceUs, customRate), 4)
// break
// 构建专营的阶梯价,现在专营只会存一个简单的成本价,阶梯数量是1,所以我这边要根据专营的阶梯系数去构建具体的阶梯价
func (ps *PriceService) GenerateLadderPrice(sku model.LySku) model.LySku {
//先去找到对应的价格系数
var ratioDataKey string
if sku.Moq <= 50 {
ratioDataKey = "cost_ladder_price_egt50_lt200"
} else {
ratioDataKey = "cost_ladder_price_egt200"
}
return priceUs
}
//构建专营的阶梯价,现在专营只会存一个简单的成本价,阶梯数量是1,所以我这边要根据专营的阶梯系数去构建具体的阶梯价
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)
//先去获取配置的redis
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
priceRatio, _ := redis.String(redisCon.Do("HGET", "magic_cube_price_rule_v2", sku.Canal))
//拿不到就去取默认的
if priceRatio == "" {
priceRatio, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_v2_default"))
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultPriceRatio := false
priceRatioCache, _ := redis.String(redisCon.Do("HGET", "magic_cube_price_rule_v2", sku.Canal))
//判断是否是空
checkNullRation := gjson.Get(priceRatioCache, ratioDataKey).String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if priceRatioCache == "" || checkNullRation == "{}" {
isDefaultPriceRatio = true
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_v2_default"))
}
var priceRatioSort int
//这个就是最终要获取到的价格系数
var priceRatioList []model.PriceRatio
//是否找到系数的标志位
foundRatio := false
//如果只有默认系数,那么就去找默认系数
if isDefaultPriceRatio {
} else {
//拿到系数以后,就要去计算
//拿出里面的所有排序
priceRatioSortMap := gjson.Get(priceRatioCache, ratioDataKey).Map()
var sortNumbers []int
for sortNumberString, _ := range priceRatioSortMap {
sortNumber, _ := strconv.Atoi(sortNumberString)
sortNumbers = append(sortNumbers, sortNumber)
}
//然后确定排序
sortNumbers = sorter.IntSliceSortDesc(sortNumbers)
//确定排序以后,就可以进行按排序(从大到小)取系数
outerLoop:
for _, sortNumber := range sortNumbers {
priceRatioSort = sortNumber
priceRatioList = nil
sortString := strconv.Itoa(sortNumber)
//这个就是找到的系数了
priceRatioArr := gjson.Get(priceRatioCache, ratioDataKey+"."+sortString).Array()
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "price").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "price_usd").Float()
priceRatio.Purchases = gjson.Get(value.String(), "purchases").Int()
priceRatioList = append(priceRatioList, priceRatio)
}
//是否满足特定条件的判断
var hasSpecialCheck = false
//判断是否有符合的商品名称
goodsNames := gjson.Get(priceRatioCache, "goods_name."+sortString).String()
if goodsNames != "" {
hasSpecialCheck = true
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
foundRatio = true
break
}
}
//判断是否有符合的标准品牌ID
brandIds := gjson.Get(priceRatioCache, "brand."+sortString).String()
if brandIds != "" {
hasSpecialCheck = true
standardBrandIdList := strings.Split(brandIds, ",")
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
foundRatio = true
break
}
}
//判断是否有符合的eccn
eccns := gjson.Get(priceRatioCache, "eccn."+sortString).String()
if eccns != "" {
hasSpecialCheck = true
eccnList := strings.Split(eccns, ",")
//找到有对应的eccn,那么优先级肯定是最高的了
for _, eccn := range eccnList {
//判断是否有百分号匹配
//如果是纯%,那就是不对的,要跳过
if strings.Replace(eccn, "%", "", 10) == "" {
continue
}
if strings.Contains(eccn, "%") {
hasPrefix := strings.HasPrefix(eccn, "%")
hasSuffix := strings.HasSuffix(eccn, "%")
if hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.Contains(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
if hasPrefix && !hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasSuffix(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
if !hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasPrefix(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
} else {
if sku.Eccn == eccn {
foundRatio = true
break outerLoop
}
}
}
}
//如果没有设置品牌和商品,那么这个优先级高的就会覆盖下面的了,不需要再去判断品牌和型号了
if hasSpecialCheck {
continue
}
foundRatio = true
break
}
}
//找不到特定的系数,那就去找全局的
if !foundRatio {
fmt.Println("去找默认系数")
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_v2_default"))
priceRatioArr := gjson.Get(priceRatioCache, ratioDataKey).Array()
priceRatioList = nil
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "price").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "price_usd").Float()
priceRatio.Purchases = gjson.Get(value.String(), "purchases").Int()
priceRatioList = append(priceRatioList, priceRatio)
}
priceRatioSort = -1
}
sku.PriceRatio = priceRatioList
sku.PriceRatioSort = priceRatioSort
//这是用来展示在商品服务的价格系数,专营的系数和代购的不一样,所以要转换一下展示形式
//是否有设置最低利润点阶梯,不足5个阶梯时,最高阶梯对应的最小利润点阶梯
ladderPriceMiniProfitLevel := int(gjson.Get(priceRatio, "ladder_price_mini_profit_level").Int())
var ladderPriceMiniProfitLevel int
fmt.Println("是否找到系数", foundRatio)
fmt.Println("系数redis数据为 : ", priceRatioCache)
fmt.Println("具体系数为 : ", priceRatioList)
if foundRatio {
priceRatioSortStr := strconv.Itoa(priceRatioSort)
ladderPriceMiniProfitLevel = int(gjson.Get(priceRatioCache, "ladder_price_mini_profit_level."+priceRatioSortStr).Int())
} else {
ladderPriceMiniProfitLevel = int(gjson.Get(priceRatioCache, "ladder_price_mini_profit_level").Int())
}
fmt.Println("最低利润点阶梯数 : ", ladderPriceMiniProfitLevel)
var generatedLadderPrice []model.LadderPrice
//先直接获取成本价原始值,判断第一个阶梯的阶梯数量是否为0,如果是0,那么代表是要走成本价生成,如果不是0,那么就要走阶梯价生成
firstLadderPurchases := sku.LadderPrice[0].Purchases
isCostPrice := bool(firstLadderPurchases == 0)
//判断是否走成本价判断还是走阶梯价判断,因为上传sku的时候,可以设置每个sku的成本价(人民币&&美金),也可以设置每个sku的阶梯价
//如果有阶梯价,就要跳过设置的成本价(假设有设置的话)
if isCostPrice {
costPriceCn := sku.LadderPrice[0].PriceCn
costPriceUs := sku.LadderPrice[0].PriceUs
//fmt.Println("人民币和美金的成本价分别为 : ", costPriceCn, costPriceUs)
//先去判断起订量,如果起订量小于50,就要走固定配置的阶梯价格系数
//先去判断起订量,如果起订量小于50,就要走固定配置的阶梯系数
if sku.Moq <= 50 {
moq := int(sku.Moq)
fixedRatio := make(map[int]float64)
//当起订量是小于等于50的时候,阶梯数量和阶梯数是固定的,但是具体的利润还是要去取配置的值,因为固定死的阶梯数量是5,所以这边要直接取魔方配置9个的后5个
costLadderPriceRatio := gjson.Get(priceRatio, "cost_ladder_price_egt50_lt200").Map()
if len(costLadderPriceRatio) == 9 {
//priceRatioAndPurchases := costLadderPriceRatio[strconv.Itoa(i)]
if len(priceRatioList) == 9 {
//priceRatioAndPurchases := priceRatioList[strconv.Itoa(i)]
//priceCnRatio := priceRatioAndPurchases.Get("price").Float()
var fixedPurchases []int
switch {
......@@ -83,11 +214,10 @@ func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPri
break
}
for index, purchase := range fixedPurchases {
//costLadderPriceRatio这个redis是字典,下标从1开始的,所以要从第5个开始取
ratio := costLadderPriceRatio[strconv.Itoa(5+index)].Get("price").Float()
ratioUsd := costLadderPriceRatio[strconv.Itoa(5+index)].Get("price_us").Float()
//costLadderPriceRatio这个redis是字典,下标从0开始的,所以要从第5个开始取
ratio := priceRatioList[4+index].Ratio
ratioUsd := priceRatioList[4+index].RatioUsd
//同时还要构建价格系数展示在商品服务
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: ratio, RatioUsd: ratioUsd})
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: int64(purchase),
PriceCn: c.MyRound(c.MulFloat(costPriceCn, ratio), 4),
......@@ -112,7 +242,6 @@ func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPri
//然后根据一开始只有一个的阶梯价去生成阶梯价格
for purchases, ratio := range fixedRatio {
//同时还要构建价格系数展示在商品服务
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: ratio, RatioUsd: ratio})
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: int64(purchases),
PriceCn: c.MyRound(c.MulFloat(costPriceCn, ratio), 4),
......@@ -121,40 +250,29 @@ func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPri
}
}
sort.Slice(showPriceRatio, func(i, j int) bool {
return showPriceRatio[i].Ratio > showPriceRatio[j].Ratio
})
sku.PriceRatio = showPriceRatio
sort.Slice(generatedLadderPrice, func(i, j int) bool {
return generatedLadderPrice[i].Purchases < generatedLadderPrice[j].Purchases
})
return generatedLadderPrice, showPriceRatio
sku.LadderPrice = generatedLadderPrice
return sku
}
//判断最小起订量是属于哪个范围
ratioKey := ""
if sku.Moq >= 50 && sku.Moq < 200 {
ratioKey = "cost_ladder_price_egt50_lt200"
} else {
ratioKey = "cost_ladder_price_egt200"
}
costLadderPriceRatio := gjson.Get(priceRatio, ratioKey).Map()
fmt.Println("是成本价")
// 成本价阶梯数 由最高库存计算得到
costLadderCount := 0
for i := 1; i <= len(costLadderPriceRatio); i++ {
costPurchases := costLadderPriceRatio[strconv.Itoa(i)].Get("purchases").Int()
for i := 0; i < len(priceRatioList); i++ {
costPurchases := priceRatioList[i].Purchases
if costPurchases*sku.Moq > sku.Stock {
break
}
costLadderCount++
}
//fmt.Println("阶梯数量为 : ", costLadderCount)
//fmt.Println("设置的利润阶梯为 : ", ladderPriceMiniProfitLevel)
fmt.Println("阶梯数量为 : ", costLadderCount)
fmt.Println("设置的利润阶梯为 : ", ladderPriceMiniProfitLevel)
if costLadderCount <= ladderPriceMiniProfitLevel {
for i := 1; i <= costLadderCount; i++ {
priceRatioAndPurchases := costLadderPriceRatio[strconv.Itoa(i)]
for i := 0; i < costLadderCount; i++ {
priceRatioAndPurchases := priceRatioList[i]
// 阶梯数量系数正序取
costPurchases := sku.Moq * priceRatioAndPurchases.Get("purchases").Int()
costPurchases := sku.Moq * priceRatioAndPurchases.Purchases
//fmt.Println(costPurchases, sku.Stock)
// 阶梯数量上限为库存量,超出则不再计算下级阶梯
if costPurchases > sku.Stock {
......@@ -168,63 +286,58 @@ func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPri
if costMapIndex <= 0 {
costMapIndex = 1
}
priceRatioAndPurchases = costLadderPriceRatio[strconv.Itoa(costMapIndex)]
priceRatioAndPurchases = priceRatioList[costMapIndex]
//fmt.Println("获取到的阶梯系数为 : ", priceRatioAndPurchases)
priceCnRatio := priceRatioAndPurchases.Get("price").Float()
priceUsRatio := priceRatioAndPurchases.Get("price_usd").Float()
priceCnRatio := priceRatioAndPurchases.Ratio
priceUsRatio := priceRatioAndPurchases.RatioUsd
// 阶梯价格系数正序取
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: priceCnRatio, RatioUsd: priceUsRatio})
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: costPurchases,
PriceCn: c.MyRound(c.MulFloat(costPriceCn, priceCnRatio), 4),
PriceUs: c.MyRound(c.MulFloat(costPriceUs, priceUsRatio), 4),
})
sku.PriceRatio = showPriceRatio
}
} else {
//价格阶梯数量超过最利润点的层级的情况
for i := 1; i <= costLadderCount; i++ {
priceRatioAndPurchases := costLadderPriceRatio[strconv.Itoa(i)]
for i := 0; i < costLadderCount; i++ {
priceRatioAndPurchases := priceRatioList[i]
// 阶梯数量系数正序取
costPurchases := sku.Moq * priceRatioAndPurchases.Get("purchases").Int()
costPurchases := sku.Moq * priceRatioAndPurchases.Purchases
// 阶梯数量上限为库存量,超出则不再计算下级阶梯
if costPurchases > sku.Stock {
break
}
priceCnRatio := priceRatioAndPurchases.Get("price").Float()
priceUsRatio := priceRatioAndPurchases.Get("price_usd").Float()
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: priceCnRatio, RatioUsd: priceUsRatio})
priceCnRatio := priceRatioAndPurchases.Ratio
priceUsRatio := priceRatioAndPurchases.RatioUsd
// 阶梯价格系数正序取
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: sku.Moq * priceRatioAndPurchases.Get("purchases").Int(),
Purchases: sku.Moq * priceRatioAndPurchases.Purchases,
PriceCn: c.MyRound(c.MulFloat(costPriceCn, priceCnRatio), 4),
PriceUs: c.MyRound(c.MulFloat(costPriceUs, priceUsRatio), 4),
})
}
sku.PriceRatio = showPriceRatio
}
return generatedLadderPrice, showPriceRatio
sku.LadderPrice = generatedLadderPrice
return sku
} else {
ladderPriceRatio := gjson.Get(priceRatio, "ladder_price_egt50_lt200").Map()
//fmt.Println(ladderPriceRatio)
ladderCount := len(sku.LadderPrice)
//走阶梯价
if ladderCount <= ladderPriceMiniProfitLevel {
for i := 1; i <= ladderCount; i++ {
ladder := sku.LadderPrice[i-1]
for i := 0; i < ladderCount; i++ {
ladder := sku.LadderPrice[i]
//利润阶梯索引
//计算出库存满足了n个价格阶梯之后 从最小利润点层级反向取n个层级,然后正序计算 如:库存满足1、2、3层阶梯,最小利润点层级是第5个层级,则利润点取3、4、5层级的
//最小利润点层级 - 库存满足多少个阶梯 + i
costMapIndex := ladderPriceMiniProfitLevel - ladderCount + i
if costMapIndex <= 0 {
costMapIndex = 1
costMapIndex = 0
}
//fmt.Println(costMapIndex)
priceRatio := ladderPriceRatio[strconv.Itoa(costMapIndex)]
priceRatio := priceRatioList[costMapIndex]
//fmt.Println("获取到的阶梯系数为 : ", priceRatio)
priceCnRatio := priceRatio.Get("ratio").Float()
priceUsRatio := priceRatio.Get("ratio_usd").Float()
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: priceCnRatio, RatioUsd: priceUsRatio})
priceCnRatio := priceRatio.Ratio
priceUsRatio := priceRatio.RatioUsd
// 阶梯价格系数正序取
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: ladder.Purchases,
......@@ -232,26 +345,277 @@ func (ps *PriceService) GenerateLadderPrice(sku model.LySku) (generatedLadderPri
PriceUs: c.MyRound(c.MulFloat(ladder.PriceUs, priceUsRatio), 4),
})
}
sku.PriceRatio = showPriceRatio
} else {
//价格阶梯数量超过最利润点的层级的情况
for i := 1; i <= 9; i++ {
ladder := sku.LadderPrice[i-1]
for i := 0; i < 9; i++ {
ladder := sku.LadderPrice[i]
// 阶梯数量系数正序取
priceRatio := ladderPriceRatio[strconv.Itoa(i)]
priceCnRatio := priceRatio.Get("ratio").Float()
priceUsRatio := priceRatio.Get("ratio_usd").Float()
showPriceRatio = append(showPriceRatio, model.PriceRatio{Ratio: priceCnRatio, RatioUsd: priceUsRatio})
priceRatio := priceRatioList[i]
priceCnRatio := priceRatio.Ratio
priceUsRatio := priceRatio.RatioUsd
// 阶梯价格系数正序取
generatedLadderPrice = append(generatedLadderPrice, model.LadderPrice{
Purchases: int64(ladder.Purchases),
PriceCn: c.MyRound(c.MulFloat(ladder.PriceCn, priceRatio.Get("ratio").Float()), 4),
PriceUs: c.MyRound(c.MulFloat(ladder.PriceUs, priceRatio.Get("ratio_usd").Float()), 4),
PriceCn: c.MyRound(c.MulFloat(ladder.PriceCn, priceCnRatio), 4),
PriceUs: c.MyRound(c.MulFloat(ladder.PriceUs, priceUsRatio), 4),
})
}
sku.PriceRatio = showPriceRatio
}
return generatedLadderPrice, showPriceRatio
sku.LadderPrice = generatedLadderPrice
return sku
}
}
// 获取折扣系数(通用)
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
}
// 获取售价组(代购)
func (ps PriceService) GetPriceRatio(sku model.LySku) (model.LySku, []model.PriceRatio) {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
//找一个标志位,因为默认的全局折扣系数的数据格式和非全局的是不一样的
isDefaultPriceRatio := false
priceRatioCache, _ := redis.String(redisCon.Do("HGET", "magic_cube_price_rule_channel", sku.SupplierId))
checkNullRation := gjson.Get(priceRatioCache, "ladder_price").String()
//如果这个渠道没有对应的折扣系数,那么就去读取全局的
if priceRatioCache == "" || checkNullRation == "{}" {
isDefaultPriceRatio = true
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_channel_default"))
}
var priceRatioSort int
//这个就是最终要获取到的价格系数
var priceRatioList []model.PriceRatio
//是否找到系数的标志位
foundRatio := false
//如果只有默认系数,那么就去找默认系数
if isDefaultPriceRatio {
} else {
//拿到系数以后,就要去计算
//拿出里面的所有排序
priceRatioSortMap := gjson.Get(priceRatioCache, "ladder_price").Map()
var sortNumbers []int
for sortNumberString, _ := range priceRatioSortMap {
sortNumber, _ := strconv.Atoi(sortNumberString)
sortNumbers = append(sortNumbers, sortNumber)
}
//然后确定排序
sortNumbers = sorter.IntSliceSortDesc(sortNumbers)
//确定排序以后,就可以进行按排序(从大到小)取系数
outerLoop:
for _, sortNumber := range sortNumbers {
priceRatioSort = sortNumber
priceRatioList = nil
sortString := strconv.Itoa(sortNumber)
priceRatioArr := gjson.Get(priceRatioCache, "ladder_price."+sortString).Array()
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "ratio").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "ratio_usd").Float()
priceRatioList = append(priceRatioList, priceRatio)
}
var hasSpecialCheck = false
//判断是否有符合的商品名称
goodsNames := gjson.Get(priceRatioCache, "goods_name."+sortString).String()
if goodsNames != "" {
hasSpecialCheck = true
goodsNameList := strings.Split(goodsNames, "@€@")
//找到有对应的商品名称,那么优先级肯定是最高的了
if php2go.InArray(sku.GoodsName, goodsNameList) {
foundRatio = true
break
}
}
//判断是否有符合的标准品牌ID
brandIds := gjson.Get(priceRatioCache, "brand."+sortString).String()
if brandIds != "" {
hasSpecialCheck = true
standardBrandIdList := strings.Split(brandIds, ",")
standardBrandId := strconv.Itoa(sku.StandardBrand.StandardBrandId)
//找到有对应的品牌,那么优先级肯定是最高的了
if php2go.InArray(standardBrandId, standardBrandIdList) {
foundRatio = true
break
}
}
//判断是否有符合的eccn
eccns := gjson.Get(priceRatioCache, "eccn."+sortString).String()
if eccns != "" {
hasSpecialCheck = true
eccnList := strings.Split(eccns, ",")
//找到有对应的eccn,那么优先级肯定是最高的了
for _, eccn := range eccnList {
//判断是否有百分号匹配
//如果是纯%,那就是不对的,要跳过
if strings.Replace(eccn, "%", "", 10) == "" {
continue
}
if strings.Contains(eccn, "%") {
hasPrefix := strings.HasPrefix(eccn, "%")
hasSuffix := strings.HasSuffix(eccn, "%")
if hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.Contains(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
if hasPrefix && !hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasSuffix(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
if !hasPrefix && hasSuffix {
eccn = strings.Replace(eccn, "%", "", 10)
if strings.HasPrefix(sku.Eccn, eccn) {
foundRatio = true
break outerLoop
}
}
} else {
if sku.Eccn == eccn {
foundRatio = true
break outerLoop
}
}
}
}
//如果没有设置品牌和商品,那么这个优先级高的就会覆盖下面的了,不需要再去判断品牌和型号了
if hasSpecialCheck {
continue
}
foundRatio = true
break
}
}
//找不到特定的系数,那就去找全局的
if !foundRatio {
priceRatioCache, _ = redis.String(redisCon.Do("GET", "magic_cube_price_rule_channel_default"))
priceRatioArr := gjson.Get(priceRatioCache, "ladder_price").Array()
priceRatioList = nil
for _, value := range priceRatioArr {
var priceRatio model.PriceRatio
priceRatio.Ratio = gjson.Get(value.String(), "ratio").Float()
priceRatio.RatioUsd = gjson.Get(value.String(), "ratio_usd").Float()
priceRatioList = append(priceRatioList, priceRatio)
}
priceRatioSort = -1
}
sku.PriceRatio = priceRatioList
sku.PriceRatioSort = priceRatioSort
return sku, priceRatioList
}
// TransformSpecialSupplierPrice 这里有个前置条件处理美金价,因为element(6)存到美金字段里面的是港币,rs(21)存到美金字段里的是人民币,buerklin(1676)是欧元
// 所以要全部先转成正确的美金价才能显示
func (ps *PriceService) TransformSpecialSupplierPrice(supplierId int64, priceUs float64, usRatio float64) float64 {
//去redis获取价格
redisCon := gredis.Conn("default_r")
defer func() {
redisCon.Close()
}()
currency, _ := redis.Int(redisCon.Do("HGET", "magic_cube_supplier_currency", supplierId))
if currency > 0 {
//这里进行转换,因为这里都只能取到对应的币种转人民币的比率,我们没有直接各种币种转美金的数据,所以我这边要
//先根据对应币种转人民币,然后根据人民币转美金,才能得到不同币种对应美金的汇率
rmbRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", currency))
//人民币汇率转美金汇率
usRatio = c.MyRound(c.DivFloat(rmbRatio, usRatio), 2)
priceUs = c.MyRound(c.DivFloat(priceUs, usRatio), 4)
}
return priceUs
}
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