258 lines
6.6 KiB
Go
258 lines
6.6 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/wecomrpc"
|
|
chore "lone-services/rpc/chore/pb"
|
|
sale "lone-services/rpc/sale/pb"
|
|
"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 WebStoreAddLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
type areaNames struct {
|
|
ProvinceId int64 `json:"province_id"`
|
|
ProvinceName string `json:"province_name"`
|
|
CityId int64 `json:"city_id"`
|
|
CityName string `json:"city_name"`
|
|
CountyId int64 `json:"county_id"`
|
|
CountyName string `json:"county_name"`
|
|
}
|
|
|
|
func NewWebStoreAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebStoreAddLogic {
|
|
return &WebStoreAddLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *WebStoreAddLogic) WebStoreAdd(in *store.WebStoreAddReq) (*store.Response, error) {
|
|
var v validator.ApiStoreCreateValidator
|
|
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)
|
|
}
|
|
|
|
uErr := l.checkUser(in.UserMobile)
|
|
if uErr != utils.Ok {
|
|
return l.fail(uErr)
|
|
}
|
|
|
|
encryptMobile, cErr := utils.EncryptPhone(v.UserMobile)
|
|
if cErr != nil {
|
|
return l.fail(utils.SetError(utils.Fail, cErr.GetMsg()))
|
|
}
|
|
|
|
encryptData, cErr := utils.Crypto{}.AESEncryptECB(v.BusinessLicense)
|
|
if cErr != nil {
|
|
return l.fail(utils.SetError(utils.Fail, cErr.GetMsg()))
|
|
}
|
|
|
|
encryptIdCard, cErr := utils.Crypto{}.AESEncryptECB(v.IdCard)
|
|
if cErr != nil {
|
|
return l.fail(utils.SetError(utils.Fail, cErr.GetMsg()))
|
|
}
|
|
|
|
groupId, sErr := l.getSaleInfo(adminInfo.ID)
|
|
if sErr != utils.Ok {
|
|
return l.fail(sErr)
|
|
}
|
|
if groupId < utils.NumberOne {
|
|
return l.fail(utils.ErrorNotFund)
|
|
}
|
|
|
|
areas, sErr := l.getAreaInfo(v.DistrictIds)
|
|
if sErr != utils.Ok {
|
|
return l.fail(sErr)
|
|
}
|
|
|
|
data := dao.StoreCreate{
|
|
UserName: v.UserName,
|
|
SourceType: dao.StoreTypeSales,
|
|
Images: v.Images,
|
|
Contact: v.Contact,
|
|
Content: v.Content,
|
|
OpenTime: v.OpenTime,
|
|
LongitudeWgs84: v.LongitudeWgs84,
|
|
LatitudeWgs84: v.LatitudeWgs84,
|
|
LongitudeGcj02: v.LongitudeGcj02,
|
|
LatitudeGcj02: v.LatitudeGcj02,
|
|
SaleId: adminInfo.ID,
|
|
UserMobile: encryptMobile,
|
|
Name: v.Name,
|
|
BankInfo: v.BankInfo,
|
|
BusinessLicense: encryptData,
|
|
District: v.DistrictIds,
|
|
Address: v.Address,
|
|
GroupId: groupId,
|
|
Status: utils.StatusWaitPass,
|
|
StoreImages: v.StoreImages,
|
|
IdCard: encryptIdCard,
|
|
Area: v.Area,
|
|
Receipt: v.Receipt,
|
|
Type: v.Type,
|
|
Province: areas.ProvinceId,
|
|
ProvinceName: areas.ProvinceName,
|
|
City: areas.CityId,
|
|
CityName: areas.CityName,
|
|
County: areas.CountyId,
|
|
CountyName: areas.CountyName,
|
|
}
|
|
|
|
modelObj := model.StoreModel{}.Init()
|
|
modelObj.Begin()
|
|
|
|
err := modelObj.Create(&data)
|
|
if err != nil {
|
|
modelObj.Rollback()
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
uErr = l.createUser(data, in.UserMobile, utils.GetRandString(utils.NumberFive))
|
|
if uErr != utils.Ok {
|
|
modelObj.Rollback()
|
|
return l.fail(uErr)
|
|
}
|
|
modelObj.Commit()
|
|
|
|
Time := utils.Now().String()
|
|
|
|
md5 := utils.MD5Encrypt(strconv.Itoa(int(data.Id)) + data.Name + Time)
|
|
wecomrpc.SendMessage(l.ctx, l.svcCtx.WeComName, wecomrpc.SendMessageJob{
|
|
BizId: int(data.Id), // 门店ID
|
|
BizType: wecomrpc.BizTypeStore,
|
|
Type: wecomrpc.ProductSubmitReview,
|
|
Key: md5,
|
|
})
|
|
|
|
modelObj.Commit()
|
|
return l.ok(utils.NumberOne)
|
|
}
|
|
|
|
func (l *WebStoreAddLogic) checkUser(mobile string) utils.Status {
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserName)
|
|
if err != nil {
|
|
logx.Errorf("get user err: %v", err)
|
|
return utils.ErrorInternalServer
|
|
}
|
|
|
|
client := user.NewUserClient(cli.Conn())
|
|
|
|
data := user.InternalCheckCreateReq{
|
|
Mobile: mobile,
|
|
}
|
|
_, err = client.InternalCheckCreate(context.Background(), &data)
|
|
if err != nil {
|
|
logx.Errorf("get order err: %v", err)
|
|
return utils.ErrorInternalServer
|
|
}
|
|
|
|
return utils.Ok
|
|
}
|
|
|
|
func (l *WebStoreAddLogic) getSaleInfo(id int64) (int64, utils.Status) {
|
|
cli, err := svc.GetRpcClient(l.svcCtx.SaleName)
|
|
if err != nil {
|
|
logx.Errorf("get order err: %v", err)
|
|
return utils.NumberZero, utils.ErrorInternalServer
|
|
}
|
|
|
|
client := sale.NewSaleClient(cli.Conn())
|
|
w := sale.InfoReq{Id: id}
|
|
info, err := client.InfoInternal(context.Background(), &w)
|
|
if err != nil {
|
|
logx.Errorf("get sale err: %v", err)
|
|
return utils.NumberZero, utils.ErrorInternalServer
|
|
}
|
|
return info.GroupId, utils.Ok
|
|
}
|
|
|
|
func (l *WebStoreAddLogic) getAreaInfo(DistrictIds string) (areaNames, utils.Status) {
|
|
var areas areaNames
|
|
var ids []int64
|
|
strArr := strings.Split(DistrictIds, utils.DecollatorComma)
|
|
for _, s := range strArr {
|
|
num, err := strconv.ParseInt(strings.TrimSpace(s), utils.NumberTen, utils.NumberSixtyFourth)
|
|
if err == nil {
|
|
ids = append(ids, num)
|
|
}
|
|
}
|
|
|
|
cli, err := svc.GetRpcClient(l.svcCtx.ChoreName)
|
|
if err != nil {
|
|
logx.Errorf("get order err: %v", err)
|
|
return areas, utils.ErrorInternalServer
|
|
}
|
|
|
|
client := chore.NewChoreClient(cli.Conn())
|
|
w := chore.RegionsByIdsReq{Ids: ids}
|
|
info, err := client.RegionsByIds(context.Background(), &w)
|
|
if err != nil {
|
|
logx.Errorf("get order err: %v", err)
|
|
return areas, utils.ErrorInternalServer
|
|
}
|
|
|
|
for k, v := range info.Items {
|
|
switch k {
|
|
case utils.NumberZero:
|
|
areas.ProvinceId = v.Id
|
|
areas.ProvinceName = v.Name
|
|
case utils.NumberOne:
|
|
areas.CityId = v.Id
|
|
areas.CityName = v.Name
|
|
case utils.NumberTwo:
|
|
areas.CountyId = v.Id
|
|
areas.CountyName = v.Name
|
|
}
|
|
}
|
|
return areas, utils.Ok
|
|
}
|
|
|
|
func (l *WebStoreAddLogic) createUser(info dao.StoreCreate, mobile, password string) utils.Status {
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserName)
|
|
if err != nil {
|
|
logx.Errorf("get user err: %v", err)
|
|
return utils.ErrorInternalServer
|
|
}
|
|
|
|
client := user.NewUserClient(cli.Conn())
|
|
|
|
data := user.InternalCreateReq{
|
|
BizId: info.Id,
|
|
Mobile: mobile,
|
|
Name: info.UserName,
|
|
ClientCode: []string{dao.StoreTypeBeauty}, //哪端
|
|
CredentialType: []string{dao.LoginTypePassword, dao.LoginTypeSms},
|
|
Identifier: mobile, //手机号
|
|
Secret: password, //密码
|
|
}
|
|
|
|
_, err = client.InternalCreate(context.Background(), &data)
|
|
if err != nil {
|
|
logx.Errorf("get user err: %v", err)
|
|
return utils.ErrorInternalServer
|
|
}
|
|
|
|
return utils.Ok
|
|
}
|