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