Commit 9ea9d127 by 杨树贤

Add SKU batch handling and formatting

Add Batch and BatchFormat fields to LySku and BatchFormatItem type.
Populate Batch in InitSkuData and invoke GetSkuBatchFormat in
LyGoodsDetail. Implement GetSkuBatchFormat and getSupplierBatch to
parse batch JSON or read supplier optional batch codes from Redis and
map them to human-readable labels
parent 6936f128
......@@ -104,7 +104,14 @@ type LySku struct {
//兼容自营下单的字段,取成本价的第一个阶梯的人民币
Cost float64 `json:"cost"`
CostUs float64 `json:"cost_us"`
IsZiying int `json:"is_ziying"`
IsZiying int `json:"is_ziying"`
Batch string `json:"-"`
BatchFormat interface{} `json:"batch_format"`
}
type BatchFormatItem struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
type DiscountRatio struct {
......@@ -306,6 +313,8 @@ func InitSkuData(sku string) (data LySku) {
data.AbilityLevel = int(gjson.Get(sku, "ability_level").Int())
data.Batch = gjson.Get(sku, "batch").String()
return
}
......
......@@ -365,6 +365,8 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, go
sku.Stock = int64(limitStock)
sku.BatchSn = "" //如果存在指定库存,则批次是空
}
//计算批次
ls.GetSkuBatchFormat(&sku)
//计算按钮
sku.LabelOp = activityService.GetLabelOp(sku)
//最后一步,将sku的全部信息放到有序map里面
......
package service
import (
"encoding/json"
"go_sku_server/model"
"go_sku_server/pkg/gredis"
"strconv"
"strings"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
)
var batchCodeMap = map[int]string{
-1: "无法指定",
1: "任意批次",
2: "1年内",
3: "2年内",
4: "3年内",
5: "4年内",
6: "5年内",
}
func (ls *LyService) GetSkuBatchFormat(sku *model.LySku) {
if sku.Batch != "" {
var batchObj map[string]interface{}
if err := json.Unmarshal([]byte(sku.Batch), &batchObj); err == nil {
var items []model.BatchFormatItem
for k, v := range batchObj {
items = append(items, model.BatchFormatItem{Name: k, Value: v})
}
sku.BatchFormat = items
} else {
sku.BatchFormat = sku.Batch
}
return
}
if sku.SupplierId == 0 {
sku.BatchFormat = []model.BatchFormatItem{}
return
}
supplierBatch := ls.getSupplierBatch(sku.SupplierId)
if len(supplierBatch) == 0 {
sku.BatchFormat = []model.BatchFormatItem{}
return
}
var items []model.BatchFormatItem
for _, code := range supplierBatch {
if label, ok := batchCodeMap[code]; ok {
items = append(items, model.BatchFormatItem{Name: label, Value: "可供应"})
}
}
sku.BatchFormat = items
}
func (ls *LyService) getSupplierBatch(supplierId int64) []int {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
info, err := redis.String(redisCon.Do("HGET", "SUPPLIER_REDIS_INFO_", supplierId))
if err != nil || info == "" {
return nil
}
optionalBatchStr := gjson.Get(info, "sku_optional_batch").String()
if optionalBatchStr == "" {
return nil
}
codes := strings.Split(optionalBatchStr, ",")
var result []int
for _, code := range codes {
code = strings.TrimSpace(code)
if code == "" {
continue
}
c, err := strconv.Atoi(code)
if err != nil {
continue
}
result = append(result, c)
}
return result
}
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