138 lines
5.1 KiB
Go
138 lines
5.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"fusenapi/constants"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/format"
|
|
"gorm.io/gorm"
|
|
|
|
"fusenapi/server/product/internal/svc"
|
|
"fusenapi/server/product/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetProductStepPriceLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetProductStepPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductStepPriceLogic {
|
|
return &GetProductStepPriceLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
// 处理进入前逻辑w,r
|
|
// func (l *GetProductStepPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
// }
|
|
|
|
func (l *GetProductStepPriceLogic) GetProductStepPrice(req *types.GetProductStepPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
if req.ProductId <= 0 {
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id")
|
|
}
|
|
//获取产品信息(只是获取id)
|
|
_, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "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")
|
|
}
|
|
//查询产品价格
|
|
modelPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdTag(l.ctx, req.ProductId, constants.TAG_MODEL, "id,size_id,part_id,step_price,packed_unit")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model price list")
|
|
}
|
|
fittingIds := make([]int64, 0, len(modelPriceList))
|
|
for _, v := range modelPriceList {
|
|
if *v.PartId > 0 {
|
|
fittingIds = append(fittingIds, *v.PartId)
|
|
}
|
|
}
|
|
//查询配件价格列表
|
|
fittingPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag(l.ctx, fittingIds, constants.TAG_PARTS, "id,price,packed_unit")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting price list")
|
|
}
|
|
mapFitting := make(map[int64]gmodel.FsProductModel3d)
|
|
for _, v := range fittingPriceList {
|
|
mapFitting[v.Id] = v
|
|
}
|
|
//遍历处理模型价格
|
|
mapRsp := make(map[string]interface{})
|
|
for _, modelPriceInfo := range modelPriceList {
|
|
var stepPrice gmodel.StepPriceJsonStruct
|
|
if err = json.Unmarshal(*modelPriceInfo.StepPrice, &stepPrice); err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price json")
|
|
}
|
|
rangeLen := len(stepPrice.PriceRange)
|
|
if rangeLen == 0 {
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price is not set:%d", modelPriceInfo.Id))
|
|
}
|
|
//最小单价
|
|
minPrice := stepPrice.PriceRange[rangeLen-1].Price
|
|
//最大单价
|
|
maxPrice := stepPrice.PriceRange[0].Price
|
|
//购买数步进基数
|
|
stepPurchaseQuantity := *modelPriceInfo.PackedUnit
|
|
//购买数步进基数描述
|
|
stepPurchaseQuantityDescription := "主体装箱数为步进基数"
|
|
if *modelPriceInfo.PartId > 0 {
|
|
fittingPriceInfo, ok := mapFitting[*modelPriceInfo.PartId]
|
|
if !ok {
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("fitting price is not exists:%d", *modelPriceInfo.PartId))
|
|
}
|
|
//最小价格要加配件
|
|
minPrice += *fittingPriceInfo.Price
|
|
//如果配件装箱基数比主体大,则基数以配件为主
|
|
if *fittingPriceInfo.PackedUnit > stepPurchaseQuantity {
|
|
stepPurchaseQuantity = *fittingPriceInfo.PackedUnit
|
|
stepPurchaseQuantityDescription = "配件装箱数为步进基数"
|
|
}
|
|
}
|
|
stepRange := make([]interface{}, 0, rangeLen)
|
|
for rIndex, rangeInfo := range stepPrice.PriceRange {
|
|
//最后一个
|
|
if rIndex+1 == rangeLen {
|
|
stepRange = append(stepRange, map[string]interface{}{
|
|
"range_description": fmt.Sprintf(">=%s Units", format.NumToStringWithThousandthPercentile(rangeInfo.StartQuantity)),
|
|
"item_price": format.CentitoDollar(rangeInfo.Price, 3),
|
|
})
|
|
break
|
|
}
|
|
stepRange = append(stepRange, map[string]interface{}{
|
|
"range_description": fmt.Sprintf("%s-%s Units", format.NumToStringWithThousandthPercentile(rangeInfo.StartQuantity), format.NumToStringWithThousandthPercentile(rangeInfo.EndQuantity)),
|
|
"item_price": format.CentitoDollar(rangeInfo.Price, 3),
|
|
})
|
|
}
|
|
mapRsp[fmt.Sprintf("_%d", *modelPriceInfo.SizeId)] = map[string]interface{}{
|
|
"step_purchase_quantity": stepPurchaseQuantity,
|
|
"step_purchase_quantity_description": stepPurchaseQuantityDescription,
|
|
"min_price": minPrice,
|
|
"max_price": maxPrice,
|
|
"step_range": stepRange,
|
|
"min_buy_units_quantity": stepPrice.MinBuyUnitsNum,
|
|
}
|
|
}
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
|
|
}
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
// func (l *GetProductStepPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
// }
|