using LogLibrary.Log; using MailKit; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.CommonEnum; using WIDESEA_Common.OrderEnum; using WIDESEA_Common.StockEnum; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.Helper; using WIDESEA_Core.Utilities; using WIDESEA_DTO.Inbound; using WIDESEA_IInboundService; using WIDESEA_IStockService; using WIDESEA_Model.Models; using WIDESEA_Model.Models.Basic; namespace WIDESEA_InboundService { public class InboundService : IInboundService { LogFactory LogFactory = new LogFactory(); private readonly IUnitOfWorkManage _unitOfWorkManage; public IInboundOrderDetailService InboundOrderDetailService { get; } public IInboundOrderService InbounOrderService { get; } private readonly IRepository _inboundOrderRepository; private readonly IRepository _warehouseAreaRepository; private readonly IRepository _locationTypeRepository; private readonly IRepository _stockInfoRepository; private readonly IRepository _inboundOrderDetailRepository; private readonly IRepository _taskRepository; private IStockService _stockService; public InboundService(IUnitOfWorkManage unitOfWorkManage, IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService, IRepository inboundOrderRepository, IRepository warehouseAreaRepository, IRepository locationTypeRepository, IRepository stockInfoRepository, IRepository inboundOrderDetailRepository, IStockService stockService, IRepository taskRepository) { _unitOfWorkManage = unitOfWorkManage; InboundOrderDetailService = inboundOrderDetailService; InbounOrderService = inbounOrderService; _inboundOrderRepository = inboundOrderRepository; _warehouseAreaRepository = warehouseAreaRepository; _locationTypeRepository = locationTypeRepository; _stockInfoRepository = stockInfoRepository; _inboundOrderDetailRepository = inboundOrderDetailRepository; _stockService = stockService; _taskRepository = taskRepository; } public async Task GroupPallet(GroupPalletDto palletDto) { WebResponseContent content = new WebResponseContent(); try { (bool, string, object?) result2 = ModelValidate.ValidateModelData(palletDto); if (!result2.Item1) return content.Error(result2.Item2); var code = _warehouseAreaRepository.Db.Queryable().Where(x => x.Code == palletDto.WarehouseType).Select(x => x.Code).First(); if (string.IsNullOrEmpty(code)) { return content.Error($"仓库中没有该{palletDto.WarehouseType}编号。"); } Dt_InboundOrder inboundOrder = new Dt_InboundOrder(); var details = _inboundOrderDetailRepository.QueryData(x => (x.OutBoxbarcodes == palletDto.Barcode|| x.Barcode == palletDto.Barcode) && x.OrderDetailStatus == (int)InOrderStatusEnum.未开始); if (details.Count() <= 0) { return content.Error("未找到该条码单据信息请确认是否已经组盘完成"); } inboundOrder = _inboundOrderRepository.Db.Queryable().Includes(x=>x.Details).Where(x => x.Id == details.First().OrderId).First(); if (inboundOrder == null) { return content.Error("未找到该条码主单信息"); } Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync(); List materielCodes = details.GroupBy(x => x.Barcode).Select(x => x.Key).ToList(); (bool, string, object?) result = CheckMaterielGroupParam(palletDto, materielCodes, inboundOrder, stockInfo); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); if (stockInfo == null) { stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None, LocationType = Convert.ToInt32(palletDto.locationType) }; stockInfo.Details = new List(); } if (stockInfo != null && stockInfo.Details.Count>0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType) { return content.Error($"该托盘组盘仓库为{stockInfo.Details.FirstOrDefault()?.WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘"); } foreach (var item in details) { stockInfo.Details.Add(new Dt_StockInfoDetail { StockId = stockInfo == null ? 0 : stockInfo.Id, Barcode = item.Barcode, MaterielCode = item.MaterielCode, BatchNo = item.BatchNo, Unit = item.Unit, InboundOrderRowNo = item.lineNo, SupplyCode = item.SupplyCode, WarehouseCode = palletDto.WarehouseType, StockQuantity = item.OrderQuantity, BarcodeQty = item.BarcodeQty, BarcodeUnit = item.BarcodeUnit, FactoryArea = inboundOrder.FactoryArea, Status = 0, OrderNo = inboundOrder.InboundOrderNo, BusinessType = inboundOrder.BusinessType, }); item.ReceiptQuantity = item.BarcodeQty; item.OrderDetailStatus = (int)OrderDetailStatusEnum.Over; item.WarehouseCode = palletDto.WarehouseType; item.ReturnToMESStatus = 0; } if (stockInfo.Id == 0) { stockInfo.PalletCode = palletDto.PalletCode; stockInfo.StockStatus = (int)StockStatusEmun.组盘暂存; } stockInfo.PalletType = (int)PalletTypeEnum.None; List updateDetailIds = details.Select(x => x.Id).ToList(); if (inboundOrder.OrderStatus == (int)InOrderStatusEnum.未开始) { inboundOrder.OrderStatus = (int)InOrderStatusEnum.入库中; } inboundOrder.Operator = App.User.UserName; content = MaterielGroupUpdateData(inboundOrder, details, stockInfo).Result; if (content.Status) { Dt_StockInfo? NewstockInfo = await _stockInfoRepository.Db.Queryable().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync(); return WebResponseContent.Instance.OK(data: NewstockInfo.Details.OrderByDescending(x => x.Id)); } else { content = WebResponseContent.Instance.Error(content.Message); } } catch (Exception ex) { LogFactory.GetLog($"组盘信息").Info(true, $"【异常】:【{ex.Message}】{Environment.NewLine}【{ex.StackTrace}】{Environment.NewLine}{Environment.NewLine}"); return content.Error(ex.Message); } return content; } /// /// 验证组盘数据 /// /// 物料组盘DTO /// 扫码序列号 /// 物料信息 /// 物料编号 /// 入库单据 /// 组盘信息 /// public (bool, string, object?) CheckMaterielGroupParam(GroupPalletDto materielGroupDTO, List barcodeCodes, 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 != (int)StockStatusEmun.组盘暂存) { return (false, "已上架的托盘不能再次组盘", materielGroupDTO); } if (_stockService.StockInfoDetailService.ExistBarcodes(barcodeCodes)) { return (false, $"{barcodeCodes[0]} 条码在库存中已存在", materielGroupDTO); } if (inboundOrder == null) { return (false, "单据不存在", materielGroupDTO); } if (inboundOrder.Details == null || inboundOrder.Details.Count == 0) { return (false, "无单据明细信息", materielGroupDTO); } if (inboundOrder.OrderStatus != (int)InOrderStatusEnum.未开始 && inboundOrder.OrderStatus != (int)InOrderStatusEnum.入库中) { return (false, "该单据不可再组盘", materielGroupDTO); } List inboundOrderDetails = inboundOrder.Details.Where(x => barcodeCodes.Contains(x.Barcode)).ToList(); if (inboundOrderDetails.GroupBy(x => x.Barcode).Count() != barcodeCodes.Count) { return (false, "有物料不在单据内", materielGroupDTO); } return (true, "成功", materielGroupDTO); } public async Task MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List inboundOrderDetails, Dt_StockInfo stockInfo) { try { _unitOfWorkManage.BeginTran(); //await _inboundOrderRepository.Db.UpdateNav(inboundOrder).Include(x=>x.Details).ExecuteCommandAsync(); _inboundOrderRepository.UpdateData(inboundOrder); _inboundOrderDetailRepository.UpdateData(inboundOrderDetails); _stockService.StockInfoService.AddMaterielGroup(stockInfo); _unitOfWorkManage.CommitTran(); return WebResponseContent.Instance.OK(); } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } } }