174 lines
5.2 KiB
Go
174 lines
5.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
"lone-services/pkg/wecomrpc"
|
|
"lone-services/services/sale/internal/dao"
|
|
"lone-services/services/sale/internal/model"
|
|
"lone-services/services/sale/validator"
|
|
"strconv"
|
|
|
|
sale "lone-services/rpc/sale/pb"
|
|
"lone-services/services/sale/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type ApiSaleAddLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewApiSaleAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiSaleAddLogic {
|
|
return &ApiSaleAddLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// API
|
|
func (l *ApiSaleAddLogic) ApiSaleAdd(in *sale.ApiSaleAddReq) (*sale.Response, error) {
|
|
var req validator.ApiCreateValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
if msg := validateSaleTypeRules(uint32(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
|
|
}
|
|
|
|
if err := checkSaleIdentityCreatable(l.ctx, l.svcCtx.UserSvcName, req.Mobile); err != nil {
|
|
l.Errorf("sale check identity: %v", err)
|
|
if st, ok := status.FromError(err); ok && st.Code() == codes.AlreadyExists {
|
|
return failResponse(utils.ErrorDataIsExist), nil
|
|
}
|
|
if msg, ok := userRPCError(err); ok {
|
|
return outResponse(utils.Fail, msg), nil
|
|
}
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
encryptMobile, encErr := utils.EncryptPhone(req.Mobile)
|
|
if encErr != nil {
|
|
l.Errorf("sale create encrypt mobile: %v", encErr)
|
|
return failResponse(utils.ErrorEncryptAesError), nil
|
|
}
|
|
|
|
m := model.SaleModel{}.Init()
|
|
var existing dao.SaleStatusUpdate
|
|
if err := m.GetOne(modelbase.Params{
|
|
Eq: map[string]string{"mobile": encryptMobile},
|
|
}, &existing); err != nil {
|
|
l.Errorf("sale create check mobile: %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, in.ProvinceId, req.ProvinceId, req.CityId, req.DistrictId)
|
|
if nErr != nil {
|
|
l.Errorf("sale regions: %v", nErr)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
territoryName := names[in.ProvinceId]
|
|
if territoryName == utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, "请选择地域"), nil
|
|
}
|
|
province := names[in.ProvinceId]
|
|
if province == utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, "请选择省份"), nil
|
|
}
|
|
city := names[in.CityId]
|
|
if city == utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, "请选择城市"), nil
|
|
}
|
|
district := names[in.DistrictId]
|
|
if district == utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, "请选择区县"), nil
|
|
}
|
|
|
|
birthDate, bErr := utils.ParseDateOnly(in.BirthDate)
|
|
if bErr != nil {
|
|
return outResponse(utils.ErrorParams, "生日格式不正确"), nil
|
|
}
|
|
|
|
data := dao.SaleCreate{
|
|
SaleId: adminInfo.BizId,
|
|
Mobile: encryptMobile,
|
|
Name: in.Name,
|
|
IdCardFront: in.IdCardFront,
|
|
IdCardBack: in.IdCardBack,
|
|
BusinessLicense: in.BusinessLicense,
|
|
Region: buildRegion(province, city, district),
|
|
ProvinceId: in.ProvinceId,
|
|
ProvinceName: province,
|
|
CityId: in.CityId,
|
|
CityName: city,
|
|
DistrictId: in.DistrictId,
|
|
DistrictName: district,
|
|
TerritoryId: in.ProvinceId,
|
|
TerritoryName: territoryName,
|
|
GroupId: adminInfo.GroupId,
|
|
Address: in.Address,
|
|
Status: utils.StatusWaitPass,
|
|
Account: in.Account,
|
|
Type: uint8(in.Type),
|
|
Sex: uint8(in.Sex),
|
|
BirthDate: birthDate,
|
|
}
|
|
m.Begin()
|
|
|
|
if err := m.Create(&data); err != nil {
|
|
m.Rollback()
|
|
l.Errorf("sale create: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
password := utils.GetRandString(utils.NumberFive)
|
|
identity, idErr := ensureSaleIdentity(l.ctx, l.svcCtx.UserSvcName, in.Mobile, password, data.Id)
|
|
if idErr != nil {
|
|
m.Rollback()
|
|
l.Errorf("sale ensure identity: %v", idErr)
|
|
return failResponse(utils.ErrorInternalServer), nil
|
|
}
|
|
if identity.GetUserId() < utils.NumberOne || identity.GetMobile() == utils.StringEmpty {
|
|
m.Rollback()
|
|
l.Error("sale rollback after empty identity")
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
if _, err := m.Edit(modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(data.Id, utils.NumberTen)},
|
|
}, map[string]interface{}{
|
|
"user_id": identity.GetUserId(),
|
|
"mobile": identity.GetMobile(),
|
|
}); err != nil {
|
|
l.Errorf("sale bind user_id: %v", err)
|
|
m.Rollback()
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
m.Commit()
|
|
Time := utils.Now().String()
|
|
md5 := utils.MD5Encrypt(strconv.Itoa(int(data.Id)) + data.Name + Time)
|
|
wecomrpc.SendMessage(l.ctx, l.svcCtx.WecomSvcName, wecomrpc.SendMessageJob{
|
|
BizId: int(data.Id), // 门店ID
|
|
BizType: wecomrpc.BizTypeSales,
|
|
Type: wecomrpc.ProductSubmitReview,
|
|
Key: md5,
|
|
})
|
|
|
|
return okResponse(nil), nil
|
|
}
|