96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/format"
|
|
"fusenapi/utils/step_price"
|
|
"gorm.io/gorm"
|
|
"strings"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/product/internal/svc"
|
|
"fusenapi/server/product/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetPriceByPidLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceByPidLogic {
|
|
return &GetPriceByPidLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
req.Pid = strings.Trim(req.Pid, " ")
|
|
if req.Pid == "" {
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
|
|
}
|
|
//获取产品信息(只是获取id)
|
|
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
|
|
}
|
|
//查询产品价格
|
|
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, []int64{productInfo.Id})
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
|
|
}
|
|
//处理价格信息
|
|
mapRsp := make(map[string]*types.GetPriceByPidRsp)
|
|
for _, price := range priceList {
|
|
stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ","))
|
|
if err != nil {
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", price.Id))
|
|
}
|
|
stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ","))
|
|
if err != nil {
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", price.Id))
|
|
}
|
|
lenStepNum := len(stepNumSlice)
|
|
itemList := make([]types.PriceItem, 0, 10)
|
|
for *price.MinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) {
|
|
itemList = append(itemList, types.PriceItem{
|
|
Num: *price.MinBuyNum,
|
|
TotalNum: (*price.MinBuyNum) * (*price.EachBoxNum),
|
|
Price: step_price.GetCentStepPrice(int(*price.MinBuyNum), stepNumSlice, stepPriceSlice),
|
|
})
|
|
*price.MinBuyNum++
|
|
}
|
|
minPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100
|
|
maxPrice := float64(stepPriceSlice[0]) / 100
|
|
mapKey := l.getSizePriceMapKey(*price.SizeId)
|
|
if _, ok := mapRsp[mapKey]; ok {
|
|
mapRsp[mapKey].Items = append(mapRsp[mapKey].Items, itemList...)
|
|
mapRsp[mapKey].MinPrice = minPrice
|
|
mapRsp[mapKey].MaxPrice = maxPrice
|
|
} else {
|
|
mapRsp[mapKey] = &types.GetPriceByPidRsp{
|
|
Items: itemList,
|
|
MinPrice: minPrice,
|
|
MaxPrice: maxPrice,
|
|
}
|
|
}
|
|
}
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
|
|
}
|
|
func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string {
|
|
return fmt.Sprintf("_%d", sizeId)
|
|
}
|