64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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 ConfirmStockCheckLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewConfirmStockCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ConfirmStockCheckLogic {
|
|
return &ConfirmStockCheckLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *ConfirmStockCheckLogic) ConfirmStockCheck(in *pb.ConfirmStockCheckReq) (*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 != 0 && check.Status != 1 {
|
|
return nil, status.Error(codes.FailedPrecondition, "stock check cannot be confirmed in current status")
|
|
}
|
|
|
|
details, err := l.svcCtx.CheckDetailModel.FindByCheckId(l.ctx, in.CheckId)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
for _, d := range details {
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, d.ProductId)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to find product %s: %v", d.ProductId, err)
|
|
}
|
|
|
|
product.StockQuantity = d.ActualQuantity
|
|
err = l.svcCtx.ProductModel.Update(l.ctx, product)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to update product %s stock: %v", d.ProductId, err)
|
|
}
|
|
}
|
|
|
|
check.Status = 2
|
|
err = l.svcCtx.StockCheckModel.Update(l.ctx, check)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|