fix
This commit is contained in:
parent
47a1ecfc79
commit
01cc115a60
@ -142,3 +142,13 @@ func (d *FsProductModel3dModel) GetAllByProductIdTag(ctx context.Context, produc
|
|||||||
err = db.Find(&resp).Error
|
err = db.Find(&resp).Error
|
||||||
return resp, err
|
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
|
||||||
|
}
|
||||||
|
@ -11,22 +11,22 @@ import (
|
|||||||
"fusenapi/server/product/internal/types"
|
"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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var req types.CaculateProductPriceReq
|
var req types.CalculateProductPriceReq
|
||||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建一个业务逻辑层实例
|
// 创建一个业务逻辑层实例
|
||||||
l := logic.NewCaculateProductPriceLogic(r.Context(), svcCtx)
|
l := logic.NewCalculateProductPriceLogic(r.Context(), svcCtx)
|
||||||
|
|
||||||
rl := reflect.ValueOf(l)
|
rl := reflect.ValueOf(l)
|
||||||
basic.BeforeLogic(w, r, rl)
|
basic.BeforeLogic(w, r, rl)
|
||||||
|
|
||||||
resp := l.CaculateProductPrice(&req, userinfo)
|
resp := l.CalculateProductPrice(&req, userinfo)
|
||||||
|
|
||||||
if !basic.AfterLogic(w, r, rl, resp) {
|
if !basic.AfterLogic(w, r, rl, resp) {
|
||||||
basic.NormalAfterLogic(w, r, resp)
|
basic.NormalAfterLogic(w, r, resp)
|
@ -79,8 +79,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Path: "/api/product/caculate_product_price",
|
Path: "/api/product/calculate_product_price",
|
||||||
Handler: CaculateProductPriceHandler(serverCtx),
|
Handler: CalculateProductPriceHandler(serverCtx),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
|
@ -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)
|
|
||||||
// }
|
|
97
server/product/internal/logic/calculateproductpricelogic.go
Normal file
97
server/product/internal/logic/calculateproductpricelogic.go
Normal file
@ -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)
|
||||||
|
// }
|
@ -335,13 +335,19 @@ type GetProductStepPriceReq struct {
|
|||||||
ProductId int64 `form:"product_id"`
|
ProductId int64 `form:"product_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CaculateProductPriceReq struct {
|
type CalculateProductPriceReq struct {
|
||||||
ProductId int64 `json:"product_id"`
|
ProductId int64 `json:"product_id"`
|
||||||
SizeId int64 `json:"size_id"`
|
SizeId int64 `json:"size_id"`
|
||||||
FittingId int64 `json:"fitting_id,optional"`
|
FittingId int64 `json:"fitting_id,optional"`
|
||||||
PurchaseQuantity int64 `json:"purchase_quantity"`
|
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 {
|
type GetSizeByPidReq struct {
|
||||||
Pid string `form:"pid"`
|
Pid string `form:"pid"`
|
||||||
TemplateTag string `form:"template_tag"`
|
TemplateTag string `form:"template_tag"`
|
||||||
|
@ -51,8 +51,8 @@ service product {
|
|||||||
@handler GetProductStepPriceHandler
|
@handler GetProductStepPriceHandler
|
||||||
get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response);
|
get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response);
|
||||||
//根据产品+配件搭配计算价格
|
//根据产品+配件搭配计算价格
|
||||||
@handler CaculateProductPriceHandler
|
@handler CalculateProductPriceHandler
|
||||||
post /api/product/caculate_product_price(CaculateProductPriceReq) returns (response);
|
post /api/product/calculate_product_price(CalculateProductPriceReq) returns (response);
|
||||||
//获取产品尺寸列表
|
//获取产品尺寸列表
|
||||||
@handler GetSizeByPidHandler
|
@handler GetSizeByPidHandler
|
||||||
get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response);
|
get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response);
|
||||||
@ -387,12 +387,17 @@ type GetProductStepPriceReq {
|
|||||||
ProductId int64 `form:"product_id"`
|
ProductId int64 `form:"product_id"`
|
||||||
}
|
}
|
||||||
//根据产品+配件搭配计算价格
|
//根据产品+配件搭配计算价格
|
||||||
type CaculateProductPriceReq {
|
type CalculateProductPriceReq {
|
||||||
ProductId int64 `json:"product_id"`
|
ProductId int64 `json:"product_id"`
|
||||||
SizeId int64 `json:"size_id"`
|
SizeId int64 `json:"size_id"`
|
||||||
FittingId int64 `json:"fitting_id,optional"`
|
FittingId int64 `json:"fitting_id,optional"`
|
||||||
PurchaseQuantity int64 `json:"purchase_quantity"`
|
PurchaseQuantity int64 `json:"purchase_quantity"`
|
||||||
}
|
}
|
||||||
|
type CalculateProductPriceRsp {
|
||||||
|
ItemPrice string `json:"item_price"`
|
||||||
|
TotalPrice string `json:"total_price"`
|
||||||
|
PurchaseQuantity int64 `json:"purchase_quantity"`
|
||||||
|
}
|
||||||
//获取产品尺寸列表
|
//获取产品尺寸列表
|
||||||
type GetSizeByPidReq {
|
type GetSizeByPidReq {
|
||||||
Pid string `form:"pid"`
|
Pid string `form:"pid"`
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"fusenapi/model/gmodel"
|
"fusenapi/model/gmodel"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 返回厘
|
// 旧的返回厘(即将废弃)
|
||||||
func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 {
|
func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 {
|
||||||
if minBuyNum > stepNum[len(stepNum)-1] {
|
if minBuyNum > stepNum[len(stepNum)-1] {
|
||||||
return int64(stepPrice[len(stepPrice)-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)
|
l := len(stepPrice.PriceRange)
|
||||||
if l == 0 {
|
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 {
|
for k, v := range stepPrice.PriceRange {
|
||||||
@ -33,34 +33,34 @@ func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruc
|
|||||||
if purchaseQuantity > v.StartQuantity {
|
if purchaseQuantity > v.StartQuantity {
|
||||||
//最后一个 || 小于等于终点
|
//最后一个 || 小于等于终点
|
||||||
if k == l-1 || purchaseQuantity <= v.EndQuantity {
|
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)
|
l := len(stepPrice.PriceRange)
|
||||||
if l == 0 {
|
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 {
|
for k, v := range stepPrice.PriceRange {
|
||||||
//购买数量>起点
|
//购买数量>起点
|
||||||
if purchaseQuantity > v.StartQuantity {
|
if purchaseQuantity > v.StartQuantity {
|
||||||
//最后一个
|
//最后一个 || 小于等于终点
|
||||||
if k == l-1 {
|
if k == l-1 || purchaseQuantity <= v.EndQuantity {
|
||||||
return v.Price + fittingPrice, nil
|
itemPrice = v.Price + fittingPrice
|
||||||
}
|
return itemPrice * purchaseQuantity, itemPrice, nil
|
||||||
//小于等于终点
|
|
||||||
if purchaseQuantity <= v.EndQuantity {
|
|
||||||
return v.Price + fittingPrice, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//遍历里面没有则返回第一个
|
//遍历里面没有则返回第一个
|
||||||
return stepPrice.PriceRange[0].Price + fittingPrice, nil
|
itemPrice = stepPrice.PriceRange[0].Price + fittingPrice
|
||||||
|
return itemPrice * purchaseQuantity, itemPrice, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user