/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-10-17
 * 描述            ：平整设计主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Tpm.Design.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import market.Api.Lms.impl.LmsApiServiceImpl;
import market.Api.Mes.impl.DesignInfo;
import market.Api.Mes.impl.MesApiDesignInfoImpl;
import market.Api.Mes.impl.MesApiServiceImpl;
import market.Api.Mes.impl.ProcessPoint;
import market.Api.Mes.impl.Utils;
import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.impl.YdmProductDetailBfServiceImpl;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Lengthwise.Plan.mapper.PlnMatReplaceLogMapper;
import market.Mes.Lengthwise.Plan.mapper.PlnMaterielDetailMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMatReplaceLog;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.Opr.mapper.MesOprLogMapper;
import market.Mes.Opr.vo.MesOprLog;
import market.Mes.OrderManager.Plan.mapper.PlnOrderStatisticMapper;
import market.Mes.OrderManager.Plan.mapper.PlnProdorderMapper;
import market.Mes.OrderManager.Plan.mapper.PlnProdorderStatisticMapper;
import market.Mes.OrderManager.Plan.vo.PlnOrderStatistic;
import market.Mes.OrderManager.Plan.vo.PlnProdorder;
import market.Mes.OrderManager.Plan.vo.PlnProdorderStatistic;
import market.Mes.Tpm.Design.service.PlnSkinrollingDsnMainService;
import market.Mes.Tpm.Design.vo.PlnSkinrollingDsnMain;
import market.Slm.FundManagement.service.SlmFundFirstDetailService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="PlnSkinrollingDsnMainService")
public class PlnSkinrollingDsnMainServiceImpl extends BaseServiceImpl<PlnSkinrollingDsnMain,Object> implements PlnSkinrollingDsnMainService {

	/**
	 * 平整挂单
	 * memo1  生产订单号
	 * memo2 1 强制充当
	 * @throws Exception 
	 * [{"memo1":"1804130000001","object":["Y1831023"]}]  挂单
	 * [{"memo1":"1804110000002","object":["Y1830550","Y1830551"]}]  挂单
	 * [{"memo1":"1804110000002","object":["Y1830550","Y1830551"],"memo2":"1"}]  强制挂单
	 * 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRelationOrder(CommonPage<String[]> oci) throws Exception{
		ResultModel rm=new ResultModel();
		Date createTime = new Date();
		String temp = "";
		String[] path = null;
		ArrayList<String> validpath = new ArrayList<>();
		
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
	    String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //操作时间
		
		// 实物卷号数组
		String[] sCoilNo=oci.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		if(oci.getMemo1()==null||"".equals(oci.getMemo1().trim())||sCoilNo==null||sCoilNo.length==0){
			rm.setState("500");
			rm.setMsgInfo("条件传入错误!");
			return rm;
		}
		
		PlnProdorder pp=new PlnProdorder();
		pp.setProd_order_no(oci.getMemo1().trim());
		List<PlnProdorder> lpp=this.getMapper(PlnProdorderMapper.class).find(pp, 0, 1);
		if(lpp==null||lpp.size()==0){
			rm.setState("500");
			rm.setMsgInfo("生产订单["+pp.getProd_order_no()+"]不存在!");
			return rm;
		}
		
		// 验证生产订单状态是否为订单保留：20230908刘侣
		if ("0PAA60".equals(lpp.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+lpp.get(0).getProd_order_no()+"]为 ‘订单保留’ 状态，不允许挂单");
			return rm;
		}
		if ("0PAA70".equals(lpp.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+lpp.get(0).getProd_order_no()+"]为 ‘订单作废’ 状态，不允许挂单");
			return rm;
		}
		
		// 验证生产订单是否锁定
		boolean flag_l = this.getBean(MesApiDesignInfoImpl.class).pOrdIsLocked(oci.getMemo1().trim());
		if (flag_l) {
			throw new Exception("生产订单已经锁定[订单变更中],不允许挂单!");
		}
		
		// 合同是否是厂内合同
		boolean flag_c = this.getBean(MesApiDesignInfoImpl.class).getContractType(oci.getMemo1().trim());
			
		// 合同是都是来料加工合同
		boolean flag_j = this.getBean(MesApiDesignInfoImpl.class).getCont_Type(oci.getMemo1().trim());
				
		//物料充当脱单履历
		List<PlnMatReplaceLog> lmmrl=this.getMapper(PlnMatReplaceLogMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
		long iNo=1;
		if(lmmrl!=null&&lmmrl.size()>0){
			iNo=lmmrl.get(0).getSerial_no()+1;
		}
		
		// 强制挂单
		String pd="";
		if("1".equals(oci.getMemo2())){
			pd="1";
		}
		
		// 判断订单是否是净重交货 (true 不走虚拟发运,不校验第一次挂单信息)
		boolean flag = this.getBean(MesApiServiceImpl.class).judgeOrderWeightType(oci.getMemo1());
		if(!"1".equals(lpp.get(0).getOrder_nature()) && !"3".equals(lpp.get(0).getOrder_nature())){
			//处理逻辑1
			if("0PAA12".compareTo(lpp.get(0).getStatus())>0){
				rm.setState("500");
				rm.setMsgInfo("[需加工]的订单，要下发生产后才能挂单！");
				return rm;
			}
			
			for(String CoilNo:sCoilNo){
				List<DesignInfo> infos = new ArrayList<>();
				//处理逻辑
				//对传入的实物卷，查询库存信息[物流接口]
				//存在信息，则读取之； 若不存在信息，则报错“库存中无实物[xxx]信息”
				CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
				YdmProductDetail ypd=new YdmProductDetail();
				ypd.setCoilno(CoilNo);
				cp.setObject(ypd);
				ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doFindCoilno(cp);
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				
				// 挂单后判断该卷是否已经iscanload：是 就置为null，让其重新判断iscanload；20230210刘侣
				YdmProductDetail ypdCanLoad = new YdmProductDetail();
				ypdCanLoad.setCoilno(CoilNo);
				ypdCanLoad.setIs_can_load("1");
				List<YdmProductDetail> ypdCanList = this.getMapper(YdmProductDetailMapper.class).find(ypdCanLoad, 0, 0);
				if (ypdCanList != null && ypdCanList.size() > 0) {
					ypdCanList.get(0).setIs_can_load("0");
					this.getMapper(YdmProductDetailMapper.class).update(ypdCanList.get(0));
					this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypdCanList.get(0).getCoilno());
				}
				// end
				
				// 2018-06-15 限制重复挂单加此操作
				@SuppressWarnings("unchecked")
				List<YdmProductDetail> lypd = (List<YdmProductDetail>) rmValid.getData();
				for (YdmProductDetail ydmp : lypd) {
					if (ydmp.getOrderno() != null) {
						rm.setState("500");
						rm.setMsgInfo("实物["+cp.getObject().getCoilno()+"]已绑定订单["+ydmp.getOrderno()+"]! 不能再次操作!");
						return rm;
					}else if(!flag_c && !flag_j){
						PlnMaterielDetail pmd = new PlnMaterielDetail();
						pmd.setObject_no(ydmp.getCoilno());
						List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmd, "nvl(invalid_flg,0) = 0", "", 0, 0);
						if (pmdd != null && pmdd.size()>0) {
							for (PlnMaterielDetail plnmaterieldetail : pmdd) {
								plnmaterieldetail.setObject_no("");
								this.getMapper(PlnMaterielDetailMapper.class).update(plnmaterieldetail);
							}
						}
					}
					if (!"1".equals(pd)) {
						// 如果物料是成品校验综合判定信息
						/*if ("500203".equals(ydmp.getStorageattr()) && !"1".equals(ydmp.getIsbl()) &&  !"40740801".equals(ydmp.getJudgeresult())){
							throw new Exception("成品挂单之前需要综合判定合格!");
						}*/
//						19-10-16改
						if ("500203".equals(ydmp.getStorageattr()) && !"1".equals(ydmp.getIsbl())&&ydmp.getJudgeresult()==null){
							throw new Exception("成品挂单之前需要综合判定!");
						}
					}
				}
				if(!lpp.get(0).getSteel_code().equals(lypd.get(0).getJudgesteelcode())){
					throw new Exception("牌号不匹配，不能挂单！");
				}
				
				if(!"1".equals(pd)){
					// 拆分订单不验证合同号
					if (!(lpp.get(0).getIs_split() != null && "1".equals(lpp.get(0).getIs_split()))) {
						if (!flag && !flag_c && !flag_j) {
							// 判断第一次挂单与第二次挂单合同号contract_no是否一致 2018-09-05 通过挂单日志表获取最后一条脱单的订单信息(原料卷订单属性为新钢最后一个)
							List<PlnProdorder> plnlog = this.getMapper(PlnMatReplaceLogMapper.class).findreplacelog(CoilNo);
							if (Utils.isNotEmpty(plnlog)) {
								if (!lpp.get(0).getContract_no().equals(plnlog.get(0).getContract_no())) {
									// 去掉这个限制lanyushan：20231113刘侣
//									throw new Exception("合同号不匹配，不能上料! 合同号 : [" + plnlog.get(0).getContract_no() + ","
//											+ lpp.get(0).getContract_no() + "]");
								}
							} else {
								// 黄涛操作和蓝雨珊不一样，所以先把这个注释掉：黄涛需求 2023011301
//								throw new Exception("未找到[" + CoilNo + "]对应原料卷订单挂单信息!");
							}
						}
					}
					if(lpp.get(0).getThick()==null){
						throw new Exception("未找到生产订单厚度，不能挂单！");
					}
					if(lypd.get(0).getThick()==null){
						throw new Exception("未找到实物厚度，不能挂单！");
					}
					// 厚度要考虑公差范围
					ResultModel rmInfo=this.getBean(MesApiServiceImpl.class).CompareValues(lpp.get(0).getThick().doubleValue(), lypd.get(0).getThick().doubleValue());
					if(!"200".equals(rmInfo.getState())){
						throw new Exception(rmInfo.getMsgInfo());
					}
					
					//判断实物，和订单原料卷的宽，如果实物比订单值小，则报错退出
					if(lypd.get(0).getWidth().doubleValue()<lpp.get(0).getWidth().doubleValue()){
						throw new Exception("实物卷的宽比订单要求值小，不能挂单！");
					}
					
					// 判断客户信息是否一致-----------------2018-07-04-----------------------------
					if (!flag && lypd.get(0).getCustomer_no() != null && lpp.get(0).getCustomer_no() != null
							&& !lypd.get(0).getCustomer_no().equals(lpp.get(0).getCustomer_no())) {
						throw new Exception("库存中实物卷的客户信息与对应生产订单的客户信息不一致! 可选强制挂单! \r\n "
										    + "卷号:["+CoilNo+"] 客户名称:["+lypd.get(0).getCustomer_nm()+"] \r\n "
										    + "生产订单号 :["+pp.getProd_order_no()+"] 客户名称 :["+ lpp.get(0).getCustomer_name()+"]");
					}
					
				}
				
				// 暂无机器限制 ! ! !
				// ---------------------------------------------------------读取基础配置 产线机组限制表  start ------------------------------------------
//				ComDsnPltlmt cdp=new ComDsnPltlmt();
//				cdp.setPlt("Y1");//工厂
//				cdp.setPlt_line("Y1");//产线
//				List<ComDsnPltlmt> lc=this.getMapper(ComDsnPltlmtMapper.class).find(cdp, 0, 0);
//				if(lc==null||lc.size()==0){
//					throw new Exception("未找到产线");
//				}
//				// 获取所有的限制条件
//				SrRestrict MachineRestrict=new SrRestrict(lc);
				
				//根据加工计划号  读取生产订单对应的生产加工表[PLN_PROCESS_PLAN]
//				PlnProcessPlan ppp=new PlnProcessPlan();
//				ppp.setProduce_no(lpp.get(0).getProduce_no());
//				ppp.setValid_flag("1");
//				List<PlnProcessPlan> lppp=this.getMapper(PlnProcessPlanMapper.class).findWithCondition(ppp, null, " cut_order_no ", 0, 0);
//				if(lppp==null||lppp.size()==0){
//					throw new Exception("未找到生产订单["+ppp.getOrder_no()+"]对应的生产加工信息");
//				}
//				
//				ResultModel thick_result=this.getBean(PbModelDbServiceImpl.class).findPlnThickRangeCmbDb();
//				if(!"200".equals(thick_result.getState())){
//					throw new Exception("获取厚度公差范围失败！");
//				}
//				List<PbModelDb> lpmdd=(List<PbModelDb>) thick_result.getData();
//				if(Utils.isEmpty(lpmdd)){
//					throw new Exception("请先配置厚度公差范围！");
//				}
				
				// 强制挂单免检
//				if(!"1".equals(pd)){
					//----------------------------------获取配置的值比较公差范围----------------------------
//					for(PbModelDb pmd:lpmdd){
//						if(pmd.getId().equals("200101")){//范围
//							if(lypd.get(0).getThick().doubleValue()-MachineRestrict.lim_max_height>(Double.parseDouble(pmd.getRegionkey()))){
//								throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的厚度超出最大公差设备限制范围内，不能挂单！");
//							}
//						}
//						
//						if(pmd.getId().equals("200102")){
//							if(lypd.get(0).getThick().doubleValue()-MachineRestrict.lim_min_height<(Double.parseDouble(pmd.getRegionkey()))){
//								throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的厚度小于最小公差设备限制范围内，不能挂单！");
//							}
//						}
//					}
					//-----------------------------------------------------------------------------------
					
//					if(lypd.get(0).getWidth().doubleValue()>MachineRestrict.lim_max_wdt
//							||lypd.get(0).getWidth().doubleValue()<MachineRestrict.lim_min_wdt){
//						throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的宽度不在设备限制范围内，不能挂单！");
//					}
//					if(lypd.get(0).getFactweight().doubleValue()>MachineRestrict.lim_max_wgt
//							|| lypd.get(0).getFactweight().doubleValue()<MachineRestrict.lim_min_wgt){
//						throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的重量不在设备限制范围内，不能挂单！");
//					}
//					//对于母卷S1的 其他规格信息(内径、外径)， 如果无值，则继续；  如果有值，则判断是否在设备原料的限制范围内
//					if(lypd.get(0).getDiameter_inn()!=null&&lypd.get(0).getDiameter_inn().intValue()!=0){
//						if(lypd.get(0).getDiameter_inn().doubleValue()>MachineRestrict.lim_max_inner
//								||lypd.get(0).getDiameter_inn().doubleValue()<MachineRestrict.lim_min_inner){
//							throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的内径不在设备限制范围内，不能挂单！");
//						}
//					}
//					if(lypd.get(0).getDiameter_out()!=null&&lypd.get(0).getDiameter_out().intValue()!=0){
//						if(lypd.get(0).getDiameter_out().doubleValue()>MachineRestrict.lim_max_outter
//								||lypd.get(0).getDiameter_out().doubleValue()<MachineRestrict.lim_min_outter){
//							throw new Exception("母卷["+lypd.get(0).getCoilno()+"]的外径不在设备限制范围内，不能挂单！");
//						}
//					}
					
//					for(PlnProcessPlan pppc:lppp){
//						//对于子卷S1的 厚、宽、重量信息，判断其是否在设备原料的限制范围内
//						if(!"1".equals(pd)){
//							//-------------------获取配置的值比较公差范围--------------------
//							for(PbModelDb pmd:lpmdd){
//								if(pmd.getId().equals("200101")){//范围
//									if(pppc.getMat_thick()-MachineRestrict.lim_max_height>(Double.parseDouble(pmd.getRegionkey()))){
//										throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷厚度超出最大公差设备限制范围内，不能分条！");
//									}
//								}
//								
//								if(pmd.getId().equals("200102")){
//									if(pppc.getMat_thick()-MachineRestrict.lim_min_height<(Double.parseDouble(pmd.getRegionkey()))){
//										throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷厚度小于最小公差设备限制范围内，不能分条！");
//									}
//								}
//							}
//							//--------------------------------------------------------------
//							
//							if(pppc.getMat_width()>MachineRestrict.lim_max_wdt
//									||pppc.getMat_width()<MachineRestrict.lim_min_wdt){
//								throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷的宽度不在设备限制范围内，不能分条！");
//							}
//							/*if(pppc.getEstimate_weight()>MachineRestrict.lim_max_wgt){
//								throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷的重量不在设备限制范围内，不能分条！");
//							}*/
//							//对于子卷S1的 其他规格信息(内径、外径)， 如果无值，则继续；  如果有值，则判断是否在设备原料的限制范围内
//							if(pppc.getMat_diameter_inn()!=null){
//								if(pppc.getMat_diameter_inn()>MachineRestrict.lim_max_inner
//										||pppc.getMat_diameter_inn()<MachineRestrict.lim_min_inner){
//									throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷的内径不在设备限制范围内，不能分条！");
//								}
//							}
//							if(pppc.getMat_diameter_out()!=null){
//								if(pppc.getMat_diameter_out()>MachineRestrict.lim_max_outter
//										||pppc.getMat_diameter_out()<MachineRestrict.lim_min_outter){
//									throw new Exception("加工计划["+pppc.getProduce_no()+"]的子卷的外径不在设备限制范围内，不能分条！");
//								}
//							}
//						}
//					}
//				}
				// ---------------------------------------------------------读取基础配置 产线机组限制表  end ------------------------------------------
				// 工序点信息
				List<ComBaseInfo> infoI = this.getMapper(ComBaseInfoMapper.class)
						.findWithCondition(null, "sort_code = '2002' AND validflag = '1'",
								null, 0, 0);
				ProcessPoint processpoint = new ProcessPoint(infoI);
				
				// 生成设计信息
				if (Utils.isNotEmpty(lpp.get(0).getWhole_process_codes()) && !"1".equals(lpp.get(0).getWhole_coil())) {
					// 1、获取有效工艺路径
					temp = processpoint.smooth_code + StringUtils.substringAfter(lpp.get(0).getWhole_process_codes(), processpoint.smooth_code);
					path = temp.split("-");
					validpath = this.getBean(MesApiDesignInfoImpl.class).getProcessPath(path);
					// 调用设计信息接口
					DesignInfo info = new DesignInfo();
					// 如果实物对应的虚拟物料信息存在,即传入虚拟物料号(厂内合同成品再加工)
					List<PlnMaterielDetail> pmd_c = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no = '"+CoilNo+"' and nvl(invalid_flg,0) = 0", "", 0, 0);
					if (Utils.isNotEmpty(pmd_c)) {
						info.setObject_no_vt(pmd_c.get(0).getObject_no_vt());
					}
					info.setCurrentProcIndex(0);
					info.setObject_no(CoilNo);
					info.setProcess(validpath);
					info.setProd_order_no(oci.getMemo1());
					info.setSingle_wgt(lypd.get(0).getFactweight());
					infos.add(info);
					ResultModel design = this.getBean(MesApiDesignInfoImpl.class).Design_Plan_Info(infos,null);
					if (!"200".equals(design.getState())) {
						throw new Exception(design.getMsgInfo());
					}
				}
				
				// 调用 【函数_通过加工计划产生设计】sheet的函数，生成主设计及拼料信息。（传入 生产订单号、操作人）(传出:平整设计号)
//				PlnAnnealDsnMain pld=this.getBean(MesApiAnnealPlanImpl.class).ProcessingPlanDsn(lpp.get(0).getProd_order_no());
				
				// 反写母卷虚拟物料机台产线
//				PlnMaterielDetail pmdM=new PlnMaterielDetail();
//				pmdM.setObject_no(CoilNo);
//				if (Utils.isEmpty(oci.getMemo3())) {
//					oci.setMemo3("M0007") ;
//					pmdM.setPline_code("C011");
//				}
//				pmdM.setMachine_code(oci.getMemo3()); // 挂单的时候确认机台产线
//				this.getMapper(PlnMaterielDetailMapper.class).updateMachine_Code(pmdM);
				
				if ("1".equals(lpp.get(0).getWhole_coil())) {
					
					//保存相关数据
					//修改生产订单进程表[PLN_PRODORDER_STATISTIC]
					PlnProdorderStatistic pps=new PlnProdorderStatistic();
					pps.setProd_order_no(lpp.get(0).getProd_order_no());
					pps.setReplaced(lypd.get(0).getFactweight());
					pps.setReplaced_cnt(1);
					pps.setReplaced_times(1);
					pps.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
					this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
					
					
					//修改订单进程表[PLN_ORDER_STATISTIC]
					PlnOrderStatistic pos=new PlnOrderStatistic();
					pos.setOrder_no(lpp.get(0).getOrder_no());
					pos.setReplaced(lypd.get(0).getFactweight());
					pos.setReplaced_cnt(1);
					pos.setReplaced_times(1);
					pos.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
					this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);
					
					//调用物流接口，关联实物和生产订单信息
					ypd.setProdorderno(lpp.get(0).getProd_order_no());
					ypd.setOrderno(lpp.get(0).getOrder_no());
					ypd.setCustomer_no(lpp.get(0).getCustomer_no());
					ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
					ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
					ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
					ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
					ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
					this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
					
					
					//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
					PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
					mmrl.setSerial_no(iNo++);
					mmrl.setObject_no(lypd.get(0).getCoilno());
					mmrl.setOpt_type("2");
					mmrl.setOrder_no(lpp.get(0).getOrder_no());
					mmrl.setProd_order_no(lpp.get(0).getProd_order_no());
					mmrl.setCreate_man_name(userName);
					mmrl.setCreate_time(new Date());
					mmrl.setRemark("整卷发运");
					this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
				}else {
					
					// 修改生产订单进程表[PLN_PRODORDER_STATISTIC] 订单进程表
					this.getBean(MesApiDesignInfoImpl.class).updateOrderStatic(lpp.get(0).getProd_order_no(), lpp.get(0).getOrder_no(), lpp.get(0).getWhole_process_codes(), lypd.get(0).getFactweight(),true);

					// 查询虚拟物料表【PLN_MATERIEL_DETAIL】---- where NVL(INVALID_FLG,0)=0 AND OBJECT_NO = 实物卷号
					List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, " NVL(INVALID_FLG,0)=0 AND OBJECT_NO = '"+CoilNo+"'", "", 0, 0);
					
					//调用物流接口，关联实物和生产订单信息
					ypd.setCustomer_no(lpp.get(0).getCustomer_no());
					ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
					ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
					ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
					ypd.setProdorderno(lpp.get(0).getProd_order_no());
					ypd.setOrderno(lpp.get(0).getOrder_no());
					ypd.setObject_no_vt(pmdd.get(0).getObject_no_vt());
					ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
					ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
					this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
					
					
					//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
					PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
					BeanUtils.copyProperties(pmdd.get(0), mmrl);
					mmrl.setSerial_no(iNo++);
					mmrl.setOpt_type("2");
					mmrl.setCreate_man_name(userName);
					mmrl.setCreate_time(createTime);
					mmrl.setRemark("1".equals(pd)?"强制充当":"");
					this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
					
					// 生成物料附加信息
					this.getBean(MesApiDesignInfoImpl.class).addMaterielExinfo(lpp.get(0), pmdd.get(0),processpoint.smooth_code);
					
					// 拆分订单如果还需继续生产,判断是否需要移动库存 2019-08-05
					if ((lpp.get(0).getIs_split() != null && "1".equals(lpp.get(0).getIs_split()))) {
						// 如果物料在成品库挪到在制库
						if ("500203".equals(lypd.get(0).getStorageattr())) {
	//						ypd.setStorageattr("500203");
							CommonPage<YdmProductDetail> tmp = new CommonPage<YdmProductDetail>();
							List<YdmProductDetail> ydm = new ArrayList<YdmProductDetail>();
							lypd.get(0).setMemo("拆分订单["+lpp.get(0).getOrder_no()+"]增加["+lpp.get(0).getWhole_process_names()+"]工序->移至在制库");
							ydm.add(lypd.get(0));
							tmp.setObjectList(ydm);
							tmp.setMemo1("500202");
							tmp.setMemo2("0");
							this.getBean(LmsApiServiceImpl.class).doChangeMaterialStorage(tmp);
						}
					}
				}
				
				ResultModel rejRes = this.getBean(SlmFundFirstDetailService.class).reAutoUltiJudge(CoilNo);
				if (!"200".equals(rejRes.getState())) {
					throw new Exception(rejRes.getMsgInfo());
				}
				
				this.getBean(SlmFundFirstDetailService.class).ycjsPYdd(CoilNo, true); // 一次结算后抛异动点
			}
		}else{
//			//处理逻辑2
//			if("0PAA12".compareTo(lpp.get(0).getStatus())<=0){
//				rm.setState("500");
//				rm.setMsgInfo("[原料卷/非计划]的订单，必须在下发生产前充当！");
//				return rm;
//			}
			for(String CoilNo:sCoilNo){
				//处理逻辑
				//对传入的实物卷，查询库存信息[物流接口]
				//存在信息，则读取之； 若不存在信息，则报错“库存中无实物[xxx]信息”
				CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
				YdmProductDetail ypd=new YdmProductDetail();
				ypd.setCoilno(CoilNo);
				cp.setObject(ypd);
				ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doFindCoilno(cp);
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				
				// 挂单后判断该卷是否已经iscanload：是 就置为null，让其重新判断iscanload；20230210刘侣
				YdmProductDetail ypdCanLoad = new YdmProductDetail();
				ypdCanLoad.setCoilno(CoilNo);
				ypdCanLoad.setIs_can_load("1");
				List<YdmProductDetail> ypdCanList = this.getMapper(YdmProductDetailMapper.class).find(ypdCanLoad, 0, 0);
				if (ypdCanList != null && ypdCanList.size() > 0) {
					ypdCanList.get(0).setIs_can_load("0");
					this.getMapper(YdmProductDetailMapper.class).update(ypdCanList.get(0));
					this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypdCanList.get(0).getCoilno());
				}
				// end
				
				// 2018-06-15 限制重复挂单加此操作
				@SuppressWarnings("unchecked")
				List<YdmProductDetail> lypd = (List<YdmProductDetail>) rmValid.getData();
				for (YdmProductDetail ydmp : lypd) {
					if (ydmp.getOrderno() != null) {
						rm.setState("500");
						rm.setMsgInfo("实物["+cp.getObject().getCoilno()+"]已绑定订单["+ydmp.getOrderno()+"]! 不能再次操作!");
					}else if(!flag_c && !flag_j){
						PlnMaterielDetail pmd = new PlnMaterielDetail();
						pmd.setObject_no(ydmp.getCoilno());
						List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmd, "nvl(invalid_flg,0) = 0", "", 0, 0);
						if (pmdd != null && pmdd.size()>0) {
							for (PlnMaterielDetail plnmaterieldetail : pmdd) {
								plnmaterieldetail.setObject_no("");
								this.getMapper(PlnMaterielDetailMapper.class).update(plnmaterieldetail);
							}
						}
					}
				}
			
				//判断实物，和订单原料卷的厚、牌号、交货状态是否匹配
//				List<YdmProductDetail> lypd=(List<YdmProductDetail>)rmValid.getData();
				if(lpp.get(0).getSteel_code()!=null&&
						!lpp.get(0).getSteel_code().equals(lypd.get(0).getJudgesteelcode())){
					throw new Exception("牌号不匹配，不能挂单！");
				}
			
				if(!"1".equals(pd)){
					if(lpp.get(0).getThick()!=null){
						ResultModel rmInfo=this.getBean(MesApiServiceImpl.class).CompareValues(lpp.get(0).getThick().doubleValue(), lypd.get(0).getThick().doubleValue());
						if(!"200".equals(rmInfo.getState())){
							throw new Exception(rmInfo.getMsgInfo());
						}
					}
					
					
					//判断实物，和订单原料卷的宽、重量信息，如果实物比订单值小，则报错退出
					if(lpp.get(0).getWidth()!=null&&
							lypd.get(0).getWidth().doubleValue()<lpp.get(0).getWidth().doubleValue()){
						throw new Exception("实物卷的宽比订单要求值小，不能挂单！");
					}
					
					if(lpp.get(0).getSingle_weight_max()!=null&&
							lpp.get(0).getSingle_weight_max()!=null){
						if(lypd.get(0).getFactweight().doubleValue() >lpp.get(0).getSingle_weight_max().doubleValue()){
							throw new Exception("实物卷的重量比订单要求重量上限大，不能挂单！");
						}
					}
					if(lpp.get(0).getSingle_weight_min()!=null&&
							lpp.get(0).getSingle_weight_min()!=null){
						if(lypd.get(0).getFactweight().doubleValue() <lpp.get(0).getSingle_weight_min().doubleValue()){
							throw new Exception("实物卷的重量比订单要求重量下限小，不能挂单！");
						}
					}
				}
				
				//保存相关数据
				//修改生产订单进程表[PLN_PRODORDER_STATISTIC]
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(lpp.get(0).getProd_order_no());
				pps.setReplaced(lypd.get(0).getFactweight());
				pps.setReplaced_cnt(1);
				pps.setReplaced_times(1);
				pps.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
				
				
				//修改订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(lpp.get(0).getOrder_no());
				pos.setReplaced(lypd.get(0).getFactweight());
				pos.setReplaced_cnt(1);
				pos.setReplaced_times(1);
				pos.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);
				
				
				//调用物流接口，关联实物和生产订单信息
				ypd.setProdorderno(pps.getProd_order_no());
				ypd.setOrderno(pos.getOrder_no());
				ypd.setCustomer_no(lpp.get(0).getCustomer_no());
				ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
				ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
				ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
				ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
				ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
				this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
				
				
				//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
				PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
				mmrl.setSerial_no(iNo++);
				mmrl.setObject_no(lypd.get(0).getCoilno());
				mmrl.setOpt_type("1");
				mmrl.setOrder_no(pos.getOrder_no());
				mmrl.setProd_order_no(pps.getProd_order_no());
				mmrl.setCreate_man_name(userName);
				mmrl.setCreate_time(createTime);
				mmrl.setRemark("1".equals(pd)?"强制充当":"");
				this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
				
				ResultModel rejRes = this.getBean(SlmFundFirstDetailService.class).reAutoUltiJudge(CoilNo);
				if (!"200".equals(rejRes.getState())) {
					throw new Exception(rejRes.getMsgInfo());
				}
				
				this.getBean(SlmFundFirstDetailService.class).ycjsPYdd(CoilNo, true); // 一次结算后抛异动点
			}
		}
		//日志
		MesOprLog mol = new MesOprLog("生产管理", "平整挂单管理", "挂单", username, opr_time, null, null, null, null, Arrays.toString(sCoilNo));
		this.getMapper(MesOprLogMapper.class).insert(mol);
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 平整挂单
	 * memo1  生产订单号
	 * @throws Exception 
	 * [{"memo1":"1804130000001","object":["Y1831023"]}]  挂单
	 * [{"memo1":"1804110000002","object":["Y1830550","Y1830551"]}]  挂单
	 * [{"memo1":"1804110000002","object":["Y1830550","Y1830551"],"memo2":"1"}]  强制挂单
	 * 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRelationOrderNoValidingSteel(CommonPage<String[]> oci) throws Exception{
		ResultModel rm=new ResultModel();
		Date createTime = new Date();
		String temp = "";
		String[] path = null;
		ArrayList<String> validpath = new ArrayList<>();
		List<DesignInfo> infos = new ArrayList<>();
		
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
	    String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //操作时间
		
		//实物卷号数组
		String[] sCoilNo=oci.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
//		if (!"黄涛".equals(userName) && !"管理员".equals(userName)) {
//			rm.setState("500");
//			rm.setMsgInfo("您无此功能操作权限,请联系管理员!");
//			return rm;
//		}
		
		if(oci.getMemo1()==null||"".equals(oci.getMemo1().trim())||sCoilNo==null||sCoilNo.length==0){
			rm.setState("500");
			rm.setMsgInfo("条件传入错误!");
			return rm;
		}
		
		PlnProdorder pp=new PlnProdorder();
		pp.setProd_order_no(oci.getMemo1().trim());
		List<PlnProdorder> lpp=this.getMapper(PlnProdorderMapper.class).find(pp, 0, 1);
		if(lpp==null||lpp.size()==0){
			rm.setState("500");
			rm.setMsgInfo("生产订单["+pp.getProd_order_no()+"]不存在!");
			return rm;
		}
		
		// 验证生产订单状态是否为订单保留：20230908刘侣
		if ("0PAA60".equals(lpp.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+lpp.get(0).getProd_order_no()+"]为 ‘订单保留’ 状态，不允许挂单");
			return rm;
		}
		if ("0PAA70".equals(lpp.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+lpp.get(0).getProd_order_no()+"]为 ‘订单作废’ 状态，不允许挂单");
			return rm;
		}
		
		// 合同是否是厂内合同
		boolean flag_c = this.getBean(MesApiDesignInfoImpl.class).getContractType(oci.getMemo1().trim());
		
		// 合同是都是来料加工合同
		boolean flag_j = this.getBean(MesApiDesignInfoImpl.class).getCont_Type(oci.getMemo1().trim());
		
		// 验证生产订单是否锁定
		boolean flag_l = this.getBean(MesApiDesignInfoImpl.class).pOrdIsLocked(oci.getMemo1().trim());
		if (flag_l) {
			throw new Exception("生产订单已经锁定[订单变更中],不允许挂单!");
		}
		
		//物料充当脱单履历
		List<PlnMatReplaceLog> lmmrl=this.getMapper(PlnMatReplaceLogMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
		long iNo=1;
		if(lmmrl!=null&&lmmrl.size()>0){
			iNo=lmmrl.get(0).getSerial_no()+1;
		}
		
		
		// 判断订单是否是净重交货 (true 不走虚拟发运,不校验第一次挂单信息)
		boolean flag = this.getBean(MesApiServiceImpl.class).judgeOrderWeightType(oci.getMemo1());
		if(!"1".equals(lpp.get(0).getOrder_nature()) && !"3".equals(lpp.get(0).getOrder_nature())){
			//处理逻辑1
			if("0PAA12".compareTo(lpp.get(0).getStatus())>0){
				rm.setState("500");
				rm.setMsgInfo("[需加工]的订单，要下发生产后才能挂单！");
				return rm;
			}
			
			for(String CoilNo:sCoilNo){
				//处理逻辑
				//对传入的实物卷，查询库存信息[物流接口]
				//存在信息，则读取之； 若不存在信息，则报错“库存中无实物[xxx]信息”
				CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
				YdmProductDetail ypd=new YdmProductDetail();
				ypd.setCoilno(CoilNo);
				cp.setObject(ypd);
				ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doFindCoilno(cp);
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				
				// 挂单后判断该卷是否已经iscanload：是 就置为null，让其重新判断iscanload；20230210刘侣
				YdmProductDetail ypdCanLoad = new YdmProductDetail();
				ypdCanLoad.setCoilno(CoilNo);
				ypdCanLoad.setIs_can_load("1");
				List<YdmProductDetail> ypdCanList = this.getMapper(YdmProductDetailMapper.class).find(ypdCanLoad, 0, 0);
				if (ypdCanList != null && ypdCanList.size() > 0) {
					ypdCanList.get(0).setIs_can_load("0");
					this.getMapper(YdmProductDetailMapper.class).update(ypdCanList.get(0));
					this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypdCanList.get(0).getCoilno());
				}
				// end
				
				// 2018-06-15 限制重复挂单加此操作
				@SuppressWarnings("unchecked")
				List<YdmProductDetail> lypd = (List<YdmProductDetail>) rmValid.getData();
				for (YdmProductDetail ydmp : lypd) {
					if (ydmp.getOrderno() != null) {
						rm.setState("500");
						rm.setMsgInfo("实物["+cp.getObject().getCoilno()+"]已绑定订单["+ydmp.getOrderno()+"]! 不能再次操作!");
						return rm;
					}else if(!flag_c && !flag_j){
						PlnMaterielDetail pmd = new PlnMaterielDetail();
						pmd.setObject_no(ydmp.getCoilno());
						List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmd, "nvl(invalid_flg,0) = 0", "", 0, 0);
						if (pmdd != null && pmdd.size()>0) {
							for (PlnMaterielDetail plnmaterieldetail : pmdd) {
								plnmaterieldetail.setObject_no("");
								this.getMapper(PlnMaterielDetailMapper.class).update(plnmaterieldetail);
							}
						}
					}
				}
				// 拆分订单不验证合同号
				if (!(lpp.get(0).getIs_split() != null && "1".equals(lpp.get(0).getIs_split()))) {
					if (!flag && !flag_c && !flag_j) {
						// 判断第一次挂单与第二次挂单合同号contract_no是否一致 2018-09-05 通过挂单日志表获取最后一条脱单的订单信息(原料卷订单 属性为新钢
						// 最后一个)
						List<PlnProdorder> plnlog = this.getMapper(PlnMatReplaceLogMapper.class).findreplacelog(CoilNo);
						if (Utils.isNotEmpty(plnlog)) {
							if (!lpp.get(0).getContract_no().equals(plnlog.get(0).getContract_no())) {
								// 去掉这个限制lanyushan：20231113刘侣
//								throw new Exception("合同号不匹配，不能上料! 合同号 : [" + plnlog.get(0).getContract_no() + ","
//										+ lpp.get(0).getContract_no() + "]");
							}
						} else {
							// 黄涛操作和蓝雨珊不一样，所以先把这个注释掉：黄涛需求 2023011301
//							throw new Exception("未找到[" + CoilNo + "]对应原料卷订单挂单信息!");
						}
					}
				}
				// 工序点信息
				List<ComBaseInfo> infoI = this.getMapper(ComBaseInfoMapper.class)
						.findWithCondition(null, "sort_code = '2002' AND validflag = '1'",
								null, 0, 0);
				ProcessPoint processpoint = new ProcessPoint(infoI);
				
				// 生成设计信息
				if (Utils.isNotEmpty(lpp.get(0).getWhole_process_codes()) && !"1".equals(lpp.get(0).getWhole_coil())) {
					// 1、获取有效工艺路径
					temp = processpoint.smooth_code + StringUtils.substringAfter(lpp.get(0).getWhole_process_codes(), processpoint.smooth_code);
					path = temp.split("-");
					validpath = this.getBean(MesApiDesignInfoImpl.class).getProcessPath(path);
					// 调用设计信息接口
					DesignInfo info = new DesignInfo();
					// 如果实物对应的虚拟物料信息存在,即传入虚拟物料号(厂内合同成品再加工)
					List<PlnMaterielDetail> pmd_c = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no = '"+CoilNo+"' and nvl(invalid_flg,0) = 0", "", 0, 0);
					if (Utils.isNotEmpty(pmd_c)) {
						info.setObject_no_vt(pmd_c.get(0).getObject_no_vt());
					}
					info.setCurrentProcIndex(0);
					info.setObject_no(CoilNo);
					info.setProcess(validpath);
					info.setProd_order_no(oci.getMemo1());
					info.setSingle_wgt(lypd.get(0).getFactweight());
					infos.add(info);
					ResultModel design = this.getBean(MesApiDesignInfoImpl.class).Design_Plan_Info(infos,null);
					if (!"200".equals(design.getState())) {
						throw new Exception(design.getMsgInfo());
					}
				}
				
				// 如果牌号不匹配
				if (!lpp.get(0).getSteel_code().equals(lypd.get(0).getJudgesteelcode()) || !lpp.get(0).getSteel_code().equals(lypd.get(0).getSteelcode())) {
					// 1、备份库存信息
					CommonPage<YdmProductDetail> sum = new CommonPage<YdmProductDetail>();
					YdmProductDetail detail = new YdmProductDetail();
					detail.setCoilno(CoilNo);
					List<YdmProductDetail> ydmlist = new ArrayList<YdmProductDetail>();
					ydmlist.add(detail);
					sum.setObjectList(ydmlist);
					this.getBean(YdmProductDetailBfServiceImpl.class).doAddDetailBf(sum);
					// 2、修改牌号
					detail.setSteelcode(lpp.get(0).getSteel_code());
					detail.setSteel_name(lpp.get(0).getSteel_name());
					this.getMapper(YdmProductDetailMapper.class).update(detail);
				}
				
				if ("1".equals(lpp.get(0).getWhole_coil())) {
					
					//保存相关数据
					//修改生产订单进程表[PLN_PRODORDER_STATISTIC]
					PlnProdorderStatistic pps=new PlnProdorderStatistic();
					pps.setProd_order_no(lpp.get(0).getProd_order_no());
					pps.setReplaced(lypd.get(0).getFactweight());
					pps.setReplaced_cnt(1);
					pps.setReplaced_times(1);
					pps.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
					this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
					
					
					//修改订单进程表[PLN_ORDER_STATISTIC]
					PlnOrderStatistic pos=new PlnOrderStatistic();
					pos.setOrder_no(lpp.get(0).getOrder_no());
					pos.setReplaced(lypd.get(0).getFactweight());
					pos.setReplaced_cnt(1);
					pos.setReplaced_times(1);
					pos.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
					this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);
					
					//调用物流接口，关联实物和生产订单信息
					ypd.setProdorderno(lpp.get(0).getProd_order_no());
					ypd.setOrderno(lpp.get(0).getOrder_no());
					ypd.setCustomer_no(lpp.get(0).getCustomer_no());
					ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
					ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
					ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
					ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
					ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
					this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
					
					
					//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
					PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
					mmrl.setSerial_no(iNo++);
					mmrl.setObject_no(lypd.get(0).getCoilno());
					mmrl.setOpt_type("2");
					mmrl.setOrder_no(lpp.get(0).getOrder_no());
					mmrl.setProd_order_no(lpp.get(0).getProd_order_no());
					mmrl.setCreate_man_name(userName);
					mmrl.setCreate_time(new Date());
					mmrl.setRemark("整卷发运");
					this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
				}else {
					
					// 修改生产订单进程表[PLN_PRODORDER_STATISTIC] 订单进程表
					this.getBean(MesApiDesignInfoImpl.class).updateOrderStatic(lpp.get(0).getProd_order_no(), lpp.get(0).getOrder_no(),lpp.get(0).getWhole_process_codes(), lypd.get(0).getFactweight(),true);
					
					// 查询虚拟物料表【PLN_MATERIEL_DETAIL】---- where NVL(INVALID_FLG,0)=0 AND OBJECT_NO = 实物卷号
					List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, " NVL(INVALID_FLG,0)=0 AND OBJECT_NO = '"+CoilNo+"'", "", 0, 0);
					
					//调用物流接口，关联实物和生产订单信息
					ypd.setCustomer_no(lpp.get(0).getCustomer_no());
					ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
					ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
					ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
					ypd.setProdorderno(lpp.get(0).getProd_order_no());
					ypd.setOrderno(lpp.get(0).getOrder_no());
					ypd.setObject_no_vt(pmdd.get(0).getObject_no_vt());
					ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
					ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
					this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
					
					
					//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
					PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
					BeanUtils.copyProperties(pmdd.get(0), mmrl);
					mmrl.setSerial_no(iNo++);
					mmrl.setOpt_type("2");
					mmrl.setCreate_man_name(userName);
					mmrl.setCreate_time(createTime);
					mmrl.setRemark("强制挂单不验证牌号");
					this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
					
					// 生成物料附加信息
					this.getBean(MesApiDesignInfoImpl.class).addMaterielExinfo(lpp.get(0), pmdd.get(0),processpoint.smooth_code);
					
					// 拆分订单如果还需继续生产,判断是否需要移动库存 2019-08-05
					if ((lpp.get(0).getIs_split() != null && "1".equals(lpp.get(0).getIs_split()))) {
						// 如果物料在成品库挪到在制库
						if ("500203".equals(lypd.get(0).getStorageattr())) {
	//						ypd.setStorageattr("500203");
							CommonPage<YdmProductDetail> tmp = new CommonPage<YdmProductDetail>();
							List<YdmProductDetail> ydm = new ArrayList<YdmProductDetail>();
							lypd.get(0).setMemo("拆分订单["+lpp.get(0).getOrder_no()+"]增加["+lpp.get(0).getWhole_process_names()+"]工序->移至在制库");
							ydm.add(lypd.get(0));
							tmp.setObjectList(ydm);
							tmp.setMemo1("500202");
							tmp.setMemo2("0");
							this.getBean(LmsApiServiceImpl.class).doChangeMaterialStorage(tmp);
						}
					}
				}
				
				ResultModel rejRes = this.getBean(SlmFundFirstDetailService.class).reAutoUltiJudge(CoilNo);
				if (!"200".equals(rejRes.getState())) {
					throw new Exception(rejRes.getMsgInfo());
				}
				
				this.getBean(SlmFundFirstDetailService.class).ycjsPYdd(CoilNo, true); // 一次结算后抛异动点
			}
		}else{
//			//处理逻辑2
//			if("0PAA12".compareTo(lpp.get(0).getStatus())<=0){
//				rm.setState("500");
//				rm.setMsgInfo("[原料卷/非计划]的订单，必须在下发生产前充当！");
//				return rm;
//			}
			for(String CoilNo:sCoilNo){
				//处理逻辑
				//对传入的实物卷，查询库存信息[物流接口]
				//存在信息，则读取之； 若不存在信息，则报错“库存中无实物[xxx]信息”
				CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
				YdmProductDetail ypd=new YdmProductDetail();
				ypd.setCoilno(CoilNo);
				cp.setObject(ypd);
				ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doFindCoilno(cp);
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				
				// 挂单后判断该卷是否已经iscanload：是 就置为null，让其重新判断iscanload；20230210刘侣
				YdmProductDetail ypdCanLoad = new YdmProductDetail();
				ypdCanLoad.setCoilno(CoilNo);
				ypdCanLoad.setIs_can_load("1");
				List<YdmProductDetail> ypdCanList = this.getMapper(YdmProductDetailMapper.class).find(ypdCanLoad, 0, 0);
				if (ypdCanList != null && ypdCanList.size() > 0) {
					ypdCanList.get(0).setIs_can_load("0");
					this.getMapper(YdmProductDetailMapper.class).update(ypdCanList.get(0));
					this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypdCanList.get(0).getCoilno());
				}
				// end
				
				// 2018-06-15 限制重复挂单加此操作
				@SuppressWarnings("unchecked")
				List<YdmProductDetail> lypd = (List<YdmProductDetail>) rmValid.getData();
				for (YdmProductDetail ydmp : lypd) {
					if (ydmp.getOrderno() != null) {
						rm.setState("500");
						rm.setMsgInfo("实物["+cp.getObject().getCoilno()+"]已绑定订单["+ydmp.getOrderno()+"]! 不能再次操作!");
					}else if(!flag_c && !flag_j){
						PlnMaterielDetail pmd = new PlnMaterielDetail();
						pmd.setObject_no(ydmp.getCoilno());
						List<PlnMaterielDetail> pmdd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmd, "nvl(invalid_flg,0) = 0", "", 0, 0);
						if (pmdd != null && pmdd.size()>0) {
							for (PlnMaterielDetail plnmaterieldetail : pmdd) {
								plnmaterieldetail.setObject_no("");
								this.getMapper(PlnMaterielDetailMapper.class).update(plnmaterieldetail);
							}
						}
					}
				}
				
				// 如果牌号不匹配
				if (!lpp.get(0).getSteel_code().equals(lypd.get(0).getJudgesteelcode()) || !lpp.get(0).getSteel_code().equals(lypd.get(0).getSteelcode())) {
					// 1、备份库存信息
					CommonPage<YdmProductDetail> sum = new CommonPage<YdmProductDetail>();
					YdmProductDetail detail = new YdmProductDetail();
					detail.setCoilno(CoilNo);
					List<YdmProductDetail> ydmlist = new ArrayList<YdmProductDetail>();
					ydmlist.add(detail);
					sum.setObjectList(ydmlist);
					this.getBean(YdmProductDetailBfServiceImpl.class).doAddDetailBf(sum);
					// 2、修改牌号
					detail.setSteelcode(lpp.get(0).getSteel_code());
					detail.setSteel_name(lpp.get(0).getSteel_name());
					this.getMapper(YdmProductDetailMapper.class).update(detail);
				}
				
				//保存相关数据
				//修改生产订单进程表[PLN_PRODORDER_STATISTIC]
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(lpp.get(0).getProd_order_no());
				pps.setReplaced(lypd.get(0).getFactweight());
				pps.setReplaced_cnt(1);
				pps.setReplaced_times(1);
				pps.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
				
				
				//修改订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(lpp.get(0).getOrder_no());
				pos.setReplaced(lypd.get(0).getFactweight());
				pos.setReplaced_cnt(1);
				pos.setReplaced_times(1);
				pos.setUnscheduled(lypd.get(0).getFactweight()==null?null:(-lypd.get(0).getFactweight()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);
				
				
				//调用物流接口，关联实物和生产订单信息
				ypd.setProdorderno(pps.getProd_order_no());
				ypd.setOrderno(pos.getOrder_no());
				ypd.setCustomer_no(lpp.get(0).getCustomer_no());
				ypd.setCustomer_nm(lpp.get(0).getCustomer_name());
				ypd.setReceiv_nm(lpp.get(0).getReciver_unit_name());
				ypd.setReceiv_no(lpp.get(0).getReciver_unit_no());
				ypd.setDelivery_state_code(lpp.get(0).getDelivery_state_code());
				ypd.setDelivery_state_desc(lpp.get(0).getDelivery_state_desc());
				this.getMapper(YdmProductDetailMapper.class).doFinshedProductActUpdate(ypd);
				
				
				//写物料充当脱单履历表[MIL_MAT_REPLACE_LOG]  MilMatReplaceLog
				PlnMatReplaceLog mmrl=new PlnMatReplaceLog();
				mmrl.setSerial_no(iNo++);
				mmrl.setObject_no(lypd.get(0).getCoilno());
				mmrl.setOpt_type("1");
				mmrl.setOrder_no(pos.getOrder_no());
				mmrl.setProd_order_no(pps.getProd_order_no());
				mmrl.setCreate_man_name(userName);
				mmrl.setCreate_time(createTime);
				mmrl.setRemark("强制挂单不验证牌号");
				this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
				
				ResultModel rejRes = this.getBean(SlmFundFirstDetailService.class).reAutoUltiJudge(CoilNo);
				if (!"200".equals(rejRes.getState())) {
					throw new Exception(rejRes.getMsgInfo());
				}
				
				this.getBean(SlmFundFirstDetailService.class).ycjsPYdd(CoilNo, true); // 一次结算后抛异动点
			}
		}
		//日志
		MesOprLog mol = new MesOprLog("生产管理", "平整不检验牌号挂单管理", "挂单", username, opr_time, null, null, null, null, Arrays.toString(sCoilNo));
		this.getMapper(MesOprLogMapper.class).insert(mol);
		rm.setState("200");
		return rm;
	}
	
}

