Files
lone-services/services/sale/internal/logic/editLogic.go
T
2026-09-02 09:47:34 +08:00

136 lines
4.1 KiB
Go

package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
sale "lone-services/rpc/sale/pb"
"lone-services/services/sale/internal/dao"
"lone-services/services/sale/internal/model"
"lone-services/services/sale/internal/svc"
"lone-services/services/sale/validator"
"github.com/zeromicro/go-zero/core/logx"
)
type EditLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
return &EditLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *EditLogic) Edit(in *sale.EditReq) (*sale.Response, error) {
var req validator.EditValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
if msg := validateSaleTypeRules(req.Type, req.Account, req.BusinessLicense); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)}}
m := model.SaleModel{}.Init()
var info dao.SaleCreate
if err := m.GetOne(w, &info); err != nil || info.Id < utils.NumberOne {
return failResponse(utils.ErrorNotFund), nil
}
encryptMobile := info.Mobile
oldPlain, _ := utils.DecryptPhone(info.Mobile)
if req.Mobile != oldPlain {
if info.UserId < utils.NumberOne {
return outResponse(utils.Fail, "销售未绑定用户,无法修改手机号"), nil
}
updated, uErr := updateSaleUserMobile(l.ctx, l.svcCtx.UserSvcName, info.UserId, req.Mobile)
if uErr != nil {
l.Errorf("sale edit sync user mobile: %v", uErr)
if msg, ok := userRPCError(uErr); ok {
return outResponse(utils.Fail, msg), nil
}
return failResponse(utils.Fail), nil
}
encryptMobile = updated.GetMobile()
if encryptMobile == utils.StringEmpty {
return failResponse(utils.Fail), nil
}
var check dao.SaleStatusUpdate
if err := m.GetOne(modelbase.Params{Eq: map[string]string{"mobile": encryptMobile}}, &check); err != nil {
l.Errorf("sale edit mobile check: %v", err)
return failResponse(utils.Fail), nil
}
if check.Id > utils.NumberZero && check.Id != req.Id {
return failResponse(utils.ErrorDataIsExist), nil
}
}
info.SaleId = req.SaleId
info.Mobile = encryptMobile
info.Name = req.Name
info.IdCardFront = req.IdCardFront
info.IdCardBack = req.IdCardBack
info.BusinessLicense = req.BusinessLicense
names, nErr := fetchRegionNames(l.ctx, l.svcCtx.ChoreSvcName, req.TerritoryId, req.ProvinceId, req.CityId, req.DistrictId)
if nErr != nil {
l.Errorf("sale regions: %v", nErr)
return failResponse(utils.Fail), nil
}
territoryName := names[req.TerritoryId]
if territoryName == utils.StringEmpty {
return outResponse(utils.ErrorParams, "请选择地域"), nil
}
province := names[req.ProvinceId]
if province == utils.StringEmpty {
return outResponse(utils.ErrorParams, "请选择省份"), nil
}
city := names[req.CityId]
if city == utils.StringEmpty {
return outResponse(utils.ErrorParams, "请选择城市"), nil
}
district := names[req.DistrictId]
if district == utils.StringEmpty {
return outResponse(utils.ErrorParams, "请选择区县"), nil
}
info.Region = buildRegion(province, city, district)
info.ProvinceId = req.ProvinceId
info.CityId = req.CityId
info.DistrictId = req.DistrictId
info.TerritoryId = req.TerritoryId
info.TerritoryName = territoryName
info.GroupId = req.GroupId
info.Address = req.Address
info.Type = uint8(req.Type)
info.Sex = uint8(req.Sex)
birthDate, bErr := utils.ParseDateOnly(req.BirthDate)
if bErr != nil {
return outResponse(utils.ErrorParams, "生日格式不正确"), nil
}
info.BirthDate = birthDate
info.Account = req.Account
info.AdminId = adminInfo.ID
info.AdminName = adminInfo.Name
if _, err := m.Edit(w, &info); err != nil {
l.Errorf("sale edit: %v", err)
return failResponse(utils.Fail), nil
}
return okStringResponse(utils.StringEmpty), nil
}