|  |  |  | 
|---|
|  |  |  | using System.Linq; | 
|---|
|  |  |  | using System.Text; | 
|---|
|  |  |  | using System.Threading.Tasks; | 
|---|
|  |  |  | using WIDESEA_Common.LocationEnum; | 
|---|
|  |  |  | using WIDESEA_Common.OrderEnum; | 
|---|
|  |  |  | using WIDESEA_Common.StockEnum; | 
|---|
|  |  |  | using WIDESEA_Core; | 
|---|
|  |  |  | using WIDESEA_Core.BaseRepository; | 
|---|
|  |  |  | using WIDESEA_Core.BaseServices; | 
|---|
|  |  |  | using WIDESEA_Core.Helper; | 
|---|
|  |  |  | using WIDESEA_IBasicService; | 
|---|
|  |  |  | using WIDESEA_IOutboundRepository; | 
|---|
|  |  |  | using WIDESEA_IOutboundService; | 
|---|
|  |  |  | using WIDESEA_IRecordService; | 
|---|
|  |  |  | using WIDESEA_IStockService; | 
|---|
|  |  |  | using WIDESEA_Model.Models; | 
|---|
|  |  |  | using WIDESEA_OutboundRepository; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEA_OutboundService | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | private readonly IUnitOfWorkManage _unitOfWorkManage; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public IProOutOrderDetailRepository Repository => BaseDal; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ProOutOrderDetailService(IProOutOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) | 
|---|
|  |  |  | private readonly IStockService _stockInfoService; | 
|---|
|  |  |  | private readonly IBasicService _basicService; | 
|---|
|  |  |  | private readonly IOutProStockInfoService _outProStockInfoService; | 
|---|
|  |  |  | private readonly IProOutOrderRepository _proOutOrderRepository; | 
|---|
|  |  |  | private readonly IRecordService _recordService; | 
|---|
|  |  |  | public ProOutOrderDetailService(IProOutOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockInfoService, IBasicService basicService,IOutProStockInfoService outProStockInfoService, IRecordService recordService, IProOutOrderRepository proOutOrderRepository) : base(BaseDal) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _unitOfWorkManage = unitOfWorkManage; | 
|---|
|  |  |  | _stockInfoService = stockInfoService; | 
|---|
|  |  |  | _basicService = basicService; | 
|---|
|  |  |  | _outProStockInfoService = outProStockInfoService; | 
|---|
|  |  |  | _recordService = recordService; | 
|---|
|  |  |  | _proOutOrderRepository=proOutOrderRepository; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// åºåºåºååé
åï¼æ´æ°æ°æ®åºæ°æ® | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | public WebResponseContent LockOutboundStockDataUpdate(List<Dt_ProStockInfo> proStockInfos, List<Dt_ProOutOrderDetail> proOutOrderDetails, List<Dt_OutProStockInfo> outProStockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //æ´æ°åºå屿§ | 
|---|
|  |  |  | _stockInfoService.ProStockInfoService.Repository.UpdateData(proStockInfos); | 
|---|
|  |  |  | List<Dt_ProStockInfoDetail> proStockInfoDetails = new List<Dt_ProStockInfoDetail>(); | 
|---|
|  |  |  | foreach (var item in proStockInfos) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | proStockInfoDetails.AddRange(item.proStockInfoDetails); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //æ´æ°åºåæç» | 
|---|
|  |  |  | proStockInfoDetails.ForEach(x => x.ProOutDetailStatus = StockStatusEmun.åºåºéå®.ObjToInt()); | 
|---|
|  |  |  | _stockInfoService.ProStockInfoDetailService.Repository.UpdateData(proStockInfoDetails); | 
|---|
|  |  |  | Dt_ProOutOrder proOutOrder = _proOutOrderRepository.QueryFirst(x => x.Id == proOutOrderDetails.FirstOrDefault().ProOrderId); | 
|---|
|  |  |  | if (proOutOrder.ProOrderStatus == OutOrderStatusEnum.æªå¼å§.ObjToInt()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | proOutOrder.ProOrderStatus = OutOrderStatusEnum.åºåºä¸.ObjToInt(); | 
|---|
|  |  |  | _proOutOrderRepository.UpdateData(proOutOrder); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BaseDal.UpdateData(proOutOrderDetails); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_OutProStockInfo> addOutStockLockInfos = outProStockInfos.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; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _outProStockInfoService.Repository.AddData(addOutStockLockInfos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Dt_OutProStockInfo> updateOutStockLockInfos = outProStockInfos.Where(x => x.Id > 0).ToList(); | 
|---|
|  |  |  | if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _outProStockInfoService.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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// åé
åºåå¤çè´§ä½æ°æ® | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | public (List<Dt_ProStockInfo>, List<Dt_ProOutOrderDetail>, List<Dt_OutProStockInfo>, List<Dt_LocationInfo>) AssignProStockOut(List<Dt_ProOutOrderDetail> proOutOrderDetails) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<Dt_ProStockInfo> outStocks = new List<Dt_ProStockInfo>(); | 
|---|
|  |  |  | List<Dt_OutProStockInfo> outProStockInfos = new List<Dt_OutProStockInfo>(); | 
|---|
|  |  |  | List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|---|
|  |  |  | //è·åæå订å | 
|---|
|  |  |  | Dt_ProOutOrder proOutOrder = _proOutOrderRepository.QueryFirst(x=>x.Id==proOutOrderDetails.FirstOrDefault().ProOrderId); | 
|---|
|  |  |  | if (proOutOrder==null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("æªæ¾å°æå订å"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | foreach (var item in proOutOrderDetails) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | float needQty = item.QtyPcs; | 
|---|
|  |  |  | //æ¥æ¾å¯ç¨åºå | 
|---|
|  |  |  | List<Dt_ProStockInfo> stockInfoss = _stockInfoService.ProStockInfoService.GetUseableStocks(proOutOrder.WarehouseId, item); | 
|---|
|  |  |  | if (!stockInfoss.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("æªæ¾å°å¯åé
åºå"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //è·ååºåºåºå | 
|---|
|  |  |  | List<Dt_ProStockInfo> assignOutStocks = _stockInfoService.ProStockInfoService.GetOutboundStocks(stockInfoss, item, needQty, out float residueQuantity); | 
|---|
|  |  |  | item.LockQtyPcs += needQty - residueQuantity; | 
|---|
|  |  |  | if (item.QtyPcs > item.LockQtyPcs) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception($"产åç¼ç {item.PCode},çæ¬{item.PVer.Substring(0, 1)}å¯åé
æ°éä¸è¶³,å¯ç¨æ°é{item.LockQtyPcs}"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | outStocks.AddRange(assignOutStocks); | 
|---|
|  |  |  | //åé
åºåºçPCSæ°é | 
|---|
|  |  |  | float assignQuantity = needQty; | 
|---|
|  |  |  | //订åæç»çåºåºPCSæ°é | 
|---|
|  |  |  | float orderQuantity = item.QtyPcs; | 
|---|
|  |  |  | for (int j = 0; j < assignOutStocks.Count; j++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //åºåºè®¢åæç»å·²åé
æ°é | 
|---|
|  |  |  | float detailAssignQuantity = outProStockInfos.Where(x => x.OrderDetailId == item.Id).Sum(x => x.AssignQuantity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float orderDetailNeedQuantity = item.QtyPcs - detailAssignQuantity; | 
|---|
|  |  |  | //çæåºåºè¯¦æ | 
|---|
|  |  |  | if (orderDetailNeedQuantity > assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | item.LockQtyPcs += assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity); | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(proOutOrder, item, assignOutStocks[j], assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity)); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(proOutOrder, item, assignOutStocks[j], item.QtyPcs - detailAssignQuantity); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | item.LockQtyPcs = item.QtyPcs; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<string> locationArr = outStocks.Select(x => x.LocationCode).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(locationArr)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return (outStocks, proOutOrderDetails, outProStockInfos, locationInfos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|