feat: sale manager
This commit is contained in:
+23
-22
@@ -7,6 +7,8 @@ import (
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/discovery"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/mysql"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
@@ -88,24 +90,24 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// db, err := mysql.New(mysql.Config{
|
||||
// Host: utils.GetConfigString("mysql.host"),
|
||||
// Port: utils.GetConfigInt("mysql.port"),
|
||||
// User: utils.GetConfigString("mysql.user"),
|
||||
// Password: utils.GetConfigString("mysql.password"),
|
||||
// Database: utils.GetConfigString("mysql.database"),
|
||||
// Charset: utils.GetConfigString("mysql.charset"),
|
||||
// Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
// ReadHost: utils.GetConfigString("mysql_read.host"),
|
||||
// ReadPort: utils.GetConfigInt("mysql_read.port"),
|
||||
// ReadUser: utils.GetConfigString("mysql_read.user"),
|
||||
// ReadPassword: utils.GetConfigString("mysql_read.password"),
|
||||
// ReadDatabase: utils.GetConfigString("mysql_read.database"),
|
||||
// })
|
||||
// if err != nil {
|
||||
// logx.Errorf("mysql init: %v", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
db, err := mysql.New(mysql.Config{
|
||||
Host: utils.GetConfigString("mysql.host"),
|
||||
Port: utils.GetConfigInt("mysql.port"),
|
||||
User: utils.GetConfigString("mysql.user"),
|
||||
Password: utils.GetConfigString("mysql.password"),
|
||||
Database: utils.GetConfigString("mysql.database"),
|
||||
Charset: utils.GetConfigString("mysql.charset"),
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
ReadHost: utils.GetConfigString("mysql_read.host"),
|
||||
ReadPort: utils.GetConfigInt("mysql_read.port"),
|
||||
ReadUser: utils.GetConfigString("mysql_read.user"),
|
||||
ReadPassword: utils.GetConfigString("mysql_read.password"),
|
||||
ReadDatabase: utils.GetConfigString("mysql_read.database"),
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("mysql init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := redis.Init(redis.Config{
|
||||
Host: utils.GetConfigString("redis.host"),
|
||||
@@ -117,16 +119,15 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// debug := utils.GetConfigBool("mysql.debug")test
|
||||
// modelbase.Init(db, modelbase.Config{Prefix: utils.GetConfigString("mysql.prefix"), Debug: debug})
|
||||
debug := utils.GetConfigBool("mysql.debug")
|
||||
modelbase.Init(db, modelbase.Config{Prefix: utils.GetConfigString("mysql.prefix"), Debug: debug})
|
||||
|
||||
rpcConf := zrpc.RpcServerConf{
|
||||
ListenOn: listenOn,
|
||||
}
|
||||
rpcConf.Mode = mode
|
||||
|
||||
// ctx := svc.NewServiceContext(c, db)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
ctx := svc.NewServiceContext(c, db)
|
||||
|
||||
s := zrpc.MustNewServer(rpcConf, func(grpcServer *grpc.Server) {
|
||||
chore.RegisterChoreServer(grpcServer, server.NewChoreServer(ctx))
|
||||
|
||||
@@ -14,13 +14,17 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
PolicyReq = chore.PolicyReq
|
||||
Response = chore.Response
|
||||
TestReq = chore.TestReq
|
||||
DecryptMobileReq = chore.DecryptMobileReq
|
||||
PolicyReq = chore.PolicyReq
|
||||
RegionsReq = chore.RegionsReq
|
||||
Response = chore.Response
|
||||
TestReq = chore.TestReq
|
||||
|
||||
Chore interface {
|
||||
Policy(ctx context.Context, in *PolicyReq, opts ...grpc.CallOption) (*Response, error)
|
||||
Test(ctx context.Context, in *TestReq, opts ...grpc.CallOption) (*Response, error)
|
||||
DecryptMobile(ctx context.Context, in *DecryptMobileReq, opts ...grpc.CallOption) (*Response, error)
|
||||
Regions(ctx context.Context, in *RegionsReq, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
defaultChore struct {
|
||||
@@ -43,3 +47,13 @@ func (m *defaultChore) Test(ctx context.Context, in *TestReq, opts ...grpc.CallO
|
||||
client := chore.NewChoreClient(m.cli.Conn())
|
||||
return client.Test(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultChore) DecryptMobile(ctx context.Context, in *DecryptMobileReq, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := chore.NewChoreClient(m.cli.Conn())
|
||||
return client.DecryptMobile(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultChore) Regions(ctx context.Context, in *RegionsReq, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := chore.NewChoreClient(m.cli.Conn())
|
||||
return client.Regions(ctx, in, opts...)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package dao
|
||||
|
||||
type Region struct {
|
||||
Id uint `gorm:"column:id" json:"id"`
|
||||
ParentId *uint `gorm:"column:parent_id" json:"parent_id"`
|
||||
Code string `gorm:"column:code" json:"code"`
|
||||
Name string `gorm:"column:name" json:"name"`
|
||||
Level uint8 `gorm:"column:level" json:"level"`
|
||||
CityCode string `gorm:"column:city_code" json:"city_code"`
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/pkg/utils"
|
||||
chore "lone-services/rpc/chore/pb"
|
||||
"lone-services/services/chore/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DecryptMobileLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
type DecryptMobileData struct {
|
||||
Mobile string `json:"mobile"`
|
||||
}
|
||||
|
||||
func NewDecryptMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DecryptMobileLogic {
|
||||
return &DecryptMobileLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DecryptMobileLogic) DecryptMobile(in *chore.DecryptMobileReq) (*chore.Response, error) {
|
||||
adminInfo := utils.GetUserFromCtx(l.ctx)
|
||||
if adminInfo.ID < utils.NumberOne {
|
||||
return failResponse(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
|
||||
originMobile := in.GetOriginMobile()
|
||||
if originMobile == utils.StringEmpty {
|
||||
return failResponse(utils.ErrorParams), nil
|
||||
}
|
||||
|
||||
mobile, err := utils.DecryptPhone(originMobile)
|
||||
if err != nil {
|
||||
l.Errorf("decrypt mobile: %v", err)
|
||||
return &chore.Response{
|
||||
Code: int32(utils.ErrorDecryptAesError.GetCode()),
|
||||
Msg: utils.ErrorDecryptAesError.GetMsg(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return okResponse(DecryptMobileData{Mobile: mobile}), nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
chore "lone-services/rpc/chore/pb"
|
||||
"lone-services/services/chore/internal/dao"
|
||||
"lone-services/services/chore/internal/model"
|
||||
"lone-services/services/chore/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegionsLogic {
|
||||
return &RegionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RegionsLogic) Regions(in *chore.RegionsReq) (*chore.Response, error) {
|
||||
params := modelbase.Params{
|
||||
Order: "id ASC",
|
||||
}
|
||||
|
||||
parentID := in.GetParentId()
|
||||
if parentID < utils.NumberOne {
|
||||
params.Eq = map[string]string{"level": strconv.Itoa(int(utils.NumberOne))}
|
||||
} else {
|
||||
params.Eq = map[string]string{"parent_id": strconv.Itoa(int(parentID))}
|
||||
}
|
||||
|
||||
var list []dao.Region
|
||||
if err := (model.RegionModel{}.Init().Items(params, &list)); err != nil {
|
||||
l.Errorf("regions list: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
list = []dao.Region{}
|
||||
}
|
||||
|
||||
return okResponse(list), nil
|
||||
}
|
||||
@@ -2,12 +2,11 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"lone-services/pkg/rpcclient"
|
||||
"lone-services/pkg/utils"
|
||||
chore "lone-services/rpc/chore/pb"
|
||||
product "lone-services/rpc/product/pb"
|
||||
sale "lone-services/rpc/sale/pb"
|
||||
"lone-services/services/chore/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -33,8 +32,10 @@ func NewTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestLogic {
|
||||
|
||||
func (l *TestLogic) Test(in *chore.TestReq) (*chore.Response, error) {
|
||||
cli, err := rpcclient.Get(l.svcCtx.ProductSvcName)
|
||||
// cli, err := rpcclient.Get(l.svcCtx.SaleSvcName)
|
||||
|
||||
if err != nil {
|
||||
l.Errorf("get product rpc: %v", err)
|
||||
l.Errorf("get rpc: %v", err)
|
||||
return failResponse(utils.ErrorInternalServer), nil
|
||||
}
|
||||
|
||||
@@ -53,21 +54,40 @@ func (l *TestLogic) Test(in *chore.TestReq) (*chore.Response, error) {
|
||||
// fmt.Println(res.GetName())
|
||||
|
||||
// 获取多个产品详情
|
||||
res, err := product.NewProductClient(cli.Conn()).ItemsByIds(l.ctx, &product.ItemsByIdsReq{
|
||||
Ids: in.GetIds(),
|
||||
// res, err := product.NewProductClient(cli.Conn()).ItemsByIds(l.ctx, &product.ItemsByIdsReq{
|
||||
// Ids: in.GetIds(),
|
||||
// })
|
||||
// for _, item := range res.GetItems() {
|
||||
// fmt.Println(item.GetName())
|
||||
// }
|
||||
|
||||
// 销售 names
|
||||
res, err := sale.NewSaleClient(cli.Conn()).NamesInternal(l.ctx, &sale.NamesReq{
|
||||
Type: in.GetNumber(),
|
||||
Level: uint32(in.GetId()),
|
||||
})
|
||||
for _, item := range res.GetItems() {
|
||||
fmt.Println(item.GetName())
|
||||
}
|
||||
|
||||
// TODO: 批量扣减库存
|
||||
// items := make([]*product.NumberItem, 0, len(in.GetIds()))
|
||||
// for _, id := range in.GetIds() {
|
||||
// items = append(items, &product.NumberItem{
|
||||
// Id: id,
|
||||
// Number: in.GetNumber(),
|
||||
// })
|
||||
// }
|
||||
// _, err = product.NewProductClient(cli.Conn()).NumberItems(l.ctx, &product.NumberItemsReq{
|
||||
// Type: productNumberTypeDecr,
|
||||
// Items: items,
|
||||
// })
|
||||
|
||||
if err != nil {
|
||||
l.Errorf("错误信息: %v", err)
|
||||
l.Errorf("XXXXXXXXXXX: %v", err)
|
||||
return failResponse(utils.ErrorInternalServer), nil
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
l.Errorf("res为空")
|
||||
l.Errorf("XXXXXXXXXXX res为空")
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
return okResponse(res), nil
|
||||
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"lone-services/pkg/modelbase"
|
||||
)
|
||||
|
||||
type RegionModel struct {
|
||||
modelbase.Base
|
||||
}
|
||||
|
||||
func (m RegionModel) TableName() string {
|
||||
return modelbase.Prefix() + "regions"
|
||||
}
|
||||
|
||||
func (m RegionModel) Init() RegionModel {
|
||||
m.Table = m.TableName()
|
||||
return m
|
||||
}
|
||||
@@ -32,3 +32,13 @@ func (s *ChoreServer) Test(ctx context.Context, in *chore.TestReq) (*chore.Respo
|
||||
l := logic.NewTestLogic(ctx, s.svcCtx)
|
||||
return l.Test(in)
|
||||
}
|
||||
|
||||
func (s *ChoreServer) DecryptMobile(ctx context.Context, in *chore.DecryptMobileReq) (*chore.Response, error) {
|
||||
l := logic.NewDecryptMobileLogic(ctx, s.svcCtx)
|
||||
return l.DecryptMobile(in)
|
||||
}
|
||||
|
||||
func (s *ChoreServer) Regions(ctx context.Context, in *chore.RegionsReq) (*chore.Response, error) {
|
||||
l := logic.NewRegionsLogic(ctx, s.svcCtx)
|
||||
return l.Regions(in)
|
||||
}
|
||||
|
||||
@@ -5,21 +5,30 @@ import (
|
||||
"lone-services/services/chore/internal/config"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
ProductSvcName string
|
||||
SaleSvcName string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
productSvc := utils.GetConfigString("services.product")
|
||||
if productSvc == utils.StringEmpty {
|
||||
logx.Error("config services.product empty")
|
||||
}
|
||||
saleSvc := utils.GetConfigString("services.sale")
|
||||
if saleSvc == utils.StringEmpty {
|
||||
logx.Error("config services.sale empty")
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
ProductSvcName: productSvc,
|
||||
SaleSvcName: saleSvc,
|
||||
}
|
||||
}
|
||||
|
||||
+20
-1
@@ -27,6 +27,24 @@
|
||||
dir = "upload/"
|
||||
expireSeconds = 3600
|
||||
|
||||
[mysql]
|
||||
host = '39.106.171.204'
|
||||
port = 33066
|
||||
user = 'root'
|
||||
password = 'MOLXRZNOU4Y4'
|
||||
database = 'dms-chore'
|
||||
charset = 'utf8mb4'
|
||||
prefix = ''
|
||||
debug = true
|
||||
[mysql_read]
|
||||
host = '39.106.171.204'
|
||||
port = 33066
|
||||
user = 'root'
|
||||
password = 'MOLXRZNOU4Y4'
|
||||
database = 'dms-chore'
|
||||
charset = 'utf8mb4'
|
||||
prefix = ''
|
||||
|
||||
[redis]
|
||||
host = '39.106.171.204'
|
||||
password = 'lLMLcuPpzSj'
|
||||
@@ -37,4 +55,5 @@
|
||||
data_key = "u2t9T3luZtoRfhBstkFN6TiIMW38BA8a"
|
||||
|
||||
[services]
|
||||
product = "product-service"
|
||||
product = "product-service"
|
||||
sale = "sale-service"
|
||||
Reference in New Issue
Block a user