From ff1698a49c877e06b36cc3b8cb09e77641fd5862 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 20 Oct 2023 10:33:33 +0800 Subject: [PATCH] fix --- .../internal/handler/getpricebypidhandler.go | 35 ---- server/product/internal/handler/routes.go | 5 - .../internal/logic/getpricebypidlogic.go | 165 ------------------ server/product/internal/types/types.go | 23 --- server_api/product.api | 23 --- 5 files changed, 251 deletions(-) delete mode 100644 server/product/internal/handler/getpricebypidhandler.go delete mode 100644 server/product/internal/logic/getpricebypidlogic.go diff --git a/server/product/internal/handler/getpricebypidhandler.go b/server/product/internal/handler/getpricebypidhandler.go deleted file mode 100644 index 254799fe..00000000 --- a/server/product/internal/handler/getpricebypidhandler.go +++ /dev/null @@ -1,35 +0,0 @@ -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 GetPriceByPidHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - - var req types.GetPriceByPidReq - userinfo, err := basic.RequestParse(w, r, svcCtx, &req) - if err != nil { - return - } - - // 创建一个业务逻辑层实例 - l := logic.NewGetPriceByPidLogic(r.Context(), svcCtx) - - rl := reflect.ValueOf(l) - basic.BeforeLogic(w, r, rl) - - resp := l.GetPriceByPid(&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 589462eb..c3ab280e 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -22,11 +22,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_model_by_pid", Handler: GetModelByPidHandler(serverCtx), }, - { - Method: http.MethodGet, - Path: "/api/product/get_price_by_pid", - Handler: GetPriceByPidHandler(serverCtx), - }, { Method: http.MethodGet, Path: "/api/product/get_product_step_price", diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go deleted file mode 100644 index 025a9c33..00000000 --- a/server/product/internal/logic/getpricebypidlogic.go +++ /dev/null @@ -1,165 +0,0 @@ -package logic - -import ( - "errors" - "fmt" - "fusenapi/model/gmodel" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "fusenapi/utils/format" - "fusenapi/utils/step_price" - "sort" - "strings" - - "gorm.io/gorm" - - "context" - - "fusenapi/server/product/internal/svc" - "fusenapi/server/product/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type GetPriceByPidLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceByPidLogic { - return &GetPriceByPidLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -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") - } - //查询产品价格 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, []int64{productInfo.Id}) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") - } - if len(priceList) == 0 { - return resp.SetStatusWithMessage(basic.CodeOK, "success:price list is empty") - } - //处理价格信息 - mapRsp := make(map[string]*types.GetPriceByPidRsp) - for _, priceInfo := range priceList { - stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepNum, ",")) - if err != nil { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", priceInfo.Id)) - } - stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepPrice, ",")) - if err != nil { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", priceInfo.Id)) - } - if len(stepPriceSlice) == 0 || len(stepNumSlice) == 0 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "number of step num or step price is zero") - } - lenStepNum := len(stepNumSlice) - itemList := make([]types.PriceItem, 0, 10) - tmpMinBuyNum := *priceInfo.MinBuyNum - for tmpMinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) { - itemList = append(itemList, types.PriceItem{ - Num: tmpMinBuyNum, - TotalNum: tmpMinBuyNum * (*priceInfo.EachBoxNum), - Price: step_price.GetCentStepPrice(int(tmpMinBuyNum), stepNumSlice, stepPriceSlice), - }) - tmpMinBuyNum++ - } - //组装阶梯数量范围价格 - stepRange := l.dealWithStepRange(stepNumSlice, stepPriceSlice, priceInfo) - //排序(必须放在其他逻辑之后) - sort.Ints(stepPriceSlice) - minPrice := float64(stepPriceSlice[0]) / 100 - maxPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100 - mapKey := l.getSizePriceMapKey(*priceInfo.SizeId) - mapRsp[mapKey] = &types.GetPriceByPidRsp{ - Items: itemList, - MinPrice: minPrice, - MaxPrice: maxPrice, - StepRange: stepRange, - } - } - return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) -} - -// 组装阶梯价格范围 -func (l *GetPriceByPidLogic) dealWithStepRange(stepNumSlice, stepPriceSlice []int, priceInfo gmodel.FsProductPrice) []types.StepRange { - //要求写死不影响前端展示 - return []types.StepRange{ - { - Begin: 1000, - End: 2999, - Price: 0.23, - }, - { - Begin: 3000, - End: 4999, - Price: 0.2, - }, - { - Begin: 5000, - End: -1, - Price: 0.1, - }, - } - //下面是正常的 - lenStepNum := len(stepNumSlice) - lenStepPrice := len(stepPriceSlice) - stepListRsp := make([]types.StepRange, 0, lenStepNum) - //只有一个阶梯价格 - if lenStepPrice == 1 { - stepListRsp = append(stepListRsp, types.StepRange{ - Begin: *priceInfo.MinBuyNum * (*priceInfo.EachBoxNum), - End: -1, - Price: float64(stepPriceSlice[0]) / 100, - }) - return stepListRsp - } - begin := int64(0) - end := int64(0) - for numKey, stepNum := range stepNumSlice { - //先取最后一个 - tmpPrice := float64(stepPriceSlice[lenStepPrice-1]) / 100 - //如果同下标下面有价格 - if numKey < lenStepPrice { - tmpPrice = float64(stepPriceSlice[numKey]) / 100 - } - begin = int64(stepNum) * (*priceInfo.EachBoxNum) - //不是最后一个 - if numKey < lenStepNum-1 { - nextBegin := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum) - end = nextBegin - 1 - } else { - end = -1 - } - stepListRsp = append(stepListRsp, types.StepRange{ - Begin: begin, - End: end, - Price: tmpPrice, - }) - } - return stepListRsp -} - -// 获取mapKey -func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string { - return fmt.Sprintf("_%d", sizeId) -} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 75a17371..79b7078f 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -74,29 +74,6 @@ type GetModelByPidReq struct { Pid string `form:"pid"` //实际上是产品sn } -type GetPriceByPidReq struct { - Pid string `form:"pid"` -} - -type GetPriceByPidRsp struct { - Items []PriceItem `json:"items"` - MinPrice float64 `json:"min_price"` - MaxPrice float64 `json:"max_price"` - StepRange []StepRange `json:"step_range"` -} - -type StepRange struct { - Begin int64 `json:"begin"` - End int64 `json:"end"` - Price float64 `json:"price"` -} - -type PriceItem struct { - Num int64 `json:"num"` - TotalNum int64 `json:"total_num"` - Price int64 `json:"price"` -} - type GetProductStepPriceReq struct { ProductId int64 `form:"product_id"` } diff --git a/server_api/product.api b/server_api/product.api index 1571492c..0bccedc0 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -16,9 +16,6 @@ service product { //获取产品模型信息 @handler GetModelByPidHandler get /api/product/get_model_by_pid(GetModelByPidReq) returns (response); - //获取产品阶梯价格列表(即将废弃) - @handler GetPriceByPidHandler - get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response); //获取产品阶梯价格信息 @handler GetProductStepPriceHandler get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response); @@ -116,26 +113,6 @@ type CoverDefaultItem { type GetModelByPidReq { Pid string `form:"pid"` //实际上是产品sn } -//获取产品阶梯价格 -type GetPriceByPidReq { - Pid string `form:"pid"` -} -type GetPriceByPidRsp { - Items []PriceItem `json:"items"` - MinPrice float64 `json:"min_price"` - MaxPrice float64 `json:"max_price"` - StepRange []StepRange `json:"step_range"` -} -type StepRange { - Begin int64 `json:"begin"` - End int64 `json:"end"` - Price float64 `json:"price"` -} -type PriceItem { - Num int64 `json:"num"` - TotalNum int64 `json:"total_num"` - Price int64 `json:"price"` -} //获取产品阶梯价格信息 type GetProductStepPriceReq { ProductId int64 `form:"product_id"`