pan
2025-11-16 e31ca4e3e1774b7ddb832e8ec498b5ada24b2608
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_InboundService/InboundOrderService.cs
@@ -1,5 +1,6 @@
using AutoMapper;
using SqlSugar;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
@@ -18,6 +19,7 @@
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
namespace WIDESEA_InboundService
{
@@ -29,12 +31,16 @@
        private readonly IRepository<Dt_Task> _taskRepository;
        private IStockService _stockService;
        private readonly IMaterialUnitService _materialUnitService;
        private readonly IInboundOrderDetailService _inboundOrderDetailService;
        private readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
        private readonly IRepository<Dt_StockInfoDetail> _stockDetailRepository;
        private readonly IRepository<Dt_InboundOrder> _inboundOrderRepository;
        private readonly IRepository<Dt_WarehouseArea> _warehouseAreaRepository;
        private readonly IRepository<Dt_StockInfo> _stockRepository;
        public IRepository<Dt_InboundOrder> Repository => BaseDal;
        public InboundOrderService(IRepository<Dt_InboundOrder> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IRepository<Dt_Task> taskRepository, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService) : base(BaseDal)
        public InboundOrderService(IRepository<Dt_InboundOrder> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IRepository<Dt_Task> taskRepository, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService, IMaterialUnitService materialUnitService, IRepository<Dt_StockInfoDetail> stockDetailRepository, IRepository<Dt_InboundOrder> inboundOrderRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository,IRepository<Dt_StockInfo> stockRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
@@ -42,16 +48,21 @@
            _taskRepository = taskRepository;
            _stockService = stockService;
            _inboundOrderDetailService = inboundOrderDetailService;
            _materialUnitService = materialUnitService;
            _stockDetailRepository = stockDetailRepository;
            _inboundOrderRepository = inboundOrderRepository;
            _warehouseAreaRepository = warehouseAreaRepository;
            _stockRepository = stockRepository;
        }
        public WebResponseContent ReceiveInboundOrder(List<Dt_InboundOrder> models, int operateType)
        public async Task<WebResponseContent> ReceiveInboundOrder(List<Dt_InboundOrder> models, int operateType)
        {
            try
            {
                return operateType switch
                {
                    1 => AddInboundOrder(models),
                    2 => UpdateInboundOrder(models),
                    1 => await AddInboundOrder(models),
                    2 => await UpdateInboundOrder(models),
                    3 => DeleteInboundOrder(models),
                    _ => WebResponseContent.Instance.OK(),
@@ -63,7 +74,7 @@
            }
        }
        public WebResponseContent AddInboundOrder(List<Dt_InboundOrder> models)
        public async Task<WebResponseContent> AddInboundOrder(List<Dt_InboundOrder> models)
        {
            try
            {
@@ -77,6 +88,13 @@
                foreach (var model in models)
                {
                    foreach (var item in model.Details)
                    {
                        var purchaseToStockResult = await _materialUnitService.ConvertPurchaseToStockAsync(item.MaterielCode, item.BarcodeQty);
                        item.Unit = purchaseToStockResult.Unit;
                        item.OrderQuantity = purchaseToStockResult.Quantity;
                    }
                    model.InboundOrderNo = CreateCodeByRule(nameof(RuleCodeEnum.InboundOrderRule));
                    Db.InsertNav(model).Include(x => x.Details).ExecuteCommand();
                }
@@ -87,7 +105,7 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent UpdateInboundOrder(List<Dt_InboundOrder> models)
        public async Task<WebResponseContent> UpdateInboundOrder(List<Dt_InboundOrder> models)
        {
            try
            {
@@ -131,6 +149,10 @@
                                    BarcodeQty = item.BarcodeQty,
                                    OrderQuantity = item.OrderQuantity
                                };
                                var purchaseToStockResult = await _materialUnitService.ConvertPurchaseToStockAsync(item.MaterielCode, item.BarcodeQty);
                                inboundOrderDetail.Unit = purchaseToStockResult.Unit;
                                inboundOrderDetail.OrderQuantity = purchaseToStockResult.Quantity;
                                inboundOrderDetails.Add(inboundOrderDetail);
                            }
                            else
@@ -300,11 +322,20 @@
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(materielGroupDTO);
                if (!result2.Item1) return content = WebResponseContent.Instance.Error(result2.Item2);
                //  materielGroupDTO.WarehouseCode
                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == materielGroupDTO.WarehouseType).Select(x=>x.Code).First();
                if(string.IsNullOrEmpty(code))
                {
                    return content = WebResponseContent.Instance.Error($"仓库中没有该{materielGroupDTO.WarehouseType}编号。");
                }
                Dt_InboundOrder inboundOrder = GetInboundOrder(materielGroupDTO.OrderNo);
                var dbinboundOrderDetails = _inboundOrderDetailService.GetByBarcode(materielGroupDTO.Barcodes);
                if (dbinboundOrderDetails != null && !dbinboundOrderDetails.Any()) {
                if (dbinboundOrderDetails != null && !dbinboundOrderDetails.Any())
                {
                    return content = WebResponseContent.Instance.Error($"单据中没有该{materielGroupDTO.Barcodes}条码数据。");
                }
@@ -319,11 +350,10 @@
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None };
                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None,LocationType=materielGroupDTO.locationType.ObjToInt() };
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                foreach (var item in dbinboundOrderDetails)
                {
                    stockInfo.Details.Add(new Dt_StockInfoDetail
@@ -332,18 +362,23 @@
                        Barcode = item.Barcode,
                        MaterielCode = item.MaterielCode,
                        BatchNo = item.BatchNo,
                        Unit = item.BarcodeUnit,
                        Unit = item.Unit,
                        InboundOrderRowNo = item.lineNo,
                        SupplyCode = item.SupplyCode,
                        WarehouseCode = item.WarehouseCode,
                        StockQuantity = item.BarcodeQty,
                        Status = 0,
                        WarehouseCode = materielGroupDTO.WarehouseType,
                        StockQuantity = item.OrderQuantity,
                        BarcodeQty=item.BarcodeQty,
                        BarcodeUnit=item.BarcodeUnit,
                        FactoryArea= inboundOrder.FactoryArea,
                        Status = 0,
                        OrderNo = inboundOrder.InboundOrderNo,
                        BusinessType=inboundOrder.BusinessType,
                        BusinessType = inboundOrder.BusinessType,
                    });
                    item.ReceiptQuantity = item.BarcodeQty;
                    item.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
                    item.WarehouseCode = materielGroupDTO.WarehouseType;
                }
                if (stockInfo.Id == 0)
@@ -351,14 +386,14 @@
                    stockInfo.PalletCode = materielGroupDTO.PalletCode;
                    stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                }
                stockInfo.PalletType = (int)PalletTypeEnum.None;
                List<int> updateDetailIds = dbinboundOrderDetails.Select(x => x.Id).ToList();
                if (inboundOrder.OrderStatus == InOrderStatusEnum.未开始.ObjToInt())
                {
                    inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                }
                inboundOrder.Operator = App.User.UserName;
                content = MaterielGroupUpdateData(inboundOrder, dbinboundOrderDetails, stockInfo);
                if (content.Status)
                {
@@ -391,7 +426,16 @@
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(materielGroupDTO);
                if (!result2.Item1) return content = WebResponseContent.Instance.Error(result2.Item2);
                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == materielGroupDTO.WarehouseCode).Select(x => x.Code).First();
                if (string.IsNullOrEmpty(code))
                {
                    return content = WebResponseContent.Instance.Error($"仓库中没有该{materielGroupDTO.WarehouseCode}编号。");
                }
                if(_stockRepository.QueryFirst(x=>x.PalletCode == materielGroupDTO.PalletCode)!=null){
                    return WebResponseContent.Instance.Error("该托盘已经组过盘");
                }
                Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode);
                if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                {
@@ -407,7 +451,7 @@
                {
                    if (stockInfo == null)
                    {
                        stockInfo = new Dt_StockInfo() { PalletType = PalletTypeEnum.Empty.ObjToInt(), StockStatus = StockStatusEmun.组盘暂存.ObjToInt(), PalletCode = materielGroupDTO.PalletCode };
                        stockInfo = new Dt_StockInfo() { PalletType = PalletTypeEnum.Empty.ObjToInt(), StockStatus = StockStatusEmun.组盘暂存.ObjToInt(), PalletCode = materielGroupDTO.PalletCode,LocationType=1 };
                        stockInfo.Details = new List<Dt_StockInfoDetail>();
                    }
                    else
@@ -416,7 +460,7 @@
                        stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                    }
                        _unitOfWorkManage.BeginTran();
                    _unitOfWorkManage.BeginTran();
                    _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                    _unitOfWorkManage.CommitTran();
@@ -429,7 +473,7 @@
                }
            }
            catch (Exception ex)
            {
@@ -534,6 +578,52 @@
        }
        public WebResponseContent UnPalletQuantity(string orderNo)
        {
            // åˆå§‹åŒ–返回DTO(默认值都为0,避免null)
            var resultDTO = new PalletSumQuantityDTO
            {
                StockSumQuantity = 0,
                StockCount = 0,
                UniqueUnit = ""
            };
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (string.IsNullOrWhiteSpace(orderNo))
                {
                    return content.Error("传入的订单号orderNo为空或空白");
                }
                var orderDetail = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).Where(s => s.InboundOrderNo == orderNo).First();
                if (orderDetail == null)
                {
                    return content.Error("未找到单据");
                }
                var unitGroups = orderDetail.Details.GroupBy(d => d.Unit).ToList();
                if (unitGroups.Count == 1)
                {
                    resultDTO.UniqueUnit = unitGroups.First().Key;
                }
                else
                {
                    resultDTO.UniqueUnit = "";
                }
                var validDetails = _stockDetailRepository.Db.Queryable<Dt_StockInfoDetail>().Where(s => s.OrderNo == orderNo).ToList();
                resultDTO.StockSumQuantity = orderDetail.Details.Sum(d => d.OrderQuantity);
                resultDTO.StockCount = orderDetail.Details.Count;
                if (validDetails.Any())
                {
                    resultDTO.StockSumQuantity -= validDetails.Sum(d => d.StockQuantity);
                    // æ˜Žç»†è®°å½•数:符合条件的有效记录条数
                    resultDTO.StockCount -= validDetails.Count;
                }
                return content.OK("", resultDTO);
            }
            catch (Exception ex)
            {
                return content.Error("SumQuantity ç»Ÿè®¡åº“存数量失败,订单号:{OrderNo}");
            }
        }
    }
}