package logic import ( "context" "encoding/json" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/pkg/validate" product "lone-services/rpc/product/pb" "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" "time" jsoniter "github.com/json-iterator/go" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type VerifyStatusLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewVerifyStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyStatusLogic { return &VerifyStatusLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *VerifyStatusLogic) VerifyFirst(in *product.VerifyStatusReq) (*product.Response, error) { return l.verifyStatus(dao.VerifyStatusChecking, in) } func (l *VerifyStatusLogic) VerifySecond(in *product.VerifyStatusReq) (*product.Response, error) { return l.verifyStatus(dao.VerifyStatusSecond, in) } func (l *VerifyStatusLogic) verifyStatus(expectStatus uint8, in *product.VerifyStatusReq) (*product.Response, error) { var req validator.ProductVerifyStatusValidator if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty { return outResponse(utils.ErrorParams, msg), nil } if req.Status == uint32(dao.VerifyActionReject) && len(req.Reason) < 1 { return failResponse(utils.ErrorReasonParams), nil } var info dao.VerifyStatusRow w := modelbase.Params{ Eq: map[string]string{ "id": strconv.FormatInt(req.Id, 10), "verify_status": strconv.Itoa(int(expectStatus)), }, } err := model.VerifyModel{}.Init().GetOne(w, &info) if err != nil { l.Errorf("verify status get: %v", err) return failResponse(utils.Fail), nil } if info.Id < 1 { return failResponse(utils.ErrorNotFund), nil } adminInfo := utils.GetUserFromCtx(l.ctx) if adminInfo.ID < utils.NumberOne { return failResponse(utils.ErrorNoLoginInfo), nil } adminId := int(adminInfo.ID) adminName := adminInfo.Name nextStatus := dao.VerifyStatusPass reason := req.Reason if req.Status == uint32(dao.VerifyActionReject) { nextStatus = dao.VerifyStatusRefuse } else if expectStatus == dao.VerifyStatusChecking { nextStatus = dao.VerifyStatusSecond } productWhere := modelbase.Params{Eq: map[string]string{"id": info.VerifyId}} var productExist dao.CheckExist err = model.ProductModel{}.Init().GetOne(productWhere, &productExist) if err != nil { l.Errorf("verify product get: %v", err) return failResponse(utils.Fail), nil } if productExist.Id < 1 { return outResponse(utils.ErrorNotFund, "没有找到产品信息"), nil } err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { vm := model.VerifyModel{}.Init().WithTX(tx) pm := model.ProductModel{}.Init().WithTX(tx) now := time.Now().Format(time.DateTime) if expectStatus == dao.VerifyStatusChecking { if _, err := vm.Edit(w, &dao.VerifyFirstUpdate{ AdminId: adminId, AdminName: adminName, VerifyStatus: nextStatus, VerifyReason: reason, VerifyTime: now, }); err != nil { return err } _, err := pm.Edit(productWhere, &dao.VerifyFirstStatus{ VerifyStatus: nextStatus, VerifyId: adminId, VerifyName: adminName, Reason: reason, }) return err } if _, err := vm.Edit(w, &dao.VerifySecondUpdate{ AdminSecondId: adminId, AdminSecondName: adminName, VerifyStatus: nextStatus, VerifyReason: reason, VerifyTime: now, }); err != nil { return err } if nextStatus == dao.VerifyStatusPass { var content dao.VerifyContent if err := jsoniter.Unmarshal([]byte(info.Content), &content); err != nil { l.Errorf("product verify content json: %v content=%s", err, info.Content) return err } _, err := pm.Edit(productWhere, &dao.VerifySecondOkStatus{ ModelCode: content.ModelCode, Price: content.Price, StorePrice: content.StorePrice, SalePrice: content.SalePrice, SharePrice: content.SharePrice, AgentPrice: content.AgentPrice, SaleReward: content.SaleReward, Type: content.Type, NormsNumber: content.NormsNumber, BoxNumber: content.BoxNumber, VerifyStatus: nextStatus, VerifySecondId: adminId, VerifySecondName: adminName, Reason: reason, }) return err } _, err := pm.Edit(productWhere, &dao.VerifySecondStatus{ VerifyStatus: nextStatus, VerifySecondId: adminId, VerifySecondName: adminName, Reason: reason, }) return err }) if err != nil { l.Errorf("verify status tx: %v", err) if isJSONError(err) { return failResponse(utils.ErrorFormatDataError), nil } return failResponse(utils.Fail), nil } notifyType := wecom.ProductFirstReview if expectStatus == dao.VerifyStatusSecond { notifyType = wecom.ProductSecondReview } objID, _ := strconv.Atoi(info.VerifyId) wecom.NotifyProduct(wecom.NotifyJob{ ObjId: objID, ObjType: dao.VerifyTypeProduct, Type: notifyType, Action: uint8(req.Status), Reason: reason, }) return okResponse(nil), nil } func isJSONError(err error) bool { switch err.(type) { case *json.SyntaxError, *json.UnmarshalTypeError: return true default: return false } }