package logic import ( "context" "encoding/json" "strconv" "product/internal/apply" "product/internal/dao" "product/internal/model" "product/internal/svc" "product/internal/wecom" "product/product" "product/validator" "pkg.local/log" "pkg.local/modelbase" "pkg.local/utils" validateService "pkg.local/validate" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type EditSusceptibleLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewEditSusceptibleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditSusceptibleLogic { return &EditSusceptibleLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *EditSusceptibleLogic) EditSusceptible(in *product.EditSusceptibleReq) (*product.Response, error) { var req validator.ProductEditSusceptibleValidator if msg := validateService.ValidateFromProto(in, &req); msg != utils.StringEmpty { return outResponse(utils.ErrorParams, msg), nil } if req.Type == dao.ProductTypeNormal { if req.AgentPrice == 0 || req.StorePrice == 0 || req.SalePrice == 0 || req.SharePrice == 0 || req.SaleReward == "" { return outResponse(utils.ErrorParams, "缺少相关价格"), nil } } // 管理员信息后续从 ctx / metadata 取 adminId := 0 adminName := "" if !apply.CanEdit(l.ctx, adminId, int(req.Id)) { return failErr(utils.ErrorAuthority), nil } w := modelbase.Params{ Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)}, In: map[string][]string{ "verify_status": { strconv.Itoa(int(dao.VerifyStatusPass)), strconv.Itoa(int(dao.VerifyStatusRefuse)), }, }, } var old dao.ProductEditSusceptible err := model.ProductModel{}.Init().GetOne(w, &old) if err != nil { log.Errorf("edit susceptible get: %v", err) return failResponse(utils.Fail), nil } if old.Id < 1 { return failResponse(utils.ErrorNotFund), nil } data := dao.ProductEditSusceptible{ Id: int(req.Id), ModelCode: req.ModelCode, Price: float32(req.Price), StorePrice: float32(req.StorePrice), SalePrice: float32(req.SalePrice), SharePrice: float32(req.SharePrice), AgentPrice: float32(req.AgentPrice), SaleReward: req.SaleReward, BoxNumber: req.BoxNumber, Type: uint8(req.Type), NormsNumber: uint8(req.NormsNumber), AdminName: adminName, AdminId: adminId, } contentBytes, err := json.Marshal(data) if err != nil { log.Errorf("edit susceptible marshal: %v", err) return failResponse(utils.Fail), nil } err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { pm := model.ProductModel{}.Init().WithTX(tx) if _, err := pm.Edit(w, &dao.ProductVerifyFirstEditStatus{ VerifyStatus: dao.VerifyStatusChecking, }); err != nil { return err } verify := dao.VerifyCreate{ VerifyId: strconv.FormatInt(req.Id, 10), Content: string(contentBytes), Type: dao.VerifyTypeProduct, } return model.VerifyModel{}.Init().WithTX(tx).Create(&verify) }) if err != nil { log.Errorf("edit susceptible tx: %v", err) return failResponse(utils.Fail), nil } addEditLog(int(req.Id), dao.EditLogTypeProduct, adminId, adminName, data, old) wecom.NotifyProduct(wecom.NotifyJob{ ObjId: int(req.Id), ObjType: dao.EditLogTypeProduct, Type: wecom.ProductSubmitReview, }) _ = apply.Del(l.ctx, adminId, int(req.Id)) return okResponse(utils.StringEmpty), nil }