76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"muyu-apiserver/rpc/inventory/internal/svc"
|
|
"muyu-apiserver/rpc/inventory/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type ApproveStockAdjustLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewApproveStockAdjustLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApproveStockAdjustLogic {
|
|
return &ApproveStockAdjustLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *ApproveStockAdjustLogic) ApproveStockAdjust(in *pb.ApproveStockAdjustReq) (*pb.Empty, error) {
|
|
adjust, err := l.svcCtx.StockAdjustModel.FindOneByAdjustId(l.ctx, in.AdjustId)
|
|
if err != nil {
|
|
return nil, status.Error(codes.NotFound, "stock adjust not found")
|
|
}
|
|
|
|
if adjust.Status != 0 {
|
|
return nil, status.Error(codes.FailedPrecondition, "stock adjust is not in pending status")
|
|
}
|
|
|
|
adjust.Approver = in.Approver
|
|
|
|
switch in.Action {
|
|
case 1: // approve
|
|
details, err := l.svcCtx.AdjustDetailModel.FindByAdjustId(l.ctx, in.AdjustId)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
for _, d := range details {
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, d.ProductId)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to find product %s: %v", d.ProductId, err)
|
|
}
|
|
|
|
product.StockQuantity = d.AfterQuantity
|
|
err = l.svcCtx.ProductModel.Update(l.ctx, product)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to update product %s stock: %v", d.ProductId, err)
|
|
}
|
|
}
|
|
|
|
adjust.Status = 1
|
|
|
|
case 2: // reject
|
|
adjust.Status = 2
|
|
|
|
default:
|
|
return nil, status.Errorf(codes.InvalidArgument, "unsupported action: %d", in.Action)
|
|
}
|
|
|
|
err = l.svcCtx.StockAdjustModel.Update(l.ctx, adjust)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|