86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"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"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
)
|
|
|
|
type NumberLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewNumberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NumberLogic {
|
|
return &NumberLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// Number 服务间增减库存:type=1 增加,type=2 减少
|
|
func (l *NumberLogic) Number(in *product.NumberReq) (*emptypb.Empty, error) {
|
|
var req validator.ProductNumberValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return nil, status.Error(codes.InvalidArgument, msg)
|
|
}
|
|
|
|
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, 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)},
|
|
}
|
|
if err := m.GetOne(w, &info); err != nil {
|
|
logger.Errorf("product number get: %v", err)
|
|
return status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
if info.Id < 1 {
|
|
return status.Error(codes.NotFound, utils.ErrorNotFund.Msg)
|
|
}
|
|
|
|
var (
|
|
rows int64
|
|
err error
|
|
)
|
|
switch typ {
|
|
case dao.NumberTypeAdd:
|
|
rows, err = m.IncrNumber(id, number)
|
|
case dao.NumberTypeSub:
|
|
rows, err = m.DecrNumber(id, number)
|
|
default:
|
|
return status.Error(codes.InvalidArgument, utils.ErrorParams.Msg)
|
|
}
|
|
if err != nil {
|
|
logger.Errorf("product number change: %v", err)
|
|
return status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
if rows < 1 {
|
|
if typ == dao.NumberTypeSub {
|
|
return status.Error(codes.FailedPrecondition, utils.ErrorStockNotEnough.Msg)
|
|
}
|
|
return status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
return nil
|
|
}
|