feat: product number
CI / changes (push) Successful in 36s
CI / docker-bff (push) Successful in 3m8s
CI / docker-product (push) Successful in 3m36s
CI / docker-admin (push) Successful in 3m35s
CI / docker-user (push) Successful in 9s
CI / docker-ad (push) Successful in 29s
CI / docker-chore (push) Failing after 4s
CI / changes (push) Successful in 36s
CI / docker-bff (push) Successful in 3m8s
CI / docker-product (push) Successful in 3m36s
CI / docker-admin (push) Successful in 3m35s
CI / docker-user (push) Successful in 9s
CI / docker-ad (push) Successful in 29s
CI / docker-chore (push) Failing after 4s
This commit is contained in:
@@ -2,6 +2,8 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
@@ -10,9 +12,11 @@ import (
|
||||
"lone-services/services/product/internal/model"
|
||||
"lone-services/services/product/internal/svc"
|
||||
"lone-services/services/product/validator"
|
||||
"strconv"
|
||||
|
||||
"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 {
|
||||
@@ -30,27 +34,30 @@ func NewNumberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NumberLogi
|
||||
}
|
||||
|
||||
// Number 服务间增减库存:type=1 增加,type=2 减少
|
||||
func (l *NumberLogic) Number(in *product.NumberReq) (*product.Response, error) {
|
||||
func (l *NumberLogic) Number(in *product.NumberReq) (*emptypb.Empty, error) {
|
||||
var req validator.ProductNumberValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
return nil, status.Error(codes.InvalidArgument, msg)
|
||||
}
|
||||
|
||||
return changeProductNumber(l.Logger, req.Id, req.Type, req.Number)
|
||||
if err := changeProductNumber(l.Logger, 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) (*product.Response, error) {
|
||||
var info dao.ProductNumberInfo
|
||||
func changeProductNumber(logger logx.Logger, 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 failResponse(utils.Fail), nil
|
||||
return status.Error(codes.Internal, utils.Fail.Msg)
|
||||
}
|
||||
if info.Id < 1 {
|
||||
return failResponse(utils.ErrorNotFund), nil
|
||||
return status.Error(codes.NotFound, utils.ErrorNotFund.Msg)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -63,18 +70,17 @@ func changeProductNumber(logger logx.Logger, id int64, typ uint32, number uint32
|
||||
case dao.NumberTypeSub:
|
||||
rows, err = m.DecrNumber(id, number)
|
||||
default:
|
||||
return failResponse(utils.ErrorParams), nil
|
||||
return status.Error(codes.InvalidArgument, utils.ErrorParams.Msg)
|
||||
}
|
||||
if err != nil {
|
||||
logger.Errorf("product number change: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
return status.Error(codes.Internal, utils.Fail.Msg)
|
||||
}
|
||||
if rows < 1 {
|
||||
if typ == dao.NumberTypeSub {
|
||||
return failResponse(utils.ErrorStockNotEnough), nil
|
||||
return status.Error(codes.FailedPrecondition, utils.ErrorStockNotEnough.Msg)
|
||||
}
|
||||
return failResponse(utils.Fail), nil
|
||||
return status.Error(codes.Internal, utils.Fail.Msg)
|
||||
}
|
||||
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user