using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_DTO.MES;
using WIDESEA_DTO;
using WIDESEA_Model.Models;
using WIDESEA_Core.Helper;
using WIDESEA_Common.WareHouseEnum;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using WIDESEA_Common.APIEnum;
using System.Reflection.Metadata;
using Microsoft.Extensions.FileSystemGlobbing.Internal;
using System.Text.RegularExpressions;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Core.CodeConfigEnum;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// MES成品入库单接收
///
///
public MesResponseContent BagInfoSync(MesBagInfoModel bagInfoModel)
{
MesResponseContent content = new MesResponseContent();
try
{
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == bagInfoModel.WarehouseCode);
if (warehouse == null)
{
return content.Error("未找到仓库信息");
}
////限制任务
//Dt_Task task = BaseDal.QueryFirst(x => x.SourceAddress == bagInfoModel.WorkCenter && (x.TaskStatus == TaskStatusEnum.New.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Executing.ObjToInt()));
//if (task!=null)
//{
// return content.Error($"线体存在任务或执行中,胶框号{task.PalletCode}");
//}
Dt_Warehouse warehouseLocation = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA71.ToString());
Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == warehouseLocation.WarehouseId);
if (locationInfo == null)
{
return content.Error($"未找到{warehouse.WarehouseCode}货位信息");
}
Dt_ProStockInfo proStockInfoOld = _stockRepository.ProStockInfoRepository.QueryFirst(x=>x.PalletCode==bagInfoModel.BatchNo);
if (proStockInfoOld != null)
{
return content.Error($"胶框{bagInfoModel.BatchNo}库存信息已存在");
}
//判断该胶框号的内包信息是否存在
Dt_MesProInOrder mesProInOrderOld = BaseDal.Db.Queryable().Where(x => x.BatchNo == bagInfoModel.BatchNo && x.MesProStatus == InOrderStatusEnum.入库中.ObjToInt()).Includes(x => x.Details).First();
if (mesProInOrderOld!=null)
{
return content.Error($"胶框{bagInfoModel.BatchNo}内包信息未完成");
}
List proDetailsExists = _stockRepository.ProStockInfoDetailRepository.QueryData().Select(x=>x.BagNo).ToList();
//内包信息组盘
List mesProInOrderDetails = new List();
string palletCode = @"^C\d{5}$"; // 正则表达式
bool isValid = Regex.IsMatch(bagInfoModel.BatchNo, palletCode);
if (!isValid && bagInfoModel.BatchNo.Substring(0,3).ToUpper()!= "CPK")
{
return content.Error($"框码格式错误{bagInfoModel.BatchNo}");
}
if (bagInfoModel.BatchNo.Substring(0, 3).ToUpper() == "CPK")
{
WebResponseContent inProRespone = InPKProStock(bagInfoModel, proDetailsExists, warehouse);
if (!inProRespone.Status)
{
return content.Error(inProRespone.Message);
}
_proInStatisticsService.SaveStatic(bagInfoModel);
return content.OK(inProRespone.Message);
}
foreach (var item in bagInfoModel.BagDetails)
{
if (proDetailsExists.Contains(item.BagNo))
{
return content.Error($"内包{item.BagNo}已存在");
}
foreach (var child in item.BagItems)
{
Dt_MesProInOrderDetail mesProInOrderDetail = new Dt_MesProInOrderDetail()
{
BagNo = item.BagNo,
ProductCode = item.ProductCode,
ProductVersion = item.ProductVersion,
SETQty = child.SETQty,
OKPCSQTY = child.OKPCSQTY,
DateCode = item.DateCode,
XQty = child.XQty,
XSite = child.XSite,
Weight = item.Weight,
PackingDate = item.PackingDate,
LotNumber = child.LotNumber,
ERPOrder = child.ERPOrder,
SaleOrder = child.SaleOrder,
MoNumber = child.MoNumber,
};
mesProInOrderDetails.Add(mesProInOrderDetail);
}
}
List proStockInfoDetails = new List();
foreach (var item in mesProInOrderDetails)
{
proStockInfoDetails.Add(_mapper.Map(item));
}
Dt_MesProInOrder mesProInOrder = new Dt_MesProInOrder()
{
WarehouseId = warehouse.WarehouseId,
ProInOrderNo = _outboundService.OutboundOrderService.CreateCodeByRule(nameof(RuleCodeEnum.ProInCodeRule)),
BatchNo = bagInfoModel.BatchNo,
WorkCenter = bagInfoModel.WorkCenter,
MesProStatus = InOrderStatusEnum.入库中.ObjToInt(),
UnPackStock = bagInfoModel.UnPackStock,
CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
Details = mesProInOrderDetails
};
ProStockAttributeEnum attributeEnum = new ProStockAttributeEnum();
switch (warehouse.WarehouseCode)
{
case "HA71":
attributeEnum = ProStockAttributeEnum.成品;
break;
case "HA72":
attributeEnum = ProStockAttributeEnum.尾数;
break;
case "HA73":
attributeEnum = ProStockAttributeEnum.研发;
break;
default:
throw new Exception("未找到该库存属性");
}
Dt_ProStockInfo proStockInfo = new Dt_ProStockInfo()
{
PalletCode = bagInfoModel.BatchNo,
PalletType = 1,
ProStockAttribute= attributeEnum.ObjToInt(),
WarehouseId = warehouse.WarehouseId,
StockStatus = StockStatusEmun.入库确认.ObjToInt(),
proStockInfoDetails = proStockInfoDetails
};
Dt_Task newTask = new Dt_Task()
{
CurrentAddress = bagInfoModel.WorkCenter,
Grade = 0,
NextAddress = "",
PalletCode = proStockInfo.PalletCode,
Roadway = locationInfo.RoadwayNo,
SourceAddress = bagInfoModel.WorkCenter,
TargetAddress = "",
TaskType = TaskTypeEnum.InProduct.ObjToInt(),
TaskStatus = TaskStatusEnum.New.ObjToInt(),
WarehouseId = warehouse.WarehouseId,
PalletType = proStockInfo.PalletType,
MaterielCode = proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).FirstOrDefault()?.ProductCode,
Quantity = (float)proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).Sum(x => x.StockPcsQty)
};
_unitOfWorkManage.BeginTran();
int taskId = BaseDal.AddData(newTask);
newTask.TaskId = taskId;
Db.InsertNav(proStockInfo).Include(x => x.proStockInfoDetails).ExecuteCommand();
//上传ERP
WebResponseContent responseContent = _inboundOrderService.FeedbackProIn(mesProInOrder);
_proInStatisticsService.SaveStatic(bagInfoModel);
if (!responseContent.Status)
{
mesProInOrder.UpErpStatus = WhetherEnum.False.ObjToInt();
mesProInOrder.Remark = responseContent.Message;
}
else
{
mesProInOrder.UpErpStatus = WhetherEnum.True.ObjToInt();
}
Db.InsertNav(mesProInOrder).Include(x => x.Details).ExecuteCommand();
_unitOfWorkManage.CommitTran();
//推送任务
PushTasksToWCS(new List { newTask });
content.OK("接收成功");
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
public WebResponseContent InPKProStock(MesBagInfoModel bagInfoModel, List proDetailsExists,Dt_Warehouse wareSource)
{
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x=>x.WarehouseCode==WarehouseEnum.HA101.ToString());
if (warehouse==null)
{
return WebResponseContent.Instance.Error("未找到平库仓库信息");
}
//内包信息组盘
List mesProInOrderDetails = new List();
foreach (var item in bagInfoModel.BagDetails)
{
if (proDetailsExists.Contains(item.BagNo))
{
return WebResponseContent.Instance.Error($"内包{item.BagNo}已存在");
}
foreach (var child in item.BagItems)
{
Dt_MesProInOrderDetail mesProInOrderDetail = new Dt_MesProInOrderDetail()
{
BagNo = item.BagNo,
ProductCode = item.ProductCode,
ProductVersion = item.ProductVersion,
SETQty = child.SETQty,
OKPCSQTY = child.OKPCSQTY,
DateCode = item.DateCode,
XQty = child.XQty,
XSite = child.XSite,
Weight = item.Weight,
PackingDate = item.PackingDate,
LotNumber = child.LotNumber,
ERPOrder = child.ERPOrder,
SaleOrder = child.SaleOrder,
MoNumber = child.MoNumber,
OverInQuantity= child.OKPCSQTY
};
mesProInOrderDetails.Add(mesProInOrderDetail);
}
}
List proStockInfoDetails = new List();
foreach (var item in mesProInOrderDetails)
{
proStockInfoDetails.Add(_mapper.Map(item));
}
Dt_MesProInOrder mesProInOrder = new Dt_MesProInOrder()
{
WarehouseId = warehouse.WarehouseId,
ProInOrderNo = _outboundService.OutboundOrderService.CreateCodeByRule(nameof(RuleCodeEnum.ProInCodeRule)),
BatchNo = bagInfoModel.BatchNo,
WorkCenter = bagInfoModel.WorkCenter,
MesProStatus = InOrderStatusEnum.入库完成.ObjToInt(),
UnPackStock = bagInfoModel.UnPackStock,
CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
Details = mesProInOrderDetails
};
ProStockAttributeEnum attributeEnum = new ProStockAttributeEnum();
switch (wareSource.WarehouseCode)
{
case "HA71":
attributeEnum = ProStockAttributeEnum.成品;
break;
case "HA72":
attributeEnum = ProStockAttributeEnum.尾数;
break;
case "HA73":
attributeEnum = ProStockAttributeEnum.研发;
break;
default:
throw new Exception("未找到该库存属性");
}
proStockInfoDetails.ForEach(x =>
{
x.ProOutDetailStatus = StockStatusEmun.平库入库完成.ObjToInt();
});
Dt_ProStockInfo proStockInfo = new Dt_ProStockInfo()
{
PalletCode = bagInfoModel.BatchNo,
PalletType = 1,
LocationCode= "成品待发货区",
ProStockAttribute = attributeEnum.ObjToInt(),
WarehouseId = warehouse.WarehouseId,
StockStatus = StockStatusEmun.平库入库完成.ObjToInt(),
proStockInfoDetails = proStockInfoDetails
};
_unitOfWorkManage.BeginTran();
Db.InsertNav(proStockInfo).Include(x => x.proStockInfoDetails).ExecuteCommand();
//上传ERP
WebResponseContent responseContent = _inboundOrderService.FeedbackProIn(mesProInOrder);
if (!responseContent.Status)
{
mesProInOrder.UpErpStatus = WhetherEnum.False.ObjToInt();
mesProInOrder.Remark = responseContent.Message;
}
else
{
mesProInOrder.UpErpStatus = WhetherEnum.True.ObjToInt();
}
Db.InsertNav(mesProInOrder).Include(x => x.Details).ExecuteCommand();
_unitOfWorkManage.CommitTran();
return WebResponseContent.Instance.OK("接收成功");
}
///
/// 外包信息接收
///
public MesResponseContent BoxStockin(MesBoxInfoModel boxInfoModel)
{
MesResponseContent content = new MesResponseContent();
try
{
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA101.ToString());
//截取出库单号
string shipmentOrder = boxInfoModel.ShipmentOrder.Substring(0, boxInfoModel.ShipmentOrder.IndexOf("-"));
//判断MES传入的出库单号是否存在
Dt_ProOutOrder proOutOrder = _outboundRepository.ProOutOrderRepository.Db.Queryable().Where(x => x.ProOutOrderNo == shipmentOrder).Includes(x => x.Details).First();
if (proOutOrder == null)
{
return content.Error($"单号{shipmentOrder}不存在");
}
//判断外箱号是否已存在
Dt_ProStockInfo ProStockOld = _stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == boxInfoModel.BoxNo);
if (ProStockOld!=null)
{
return content.Error($"外箱号{ProStockOld.PalletCode}存库信息已存在");
}
//获取所有成品出库内包信息
List dt_ProStockInfos = _stockRepository.ProStockInfoDetailRepository.QueryData(x => x.ProOutDetailStatus == StockStatusEmun.出库完成.ObjToInt());
//获取MES内包信息
List mesProInOrderDetails = new List();
foreach (var item in boxInfoModel.Bags)
{
foreach (var child in item.BagItems)
{
Dt_ProStockInfoDetail? proStockInfoDetail = dt_ProStockInfos.Where(x => x.BagNo == item.BagNo)?.FirstOrDefault();
if (proStockInfoDetail == null)
{
return content.Error($"内包{item.BagNo}货物状态异常");
}
if (proStockInfoDetail.OutboundQuantity<=0)
{
return content.Error($"内包{item.BagNo}出库数量未分配");
}
Dt_MesProInOrderDetail mesProInOrderDetail = new Dt_MesProInOrderDetail()
{
BagNo = item.BagNo,
ProductCode = item.ProductCode,
ProductVersion = item.ProductVersion,
SETQty = child.SETQty,
OKPCSQTY = child.OKPCSQTY,
DateCode = item.DateCode,
XQty = child.XQty,
XSite = child.XSite,
Weight = item.Weight,
PackingDate = item.PackingDate,
LotNumber = child.LotNumber,
ERPOrder = child.ERPOrder,
SaleOrder = child.SaleOrder,
MoNumber = child.MoNumber,
};
mesProInOrderDetails.Add(mesProInOrderDetail);
}
}
List proStockInfoDetails = new List();
foreach (var item in mesProInOrderDetails)
{
proStockInfoDetails.Add(_mapper.Map(item));
}
proStockInfoDetails.ForEach(x =>
{
x.ProOutDetailStatus = StockStatusEmun.平库入库完成.ObjToInt();
});
_unitOfWorkManage.BeginTran();
WebResponseContent UnBindContent = _stockService.ProStockInfoService.UnBindStock(proStockInfoDetails);
if (!UnBindContent.Status)
{
throw new Exception(UnBindContent.Message);
}
if (UnBindContent.Data!=null)
{
Dt_ProStockInfo proStockInfo = new Dt_ProStockInfo()
{
PalletCode = boxInfoModel.BoxNo,
PalletType = 0,
LocationCode = "成品待发货区",
WarehouseId = warehouse.WarehouseId,
ShipmentOrder = shipmentOrder,
StockStatus = StockStatusEmun.平库入库完成.ObjToInt(),
proStockInfoDetails = UnBindContent.Data as List,
ProStockAttribute = ProStockAttributeEnum.成品.ObjToInt()
};
//根据外包信息解绑内包与胶框库存关系
Db.InsertNav(proStockInfo).Include(x => x.proStockInfoDetails).ExecuteCommand();
}
_unitOfWorkManage.CommitTran();
return content.OK("外包接收成功");
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// MES成品出库单同步数据处理
///
public MesShipmentOrderSync MesOutSync(Dt_OutProStockInfo outProStockInfo, Dt_ProOutOrderDetail proOutOrderDetail,List proStockInfoDetails)
{
List mesShipOrderDetails = new List();
//获取出库单
Dt_ProOutOrder proOutOrder = _outboundService.ProOutOrderService.Repository.QueryFirst(x => x.Id == proOutOrderDetail.ProOrderId);
//获取客户
Dt_CustomerInfo customerInfo = _basicRepository.CustomerInfoRepository.QueryFirst(x => x.Code == proOutOrderDetail.EndCustomer);
List outBags = proStockInfoDetails.Where(x => x.OutboundQuantity > 0).Select(x => x.BagNo).ToList();
outBags.ForEach(x =>
{
x = string.Format($"{0},{1},{2},0,{3}",x,outProStockInfo.PCode,outProStockInfo.DateCode,outProStockInfo.AssignQuantity.ObjToInt());
});
MesShipOrderDetail orderDetail = new MesShipOrderDetail()
{
ProductCode = proOutOrderDetail.PCode,
ProductVersion = proOutOrderDetail.PVer,
Qty = outProStockInfo.AssignQuantity,
DateCode = outProStockInfo.DateCode,
SaleOrder = proOutOrderDetail.SaleOrder,
Sequence = outProStockInfo.Id,
WorkCenter = "SPCK_OUTER-001",
PlasticBox = new List { new PlasticBox()
{
PlasticBoxNumber= outProStockInfo.PalletCode,
Insourcings = outBags
}
}
};
mesShipOrderDetails.Add(orderDetail);
MesShipmentOrderSync shipmentOrderSync = new MesShipmentOrderSync()
{
ShipmentOrder = proOutOrder.ProOutOrderNo+"-"+ outProStockInfo.TaskNum,
PlantShipDate = proOutOrder.PlantShipDate.ToString("yyyy-MM-dd HH:mm:ss"),
Customer = proOutOrderDetail.EndCustomer,
FactoryCode = proOutOrderDetail.FactoryCode,
ShipName = customerInfo.Name,
ShippingAddress = customerInfo.NickName,
OrderDetails = mesShipOrderDetails
};
return shipmentOrderSync;
}
///
/// WMS同步成品出库至MES
///
public WebResponseContent ShipmentOrderSync(MesShipmentOrderSync model)
{
WebResponseContent content = new WebResponseContent();
try
{
Dt_ApiInfo apiInfo = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMS_MES_ShipmentOrderSync.ToString());
MESRoot root = new MESRoot()
{
From = "WMS",
DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Content = model
};
JsonSerializerSettings settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
string request = JsonConvert.SerializeObject(root, settings);
string response = HttpMesHelper.Post(apiInfo.ApiAddress, request);
MesResponseContent mesResponseContent = response.DeserializeObject();
//调用接口
if (mesResponseContent.BSucc == true)
{
content.OK(mesResponseContent.StrMsg);
}
else
{
content.Error(mesResponseContent.StrMsg);
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// MES成品返工提库
///
///
///
public MesResponseContent RworkTask(RworkTaskModel model)
{
MesResponseContent content = new MesResponseContent();
try
{
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA72.ToString());
if (warehouse == null)
{
return content.Error($"尾数仓信息未配置");
}
Dt_MesRworkOutboundOrder rworkOutboundOrderOld = _outboundRepository.RworkOutboundOrderRepository.QueryFirst(x => x.TaskNo == model.TaskNo);
if (rworkOutboundOrderOld != null)
{
return content.Error($"提库任务单{model.TaskNo}已存在");
}
Dt_MesRworkOutboundOrder mesRworkOutboundOrder = new Dt_MesRworkOutboundOrder()
{
WarehouseId = warehouse.WarehouseId,
TaskNo = model.TaskNo,
OrderStatus = OutOrderStatusEnum.未开始.ObjToInt(),
CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
ProductCode = model.ProductCode,
ProductName = model.ProductName,
ProductVersion = model.ProductVersion,
DateCode = model.DateCode,
RequiredQuantity = model.RequiredQuantity,
FactoryCode = model.FactoryCode,
SaleOrder = model.SaleOrder,
OrderType = model.InventoryType
};
List tasks = new List();
List? proStockInfos = null;
List? outProStockInfos = null;
List? locationInfos = null;
//分配库存生成提库任务
(List?, Dt_MesRworkOutboundOrder?, List?, List) result = _outboundService.RworkOutboundOrderService.AssignMesStocks(mesRworkOutboundOrder);
if (result.Item1 != null && result.Item1.Count > 0)
{
tasks = GetTasks(result.Item1, TaskTypeEnum.OutMesRworkProduct);
result.Item2.OrderStatus = OutOrderStatusEnum.出库中.ObjToInt();
result.Item3.ForEach(x =>
{
x.Status = OutLockStockStatusEnum.出库中.ObjToInt();
});
proStockInfos = result.Item1;
mesRworkOutboundOrder = result.Item2;
outProStockInfos = result.Item3;
locationInfos = result.Item4;
}
else
{
throw new Exception("无库存分配");
}
tasks.ForEach(x =>
{
x.TargetAddress = "5236";
x.OrderNo = mesRworkOutboundOrder.TaskNo;
});
_unitOfWorkManage.BeginTran();
int id = BaseDal.AddData(tasks);
outProStockInfos.ForEach(x =>
{
x.OrderNo = mesRworkOutboundOrder.TaskNo;
x.OrderDetailId = id;
});
if (proStockInfos != null && proStockInfos.Count > 0 && outProStockInfos != null && outProStockInfos.Count > 0 && locationInfos != null && locationInfos.Count > 0)
{
WebResponseContent updateContent = _outboundService.RworkOutboundOrderService.LockOutboundStockDataUpdate(proStockInfos, outProStockInfos, locationInfos, tasks: tasks);
if (!updateContent.Status)
{
_unitOfWorkManage.RollbackTran();
return content.Error(updateContent.Message);
}
}
_outboundService.RworkOutboundOrderService.Repository.AddData(mesRworkOutboundOrder);
_unitOfWorkManage.CommitTran();
return content.OK("提供返库单接收成功");
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
#region 弃用
///
/// 库存板数据转换
///
///
//public MesProductOutBound MesProOutBound(Dt_MesRworkOutboundOrder mesRworkOutboundOrder,List proStockInfoDetails)
//{
// //获取仓库信息
// Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == mesRworkOutboundOrder.WarehouseId);
// List inventoryInfos = new List();
// foreach (var item in proStockInfoDetails)
// {
// MesInventoryInfo mesInventoryInfo = new MesInventoryInfo()
// {
// Warhouseno = warehouse.WarehouseCode,
// InternalPackageNumber = item.BagNo,
// SetCount = (int)item.SETQty,
// EligiblePcsCount = (int)item.StockPcsQty
// };
// inventoryInfos.Add(mesInventoryInfo);
// }
// MesProductOutBound mesProductOutBound = new MesProductOutBound()
// {
// TaskNo = mesRworkOutboundOrder.TaskNo,
// ProductCode = mesRworkOutboundOrder.ProductCode,
// ProductVersion = mesRworkOutboundOrder.ProductVersion,
// DateCode = mesRworkOutboundOrder.DateCode,
// SaleOrder = mesRworkOutboundOrder.SaleOrder,
// InventoryInfo = inventoryInfos
// };
// return mesProductOutBound;
//}
#endregion
}
}