fusenapi/server/product/internal/logic/getpricebypidlogic.go

165 lines
4.9 KiB
Go
Raw Normal View History

2023-07-14 11:25:52 +00:00
package logic
import (
"errors"
"fmt"
2023-07-21 04:14:19 +00:00
"fusenapi/model/gmodel"
2023-07-14 11:25:52 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
2023-07-17 02:54:37 +00:00
"fusenapi/utils/step_price"
2023-07-14 11:25:52 +00:00
"gorm.io/gorm"
2023-07-21 03:34:36 +00:00
"sort"
2023-07-14 11:25:52 +00:00
"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")
}
2023-07-17 06:40:24 +00:00
if len(priceList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:price list is empty")
}
2023-07-14 11:25:52 +00:00
//处理价格信息
2023-07-17 02:54:37 +00:00
mapRsp := make(map[string]*types.GetPriceByPidRsp)
2023-07-21 03:34:36 +00:00
for _, priceInfo := range priceList {
stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepNum, ","))
2023-07-14 11:25:52 +00:00
if err != nil {
2023-07-21 03:34:36 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", priceInfo.Id))
2023-07-17 02:54:37 +00:00
}
2023-07-21 03:34:36 +00:00
stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepPrice, ","))
2023-07-17 02:54:37 +00:00
if err != nil {
2023-07-21 03:34:36 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", priceInfo.Id))
2023-07-17 02:54:37 +00:00
}
2023-07-21 09:27:31 +00:00
if len(stepPriceSlice) == 0 || len(stepNumSlice) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "number of step num or step price is zero")
}
2023-07-17 02:54:37 +00:00
lenStepNum := len(stepNumSlice)
itemList := make([]types.PriceItem, 0, 10)
2023-07-21 09:49:02 +00:00
tmpMinBuyNum := *priceInfo.MinBuyNum
for tmpMinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) {
2023-07-17 02:54:37 +00:00
itemList = append(itemList, types.PriceItem{
2023-07-21 09:49:02 +00:00
Num: tmpMinBuyNum,
TotalNum: tmpMinBuyNum * (*priceInfo.EachBoxNum),
Price: step_price.GetCentStepPrice(int(tmpMinBuyNum), stepNumSlice, stepPriceSlice),
2023-07-17 02:54:37 +00:00
})
2023-07-21 09:49:02 +00:00
tmpMinBuyNum++
2023-07-17 02:54:37 +00:00
}
2023-07-21 04:08:32 +00:00
//组装阶梯数量范围价格
2023-07-21 09:31:28 +00:00
stepRange := l.dealWithStepRange(stepNumSlice, stepPriceSlice, priceInfo)
2023-07-21 03:34:36 +00:00
//排序(必须放在其他逻辑之后)
sort.Ints(stepPriceSlice)
minPrice := float64(stepPriceSlice[0]) / 100
maxPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100
mapKey := l.getSizePriceMapKey(*priceInfo.SizeId)
mapRsp[mapKey] = &types.GetPriceByPidRsp{
Items: itemList,
MinPrice: minPrice,
MaxPrice: maxPrice,
2023-07-21 09:31:28 +00:00
StepRange: stepRange,
2023-07-14 11:25:52 +00:00
}
}
2023-07-17 02:54:37 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
}
2023-07-21 04:14:19 +00:00
// 组装阶梯价格范围
2023-07-21 09:31:28 +00:00
func (l *GetPriceByPidLogic) dealWithStepRange(stepNumSlice, stepPriceSlice []int, priceInfo gmodel.FsProductPrice) []types.StepRange {
2023-08-10 03:34:04 +00:00
//要求写死不影响前端展示
return []types.StepRange{
{
Begin: 1000,
End: 2999,
Price: 0.23,
},
{
Begin: 3000,
End: 4999,
Price: 0.2,
},
{
Begin: 5000,
End: -1,
Price: 0.1,
},
}
//下面是正常的
2023-07-21 04:14:19 +00:00
lenStepNum := len(stepNumSlice)
lenStepPrice := len(stepPriceSlice)
2023-07-21 09:31:28 +00:00
stepListRsp := make([]types.StepRange, 0, lenStepNum)
2023-08-10 02:47:48 +00:00
//只有一个阶梯价格
if lenStepPrice == 1 {
stepListRsp = append(stepListRsp, types.StepRange{
Begin: *priceInfo.MinBuyNum * (*priceInfo.EachBoxNum),
End: -1,
Price: float64(stepPriceSlice[0]) / 100,
})
return stepListRsp
}
begin := int64(0)
end := int64(0)
2023-07-21 04:14:19 +00:00
for numKey, stepNum := range stepNumSlice {
//先取最后一个
tmpPrice := float64(stepPriceSlice[lenStepPrice-1]) / 100
//如果同下标下面有价格
if numKey < lenStepPrice {
tmpPrice = float64(stepPriceSlice[numKey]) / 100
}
2023-08-10 02:47:48 +00:00
begin = int64(stepNum) * (*priceInfo.EachBoxNum)
//不是最后一个
if numKey < lenStepNum-1 {
nextBegin := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum)
end = nextBegin - 1
} else {
2023-07-21 09:49:02 +00:00
end = -1
2023-07-21 04:14:19 +00:00
}
2023-07-21 09:31:28 +00:00
stepListRsp = append(stepListRsp, types.StepRange{
2023-07-21 09:49:02 +00:00
Begin: begin,
End: end,
Price: tmpPrice,
2023-07-21 04:14:19 +00:00
})
}
return stepListRsp
}
// 获取mapKey
2023-07-17 02:54:37 +00:00
func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string {
return fmt.Sprintf("_%d", sizeId)
2023-07-14 11:25:52 +00:00
}