2026-02-28 15:29:16 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
"math"
|
|
|
|
|
|
2026-02-28 15:29:16 +08:00
|
|
|
"muyu-apiserver/model"
|
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/pkg/uid"
|
|
|
|
|
"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 CreateStockAdjustLogic struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
logx.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCreateStockAdjustLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateStockAdjustLogic {
|
|
|
|
|
return &CreateStockAdjustLogic{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
// currentQty returns the total bolt length (meters) for a product.
|
|
|
|
|
func (l *CreateStockAdjustLogic) currentQty(productId string) float64 {
|
|
|
|
|
bolts, err := l.svcCtx.BoltModel.FindByProductId(l.ctx, productId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
var total float64
|
|
|
|
|
for _, b := range bolts {
|
|
|
|
|
total += b.LengthM
|
|
|
|
|
}
|
|
|
|
|
return total
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 15:29:16 +08:00
|
|
|
func (l *CreateStockAdjustLogic) CreateStockAdjust(in *pb.CreateStockAdjustReq) (*pb.IdResp, error) {
|
|
|
|
|
adjustId := uid.Generate()
|
|
|
|
|
adjustNo := generateNo("TZ")
|
|
|
|
|
|
|
|
|
|
adjustDate, err := time.Parse("2006-01-02", in.AdjustDate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid adjust_date format, expected YYYY-MM-DD")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adjust := &model.InvStockAdjust{
|
|
|
|
|
AdjustId: adjustId,
|
|
|
|
|
AdjustNo: adjustNo,
|
|
|
|
|
AdjustDate: adjustDate,
|
|
|
|
|
AdjustReason: in.AdjustReason,
|
|
|
|
|
Operator: in.Operator,
|
|
|
|
|
Status: 0,
|
|
|
|
|
Remark: sql.NullString{String: in.Remark, Valid: in.Remark != ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = l.svcCtx.StockAdjustModel.Insert(l.ctx, adjust)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
details := make([]*model.InvStockAdjustDetail, 0, len(in.Details))
|
|
|
|
|
for _, d := range in.Details {
|
2026-06-14 15:24:02 +08:00
|
|
|
_, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, d.ProductId)
|
2026-02-28 15:29:16 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Errorf(codes.NotFound, "product %s not found", d.ProductId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
beforeQty := l.currentQty(d.ProductId)
|
2026-02-28 15:29:16 +08:00
|
|
|
adjustQty, _ := strconv.ParseFloat(d.AdjustQuantity, 64)
|
2026-06-14 15:24:02 +08:00
|
|
|
afterQty := beforeQty + adjustQty
|
2026-02-28 15:29:16 +08:00
|
|
|
|
|
|
|
|
details = append(details, &model.InvStockAdjustDetail{
|
|
|
|
|
DetailId: uid.Generate(),
|
|
|
|
|
AdjustId: adjustId,
|
|
|
|
|
ProductId: d.ProductId,
|
2026-06-14 15:24:02 +08:00
|
|
|
BeforeQuantity: beforeQty,
|
2026-02-28 15:29:16 +08:00
|
|
|
AdjustQuantity: adjustQty,
|
|
|
|
|
AfterQuantity: afterQty,
|
|
|
|
|
Remark: sql.NullString{String: d.Remark, Valid: d.Remark != ""},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = l.svcCtx.AdjustDetailModel.BatchInsert(l.ctx, details)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
|
|
|
|
metrics.StockAdjustCreatedTotal.WithLabelValues(tenantId, in.AdjustReason).Inc()
|
|
|
|
|
for _, d := range details {
|
|
|
|
|
if d.AdjustQuantity >= 0 {
|
|
|
|
|
metrics.StockAdjustQuantityMeters.WithLabelValues(tenantId, "increase").Add(d.AdjustQuantity)
|
|
|
|
|
} else {
|
|
|
|
|
metrics.StockAdjustQuantityMeters.WithLabelValues(tenantId, "decrease").Add(math.Abs(d.AdjustQuantity))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 15:29:16 +08:00
|
|
|
return &pb.IdResp{Id: adjustId}, nil
|
|
|
|
|
}
|