package logic import ( "context" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/pkg/validate" product "lone-services/rpc/product/pb" "lone-services/services/product/internal/apply" "lone-services/services/product/internal/dao" "lone-services/services/product/internal/model" "lone-services/services/product/internal/svc" "lone-services/services/product/internal/wecom" "lone-services/services/product/validator" "strconv" jsoniter "github.com/json-iterator/go" "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 := validate.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 } } adminInfo := utils.GetUserFromCtx(l.ctx) if adminInfo.ID < utils.NumberOne { return failResponse(utils.ErrorNoLoginInfo), nil } adminId := int(adminInfo.ID) adminName := adminInfo.Name if !apply.CanEdit(l.ctx, adminId, int(req.Id)) { return failResponse(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.EditSusceptible err := model.ProductModel{}.Init().GetOne(w, &old) if err != nil { l.Errorf("edit susceptible get: %v", err) return failResponse(utils.Fail), nil } if old.Id < 1 { return failResponse(utils.ErrorNotFund), nil } data := dao.EditSusceptible{ 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 := jsoniter.Marshal(data) if err != nil { l.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.VerifyFirstEditStatus{ 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 { l.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(nil), nil }