This commit is contained in:
2026-09-23 11:24:25 +08:00
parent 1ed80406ba
commit 68828285e2
16 changed files with 386 additions and 296 deletions
+34
View File
@@ -15,6 +15,7 @@ const (
SaleStatusEnabled uint8 = 1 // 开启
SaleStatusDisabled uint8 = 2 // 禁用
)
type RegionNames struct {
@@ -63,8 +64,11 @@ type SaleCreate struct {
BusinessLicense string `gorm:"column:business_license" json:"business_license"`
Region RegionNames `gorm:"column:region;type:json" json:"region"`
ProvinceId int64 `gorm:"column:province_id" json:"province_id"`
ProvinceName string `json:"province_name" gorm:"province_name"`
CityId int64 `gorm:"column:city_id" json:"city_id"`
CityName string `json:"city_name" gorm:"city_name"`
DistrictId int64 `gorm:"column:district_id" json:"district_id"`
DistrictName string `json:"district_name" gorm:"district_name"`
TerritoryId int64 `gorm:"column:territory_id" json:"territory_id"`
TerritoryName string `gorm:"column:territory_name" json:"territory_name"`
GroupId int64 `gorm:"column:group_id" json:"group_id"`
@@ -90,8 +94,11 @@ type SaleInfo struct {
BusinessLicense string `gorm:"column:business_license" json:"business_license"`
Region RegionNames `gorm:"column:region;type:json" json:"region"`
ProvinceId int64 `gorm:"column:province_id" json:"province_id"`
ProvinceName string `json:"province_name" gorm:"province_name"`
CityId int64 `gorm:"column:city_id" json:"city_id"`
CityName string `json:"city_name" gorm:"city_name"`
DistrictId int64 `gorm:"column:district_id" json:"district_id"`
DistrictName string `json:"district_name" gorm:"district_name"`
TerritoryId int64 `gorm:"column:territory_id" json:"territory_id"`
TerritoryName string `gorm:"column:territory_name" json:"territory_name"`
GroupId int64 `gorm:"column:group_id" json:"group_id"`
@@ -132,3 +139,30 @@ type SaleTypeOption struct {
Value uint8 `json:"value"`
Label string `json:"label"`
}
type APiSaleInfo struct {
Id int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
SaleId int `gorm:"column:sale_id;type:int(11);default:0;comment:父级销售;NOT NULL" json:"sale_id"`
Mobile string `gorm:"column:mobile;type:char(60);comment:手机号,也是登录账号;NOT NULL" json:"mobile"`
Name string `gorm:"column:name;type:varchar(256);comment:姓名或公司名;NOT NULL" json:"name"`
IdCardFront string `gorm:"column:id_card_front" json:"id_card_front"`
IdCardBack string `gorm:"column:id_card_back" json:"id_card_back"`
BusinessLicense string `gorm:"column:business_license" json:"business_license"`
Region RegionNames `gorm:"column:region;type:json" json:"region"`
ProvinceId int64 `gorm:"column:province_id" json:"province_id"`
ProvinceName string `gorm:"column:province_name;type:varchar(255);comment:身份名;NOT NULL" json:"province_name"`
CityId int64 `gorm:"column:city_id" json:"city_id"`
CityName string `json:"city_name" gorm:"city_name"`
DistrictId int64 `gorm:"column:district_id" json:"district_id"`
DistrictName string `json:"district_name" gorm:"district_name"`
TerritoryId int64 `gorm:"column:territory_id" json:"territory_id"`
TerritoryName string `gorm:"column:territory_name" json:"territory_name"`
Address string `gorm:"column:address;type:varchar(255);comment:地址" json:"address"`
Account string `gorm:"column:account;type:varchar(255);comment:账户信息" json:"account"`
Status uint8 `gorm:"column:status;type:tinyint(1);default:9;comment:9是待处理 1是已通过 2是未通过,3为禁用;NOT NULL" json:"status"`
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
Type uint8 `gorm:"column:type;type:tinyint(1);default:1;comment:类型,默认为1.1为公司销售,2为公司代理,3为个人代理" json:"type"`
Sex uint8 `gorm:"column:sex;type:tinyint(1);default:3;comment:性别,1男,2女,3未知" json:"sex"`
BirthDate string `gorm:"column:birth_date;type:char(12);comment:生日" json:"birth_date"`
Reason string `gorm:"column:reason;type:varchar(255);comment:理由" json:"reason"`
}
@@ -2,9 +2,15 @@ package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
"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"
"strconv"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -24,7 +30,55 @@ func NewApiLowerSaleItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *ApiLowerSaleItemsLogic) ApiLowerSaleItems(in *sale.ApiLowerSaleItemsReq) (*sale.Response, error) {
// todo: add your logic here and delete this line
var req validator.ApiSalesItemsValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
return &sale.Response{}, nil
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
var info []dao.APiSaleInfo
w := modelbase.Params{
Page: int(in.Page),
Size: int(in.Size),
Order: "id desc",
Eq: map[string]string{"sale_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen)},
In: map[string][]string{
"status": {
utils.StringStatusOk,
utils.StatusForbiddenString,
strconv.Itoa(int(utils.StatusWaitPass))},
},
}
if in.Type > utils.NumberZero {
if in.Type == utils.NumberNine {
w.In["type"] = []string{strconv.Itoa(int(dao.SaleTypeAgentCorp)), strconv.Itoa(int(dao.SaleTypeAgentPerson))}
} else {
w.Eq["type"] = strconv.Itoa(int(in.Type))
}
}
w.Like = make(map[string]string)
if len(in.Name) > utils.NumberZero {
w.Like["name LIKE ? "] = "%%" + in.Name + "%%"
}
items, err := model.SaleModel{}.Init().Page(w, &info)
if err != nil {
l.Logger.Error(err)
return failResponse(utils.Fail), nil
}
for key, v := range info {
mobile, aErr := utils.Crypto{}.AESDecryptECB(v.Mobile)
if aErr == nil {
info[key].Mobile = mobile
}
}
items.Items = info
return okResponse(items), nil
}
+135 -2
View File
@@ -2,11 +2,20 @@ package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
"lone-services/services/sale/internal/dao"
"lone-services/services/sale/internal/model"
"lone-services/services/sale/validator"
"strconv"
"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 {
@@ -25,7 +34,131 @@ func NewApiSaleAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiSal
// API
func (l *ApiSaleAddLogic) ApiSaleAdd(in *sale.ApiSaleAddReq) (*sale.Response, error) {
// todo: add your logic here and delete this line
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
}
return &sale.Response{}, 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.ID,
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()
return okResponse(nil), nil
}
@@ -1,30 +0,0 @@
package logic
import (
"context"
"lone-services/rpc/sale/pb"
"lone-services/services/sale/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type ApiSaleInfoLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewApiSaleInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiSaleInfoLogic {
return &ApiSaleInfoLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ApiSaleInfoLogic) ApiSaleInfo(in *sale.EmptyReq) (*sale.Response, error) {
// todo: add your logic here and delete this line
return &sale.Response{}, nil
}
@@ -112,8 +112,11 @@ func (l *CreateLogic) Create(in *sale.CreateReq) (*sale.Response, error) {
BusinessLicense: req.BusinessLicense,
Region: buildRegion(province, city, district),
ProvinceId: req.ProvinceId,
ProvinceName: province,
CityId: req.CityId,
CityName: city,
DistrictId: req.DistrictId,
DistrictName: district,
TerritoryId: req.TerritoryId,
TerritoryName: territoryName,
GroupId: req.GroupId,
@@ -91,11 +91,6 @@ func (s *SaleServer) ApiLowerSaleItems(ctx context.Context, in *sale.ApiLowerSal
return l.ApiLowerSaleItems(in)
}
func (s *SaleServer) ApiSaleInfo(ctx context.Context, in *sale.EmptyReq) (*sale.Response, error) {
l := logic.NewApiSaleInfoLogic(ctx, s.svcCtx)
return l.ApiSaleInfo(in)
}
// 销售详情
func (s *SaleServer) InfoInternal(ctx context.Context, in *sale.InfoReq) (*sale.InfoData, error) {
l := logic.NewInfoInternalLogic(ctx, s.svcCtx)