package logic import ( "context" "muyu-apiserver/pkg/metrics" "muyu-apiserver/pkg/tenantctx" "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 // In the three-layer model, actual stock adjustments are done via // SaveBolts/SavePans. Approving records the approval decision only. 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()) } tenantId := tenantctx.ExtractTenantId(l.ctx) actionLabel := "approve" if in.Action == 2 { actionLabel = "reject" } metrics.StockAdjustApprovedTotal.WithLabelValues(tenantId, actionLabel).Inc() return &pb.Empty{}, nil }