xiazhengtongxue
9 小时以前 56538e602b927c232ac5b991ef84175edd2ce3ce
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
@@ -30,6 +30,8 @@
        /// </summary>
        private readonly IWarehouseService _warehouseService;
        private readonly IRecordService _recordService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IStockInfoDetailService _stockInfoDetailService;
        /// <summary>
        /// 构造函数
@@ -39,11 +41,15 @@
            IRepository<Dt_StockInfo> baseDal,
            ILocationInfoService locationInfoService,
            IWarehouseService warehouseService,
            IRecordService recordService) : base(baseDal)
            IRecordService recordService,
            IUnitOfWorkManage unitOfWorkManage,
            IStockInfoDetailService stockInfoDetailService) : base(baseDal)
        {
            _locationInfoService = locationInfoService;
            _warehouseService = warehouseService;
            _recordService = recordService;
            _unitOfWorkManage = unitOfWorkManage;
            _stockInfoDetailService = stockInfoDetailService;
        }
        /// <summary>
@@ -191,7 +197,7 @@
        /// <returns>库存信息</returns>
        public async Task<Dt_StockInfo> GetStockInfoAsync(string palletCode, string locationCode)
        {
            return await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode && x.LocationCode == locationCode);
            return await BaseDal.QueryDataNavFirstAsync(x => x.PalletCode == palletCode && x.LocationCode == locationCode);
        }
        /// <summary>
@@ -251,12 +257,12 @@
                    // 空托盘也有库存记录,只是不包含明细
                    item.PalletCode = stockInfo.PalletCode;
                    item.StockStatus = stockInfo.StockStatus; // 直接使用后端库存状态
                    // 只有当Details不为null且有数据时才处理库存明细
                    if (stockInfo.Details != null && stockInfo.Details.Any())
                    {
                        item.StockQuantity = stockInfo.Details.Sum(d => d.StockQuantity);
                        item.OutboundDate = stockInfo.OutboundDate;
                        // 获取第一个明细的物料信息(如果存在)
                        var firstDetail = stockInfo.Details.FirstOrDefault();
                        if (firstDetail != null)
@@ -272,6 +278,7 @@
                            Id = d.Id,
                            MaterielCode = d.MaterielCode,
                            MaterielName = d.MaterielName,
                            SerialNumber = d.SerialNumber,
                            BatchNo = d.BatchNo,
                            StockQuantity = d.StockQuantity,
                            Unit = d.Unit,
@@ -316,5 +323,86 @@
                Locations = locationItems
            };
        }
        /// <summary>
        /// 使用事务删除库存和明细信息(先查询再删除)
        /// </summary>
        /// <param name="stockId">库存ID</param>
        /// <returns>删除结果</returns>
        public async Task<WebResponseContent> DeleteStockWithDetailsAsync(int stockId)
        {
            if (stockId <= 0)
                return WebResponseContent.Instance.Error("库存ID无效");
            _unitOfWorkManage.BeginTran();
            try
            {
                // 先查询库存信息(包含明细)
                var stockInfo = await BaseDal.QueryDataNavFirstAsync(x => x.Id == stockId);
                if (stockInfo == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return WebResponseContent.Instance.Error("库存记录不存在");
                }
                // 查询并删除库存明细记录
                var existingDetails = await _stockInfoDetailService.Repository.QueryDataAsync(x => x.StockId == stockId);
                if (existingDetails != null && existingDetails.Any())
                {
                    var deleteDetailResult = await _stockInfoDetailService.Repository.DeleteDataAsync(existingDetails);
                    if (!deleteDetailResult)
                    {
                        _unitOfWorkManage.RollbackTran();
                        return WebResponseContent.Instance.Error("删除库存明细记录失败");
                    }
                }
                // 删除库存主记录
                var deleteStockResult = await BaseDal.DeleteDataAsync(stockInfo);
                if (!deleteStockResult)
                {
                    _unitOfWorkManage.RollbackTran();
                    return WebResponseContent.Instance.Error("删除库存主记录失败");
                }
                _unitOfWorkManage.CommitTran();
                // 记录库存变更日志
                var saveRecordResult = await _recordService.AddStockChangeRecordAsync(stockInfo, null, StockChangeTypeEnum.Outbound, remark: "库存删除");
                if (!saveRecordResult)
                {
                    return WebResponseContent.Instance.Error("库存变更记录保存失败");
                }
                return WebResponseContent.Instance.OK("库存删除成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"删除库存和明细时发生异常: {ex.Message}");
            }
        }
        /// <summary>
        /// 更新MES上传状态
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="status">MES上传状态值</param>
        /// <returns>更新是否成功</returns>
        public async Task<bool> UpdateMesUploadStatusAsync(string palletCode, int status)
        {
            try
            {
                var stockInfo = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
                if (stockInfo == null)
                    return false;
                stockInfo.MesUploadStatus = status;
                return await BaseDal.UpdateDataAsync(stockInfo);
            }
            catch
            {
                return false;
            }
        }
    }
}