using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_Core.Utilities; using WIDESEA_DTO; using WIDESEA_DTO.Inbound; using WIDESEA_IBasicService; using WIDESEA_IInboundRepository; using WIDESEA_IInboundService; using WIDESEA_IStockService; using WIDESEA_ITaskInfoRepository; using WIDESEA_Model.Models; namespace WIDESEA_InboundService { public class InboundOrderService : ServiceBase, IInboundOrderService { private readonly IMapper _mapper; private readonly IMaterielInfoService _materielInfoService; private readonly IStockInfoService _stockInfoService; private readonly IStockInfoDetailService _stockDetailService; private readonly ITaskRepository _taskRepository; private readonly IInboundOrderDetailService _inboundOrderDetailService; private readonly IUnitOfWorkManage _unitOfWorkManage; public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IMaterielInfoService materielInfoService, IStockInfoDetailService stockDetailService, IStockInfoService stockInfoService, ITaskRepository taskRepository, IInboundOrderDetailService inboundOrderDetailService, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) { _mapper = mapper; _materielInfoService = materielInfoService; _stockDetailService = stockDetailService; _stockInfoService = stockInfoService; _taskRepository = taskRepository; _inboundOrderDetailService = inboundOrderDetailService; _unitOfWorkManage = unitOfWorkManage; } public override WebResponseContent AddData(SaveModel saveModel) { InboundOrderAddDTO orderAddDTO = saveModel.MainData.DicToModel(); orderAddDTO.Details = saveModel.DetailData.DicToIEnumerable(); return AddInboundOrder(orderAddDTO); } public WebResponseContent AddInboundOrder(InboundOrderAddDTO orderAddDTO) { WebResponseContent content = new(); try { #region 验证数据 (bool, string, object?) result = CheckInboundOrderAddData(orderAddDTO); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); #endregion Dt_InboundOrder inboundOrder = _mapper.Map(orderAddDTO); inboundOrder.OrderStatus = InboundStatusEmun.未开始.ObjToInt(); bool a = BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand(); content = WebResponseContent.Instance.OK(); } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } finally { } return content; } private (bool, string, object?) CheckInboundOrderAddData(InboundOrderAddDTO inboundOrderAddDTO) { (bool, string, object?) result1 = ModelValidate.ValidateModelData(inboundOrderAddDTO); if (!result1.Item1) return result1; (bool, string, object?) result2 = ModelValidate.ValidateModelData(inboundOrderAddDTO.Details); if (!result2.Item1) return result2; IEnumerable inOrderTypes = Enum.GetValues().Cast(); if (!inOrderTypes.Contains(inboundOrderAddDTO.OrderType)) { return (false, "未找到该单据类型", inboundOrderAddDTO); } List materielCodes = inboundOrderAddDTO.Details.Select(x => x.MaterielCode).ToList(); if (!_materielInfoService.ExsitMateriels(materielCodes)) { return (false, "有物料信息未录入,请录入物料信息", inboundOrderAddDTO); } if (BaseDal.QueryFirst(x => x.UpperOrderNo == inboundOrderAddDTO.UpperOrderNo && !string.IsNullOrEmpty(x.UpperOrderNo)) != null) { return (false, "单据已存在", inboundOrderAddDTO); } return (true, "成功", inboundOrderAddDTO); } public Dt_InboundOrder GetInboundOrder(string inboundOrderNo) { return BaseDal.Db.Queryable().Includes(x => x.Details).First(x => x.OrderNo == inboundOrderNo); ; } /// /// 组盘 /// /// /// public WebResponseContent MaterielGroup(MaterielGroupDTO materielGroupDTO) { WebResponseContent content = new WebResponseContent(); try { List matSerialNumberDTOs = CodeAnalysisHelper.CodeAnalysis(AnalysisCode.InnerCode, materielGroupDTO.SerialNumbers); (bool, string, object?) result2 = ModelValidate.ValidateModelData(matSerialNumberDTOs); if (!result2.Item1) return content = WebResponseContent.Instance.Error(result2.Item2); List materielCodes = matSerialNumberDTOs.GroupBy(x => x.MaterielCode).Select(x => x.Key).ToList(); List materielInfos = _materielInfoService.GetMaterielInfos(materielCodes); Dt_InboundOrder inboundOrder = GetInboundOrder(materielGroupDTO.OrderNo); Dt_StockInfo? stockInfo = _stockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode); (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, matSerialNumberDTOs, materielInfos, materielCodes, inboundOrder, stockInfo); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); if (stockInfo == null) { stockInfo = new Dt_StockInfo(); stockInfo.Details = new List(); } List stockInfoDetails = _mapper.Map>(matSerialNumberDTOs); stockInfoDetails.ForEach(x => { x.Status = 0; x.OrderNo = inboundOrder.OrderNo; x.MaterielName = materielInfos.FirstOrDefault(v => v.MaterielCode == x.MaterielCode)?.MaterielName ?? ""; x.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0; }); if (stockInfo.Id == 0) { stockInfo.PalletCode = materielGroupDTO.PalletCode; stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt(); } stockInfo.Details.AddRange(stockInfoDetails); List inboundOrderDetails = new List(); for (int i = 0; i < materielCodes.Count; i++) { decimal stockQuantity = stockInfoDetails.Where(x => x.MaterielCode == materielCodes[i]).Sum(x => x.StockQuantity); inboundOrderDetails.AddRange(_inboundOrderDetailService.UpdateReceiptQuantity(inboundOrder.Details.Where(x => x.MaterielCode == materielCodes[i]).ToList(), stockQuantity)); } List updateDetailIds = inboundOrderDetails.Select(x => x.Id).ToList(); if (inboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null && inboundOrder.Details.FirstOrDefault(x => !updateDetailIds.Contains(x.Id) && x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null) { inboundOrder.OrderStatus = InboundStatusEmun.入库完成.ObjToInt(); } else if (inboundOrder.OrderStatus == InboundStatusEmun.未开始.ObjToInt()) { inboundOrder.OrderStatus = InboundStatusEmun.入库中.ObjToInt(); } content = MaterielGroupUpdateData(inboundOrder, inboundOrderDetails, stockInfo); } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } finally { } return content; } public WebResponseContent MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List inboundOrderDetails, Dt_StockInfo stockInfo) { try { _unitOfWorkManage.BeginTran(); UpdateData(inboundOrder); _inboundOrderDetailService.UpdateData(inboundOrderDetails); _stockInfoService.AddMaterielGroup(stockInfo); _unitOfWorkManage.CommitTran(); return WebResponseContent.Instance.OK(); } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } public (bool, string, object?) CheckMaterielGroupParam(MaterielGroupDTO materielGroupDTO, List matSerialNumberDTOs, List materielInfos, List materielCodes, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfo) { (bool, string, object?) result = ModelValidate.ValidateModelData(materielGroupDTO); if (!result.Item1) return result; if (_taskRepository.QueryFirst(x => x.PalletCode == materielGroupDTO.PalletCode) != null) { return (false, "该托盘号已有任务", materielGroupDTO); } if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt()) { return (false, "已上架的托盘不能再次组盘", materielGroupDTO); } if (_stockDetailService.ExistSerialNumbers(materielGroupDTO.SerialNumbers)) { return (false, "有序列号在库存中已存在", materielGroupDTO); } if (materielInfos.Count != materielCodes.Count) { return (false, "有物料信息未录入,请录入物料信息", materielGroupDTO); } if (materielCodes.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null) { return (false, "有物料不可混料组盘", materielGroupDTO); } List batchs = matSerialNumberDTOs.GroupBy(x => x.BatchNo).Select(x => x.Key).ToList(); if (batchs.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null) { return (false, "有物料不可混批组盘", materielGroupDTO); } if (inboundOrder == null) { return (false, "单据不存在", materielGroupDTO); } if (inboundOrder.Details == null || inboundOrder.Details.Count == 0) { return (false, "无单据明细信息", materielGroupDTO); } if (inboundOrder.OrderStatus != InboundStatusEmun.未开始.ObjToInt() && inboundOrder.OrderStatus != InboundStatusEmun.入库中.ObjToInt()) { return (false, "该单据不可再组盘", materielGroupDTO); } List inboundOrderDetails = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).ToList(); if (inboundOrderDetails.GroupBy(x => x.MaterielCode).Count() != materielCodes.Count) { return (false, "有物料不在单据内", materielGroupDTO); } IGrouping? temp = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).GroupBy(x => x.MaterielCode).FirstOrDefault(x => x.Sum(v => v.OverInQuantity) >= x.Sum(v => v.OrderQuantity) || x.Sum(v => v.ReceiptQuantity) >= x.Sum(v => v.OrderQuantity)); if (temp != null) { return (false, "有物料超出单据数量", materielGroupDTO); } return (true, "成功", materielGroupDTO); } } }