From a2fad4a095f72dffc5d79ee7d0e93bdbe735fc79 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 20 Apr 2026 16:58:33 +0800 Subject: [PATCH] f --- app/main/api/etc/main.example.yaml | 2 +- .../admin_product/admincreateproductlogic.go | 37 ++++++++++++++++--- .../admin_product/admindeleteproductlogic.go | 22 +++++++++-- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/app/main/api/etc/main.example.yaml b/app/main/api/etc/main.example.yaml index d5888cd..c04ee99 100644 --- a/app/main/api/etc/main.example.yaml +++ b/app/main/api/etc/main.example.yaml @@ -50,7 +50,7 @@ Wxpay: MchPublicKeyPath: "etc/merchant/wxpay/XXXXXXXXXXXX_XXXXXXXXXXXX_cert/pub_key.pem" MchPlatformRAS: "xxxx" NotifyUrl: "https://www.XXXXXXXXXXXXXX.com/api/v1/pay/wechat/callback" - RefundNotifyUrl: "https://www.XXXXXXXXXXXXXX.com/api/v1/wechat/refund_callback" + RefundNotifyUrl: "https://www.XXXXXXXXXXXXXX.com/api/v1/pay/wechat/refund_callback" Applepay: ProductionVerifyURL: "https://api.storekit.itunes.apple.com/inApps/v1/transactions/receipt" SandboxVerifyURL: "https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/receipt" diff --git a/app/main/api/internal/logic/admin_product/admincreateproductlogic.go b/app/main/api/internal/logic/admin_product/admincreateproductlogic.go index fb1ab1a..bf1debf 100644 --- a/app/main/api/internal/logic/admin_product/admincreateproductlogic.go +++ b/app/main/api/internal/logic/admin_product/admincreateproductlogic.go @@ -1,15 +1,17 @@ package admin_product import ( - "context" "bdrp-server/app/main/api/internal/svc" "bdrp-server/app/main/api/internal/types" "bdrp-server/app/main/model" "bdrp-server/common/xerr" + "context" "database/sql" + "math" "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/core/stores/sqlx" ) type AdminCreateProductLogic struct { @@ -27,7 +29,7 @@ func NewAdminCreateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *AdminCreateProductLogic) AdminCreateProduct(req *types.AdminCreateProductReq) (resp *types.AdminCreateProductResp, err error) { - // 1. 数据转换 + // 1. 构建产品数据 data := &model.Product{ ProductName: req.ProductName, ProductEn: req.ProductEn, @@ -37,14 +39,37 @@ func (l *AdminCreateProductLogic) AdminCreateProduct(req *types.AdminCreateProdu SellPrice: req.SellPrice, } - // 2. 数据库操作 - result, err := l.svcCtx.ProductModel.Insert(l.ctx, nil, data) + // 2. 开启事务,确保产品与代理产品配置同步创建 + var id int64 + err = l.svcCtx.ProductModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error { + result, txErr := l.svcCtx.ProductModel.Insert(ctx, session, data) + if txErr != nil { + return txErr + } + + productID, txErr := result.LastInsertId() + if txErr != nil { + return txErr + } + id = productID + + priceRangeMax := math.Max(req.CostPrice, req.SellPrice) + agentConfig := &model.AgentProductConfig{ + ProductId: productID, + CostPrice: req.CostPrice, + PriceRangeMin: req.CostPrice, + PriceRangeMax: priceRangeMax, + PricingStandard: priceRangeMax, + OverpricingRatio: 0.10, + } + _, txErr = l.svcCtx.AgentProductConfigModel.Insert(ctx, session, agentConfig) + return txErr + }) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), - "创建产品失败, err: %v, req: %+v", err, req) + "创建产品及代理产品配置失败, err: %v, req: %+v", err, req) } // 3. 返回结果 - id, _ := result.LastInsertId() return &types.AdminCreateProductResp{Id: id}, nil } diff --git a/app/main/api/internal/logic/admin_product/admindeleteproductlogic.go b/app/main/api/internal/logic/admin_product/admindeleteproductlogic.go index d02b9a7..dfd82d3 100644 --- a/app/main/api/internal/logic/admin_product/admindeleteproductlogic.go +++ b/app/main/api/internal/logic/admin_product/admindeleteproductlogic.go @@ -1,13 +1,15 @@ package admin_product import ( - "context" "bdrp-server/app/main/api/internal/svc" "bdrp-server/app/main/api/internal/types" + "bdrp-server/app/main/model" "bdrp-server/common/xerr" + "context" "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/core/stores/sqlx" ) type AdminDeleteProductLogic struct { @@ -25,15 +27,27 @@ func NewAdminDeleteProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *AdminDeleteProductLogic) AdminDeleteProduct(req *types.AdminDeleteProductReq) (resp *types.AdminDeleteProductResp, err error) { - // 1. 查询记录是否存在 + // 1. 查询产品是否存在 record, err := l.svcCtx.ProductModel.FindOne(l.ctx, req.Id) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找产品失败, err: %v, id: %d", err, req.Id) } - // 2. 执行软删除 - err = l.svcCtx.ProductModel.DeleteSoft(l.ctx, nil, record) + // 2. 事务内执行物理删除,保持 product 与 agent_product_config 同步 + err = l.svcCtx.ProductModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error { + agentCfg, findErr := l.svcCtx.AgentProductConfigModel.FindOneByProductId(ctx, req.Id) + if findErr != nil && !errors.Is(findErr, model.ErrNotFound) { + return findErr + } + if findErr == nil { + if deleteCfgErr := l.svcCtx.AgentProductConfigModel.Delete(ctx, session, agentCfg.Id); deleteCfgErr != nil { + return deleteCfgErr + } + } + + return l.svcCtx.ProductModel.Delete(ctx, session, record.Id) + }) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除产品失败, err: %v, id: %d", err, req.Id)