129 lines
3.8 KiB
Go
129 lines
3.8 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 CreateLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLogic {
|
|
return &CreateLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *CreateLogic) Create(in *sale.CreateReq) (*sale.Response, error) {
|
|
var req validator.CreateValidator
|
|
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
|
|
}
|
|
|
|
identity, idErr := ensureSaleIdentity(l.ctx, l.svcCtx.UserSvcName, req.Mobile, req.Password)
|
|
if idErr != nil {
|
|
l.Errorf("sale ensure identity: %v", idErr)
|
|
if msg, ok := userRPCError(idErr); ok {
|
|
return outResponse(utils.Fail, msg), nil
|
|
}
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if identity.GetUserId() < utils.NumberOne || identity.GetMobile() == utils.StringEmpty {
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
m := model.SaleModel{}.Init()
|
|
var existing dao.SaleStatusUpdate
|
|
if err := m.GetOne(modelbase.Params{
|
|
Eq: map[string]string{"user_id": strconv.FormatInt(identity.GetUserId(), utils.NumberTen)},
|
|
}, &existing); err != nil {
|
|
l.Errorf("sale create check user_id: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if existing.Id > utils.NumberZero {
|
|
return failResponse(utils.ErrorDataIsExist), nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
birthDate, bErr := utils.ParseDateOnly(req.BirthDate)
|
|
if bErr != nil {
|
|
return outResponse(utils.ErrorParams, "生日格式不正确"), nil
|
|
}
|
|
|
|
data := dao.SaleCreate{
|
|
UserId: identity.GetUserId(),
|
|
SaleId: req.SaleId,
|
|
Mobile: identity.GetMobile(),
|
|
Name: req.Name,
|
|
IdCardFront: req.IdCardFront,
|
|
IdCardBack: req.IdCardBack,
|
|
BusinessLicense: req.BusinessLicense,
|
|
Region: buildRegion(province, city, district),
|
|
ProvinceId: req.ProvinceId,
|
|
CityId: req.CityId,
|
|
DistrictId: req.DistrictId,
|
|
TerritoryId: req.TerritoryId,
|
|
TerritoryName: territoryName,
|
|
GroupId: req.GroupId,
|
|
Address: req.Address,
|
|
Status: dao.SaleStatusEnabled,
|
|
Account: req.Account,
|
|
Type: uint8(req.Type),
|
|
Sex: uint8(req.Sex),
|
|
BirthDate: birthDate,
|
|
AdminId: adminInfo.ID,
|
|
AdminName: adminInfo.Name,
|
|
}
|
|
if err := m.Create(&data); err != nil {
|
|
l.Errorf("sale create: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
return okResponse(nil), nil
|
|
}
|