From 01cc115a6065a1ed12c7f9b1d3c24f095aa5c6b3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:40:27 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_model3d_logic.go | 10 ++ ...ler.go => calculateproductpricehandler.go} | 8 +- server/product/internal/handler/routes.go | 4 +- .../logic/caculateproductpricelogic.go | 57 ----------- .../logic/calculateproductpricelogic.go | 97 +++++++++++++++++++ server/product/internal/types/types.go | 8 +- server_api/product.api | 11 ++- utils/step_price/price.go | 30 +++--- 8 files changed, 143 insertions(+), 82 deletions(-) rename server/product/internal/handler/{caculateproductpricehandler.go => calculateproductpricehandler.go} (69%) delete mode 100644 server/product/internal/logic/caculateproductpricelogic.go create mode 100644 server/product/internal/logic/calculateproductpricelogic.go diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index dba92122..df5f7200 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -142,3 +142,13 @@ func (d *FsProductModel3dModel) GetAllByProductIdTag(ctx context.Context, produc err = db.Find(&resp).Error return resp, err } +func (d *FsProductModel3dModel) FindOneByProductIdSizeIdTag(ctx context.Context, productId, sizeId, tag int64, fields ...string) (resp *FsProductModel3d, err error) { + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}). + Where("`product_id` = ? and `size_id` = ? and `tag` = ? and `status` = ?", productId, sizeId, tag, 1). + Order("sort DESC") + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Take(&resp).Error + return resp, err +} diff --git a/server/product/internal/handler/caculateproductpricehandler.go b/server/product/internal/handler/calculateproductpricehandler.go similarity index 69% rename from server/product/internal/handler/caculateproductpricehandler.go rename to server/product/internal/handler/calculateproductpricehandler.go index 1624b5bc..e50829dd 100644 --- a/server/product/internal/handler/caculateproductpricehandler.go +++ b/server/product/internal/handler/calculateproductpricehandler.go @@ -11,22 +11,22 @@ import ( "fusenapi/server/product/internal/types" ) -func CaculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func CalculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.CaculateProductPriceReq + var req types.CalculateProductPriceReq userinfo, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { return } // 创建一个业务逻辑层实例 - l := logic.NewCaculateProductPriceLogic(r.Context(), svcCtx) + l := logic.NewCalculateProductPriceLogic(r.Context(), svcCtx) rl := reflect.ValueOf(l) basic.BeforeLogic(w, r, rl) - resp := l.CaculateProductPrice(&req, userinfo) + resp := l.CalculateProductPrice(&req, userinfo) if !basic.AfterLogic(w, r, rl, resp) { basic.NormalAfterLogic(w, r, resp) diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 75311158..67db4302 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -79,8 +79,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/api/product/caculate_product_price", - Handler: CaculateProductPriceHandler(serverCtx), + Path: "/api/product/calculate_product_price", + Handler: CalculateProductPriceHandler(serverCtx), }, { Method: http.MethodGet, diff --git a/server/product/internal/logic/caculateproductpricelogic.go b/server/product/internal/logic/caculateproductpricelogic.go deleted file mode 100644 index 2d31f963..00000000 --- a/server/product/internal/logic/caculateproductpricelogic.go +++ /dev/null @@ -1,57 +0,0 @@ -package logic - -import ( - "errors" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "gorm.io/gorm" - - "context" - - "fusenapi/server/product/internal/svc" - "fusenapi/server/product/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type CaculateProductPriceLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewCaculateProductPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaculateProductPriceLogic { - return &CaculateProductPriceLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *CaculateProductPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -func (l *CaculateProductPriceLogic) CaculateProductPrice(req *types.CaculateProductPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { - if req.ProductId <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id") - } - if req.SizeId <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:size 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") - } - return resp.SetStatus(basic.CodeOK) -} - -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *CaculateProductPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } diff --git a/server/product/internal/logic/calculateproductpricelogic.go b/server/product/internal/logic/calculateproductpricelogic.go new file mode 100644 index 00000000..6c5cf0e4 --- /dev/null +++ b/server/product/internal/logic/calculateproductpricelogic.go @@ -0,0 +1,97 @@ +package logic + +import ( + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/format" + "fusenapi/utils/step_price" + "gorm.io/gorm" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CalculateProductPriceLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCalculateProductPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CalculateProductPriceLogic { + return &CalculateProductPriceLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *CalculateProductPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateProductPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if req.ProductId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id") + } + if req.SizeId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:size id") + } + if req.PurchaseQuantity <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:purchase quantity") + } + //获取产品信息(只是获取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") + } + //根据产品跟尺寸获取模型价格信息 + modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOneByProductIdSizeIdTag(l.ctx, req.ProductId, req.SizeId, constants.TAG_MODEL, "id,step_price") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "model info is not exists") + } + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model info") + } + var stepPrice gmodel.StepPriceJsonStruct + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price") + } + //配件 + fittingPrice := int64(0) + if req.FittingId > 0 { + fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId, "id,price") + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "fitting info is not exists") + } + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info") + fittingPrice = *fittingInfo.Price + } + totalPrice, itemPrice, err := step_price.GetNewCentStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ") + } + return resp.SetStatus(basic.CodeOK, "success", types.CalculateProductPriceRsp{ + ItemPrice: format.CentitoDollar(itemPrice, 3), + TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2), + PurchaseQuantity: req.PurchaseQuantity, + }) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *CalculateProductPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 8d4c0e2d..61d79237 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -335,13 +335,19 @@ type GetProductStepPriceReq struct { ProductId int64 `form:"product_id"` } -type CaculateProductPriceReq struct { +type CalculateProductPriceReq struct { ProductId int64 `json:"product_id"` SizeId int64 `json:"size_id"` FittingId int64 `json:"fitting_id,optional"` PurchaseQuantity int64 `json:"purchase_quantity"` } +type CalculateProductPriceRsp struct { + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} + type GetSizeByPidReq struct { Pid string `form:"pid"` TemplateTag string `form:"template_tag"` diff --git a/server_api/product.api b/server_api/product.api index 10007e2e..0fe9971e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -51,8 +51,8 @@ service product { @handler GetProductStepPriceHandler get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response); //根据产品+配件搭配计算价格 - @handler CaculateProductPriceHandler - post /api/product/caculate_product_price(CaculateProductPriceReq) returns (response); + @handler CalculateProductPriceHandler + post /api/product/calculate_product_price(CalculateProductPriceReq) returns (response); //获取产品尺寸列表 @handler GetSizeByPidHandler get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response); @@ -387,12 +387,17 @@ type GetProductStepPriceReq { ProductId int64 `form:"product_id"` } //根据产品+配件搭配计算价格 -type CaculateProductPriceReq { +type CalculateProductPriceReq { ProductId int64 `json:"product_id"` SizeId int64 `json:"size_id"` FittingId int64 `json:"fitting_id,optional"` PurchaseQuantity int64 `json:"purchase_quantity"` } +type CalculateProductPriceRsp { + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} //获取产品尺寸列表 type GetSizeByPidReq { Pid string `form:"pid"` diff --git a/utils/step_price/price.go b/utils/step_price/price.go index ec3748cd..8230f39f 100644 --- a/utils/step_price/price.go +++ b/utils/step_price/price.go @@ -5,7 +5,7 @@ import ( "fusenapi/model/gmodel" ) -// 返回厘 +// 旧的返回厘(即将废弃) func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { if minBuyNum > stepNum[len(stepNum)-1] { return int64(stepPrice[len(stepPrice)-1]) @@ -22,10 +22,10 @@ func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { } // 新的阶梯价格(返回美元) -func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (price float64, err error) { +func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice float64, err error) { l := len(stepPrice.PriceRange) if l == 0 { - return 0, errors.New("price range is not set") + return 0, 0, errors.New("price range is not set") } //遍历查询合适的价格 for k, v := range stepPrice.PriceRange { @@ -33,34 +33,34 @@ func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruc if purchaseQuantity > v.StartQuantity { //最后一个 || 小于等于终点 if k == l-1 || purchaseQuantity <= v.EndQuantity { - return float64(v.Price+fittingPrice) / 1000, nil + itemPrice = float64(v.Price+fittingPrice) / 1000 + return itemPrice * float64(purchaseQuantity), itemPrice / 1000, nil } } } //遍历里面没有则返回第一个 - return float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000, nil + itemPrice = float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000 + return itemPrice * float64(purchaseQuantity), itemPrice, nil } // 新的阶梯价格(返回厘) -func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (price int64, err error) { +func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) { l := len(stepPrice.PriceRange) if l == 0 { - return 0, errors.New("price range is not set") + return 0, 0, errors.New("price range is not set") } //遍历查询合适的价格 for k, v := range stepPrice.PriceRange { //购买数量>起点 if purchaseQuantity > v.StartQuantity { - //最后一个 - if k == l-1 { - return v.Price + fittingPrice, nil - } - //小于等于终点 - if purchaseQuantity <= v.EndQuantity { - return v.Price + fittingPrice, nil + //最后一个 || 小于等于终点 + if k == l-1 || purchaseQuantity <= v.EndQuantity { + itemPrice = v.Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil } } } //遍历里面没有则返回第一个 - return stepPrice.PriceRange[0].Price + fittingPrice, nil + itemPrice = stepPrice.PriceRange[0].Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil }