2026-02-28 15:29:16 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"muyu-apiserver/model"
|
|
|
|
|
"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 UpdateStockCheckLogic struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
logx.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUpdateStockCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateStockCheckLogic {
|
|
|
|
|
return &UpdateStockCheckLogic{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 15:24:02 +08:00
|
|
|
func (l *UpdateStockCheckLogic) systemQty(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 *UpdateStockCheckLogic) UpdateStockCheck(in *pb.UpdateStockCheckReq) (*pb.Empty, error) {
|
|
|
|
|
check, err := l.svcCtx.StockCheckModel.FindOneByCheckId(l.ctx, in.CheckId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.NotFound, "stock check not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if check.Status == 2 {
|
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "completed stock check cannot be modified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check.Remark = sql.NullString{String: in.Remark, Valid: in.Remark != ""}
|
|
|
|
|
check.Status = 1
|
|
|
|
|
|
|
|
|
|
err = l.svcCtx.StockCheckModel.Update(l.ctx, check)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = l.svcCtx.CheckDetailModel.DeleteByCheckId(l.ctx, in.CheckId)
|
|
|
|
|
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
|
|
|
sysQty := l.systemQty(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 - sysQty
|
|
|
|
|
diffAmount := diffQty * product.SalesPrice
|
2026-02-28 15:29:16 +08:00
|
|
|
|
|
|
|
|
details = append(details, &model.InvStockCheckDetail{
|
|
|
|
|
DetailId: uid.Generate(),
|
|
|
|
|
CheckId: in.CheckId,
|
|
|
|
|
ProductId: d.ProductId,
|
2026-06-14 15:24:02 +08:00
|
|
|
SystemQuantity: sysQty,
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.Empty{}, nil
|
|
|
|
|
}
|