UpstreamSendWorkOrderMethod.cs 15.2 KB
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Request;
using Hh.Mes.POJO.Response;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Hh.Mes.Service.ApiService
{
	public partial class UpstreamService
	{
		/// <summary>
		/// 订单下发接受数据处理Head 校验
		/// </summary>
		public ResponseUpstream<string> HandleAcceptOrderHeadBefore()
		{
			if (MomEntity == null)
			{
				ResponseOrder.ResponseError("【上位系统】生产订单下发:工单数据为空,请检查!");
				return ResponseOrder;
			}

			#region   plmeid 判断逻辑
			if (MomEntity.type == EnumAction.I.ToString())
			{
				if (Context.Queryable<bus_workOrder_head>().Any(t => t.plmeId == MomEntity.plmeid && t.isDelete == AddOrUpdateFlag))
				{
					ResponseOrder.ResponseError($"【上位系统】生产订单【plmeId、{MomEntity.plmeid}】【订单类型:{proOrderTypeName}】信息已存在,请勿重复添加!");
					return ResponseOrder;
				}
			} //更新
			else if (MomEntity.type == EnumAction.U.ToString())
			{
				busWkHead = Context.Queryable<bus_workOrder_head>().First(t => t.plmeId == MomEntity.plmeid && t.isDelete == AddOrUpdateFlag);
				if (busWkHead == null)
				{
					ResponseOrder.ResponseError($"【上位系统】生产订单【plmeId、{MomEntity.plmeid}】【订单类型:{proOrderTypeName}】信息更新,订单不存在或已删除!");
					return ResponseOrder;
				}
				else if (busWkHead.state != (int)EnumOrderHeadStatus.初始化)
				{
					ResponseOrder.ResponseError($"【上位系统】生产订单【plmeId、{MomEntity.plmeid}】【订单类型:{proOrderTypeName}】信息更新,只能更新“初始化”状态的订单!");
					return ResponseOrder;
				}
			}
			#endregion

			#region 空值检查

			if (string.IsNullOrWhiteSpace(MomEntity.pro_order_code))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:订单号【pro_order_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
				return ResponseOrder;
			}
			if (string.IsNullOrWhiteSpace(MomEntity.plan_code))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:生产计划号【plan_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
				return ResponseOrder;
			}
			if (string.IsNullOrWhiteSpace(MomEntity.product_code))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:产品编码【product_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
				return ResponseOrder;
			}
			if (string.IsNullOrEmpty(MomEntity.factory_code))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:工厂编码【factory_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
				return ResponseOrder;
			}
			if (string.IsNullOrEmpty(MomEntity.line_code))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:产线编码【line_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
				return ResponseOrder;
			}
			#endregion

			#region 生产订单的产品号要先存在工艺路线的产品表中
			Sys_ProductHeader = base.Context.Queryable<base_product_header>().First(x => x.productCode == MomEntity.product_code && x.isDelete == AddOrUpdateFlag);
			if (Sys_ProductHeader == null)
			{
				ResponseOrder.ResponseError($"【上位系统】工艺路线产品表【产品编码productCode、isDelete】[{MomEntity.product_code}、{AddOrUpdateFlag}] 【订单类型:{proOrderTypeName}】不存在,请【上位系统】先发送工艺线路基础数据!");
				return ResponseOrder;
			}
			#endregion

			
			#region 生产订单的产品号要先存在工艺路线的产品表中
			var  isExtWkOrder = base.Context.Queryable<bus_workOrder_head>().Any(x => x.planCode == MomEntity.plan_code);
			if (isExtWkOrder)
			{
				ResponseOrder.ResponseError($"【上位系统】【订单类型:{proOrderTypeName}】工序任务表已相同的计划号【产品编码planCode】[{MomEntity.plan_code}],请【上位系统】检查核实!");
				return ResponseOrder;
			}
			#endregion

			#region 线体映射
			var line = base.Context.Queryable<base_line>().First(x => x.otherCode == MomEntity.line_code);
			if (line == null || string.IsNullOrEmpty(line.lineCode))
			{
				ResponseOrder.ResponseError($"订单下发:没有查询到线体【line_code】和华恒线体一致的数据,请通知管理员在【工厂模型->线体设置】维护外部编码!");
				return ResponseOrder;
			}
			Sys_LineCode = line.lineCode;
			#endregion

			#region 生产订单查询工艺路线基础信息
			var pp = base.Context.Queryable<base_process_product_rel>().First(x => x.productCode == MomEntity.product_code);
			if (pp == null || string.IsNullOrEmpty(pp.lineCode))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:没有查询到工艺产品关系productCode【{MomEntity.product_code}】【订单类型:{proOrderTypeName}】对应的数据,请先发送工艺路线接口数据!");
				return ResponseOrder;
			}
			Sys_ProcessHead = base.Context.Queryable<base_process_route_head>().First(x => x.keys == pp.processHeadKeys && x.edition == MomEntity.edition);
			if (Sys_ProcessHead == null || string.IsNullOrEmpty(Sys_ProcessHead.lineCode))
			{
				ResponseOrder.ResponseError($"【上位系统】订单下发:没有查询到工艺路线lineCode【{Sys_LineCode}】【processCode】【{MomEntity.product_code}】版本【{MomEntity.edition}】对应的工艺路线,请先发送工艺路线接口数据!");
				return ResponseOrder;
			}
			Sys_ProcessDetail = Context.Queryable<base_process_route_detail>().Where(x => x.headkeys == Sys_ProcessHead.keys && x.isDelete == AddOrUpdateFlag).OrderBy(t => t.oprSequence).ToList();
			if (Sys_ProcessDetail.Count == 0)
			{
				ResponseOrder.ResponseError($"订单下发:没有查询到工艺路线明细【headkeys】【{Sys_ProcessHead.keys}】【订单类型:{proOrderTypeName}】,反复出现请联系管理员!!");
				return ResponseOrder;
			}
			#endregion

			return ResponseOrder;
		}

		/// <summary>
		/// 订单下发接受数据处理Body 校验
		/// </summary>
		public ResponseUpstream<string> HandleAcceptOrderBodyBefore()
		{
			#region 切割、组队、焊接工序是否为空
			Sys_process_cut = GetNowBaseprocessRouteDetail(Sys_ProcessHead.keys, EnumoprSequenceCode.切割坡口);
			if (Sys_process_cut == null)
			{
				ResponseOrder.ResponseError($"【MOM】订单下发:工艺路线【{Sys_ProcessHead.keys}、{Sys_ProcessHead.processName}】【订单类型:{proOrderTypeName}】不存在切割工序【{EnumoprSequenceCode.切割坡口},请核实该产品【{MomEntity.product_code}】的工艺路线工序!");
				return ResponseOrder;
			}
			Sys_process_fitUp = GetNowBaseprocessRouteDetail(Sys_ProcessHead.keys, EnumoprSequenceCode.组对);
			if (Sys_process_fitUp == null)
			{
				ResponseOrder.ResponseError($"【MOM】订单下发:工艺路线【{Sys_ProcessHead.keys}、{Sys_ProcessHead.processName}】【订单类型:{proOrderTypeName}】不存在组队工序【{EnumoprSequenceCode.组对},请核实该产品【{MomEntity.product_code}】的工艺路线工序!");
				return ResponseOrder;
			}
			Sys_process_weld = GetNowBaseprocessRouteDetail(Sys_ProcessHead.keys, EnumoprSequenceCode.焊接);
			if (Sys_process_weld == null)
			{
				ResponseOrder.ResponseError($"【MOM】订单下发:工艺路线【{Sys_ProcessHead.keys}、{Sys_ProcessHead.processName}】【订单类型:{proOrderTypeName}】不存在焊接工序【{EnumoprSequenceCode.焊接},请核实该产品【{MomEntity.product_code}】的工艺路线工序!");
				return ResponseOrder;
			}
			#endregion

			#region 生产订单工序是否在工艺路线中、 切割工序物料不能为空、是否存在物料表信息
			if (MomEntity.details.Count < Sys_ProcessDetail.Count)
			{
				ResponseOrder.ResponseError($"【MOM】订单下发:【订单类型:{proOrderTypeName}】 json数据details明细行数【{MomEntity.details.Count}】小于【工艺路线keys:{Sys_ProcessDetail[0].headkeys}】行数【{Sys_ProcessDetail.Count}】不一致,请核实订单计划数据明细数据!");
				return ResponseOrder;
			}
			foreach (var item in MomEntity.details)
			{
				var isOk = Sys_ProcessDetail.Any(x => x.oprSequenceCode == item.proced_code);
				if (!isOk)
				{
					ResponseOrder.ResponseError($"【MOM】订单下发:子表明细工序编码proced_code【{item.proced_code}】【订单类型:{proOrderTypeName}】不存在工艺路线工序集合中,请核实订单计划数据明细工序编码数据!");
					return ResponseOrder;
				}

				if (item.proced_code == Sys_process_cut.oprSequenceCode)
				{
					foreach (var itemComponent in item.componentDetails)
					{
						if (string.IsNullOrEmpty(itemComponent.cut_mater_code))
						{
							ResponseOrder.ResponseError($"【上位系统】订单下发-切割工序:物料编码【cut_mater_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
							return ResponseOrder;
						}
						else
						{
							var isExt = base.Context.Queryable<base_material>().Any(x => x.materialCode == itemComponent.cut_mater_code && x.isDelete == AddOrUpdateFlag);
							if (isExt) continue;
							ResponseOrder.ResponseError($"【上位系统】订单下发-切割工序:没有查询到物料主数据cut_mater_code【{itemComponent.cut_mater_code}】【订单类型:{proOrderTypeName}】对应的信息,请先发送物料主数据接口数据,或者在物料管理模块点击按钮【同步信科物料 】!");
							return ResponseOrder;
						}
					}
				}
				else if (item.proced_code == Sys_process_weld.oprSequenceCode)
				{
					foreach (var itemComponent in item.componentDetails)
					{
						if (string.IsNullOrEmpty(itemComponent.weld_mater_code))
						{
							ResponseOrder.ResponseError($"【上位系统】订单下发-焊接工序:焊口物料编码【weld_mater_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
							return ResponseOrder;
						}
						else if (string.IsNullOrEmpty(itemComponent.cut_mater_code))
						{
							ResponseOrder.ResponseError($"【上位系统】订单下发-焊接工序:管段物料编码【cut_mater_code】【订单类型:{proOrderTypeName}】不能为空值,请检查!");
							return ResponseOrder;
						}
						else
						{
							var weldMaterCodeList = itemComponent.weld_mater_code.Split(',', StringSplitOptions.RemoveEmptyEntries);
							for (int xp = 0; xp < weldMaterCodeList.Length; xp++)
							{
								var tempMaterCode = weldMaterCodeList[xp];
								var isExt = base.Context.Queryable<base_material>().Any(x => x.materialCode == tempMaterCode && x.isDelete == AddOrUpdateFlag);
								if (isExt) continue;
								ResponseOrder.ResponseError($"【上位系统】订单下发-焊接工序:没有查询到物料主数据weld_mater_code【{tempMaterCode}】【订单类型:{proOrderTypeName}】对应的信息,请先发送物料主数据接口数据,或者在物料管理模块点击按钮【同步信科物料 】!");
								return ResponseOrder;
							}
						}
					}

				}
			}
			#endregion

			return ResponseOrder;
		}



		/// <summary>
		/// 生成数据订单数据 除 正常订单之外
		/// </summary>
		private ValueTuple<bus_workOrder_head, List<bus_workOrder_detail>> HandleAcceptOhterOrderData()
		{
			#region  生成工序任务头 bus_workOrder_head
			var busWorkOrderHead = new bus_workOrder_head
			{
				keys = Guid.NewGuid(),
				plmeId = MomEntity.plmeid,
				workOrderCode = MomEntity.pro_order_code,
				workOrderName = MomEntity.pro_order_code + "_订单名称",
				planCode = MomEntity.plan_code,
				processHeadKeys = Sys_ProcessHead.keys,//工艺路线keys
				processCode = Sys_ProductHeader.processCode,        //工艺路线编码
				productHeaderCode = MomEntity.product_code,     //产品编码
				productName = MomEntity.product_name,

				factoryCode = MomEntity.factory_code,
				lineCode = Sys_LineCode,
				orderType = MomEntity.order_type,
				edition = MomEntity.edition,

				projectCode = MomEntity.project_code,   //项目编码
				projectName = MomEntity.project_name,
				lineNo = MomEntity.line_no,               //管线号/图纸号

				planStartTime = MomEntity.oprplan_start_time,
				planEndTime = MomEntity.oprplan_end_time,

				extend1 = MomEntity.extend1,
				extend2 = MomEntity.extend2,
				extend3 = MomEntity.extend3,
				extend4 = MomEntity.extend4,

				otherOrderCode = MomEntity.plan_code,
				state = (int)EnumOrderHeadStatus.初始化,
				isDelete = AddOrUpdateFlag,
				createTime = DateTime.Now,
				createBy = SystemVariable.DefaultCreated
			};
			#endregion

			#region  生成工序任务明细 busWorkOrderDetailList
			var busWorkOrderDetailList = new List<bus_workOrder_detail>();
			foreach (var item in Sys_ProcessDetail)
			{
				var jsonMomItem = MomEntity.details.FirstOrDefault(x => x.proced_code == item.oprSequenceCode);
				if (jsonMomItem == null)
				{
				    ResponseOrder.ResponseError($"【上位系统】订单下发:【订单类型:{proOrderTypeName}】【工序{item.oprSequenceCode}】在【工艺路线:{Sys_ProcessDetail[0].headkeys}】没有找到行记录,请确认【产品:{MomEntity.product_code}】关联的工艺路线是否正确!!");
					break;
				}
				for (int i = 0; i < jsonMomItem.componentDetails.Count; i++)
				{
					var nowWorkDetail = new bus_workOrder_detail
					{
						headKeys = busWorkOrderHead.keys,
						productHeaderCode = busWorkOrderHead.productHeaderCode,
						workOrderCode = busWorkOrderHead.workOrderCode,

						cutMaterCode = jsonMomItem.componentDetails[i].cut_mater_code,
						cuttingLength = (decimal)jsonMomItem.componentDetails[i].cutting_len,

						lineCode = Sys_LineCode,
						designNo = jsonMomItem.componentDetails[i].design_no,
						designUrl = jsonMomItem.componentDetails[i].design_url,
						partCode = jsonMomItem.componentDetails[i].part_code,

						serialNumber = item.oprSequence,
						oprSequenceCode = item.oprSequenceCode,
						oprSequenceName = item.oprSequenceName,
						workCenterCode = item.workCenterCode,
						planStartTime = jsonMomItem.oprplan_start_time,
						planEndTime = jsonMomItem.oprplan_end_time,

						weldType= jsonMomItem.componentDetails[i].weld_type,
                        weldNo = jsonMomItem.componentDetails[i].weld_no,
                        weldMaterCode = jsonMomItem.componentDetails[i].weld_mater_code,
						


                        isEndProduct = jsonMomItem.isend_product,
						isDelete = AddOrUpdateFlag,
						state = (int)EnumOrderBodyStatus.初始化,
						workReportStatus = (int)EnumWorkReportStatus.未报工,
						createTime = DateTime.Now,
						createBy = SystemVariable.DefaultCreated
					};
					nowWorkDetail.bodyKeys = Guid.NewGuid();
					nowWorkDetail.serialNumberName = nowWorkDetail.serialNumber + "_1";
					busWorkOrderDetailList.Add(nowWorkDetail);
				}
			}

			#endregion

			// 设置工序任务头的当前工序,为第一个工序任务明细的工序
			if (busWorkOrderDetailList.Count > 0)
			{
				busWorkOrderHead.nowOprSequenceCode = busWorkOrderDetailList[0].oprSequenceCode;
			}
			return (busWorkOrderHead, busWorkOrderDetailList);
		}
	}
}