using AutoMapper; using LogLibrary.Log; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Data; 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_InboundRepository; using WIDESEA_IStockService; using WIDESEA_ITaskInfoRepository; using WIDESEA_Model; using WIDESEA_Model.Models; using WIDESEA_Model.Models.Inbound; using WIDESEA_Model.Models.System.Request; namespace WIDESEA_InboundService { public partial class InboundOrderService : ServiceBase, IInboundOrderService { /// /// 根据入库单号获取入库单(含明细) /// /// /// 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 { materielGroupDTO.OrderNo = GetOrderNo(); Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode); (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, stockInfo); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); if (stockInfo == null) { stockInfo = new Dt_StockInfo(); stockInfo.PalletCode = materielGroupDTO.PalletCode; stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt(); stockInfo.Creater = "立库WMS"; stockInfo.Details = new List(); } List stockInfoDetails = new List(); List notExistLabels = new List(); foreach (var lablel in materielGroupDTO.SerialNumbers) { Dt_LabelMaster labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == lablel); if(labmaster == null) { //通过条码接口同步条码主数据 var res= _sys_JobService.GetLabMaster(lablel); if (res != null && res.Status) { labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == lablel); if (labmaster == null) { notExistLabels.Add(labmaster); } } else { return content = WebResponseContent.Instance.Error("组盘条码在上游WMS系统中不存在!"); } } if(labmaster!=null) { //主数据条码状态允许组盘:01已收料待质检,02已收料无需质检,03待进仓已质检,09已下架; //主数据条码状态不允许组盘:00创建,04已质检待退货,05收料房退货,06库房退货,07已进仓,08已上架,10已出库,11已冻结,12已锁定,20关闭的; if (labmaster.LABEL_STATUS=="00"|| labmaster.LABEL_STATUS == "04" || labmaster.LABEL_STATUS == "05"|| labmaster.LABEL_STATUS == "06" || labmaster.LABEL_STATUS == "07" || labmaster.LABEL_STATUS == "08" || labmaster.LABEL_STATUS == "10" || labmaster.LABEL_STATUS == "11" || labmaster.LABEL_STATUS == "12" || labmaster.LABEL_STATUS == "20") { return content = WebResponseContent.Instance.Error("主数据条码状态不允许组盘!条码状态:"+ labmaster.LABEL_STATUS); } } } if (notExistLabels.Count == 0) { foreach (var item in materielGroupDTO.SerialNumbers) { Dt_LabelMaster labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == item); Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail(); dt_StockInfoDetail.Status = 0; dt_StockInfoDetail.OrderNo = materielGroupDTO.OrderNo; dt_StockInfoDetail.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0; dt_StockInfoDetail.MaterielCode = labmaster.MATNR; dt_StockInfoDetail.MaterielName = labmaster.MAKTX; dt_StockInfoDetail.BatchNo = labmaster.BATCH; dt_StockInfoDetail.SerialNumber = labmaster.LABEL_NO; dt_StockInfoDetail.StockQuantity = int.Parse(labmaster.BOX_QTY); dt_StockInfoDetail.OutboundQuantity = 0; dt_StockInfoDetail.Creater = "System"; stockInfoDetails.Add(dt_StockInfoDetail); stockInfo.Details.AddRange(stockInfoDetails); } content = MaterielGroupUpdateData(stockInfo); } else { content = WebResponseContent.Instance.Error("组盘条码在上游WMS系统中不存在!"); } } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } finally { } return content; } /// /// 余料退回组盘 /// /// /// public WebResponseContent ReturnMaterielGroup(MaterielGroupDTO materielGroupDTO, ReturnInventoryRequest inventoryRequest) { WebResponseContent content = new WebResponseContent(); try { Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode); (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, stockInfo); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); if (stockInfo == null) { stockInfo = new Dt_StockInfo(); stockInfo.PalletCode = materielGroupDTO.PalletCode; stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt(); stockInfo.Creater = "WMS"; stockInfo.Details = new List(); } List stockInfoDetails = new List(); foreach (var item in inventoryRequest.DATA) { Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail(); dt_StockInfoDetail.Status = 0; dt_StockInfoDetail.OrderNo = materielGroupDTO.OrderNo; dt_StockInfoDetail.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0; dt_StockInfoDetail.MaterielCode = item.MATNR; dt_StockInfoDetail.MaterielName = ""; dt_StockInfoDetail.BatchNo = ""; dt_StockInfoDetail.SerialNumber = item.LABEL_NO; dt_StockInfoDetail.StockQuantity = int.Parse(item.QTY); dt_StockInfoDetail.OutboundQuantity = 0; dt_StockInfoDetail.Creater = "WMS"; stockInfoDetails.Add(dt_StockInfoDetail); stockInfo.Details.AddRange(stockInfoDetails); } content = MaterielGroupUpdateData(stockInfo); } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } finally { } return content; } /// /// 根据BYDWMS组盘信息,再生成立库组盘信息 /// /// /// public ReturnInventoryResponse returnInventory(ReturnInventoryRequest inventoryRequest) { string inparam = JsonConvert.SerializeObject(inventoryRequest); new LogFactory().GetLog("WMS接口").InfoFormat(true, "returnInventory", "余料退回信息", $"{inparam}"); ReturnInventoryResponse response= new ReturnInventoryResponse(); MaterielGroupDTO materielGroupDTO = new MaterielGroupDTO(); materielGroupDTO.PalletCode = inventoryRequest.TPNUM; materielGroupDTO.OrderNo = inventoryRequest.IZLID; //退货入库指令 List SerialNumbers = new List(); //保存回退数据到本地 Dt_MainReturnInventory dt_MainReturnInventory = new Dt_MainReturnInventory(); dt_MainReturnInventory.WH_NUMBER= inventoryRequest.WH_NUMBER; dt_MainReturnInventory.BUSINESS_CODE = inventoryRequest.BUSINESS_CODE; dt_MainReturnInventory.BUSINESS_NAME = inventoryRequest.BUSINESS_NAME; dt_MainReturnInventory.WERKS = inventoryRequest.WERKS; dt_MainReturnInventory.LGORT = inventoryRequest.LGORT; dt_MainReturnInventory.TOTAL_RETURN_QTY=inventoryRequest.TOTAL_RETURN_QTY; dt_MainReturnInventory.RETURN_NO = inventoryRequest.RETURN_NO; dt_MainReturnInventory.RETURN_ITEM_NO = inventoryRequest.RETURN_ITEM_NO; dt_MainReturnInventory.TPNUM = inventoryRequest.TPNUM; dt_MainReturnInventory.YLZD1 = inventoryRequest.YLZD1; dt_MainReturnInventory.YLZD2 = inventoryRequest.YLZD2; dt_MainReturnInventory.YLZD3 = inventoryRequest.YLZD3; dt_MainReturnInventory.YLZD4 = inventoryRequest.YLZD4; dt_MainReturnInventory.YLZD5 = inventoryRequest.YLZD5; dt_MainReturnInventory.IZLID = inventoryRequest.IZLID; dt_MainReturnInventory.SYSNOD = inventoryRequest.SYSNOD; dt_MainReturnInventory.MO_NO = inventoryRequest.MO_NO; foreach (ReturnInventory item in inventoryRequest.DATA) { Dt_ReturnInventoryDetail dt_ReturnInventoryDetail = new Dt_ReturnInventoryDetail(); dt_ReturnInventoryDetail.LABEL_NO = item.LABEL_NO; dt_ReturnInventoryDetail.SOBKZ = item.SOBKZ; dt_ReturnInventoryDetail.UNIT = item.UNIT; dt_ReturnInventoryDetail.LGORT = item.LGORT; dt_ReturnInventoryDetail.QTY = item.QTY; dt_ReturnInventoryDetail.LIFNR = item.LIFNR; dt_ReturnInventoryDetail.F_LGORT = item.F_LGORT; dt_ReturnInventoryDetail.MATNR = item.MATNR; _ReturnInventoryDetailRepository.AddData(dt_ReturnInventoryDetail); SerialNumbers.Add(item.LABEL_NO); } materielGroupDTO.SerialNumbers = SerialNumbers; _mainReturnInventoryRepository.AddData(dt_MainReturnInventory); WebResponseContent content = ReturnMaterielGroup(materielGroupDTO, inventoryRequest); if(content.Status) { response.MSGTY = "S"; } else { response.MSGTY = "E"; response.MSGTX = content.Message; } return response; } /// /// 生成订单号 /// /// public string GetOrderNo() { DataTable dt = BaseDal.QueryTable("SELECT FORMAT(NEXT VALUE FOR dbo.seqOrderNum, '000000000');"); return DateTime.Now.ToString("yyyyMMdd") + dt.Rows[0][0].ToString(); } /// /// 撤销组盘 /// /// /// public WebResponseContent MaterielGroupRevoke(string PalletCode) { WebResponseContent content = new WebResponseContent(); try { Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(PalletCode); if(stockInfo != null) { if(stockInfo.StockStatus!= (int)StockStatusEmun.组盘暂存) { return content = WebResponseContent.Instance.Error("组盘暂存状态才可以撤销组盘!"); } else { stockInfo.StockStatus = (int)StockStatusEmun.组盘撤销; foreach (var item in stockInfo.Details) { item.Status = (int)OutStockStatus.撤销; } } content = MaterielGroupUpdateData(stockInfo); } else { return content = WebResponseContent.Instance.Error("组盘信息不存在!"); } } 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); _stockService.StockInfoService.AddMaterielGroup(stockInfo); _unitOfWorkManage.CommitTran(); return WebResponseContent.Instance.OK(); } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } /// /// 组盘数据更新 /// /// 入库单 /// 入库单明细 /// 组盘数据 /// public WebResponseContent MaterielGroupUpdateData(Dt_StockInfo stockInfo) { try { _unitOfWorkManage.BeginTran(); _stockService.StockInfoService.AddMaterielGroup(stockInfo); _unitOfWorkManage.CommitTran(); return WebResponseContent.Instance.OK(); } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } /// /// 验证组盘数据 /// /// 物料组盘DTO /// 扫码序列号 /// 物料信息 /// 物料编号 /// 入库单据 /// 组盘信息 /// 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 (_stockService.StockInfoDetailService.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 != InboundStatusEnum.未开始.ObjToInt() && inboundOrder.OrderStatus != InboundStatusEnum.入库中.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); } /// /// 验证组盘数据 /// /// 物料组盘DTO /// 组盘信息 /// public (bool, string, object?) CheckMaterielGroupParam(MaterielGroupDTO materielGroupDTO, 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 (_stockService.StockInfoDetailService.ExistSerialNumbers(materielGroupDTO.SerialNumbers)) { return (false, "有序列号在库存中已存在", materielGroupDTO); } return (true, "成功", materielGroupDTO); } /// /// PDA分页查询数据 /// /// 页码 /// 订单号 /// public WebResponseContent QueryOrderInfo(int pageNo, string orderNo) { if (string.IsNullOrEmpty(orderNo)) { object obj = Repository.QueryPage(x => true, pageNo, 10, new Dictionary { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new { x.OrderNo, x.UpperOrderNo, x.CreateDate, x.Creater }); return WebResponseContent.Instance.OK(data: obj); } else { object obj = Repository.QueryPage(x => x.OrderNo == orderNo, pageNo, 10, new Dictionary { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new { x.OrderNo, x.UpperOrderNo, x.CreateDate, x.Creater }); return WebResponseContent.Instance.OK(data: obj); } } } }