2023-09-26 02:54:11 +00:00
|
|
|
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 := "主体装箱数为步进基数"
|
2023-09-27 08:01:22 +00:00
|
|
|
//配件价格
|
|
|
|
fittingPrice := int64(0)
|
2023-09-26 02:54:11 +00:00
|
|
|
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))
|
|
|
|
}
|
2023-09-27 08:01:22 +00:00
|
|
|
//最小/最大价格要加配件
|
|
|
|
fittingPrice = *fittingPriceInfo.Price
|
|
|
|
minPrice += fittingPrice
|
|
|
|
maxPrice += fittingPrice
|
2023-09-26 02:54:11 +00:00
|
|
|
//如果配件装箱基数比主体大,则基数以配件为主
|
|
|
|
if *fittingPriceInfo.PackedUnit > stepPurchaseQuantity {
|
|
|
|
stepPurchaseQuantity = *fittingPriceInfo.PackedUnit
|
|
|
|
stepPurchaseQuantityDescription = "配件装箱数为步进基数"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stepRange := make([]interface{}, 0, rangeLen)
|
2023-09-26 10:35:27 +00:00
|
|
|
for rIndex, rangeInfo := range stepPrice.PriceRange {
|
|
|
|
//最后一个
|
|
|
|
if rIndex+1 == rangeLen {
|
2023-09-27 07:12:18 +00:00
|
|
|
begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity))
|
2023-09-26 10:35:27 +00:00
|
|
|
stepRange = append(stepRange, map[string]interface{}{
|
2023-09-27 09:10:17 +00:00
|
|
|
"start": rangeInfo.StartQuantity,
|
|
|
|
"end": rangeInfo.EndQuantity,
|
2023-09-27 07:12:18 +00:00
|
|
|
"range_description": fmt.Sprintf(">=%s Units", begin),
|
2023-09-27 08:01:22 +00:00
|
|
|
"item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3),
|
2023-09-26 10:35:27 +00:00
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2023-09-27 07:12:18 +00:00
|
|
|
begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity))
|
|
|
|
end := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.EndQuantity))
|
2023-09-26 02:54:11 +00:00
|
|
|
stepRange = append(stepRange, map[string]interface{}{
|
2023-09-27 09:10:17 +00:00
|
|
|
"start": rangeInfo.StartQuantity,
|
|
|
|
"end": rangeInfo.EndQuantity,
|
2023-09-27 07:12:18 +00:00
|
|
|
"range_description": fmt.Sprintf("%s-%s Units", begin, end),
|
2023-09-27 08:01:22 +00:00
|
|
|
"item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3),
|
2023-09-26 02:54:11 +00:00
|
|
|
})
|
|
|
|
}
|
2023-09-27 08:51:29 +00:00
|
|
|
//计算起购数量的单价
|
|
|
|
_, minBuyUnitsQuantityPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(stepPrice.MinBuyUnitsNum, stepPrice, fittingPrice)
|
|
|
|
if err != nil {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get min buy quantity item price")
|
|
|
|
}
|
2023-09-26 02:54:11 +00:00
|
|
|
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,
|
2023-09-27 09:03:15 +00:00
|
|
|
"min_buy_units_quantity_total_price": format.CentitoDollarWithNoHalfAdjust(minBuyUnitsQuantityPrice*stepPrice.MinBuyUnitsNum, 2),
|
2023-09-27 08:51:29 +00:00
|
|
|
"min_buy_units_quantity_item_price": format.CentitoDollar(minBuyUnitsQuantityPrice, 3),
|
2023-09-26 02:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
// }
|