| using MailKit.Search; | 
| using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Reflection; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_Common.LocationEnum; | 
| using WIDESEA_Common.OrderEnum; | 
| using WIDESEA_Common.StockEnum; | 
| using WIDESEA_Common.WareHouseEnum; | 
| using WIDESEA_Core; | 
| using WIDESEA_Core.BaseRepository; | 
| using WIDESEA_Core.BaseServices; | 
| using WIDESEA_Core.Enums; | 
| using WIDESEA_Core.Helper; | 
| using WIDESEA_DTO.Stock; | 
| using WIDESEA_IBasicRepository; | 
| using WIDESEA_IBasicService; | 
| using WIDESEA_IOutboundRepository; | 
| using WIDESEA_IOutboundService; | 
| using WIDESEA_IRecordService; | 
| using WIDESEA_IStockRepository; | 
| using WIDESEA_IStockService; | 
| using WIDESEA_Model.Models; | 
| using WIDESEA_OutboundRepository; | 
| using WIDESEA_StockRepository; | 
|   | 
| namespace WIDESEA_OutboundService | 
| { | 
|     public partial class OutboundOrderDetailService : ServiceBase<Dt_OutboundOrderDetail, IOutboundOrderDetailRepository>, IOutboundOrderDetailService | 
|     { | 
|         private readonly IUnitOfWorkManage _unitOfWorkManage; | 
|         private readonly IStockService _stockService; | 
|         private readonly IOutboundRepository _outboundRepository; | 
|         private readonly IOutStockLockInfoService _outStockLockInfoService; | 
|         private readonly IBasicService _basicService; | 
|         private readonly IRecordService _recordService; | 
|   | 
|         public IOutboundOrderDetailRepository Repository => BaseDal; | 
|   | 
|         public OutboundOrderDetailService(IOutboundOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutboundRepository outboundRepository, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal) | 
|         { | 
|             _unitOfWorkManage = unitOfWorkManage; | 
|             _stockService = stockService; | 
|             _outboundRepository = outboundRepository; | 
|             _outStockLockInfoService = outStockLockInfoService; | 
|             _basicService = basicService; | 
|             _recordService = recordService; | 
|         } | 
|         public WebResponseContent GetOutboundOrderDetails(SaveModel saveModel) | 
|         { | 
|             WebResponseContent content = new WebResponseContent(); | 
|             try | 
|             { | 
|                 string orderNo = saveModel.MainData["orderNo"].ToString(); | 
|                 Dt_OutboundOrder inboundOrder = Db.Queryable<Dt_OutboundOrder>().Where(x => x.OrderNo == orderNo).Includes(x => x.Details).First(); | 
|                 content.OK(data: inboundOrder.Details); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 content.Error(ex.Message); | 
|             } | 
|             return content; | 
|         } | 
|         /// <summary> | 
|         /// 锁定库存,由系统分配(仅逻辑运算,不生成任务,不修改数据库数据) | 
|         /// </summary> | 
|         /// <param name="orderDetailId"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LockOutboundStock(int orderDetailId) | 
|         { | 
|             Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); | 
|             if (outboundOrderDetail == null) | 
|             { | 
|                 return WebResponseContent.Instance.Error("未找到出库单明细信息"); | 
|             } | 
|             if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) | 
|             { | 
|                 return WebResponseContent.Instance.Error("该明细不可操作"); | 
|             } | 
|             Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId); | 
|   | 
|             float needQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity; | 
|   | 
|             List<Dt_StockInfo> outStock = new List<Dt_StockInfo>(); | 
|   | 
|             List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode, outboundOrderDetail.BatchNo, outboundOrder.WarehouseId); | 
|             float stockTotalQuantity = stockInfos.Select(x => x.Details.Sum(v => v.StockQuantity - v.OutboundQuantity)).Sum(x => x); | 
|             if (stockTotalQuantity >= needQuantity)//库存够 | 
|             { | 
|                 int index = 0; | 
|                 while (needQuantity > 0) | 
|                 { | 
|                     Dt_StockInfo stockInfo = stockInfos[index]; | 
|                     float useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity); | 
|                     if (useableStockQuantity < needQuantity) | 
|                     { | 
|                         outboundOrderDetail.LockQuantity += useableStockQuantity; | 
|                         stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity); | 
|                         needQuantity -= useableStockQuantity; | 
|                     } | 
|                     else | 
|                     { | 
|                         outboundOrderDetail.LockQuantity += needQuantity; | 
|                         stockInfo.Details.ForEach(x => | 
|                         { | 
|                             if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode) | 
|                             { | 
|                                 if (x.StockQuantity - x.OutboundQuantity >= needQuantity) | 
|                                 { | 
|                                     x.OutboundQuantity += needQuantity; | 
|                                     needQuantity = 0; | 
|                                 } | 
|                                 else | 
|                                 { | 
|                                     needQuantity -= (x.StockQuantity - x.OutboundQuantity); | 
|                                     x.OutboundQuantity = x.StockQuantity; | 
|                                 } | 
|                             } | 
|                         }); | 
|                     } | 
|                     outStock.Add(stockInfo); | 
|                     index++; | 
|                 } | 
|                 return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail }); | 
|             } | 
|             else | 
|             { | 
|                 for (int i = 0; i < stockInfos.Count; i++) | 
|                 { | 
|                     Dt_StockInfo stockInfo = stockInfos[i]; | 
|                     float useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity); | 
|                     if (useableStockQuantity < needQuantity) | 
|                     { | 
|                         outboundOrderDetail.LockQuantity += useableStockQuantity; | 
|                         stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity); | 
|                         needQuantity -= useableStockQuantity; | 
|                     } | 
|                     else | 
|                     { | 
|                         outboundOrderDetail.LockQuantity += needQuantity; | 
|                         stockInfo.Details.ForEach(x => | 
|                         { | 
|                             if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode) | 
|                             { | 
|                                 if (x.StockQuantity - x.OutboundQuantity >= needQuantity) | 
|                                 { | 
|                                     x.OutboundQuantity += needQuantity; | 
|                                     needQuantity = 0; | 
|                                 } | 
|                                 else | 
|                                 { | 
|                                     needQuantity -= (x.StockQuantity - x.OutboundQuantity); | 
|                                     x.OutboundQuantity = x.StockQuantity; | 
|                                 } | 
|                             } | 
|                         }); | 
|                     } | 
|                     outStock.Add(stockInfo); | 
|                 } | 
|                 return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail }); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 单个出库单明细库存分配 | 
|         /// </summary> | 
|         /// <param name="outboundOrderDetail"></param> | 
|         /// <param name="stockSelectViews"></param> | 
|         /// <returns></returns> | 
|         /// <exception cref="Exception"></exception> | 
|         public (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews) | 
|         { | 
|             (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); | 
|             if (!checkResult.Item1) throw new Exception(checkResult.Item2); | 
|   | 
|             Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId); | 
|             float originalNeedQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity; | 
|   | 
|             float needQuantity = originalNeedQuantity; | 
|   | 
|             List<Dt_StockInfo> outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList()); | 
|             float assignQuantity = 0; | 
|             outStocks.ForEach(x => | 
|             { | 
|                 x.Details.ForEach(v => | 
|                 { | 
|                     assignQuantity += v.StockQuantity - v.OutboundQuantity; | 
|                 }); | 
|             }); | 
|   | 
|             outboundOrderDetail.LockQuantity += assignQuantity; | 
|             outStocks.ForEach(x => | 
|             { | 
|                 x.Details.ForEach(v => | 
|                 { | 
|                     v.OutboundQuantity = v.StockQuantity; | 
|                 }); | 
|             }); | 
|             needQuantity -= assignQuantity; | 
|             if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity) | 
|             { | 
|                 List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode, outboundOrderDetail.BatchNo, outboundOrder.WarehouseId); | 
|                 stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList(); | 
|                 List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out float residueQuantity); | 
|                 outboundOrderDetail.LockQuantity += needQuantity - residueQuantity; | 
|                 outStocks.AddRange(autoAssignStocks); | 
|                 outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt(); | 
|                 if (residueQuantity > 0) | 
|                 { | 
|                     outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt(); | 
|                 } | 
|             } | 
|   | 
|             List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrderDetail, outStocks); | 
|   | 
|             List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()); | 
|   | 
|             return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos); | 
|         } | 
|   | 
|         /// <summary> | 
|         ///  | 
|         /// </summary> | 
|         /// <param name="outboundOrderDetails"></param> | 
|         /// <returns></returns> | 
|         public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails) | 
|         { | 
|             if (!outboundOrderDetails.Any()) | 
|             { | 
|                 throw new Exception($"未找到出库单明细信息"); | 
|             } | 
|   | 
|             if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1) | 
|             { | 
|                 throw new Exception($"请勿同时操作多个单据明细"); | 
|             } | 
|             Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId); | 
|             List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>(); | 
|             List<Dt_OutboundOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo }).Select(x => new Dt_OutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo }).ToList(); | 
|             Dt_Warehouse warehouse = _basicService.WarehouseService.Repository.QueryFirst(x=>x.WarehouseId== outboundOrder.WarehouseId); | 
|             List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>(); | 
|             List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|             foreach (var item in groupDetails) | 
|             { | 
|                 float originalNeedQuantity = item.OrderQuantity; | 
|   | 
|                 float needQuantity = originalNeedQuantity; | 
|   | 
|                 List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, outboundOrder.WarehouseId); | 
|                 if (!stockInfos.Any()) | 
|                 { | 
|                     throw new Exception($"未找到可分配库存"); | 
|                 } | 
|                 List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out float residueQuantity); | 
|                 if (warehouse.WarehouseCode==WarehouseEnum.HA152.ToString()) | 
|                 { | 
|                     if (needQuantity< autoAssignStocks.Count) | 
|                     { | 
|                         throw new Exception($"系统超时,请稍后再试"); | 
|                     } | 
|                 } | 
|                 item.LockQuantity += needQuantity - residueQuantity; | 
|                 outStocks.AddRange(autoAssignStocks); | 
|                 float assignQuantity = needQuantity - residueQuantity; | 
|   | 
|                 List<Dt_OutboundOrderDetail> details = outboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList(); | 
|   | 
|                 for (int i = 0; i < details.Count; i++) | 
|                 { | 
|                     float orderQuantity = details[i].OrderQuantity; | 
|                     for (int j = 0; j < autoAssignStocks.Count; j++) | 
|                     { | 
|                         float detailAssignQuantity = outStockLockInfos.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//出库订单明细已分配数量 | 
|   | 
|                         float palletAssignQuantity = outStockLockInfos.Where(x => x.BatchNo == item.BatchNo && x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 | 
|                         if (string.IsNullOrEmpty(item.BatchNo)) | 
|                         { | 
|                             palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 | 
|                         } | 
|                         float palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity); | 
|                         if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息 | 
|                         { | 
|                             float orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity; | 
|                             if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity) | 
|                             { | 
|                                 details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity; | 
|                                 Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity); | 
|                                 outStockLockInfos.Add(outStockLockInfo); | 
|                             } | 
|                             else | 
|                             { | 
|                                 Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity); | 
|                                 outStockLockInfos.Add(outStockLockInfo); | 
|                                 details[i].LockQuantity = details[i].OrderQuantity; | 
|                                 break; | 
|                             } | 
|   | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|   | 
|                 locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList())); | 
|             } | 
|   | 
|             return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos); | 
|         } | 
|   | 
|         /// <summary> | 
|         ///  锁定库存,由人工指定,或加上系统分配(包含逻辑运算,且修改数据库数据) | 
|         /// </summary> | 
|         /// <param name="orderDetailId"></param> | 
|         /// <param name="stockSelectViews"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LockOutboundStock(int orderDetailId, List<StockSelectViewDTO> stockSelectViews) | 
|         { | 
|             try | 
|             { | 
|                 Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); | 
|                 (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); | 
|                 if (!checkResult.Item1) throw new Exception(checkResult.Item2); | 
|   | 
|                 (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews); | 
|   | 
|                 _unitOfWorkManage.BeginTran(); | 
|                 WebResponseContent content = LockOutboundStockDataUpdate(result.Item1, new List<Dt_OutboundOrderDetail> { result.Item2 }, result.Item3, result.Item4); | 
|                 if (content.Status) | 
|                 { | 
|                     _unitOfWorkManage.CommitTran(); | 
|                 } | 
|                 else | 
|                 { | 
|                     _unitOfWorkManage.RollbackTran(); | 
|                 } | 
|                 return content; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 _unitOfWorkManage.RollbackTran(); | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         ///  锁定库存,由系统分配(包含逻辑运算,且修改数据库数据) | 
|         /// </summary> | 
|         /// <param name="orderDetailId"></param> | 
|         /// <param name="stockSelectViews"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LockOutboundStock(int[] keys) | 
|         { | 
|             try | 
|             { | 
|                 List<StockSelectViewDTO> stockSelectViews = new List<StockSelectViewDTO>(); | 
|                 List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>(); | 
|                 List<Dt_OutboundOrderDetail> outboundOrderDetails = new List<Dt_OutboundOrderDetail>(); | 
|                 List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>(); | 
|                 List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|                 foreach (var item in keys) | 
|                 { | 
|                     Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == item); | 
|                     (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); | 
|                     if (!checkResult.Item1) throw new Exception(checkResult.Item2); | 
|   | 
|                     (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews); | 
|                     if (result.Item1.Count > 0) | 
|                     { | 
|                         stockInfos.AddRange(result.Item1); | 
|                         outboundOrderDetails.Add(result.Item2); | 
|                         outStockLockInfos.AddRange(result.Item3); | 
|                         locationInfos.AddRange(result.Item4); | 
|                     } | 
|                 } | 
|   | 
|                 _unitOfWorkManage.BeginTran(); | 
|                 WebResponseContent content = LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos); | 
|                 if (content.Status) | 
|                 { | 
|                     _unitOfWorkManage.CommitTran(); | 
|                 } | 
|                 else | 
|                 { | 
|                     _unitOfWorkManage.RollbackTran(); | 
|                 } | 
|                 return content; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 _unitOfWorkManage.RollbackTran(); | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 出库库存分配后,更新数据库数据 | 
|         /// </summary> | 
|         /// <param name="stockInfos"></param> | 
|         /// <param name="outboundOrderDetails"></param> | 
|         /// <param name="outStockLockInfos"></param> | 
|         /// <param name="locationInfos"></param> | 
|         /// <param name="locationStatus"></param> | 
|         /// <param name="tasks"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null) | 
|         { | 
|             try | 
|             { | 
|                 _stockService.StockInfoService.Repository.UpdateData(stockInfos); | 
|                 List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>(); | 
|                 foreach (var item in stockInfos) | 
|                 { | 
|                     foreach (var detail in item.Details) | 
|                     { | 
|                         // 进行安全转换 | 
|                         if (detail.OutboundQuantity != null && decimal.TryParse(detail.OutboundQuantity.ToString(), out decimal outboundDecimal))   | 
|                         { | 
|                             decimal outboundDecimal1 = Convert.ToDecimal(detail.OutboundQuantity); | 
|                         } | 
|                         else | 
|                         { | 
|                             detail.OutboundQuantity = 0; // 默认值或记录错误 | 
|                         } | 
|                     } | 
|                     stockInfoDetails.AddRange(item.Details); | 
|   | 
|                 } | 
|                 _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails); | 
|                 BaseDal.UpdateData(outboundOrderDetails); | 
|   | 
|                 List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList(); | 
|                 if (addOutStockLockInfos != null && addOutStockLockInfos.Any()) | 
|                 { | 
|                     if (tasks != null) | 
|                     { | 
|                         addOutStockLockInfos.ForEach(x => | 
|                         { | 
|                             x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum; | 
|                         }); | 
|                     } | 
|   | 
|                     _outStockLockInfoService.Repository.AddData(addOutStockLockInfos); | 
|                 } | 
|                 List<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList(); | 
|                 if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) | 
|                 { | 
|                     _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos); | 
|                 } | 
|   | 
|                 _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", tasks?.Select(x => x.TaskNum).ToList()); | 
|                 _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus); | 
|                 return WebResponseContent.Instance.OK(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|   | 
|         private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews) | 
|         { | 
|             if (outboundOrderDetail == null) | 
|             { | 
|                 return (false, "未找到出库单明细信息"); | 
|             } | 
|             if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) | 
|             { | 
|                 return (false, "该明细不可操作"); | 
|             } | 
|             if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity) | 
|             { | 
|                 return (false, "选择数量超出单据数量"); | 
|             } | 
|             return (true, "成功"); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 撤销锁定库存 | 
|         /// </summary> | 
|         /// <param name="orderDetailId"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent RevokeLockOutboundStock(int orderDetailId) | 
|         { | 
|             Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); | 
|             (bool, string) result = CheckRevoke(outboundOrderDetail); | 
|             if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); | 
|   | 
|             List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetByOrderDetailId(orderDetailId, OutLockStockStatusEnum.已分配); | 
|             if (outStockLockInfos.Count > 0) | 
|             { | 
|                 List<Dt_StockInfo> stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(outStockLockInfos.Select(x => x.PalletCode).ToList()); | 
|                 if (stocks.Count > 0) | 
|                 { | 
|                     stocks.ForEach(x => | 
|                     { | 
|                         x.Details.ForEach(v => | 
|                         { | 
|                             v.OutboundQuantity = 0; | 
|                         }); | 
|                     }); | 
|   | 
|                     outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(); | 
|                     outboundOrderDetail.LockQuantity = outboundOrderDetail.OverOutQuantity; | 
|   | 
|                     outStockLockInfos.ForEach(x => | 
|                     { | 
|                         x.Status = OutLockStockStatusEnum.撤销.ObjToInt(); | 
|                     }); | 
|   | 
|                     List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(stocks.Select(x => x.LocationCode).ToList()); | 
|   | 
|                     return LockOutboundStockDataUpdate(stocks, new List<Dt_OutboundOrderDetail> { outboundOrderDetail }, outStockLockInfos, locationInfos, LocationStatusEnum.InStock); | 
|                 } | 
|                 return WebResponseContent.Instance.Error("未找到库存信息"); | 
|             } | 
|             return WebResponseContent.Instance.Error("未找到已分配的出库信息"); | 
|   | 
|         } | 
|   | 
|         private (bool, string) CheckRevoke(Dt_OutboundOrderDetail outboundOrderDetail) | 
|         { | 
|             if (outboundOrderDetail == null) | 
|             { | 
|                 return (false, "未找到出库单明细信息"); | 
|             } | 
|             if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOver.ObjToInt()) | 
|             { | 
|                 return (false, "该明细不可操作"); | 
|             } | 
|             return (true, "成功"); | 
|         } | 
|     } | 
| } |