feat: sale manager
This commit is contained in:
@@ -155,7 +155,7 @@ func (l *CreateLogic) Create(in *product.CreateReq) (*product.Response, error) {
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
func parsePublishTime(s string) (time.Time, error) {
|
||||
|
||||
@@ -68,7 +68,7 @@ func (l *EditApplyLogic) EditApply(in *product.EditApplyReq) (*product.Response,
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
// EditApplyPass TODO: 临时接口,企微编辑申请审批对接后删除
|
||||
@@ -104,5 +104,5 @@ func (l *EditApplyLogic) EditApplyPass(in *product.EditApplyPassReq) (*product.R
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -123,5 +123,5 @@ func (l *EditBaseLogic) EditBase(in *product.EditBaseReq) (*product.Response, er
|
||||
})
|
||||
_ = apply.Del(l.ctx, adminId, int(req.Id))
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -127,5 +127,5 @@ func (l *EditSusceptibleLogic) EditSusceptible(in *product.EditSusceptibleReq) (
|
||||
})
|
||||
_ = apply.Del(l.ctx, adminId, int(req.Id))
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -57,5 +57,5 @@ func (l *InfoLogic) Info(in *product.InfoReq) (*product.Response, error) {
|
||||
info.Edit = applyData.Status
|
||||
}
|
||||
|
||||
return okResponse(utils.StructToJson(toProductItem(info))), nil
|
||||
return okResponse(toProductItem(info)), nil
|
||||
}
|
||||
|
||||
@@ -76,8 +76,8 @@ func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.Response, error) {
|
||||
items = append(items, toProductItem(row))
|
||||
}
|
||||
|
||||
return okResponse(utils.StructToJson(&product.ItemsData{
|
||||
return okResponse(&product.ItemsData{
|
||||
Count: result.Count,
|
||||
Items: items,
|
||||
})), nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
@@ -44,5 +44,5 @@ func (l *NamesLogic) Names(_ *product.NamesReq) (*product.Response, error) {
|
||||
Name: row.Name,
|
||||
})
|
||||
}
|
||||
return okResponse(utils.StructToJson(items)), nil
|
||||
return okResponse(items), nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"lone-services/pkg/validate"
|
||||
product "lone-services/rpc/product/pb"
|
||||
"lone-services/services/product/internal/dao"
|
||||
"lone-services/services/product/internal/model"
|
||||
"lone-services/services/product/internal/svc"
|
||||
"lone-services/services/product/validator"
|
||||
|
||||
@@ -40,10 +41,10 @@ func (l *NumberAddLogic) NumberAdd(in *product.NumberAddReq) (*product.Response,
|
||||
return failResponse(utils.ErrorNoLoginInfo), nil
|
||||
}
|
||||
|
||||
if err := changeProductNumber(l.Logger, req.Id, dao.NumberTypeAdd, req.Number); err != nil {
|
||||
if err := changeProductNumber(l.Logger, model.ProductModel{}.Init(), req.Id, dao.NumberTypeAdd, req.Number); err != nil {
|
||||
return mapNumberErr(err), nil
|
||||
}
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
func mapNumberErr(err error) *product.Response {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
product "lone-services/rpc/product/pb"
|
||||
"lone-services/services/product/internal/model"
|
||||
"lone-services/services/product/internal/svc"
|
||||
"lone-services/services/product/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type NumberItemsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewNumberItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NumberItemsLogic {
|
||||
return &NumberItemsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *NumberItemsLogic) NumberItems(in *product.NumberItemsReq) (*emptypb.Empty, error) {
|
||||
var req validator.ProductNumberItemsValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return nil, status.Error(codes.InvalidArgument, msg)
|
||||
}
|
||||
if len(in.GetItems()) < 1 {
|
||||
return nil, status.Error(codes.InvalidArgument, "产品列表不能为空")
|
||||
}
|
||||
|
||||
items := make([]*product.NumberItem, 0, len(in.GetItems()))
|
||||
for _, item := range in.GetItems() {
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
if item.GetId() < 1 {
|
||||
return nil, status.Error(codes.InvalidArgument, "产品ID必须大于0")
|
||||
}
|
||||
if item.GetNumber() < 1 {
|
||||
return nil, status.Error(codes.InvalidArgument, "数量必须大于0")
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if len(items) < 1 {
|
||||
return nil, status.Error(codes.InvalidArgument, "产品列表不能为空")
|
||||
}
|
||||
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
return items[i].GetId() < items[j].GetId()
|
||||
})
|
||||
|
||||
err := l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
||||
m := model.ProductModel{}.Init().WithTX(tx)
|
||||
for _, item := range items {
|
||||
if err := changeProductNumber(l.Logger, m, item.GetId(), req.Type, item.GetNumber()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
@@ -40,18 +40,17 @@ func (l *NumberLogic) Number(in *product.NumberReq) (*emptypb.Empty, error) {
|
||||
return nil, status.Error(codes.InvalidArgument, msg)
|
||||
}
|
||||
|
||||
if err := changeProductNumber(l.Logger, req.Id, req.Type, req.Number); err != nil {
|
||||
if err := changeProductNumber(l.Logger, model.ProductModel{}.Init(), req.Id, req.Type, req.Number); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func changeProductNumber(logger logx.Logger, id int64, typ uint32, number uint32) error {
|
||||
func changeProductNumber(logger logx.Logger, m model.ProductModel, id int64, typ uint32, number uint32) error {
|
||||
var info dao.NumberInfo
|
||||
w := modelbase.Params{
|
||||
Eq: map[string]string{"id": strconv.FormatInt(id, 10)},
|
||||
}
|
||||
m := model.ProductModel{}.Init()
|
||||
if err := m.GetOne(w, &info); err != nil {
|
||||
logger.Errorf("product number get: %v", err)
|
||||
return status.Error(codes.Internal, utils.Fail.Msg)
|
||||
|
||||
@@ -3,13 +3,16 @@ package logic
|
||||
import (
|
||||
"lone-services/pkg/utils"
|
||||
product "lone-services/rpc/product/pb"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func okResponse(data string) *product.Response {
|
||||
func okResponse(data any) *product.Response {
|
||||
buf, _ := jsoniter.Marshal(data)
|
||||
return &product.Response{
|
||||
Code: utils.Ok.Code,
|
||||
Msg: utils.Ok.Msg,
|
||||
Data: data,
|
||||
Data: string(buf),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,5 +63,5 @@ func (l *SortLogic) Sort(in *product.SortReq) (*product.Response, error) {
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -72,5 +72,5 @@ func (l *StatusLogic) Status(in *product.StatusReq) (*product.Response, error) {
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
@@ -67,10 +67,10 @@ func (l *VerifyLogic) Verify(in *product.VerifyReq) (*product.Response, error) {
|
||||
|
||||
items := make([]*product.VerifyItem, 0)
|
||||
if len(rows) == 0 {
|
||||
return okResponse(utils.StructToJson(&product.VerifyItemsData{
|
||||
return okResponse(&product.VerifyItemsData{
|
||||
Count: result.Count,
|
||||
Items: items,
|
||||
})), nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(rows))
|
||||
@@ -142,8 +142,8 @@ func (l *VerifyLogic) Verify(in *product.VerifyReq) (*product.Response, error) {
|
||||
})
|
||||
}
|
||||
|
||||
return okResponse(utils.StructToJson(&product.VerifyItemsData{
|
||||
return okResponse(&product.VerifyItemsData{
|
||||
Count: result.Count,
|
||||
Items: items,
|
||||
})), nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
@@ -183,7 +183,7 @@ func (l *VerifyStatusLogic) verifyStatus(expectStatus uint8, in *product.VerifyS
|
||||
Reason: reason,
|
||||
})
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
|
||||
func isJSONError(err error) bool {
|
||||
|
||||
@@ -20,19 +20,24 @@ func (m ProductModel) Init() ProductModel {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m ProductModel) WithTX(tx *gorm.DB) ProductModel {
|
||||
m.Base = m.Base.WithTX(tx)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m ProductModel) Create(data *dao.Create) error {
|
||||
return m.Base.Create(data)
|
||||
}
|
||||
|
||||
func (m ProductModel) IncrNumber(id int64, n uint32) (int64, error) {
|
||||
ret := modelbase.DB().Table(m.TableName()).
|
||||
ret := m.Session().
|
||||
Where("id = ?", id).
|
||||
Update("number", gorm.Expr("number + ?", n))
|
||||
return ret.RowsAffected, ret.Error
|
||||
}
|
||||
|
||||
func (m ProductModel) DecrNumber(id int64, n uint32) (int64, error) {
|
||||
ret := modelbase.DB().Table(m.TableName()).
|
||||
ret := m.Session().
|
||||
Where("id = ? AND number >= ?", id, n).
|
||||
Update("number", gorm.Expr("number - ?", n))
|
||||
return ret.RowsAffected, ret.Error
|
||||
|
||||
@@ -102,6 +102,12 @@ func (s *ProductServer) Number(ctx context.Context, in *product.NumberReq) (*emp
|
||||
return l.Number(in)
|
||||
}
|
||||
|
||||
// 批量增减库存(服务内按 id 排序后在同一事务中执行,防死锁)
|
||||
func (s *ProductServer) NumberItems(ctx context.Context, in *product.NumberItemsReq) (*emptypb.Empty, error) {
|
||||
l := logic.NewNumberItemsLogic(ctx, s.svcCtx)
|
||||
return l.NumberItems(in)
|
||||
}
|
||||
|
||||
func (s *ProductServer) InfoById(ctx context.Context, in *product.InfoByIdReq) (*product.Item, error) {
|
||||
l := logic.NewInfoByIdLogic(ctx, s.svcCtx)
|
||||
return l.InfoById(in)
|
||||
|
||||
Reference in New Issue
Block a user