package logic import ( "context" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/rpc/store/pb" user "lone-services/rpc/user/pb" "lone-services/services/store/internal/dao" "lone-services/services/store/internal/model" "lone-services/services/store/internal/svc" "lone-services/services/store/validator" "strconv" "strings" "github.com/zeromicro/go-zero/core/logx" ) type EditLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger BaseLogic } func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic { return &EditLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *EditLogic) Edit(in *store.EditRequest) (*store.Response, error) { var v validator.StoreEditValidator if fail := l.checkParams(in, &v); fail != nil { return fail, nil } adminInfo := utils.GetUserFromCtx(l.ctx) if adminInfo.ID < utils.NumberOne { return l.fail(utils.ErrorNoLoginInfo) } var info dao.StoreCreate w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(in.Id, utils.NumberTen)}} modelObj := model.StoreModel{}.Init() err := modelObj.GetOne(w, &info) if err != nil || info.Id < utils.NumberOne { return l.fail(utils.ErrorNotFund) } action := utils.ActionAdd{ OldContent: info, Type: utils.LogActionTypeEdit, ModuleName: utils.LogActionModuleStore, } if info.Status != utils.StatusOk && info.Status != utils.StatusNotPass && info.Status != utils.StatusAdminSecondRefuse && info.Status != utils.StatusForbidden { return l.fail(utils.SetError(utils.Fail, "当前状态不允许修改")) } oldMobile, cErr := utils.DecryptPhone(info.UserMobile) var oldMobile2 string if cErr == nil { oldMobile2 = utils.DecryptPhoneReplace(oldMobile) } encryptMobile, cErr := utils.EncryptPhone(in.UserMobile) if cErr != nil { return l.fail(utils.SetError(utils.Fail, cErr.GetMsg())) } if oldMobile != in.UserMobile && oldMobile2 != in.UserMobile { var check dao.StoreStatus ww := modelbase.Params{Eq: map[string]string{"user_mobile": encryptMobile}} err = modelObj.GetOne(ww, &check) if err != nil { l.Logger.Error(err) return l.fail(utils.Fail) } if check.Id > utils.NumberZero && check.Id != info.Id { return l.fail(utils.ErrorDataIsExist) } } uErr := l.checkUser(in.UserMobile, in.Id) if uErr != utils.Ok { return l.fail(uErr) } if in.BusinessLicense != info.BusinessLicense { encryptData, cErr := utils.Crypto{}.AESEncryptECB(in.BusinessLicense) if cErr != nil { return l.fail(utils.SetError(utils.Fail, cErr.GetMsg())) } info.BusinessLicense = encryptData } if in.IdCard != info.IdCard { encryptIdCard, cErr := utils.Crypto{}.AESEncryptECB(in.IdCard) if cErr != nil { return l.fail(utils.SetError(utils.Fail, cErr.GetMsg())) } info.IdCard = encryptIdCard } info.SaleId = in.SaleId info.UserName = in.UserName info.Name = in.Name info.District = in.DistrictIds info.Address = in.Address info.BankInfo = in.BankInfo info.Images = in.Images info.Content = in.Content info.AdminId = adminInfo.ID info.AdminName = adminInfo.Name info.OpenTime = in.OpenTime info.LongitudeWgs84 = in.LongitudeWgs84 info.LatitudeWgs84 = in.LatitudeWgs84 info.LongitudeGcj02 = in.LongitudeGcj02 info.LatitudeGcj02 = in.LatitudeGcj02 info.StoreImages = in.StoreImages info.StoreType = strings.Join(in.StoreType, utils.DecollatorComma) info.Area = in.Area info.Receipt = in.Receipt info.Type = in.Type info.Province = in.Province info.ProvinceName = in.ProvinceName info.City = in.City info.CityName = in.CityName info.County = in.County info.CountyName = in.CountyName if oldMobile != in.UserMobile && oldMobile2 != in.UserMobile { info.UserMobile = encryptMobile } if (oldMobile != in.UserMobile && oldMobile2 != in.UserMobile) || in.UserName != info.UserName { uErr = l.updateUser(info, in.UserMobile) if uErr != utils.Ok { return l.fail(uErr) } } w = modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(in.Id, utils.NumberTen)}} edit := dao.StoreNewContent{ NewContent: utils.StructToJson(info), AdminId: adminInfo.ID, AdminName: adminInfo.Name, Status: utils.StatusAdminFirstVerify, } row, err := modelObj.Edit(w, &edit) if row > utils.NumberZero { action.NewContent = info utils.SetActionLog(adminInfo, action) } return l.ok(utils.NumberOne) } func (l *EditLogic) updateUser(info dao.StoreCreate, mobile string) utils.Status { cli, err := svc.GetRpcClient(l.svcCtx.OrderName) if err != nil { logx.Errorf("get user err: %v", err) return utils.ErrorInternalServer } client := user.NewUserClient(cli.Conn()) data := user.InternalUpdateReq{ BizId: info.Id, SubjectType: dao.UserTypeName, Mobile: mobile, Name: info.UserName, ClientCode: strings.Split(info.StoreType, utils.DecollatorComma), //哪端 CredentialType: []string{dao.LoginTypePassword, dao.LoginTypeSms}, Identifier: mobile, } _, err = client.InternalUpdate(context.Background(), &data) if err != nil { logx.Errorf("get order err: %v", err) return utils.ErrorInternalServer } return utils.Ok } func (l *EditLogic) checkUser(mobile string, id int64) utils.Status { cli, err := svc.GetRpcClient(l.svcCtx.OrderName) if err != nil { logx.Errorf("get user err: %v", err) return utils.ErrorInternalServer } client := user.NewUserClient(cli.Conn()) data := user.InternalCheckUpdateReq{ Mobile: mobile, SubjectType: dao.UserTypeName, BizId: id, } _, err = client.InternalCheckUpdate(context.Background(), &data) if err != nil { logx.Errorf("get order err: %v", err) return utils.ErrorInternalServer } return utils.Ok }