2026-02-28 15:29:16 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"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 CreateStockCheckLogic struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
logx.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCreateStockCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateStockCheckLogic {
|
|
|
|
|
return &CreateStockCheckLogic{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateNo(prefix string) string {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
return fmt.Sprintf("%s%s%04d", prefix, now.Format("20060102"), now.UnixNano()%10000)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
// computeSystemQty returns the total bolt length (meters) for a product.
|
|
|
|
|
func (l *CreateStockCheckLogic) computeSystemQty(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 *CreateStockCheckLogic) CreateStockCheck(in *pb.CreateStockCheckReq) (*pb.IdResp, error) {
|
|
|
|
|
checkId := uid.Generate()
|
|
|
|
|
checkNo := generateNo("PD")
|
|
|
|
|
|
|
|
|
|
checkDate, err := time.Parse("2006-01-02", in.CheckDate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid check_date format, expected YYYY-MM-DD")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check := &model.InvStockCheck{
|
|
|
|
|
CheckId: checkId,
|
|
|
|
|
CheckNo: checkNo,
|
|
|
|
|
CheckDate: checkDate,
|
|
|
|
|
Checker: in.Checker,
|
|
|
|
|
Status: 0,
|
|
|
|
|
Remark: sql.NullString{String: in.Remark, Valid: in.Remark != ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = l.svcCtx.StockCheckModel.Insert(l.ctx, check)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
details := make([]*model.InvStockCheckDetail, 0, len(in.Details))
|
|
|
|
|
for _, d := range in.Details {
|
|
|
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, d.ProductId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Errorf(codes.NotFound, "product %s not found", d.ProductId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
systemQty := l.computeSystemQty(d.ProductId)
|
2026-02-28 15:29:16 +08:00
|
|
|
actualQty, _ := strconv.ParseFloat(d.ActualQuantity, 64)
|
2026-06-14 15:24:02 +08:00
|
|
|
diffQty := actualQty - systemQty
|
|
|
|
|
diffAmount := diffQty * product.SalesPrice
|
2026-02-28 15:29:16 +08:00
|
|
|
|
|
|
|
|
details = append(details, &model.InvStockCheckDetail{
|
|
|
|
|
DetailId: uid.Generate(),
|
|
|
|
|
CheckId: checkId,
|
|
|
|
|
ProductId: d.ProductId,
|
2026-06-14 15:24:02 +08:00
|
|
|
SystemQuantity: systemQty,
|
2026-02-28 15:29:16 +08:00
|
|
|
ActualQuantity: actualQty,
|
|
|
|
|
DiffQuantity: diffQty,
|
|
|
|
|
DiffAmount: diffAmount,
|
|
|
|
|
Remark: sql.NullString{String: d.Remark, Valid: d.Remark != ""},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = l.svcCtx.CheckDetailModel.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.StockCheckCreatedTotal.WithLabelValues(tenantId).Inc()
|
|
|
|
|
|
2026-02-28 15:29:16 +08:00
|
|
|
return &pb.IdResp{Id: checkId}, nil
|
|
|
|
|
}
|