diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 9b01334a..dba92122 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -16,7 +16,7 @@ type StepPriceJsonStruct struct { } func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProductModel3d, err error) { - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? and `status` =? ", id, 1) if len(fields) > 0 { db = db.Select(fields[0]) } @@ -41,7 +41,7 @@ func (d *FsProductModel3dModel) GetAllByIdsWithoutStatus(ctx context.Context, id if len(ids) == 0 { return } - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?)", ids) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1) if len(fields) > 0 { db = db.Select(fields[0]) } @@ -80,13 +80,13 @@ func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3 return resp, err } func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error { - return d.db.WithContext(ctx).Where("`id` = ? ", id).Updates(&data).Error + return d.db.WithContext(ctx).Where("`id` = ? and `status` =? ", id, 1).Updates(&data).Error } func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) { if len(sizeIds) == 0 { return } - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ? and `status` = ?", sizeIds, tag, 1) if len(fields) != 0 { db = db.Select(fields[0]) } @@ -94,7 +94,7 @@ func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds return resp, err } func (d *FsProductModel3dModel) GetAll(ctx context.Context) (resp []FsProductModel3d, err error) { - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Find(&resp).Error + err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`status` = ?", 1).Find(&resp).Error return resp, err } @@ -107,7 +107,7 @@ func (d *FsProductModel3dModel) GetGroupPartListByProductIds(ctx context.Context if len(productIds) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}). + err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`product_id` in(?) and `status` =? ", productIds, 1). Select("product_id,group_concat(part_list) as part_list"). Group("product_id").Find(&resp).Error return resp, err diff --git a/server/product/internal/handler/caculateproductpricehandler.go b/server/product/internal/handler/caculateproductpricehandler.go new file mode 100644 index 00000000..1624b5bc --- /dev/null +++ b/server/product/internal/handler/caculateproductpricehandler.go @@ -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 CaculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.CaculateProductPriceReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewCaculateProductPriceLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.CaculateProductPrice(&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 260422e3..75311158 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -77,6 +77,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_product_step_price", Handler: GetProductStepPriceHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/product/caculate_product_price", + Handler: CaculateProductPriceHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/api/product/get_size_by_pid", diff --git a/server/product/internal/logic/caculateproductpricelogic.go b/server/product/internal/logic/caculateproductpricelogic.go new file mode 100644 index 00000000..2d31f963 --- /dev/null +++ b/server/product/internal/logic/caculateproductpricelogic.go @@ -0,0 +1,57 @@ +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/types/types.go b/server/product/internal/types/types.go index b9b96864..8d4c0e2d 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -335,6 +335,13 @@ type GetProductStepPriceReq struct { ProductId int64 `form:"product_id"` } +type CaculateProductPriceReq struct { + ProductId int64 `json:"product_id"` + SizeId int64 `json:"size_id"` + FittingId int64 `json:"fitting_id,optional"` + 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 94715b66..10007e2e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -50,6 +50,9 @@ 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 GetSizeByPidHandler get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response); @@ -383,6 +386,13 @@ type PriceItem { type GetProductStepPriceReq { ProductId int64 `form:"product_id"` } +//根据产品+配件搭配计算价格 +type CaculateProductPriceReq { + ProductId int64 `json:"product_id"` + SizeId int64 `json:"size_id"` + FittingId int64 `json:"fitting_id,optional"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} //获取产品尺寸列表 type GetSizeByPidReq { Pid string `form:"pid"`