This commit is contained in:
laodaming 2023-09-26 10:54:11 +08:00
parent 5b3e3f047e
commit 916e050585
6 changed files with 181 additions and 77 deletions

View File

@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
)
func GetProductStepPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductStepPriceReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetProductStepPriceLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.GetProductStepPrice(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/product/get_price_by_pid",
Handler: GetPriceByPidHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/product/get_product_step_price",
Handler: GetProductStepPriceHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/product/get_size_by_pid",

View File

@ -35,83 +35,6 @@ func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get
}
}
// 新的阶梯价格(到时候替换)
/*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")
}
//查询产品价格
modelPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdTag(l.ctx, productInfo.Id, constants.TAG_MODEL, "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")
}
if len(stepPrice.PriceRange) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price is not set:%d", modelPriceInfo.Id))
}
//最小单价
minPrice := stepPrice.PriceRange[len(stepPrice.PriceRange)-1].Price
//最大单价
maxPrice := stepPrice.PriceRange[0].Price
//购买数步进基数
stepPurchaseQuantity := *modelPriceInfo.PackedUnit
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
}
}
mapRsp[fmt.Sprintf("_%d", *modelPriceInfo.SizeId)] = map[string]interface{}{
"step_purchase_quantity": stepPurchaseQuantity,
"min_price": minPrice,
"max_price": maxPrice,
"step_range": stepPrice.PriceRange,
"min_buy_units_num": stepPrice.MinBuyUnitsNum,
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
}
*/
func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Pid = strings.Trim(req.Pid, " ")
if req.Pid == "" {

View File

@ -0,0 +1,130 @@
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 _, rangeInfo := range stepPrice.PriceRange {
stepRange = append(stepRange, map[string]interface{}{
"start_quantity": rangeInfo.StartQuantity,
"end_quantity": 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)
// }

View File

@ -331,6 +331,10 @@ type PriceItem struct {
Price int64 `json:"price"`
}
type GetProductStepPriceReq struct {
ProductId int64 `form:"product_id"`
}
type GetSizeByPidReq struct {
Pid string `form:"pid"`
TemplateTag string `form:"template_tag"`

View File

@ -47,6 +47,9 @@ service product {
//获取产品阶梯价格列表
@handler GetPriceByPidHandler
get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response);
//获取产品阶梯价格信息
@handler GetProductStepPriceHandler
get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response);
//获取产品尺寸列表
@handler GetSizeByPidHandler
get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response);
@ -376,6 +379,10 @@ type PriceItem {
TotalNum int64 `json:"total_num"`
Price int64 `json:"price"`
}
//获取产品阶梯价格信息
type GetProductStepPriceReq {
ProductId int64 `form:"product_id"`
}
//获取产品尺寸列表
type GetSizeByPidReq {
Pid string `form:"pid"`