package market.Api.Mes.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Mes.service.MesApiSkinrollingPlanService;
import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ResultModel;
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.mapper.PlnMaterielInvalidMapper;
import market.Mes.Lengthwise.Plan.mapper.PlnProcessPlanMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMatReplaceLog;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielInvalid;
import market.Mes.Lengthwise.Plan.vo.PlnProcessPlan;
import market.Mes.MaterielTrack.mapper.PrcMaterielExtinfoMapper;
import market.Mes.MaterielTrack.mapper.PrcMaterielRelationMapper;
import market.Mes.MaterielTrack.mapper.PrcMaterielVtProcessMapper;
import market.Mes.MaterielTrack.vo.PrcMaterielProcess;
import market.Mes.MaterielTrack.vo.PrcMaterielRelation;
import market.Mes.MaterielTrack.vo.PrcMaterielVtProcess;
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.mapper.PlnSkinrollingDsnMainMapper;
import market.Mes.Tpm.Design.mapper.PlnSkinrollingDsnMultinfoMapper;
import market.Mes.Tpm.Design.mapper.PlnSrPlanStatisticMapper;
import market.Mes.Tpm.Design.vo.PlnSkinrollingDsnMain;
import market.Mes.Tpm.Design.vo.PlnSkinrollingDsnMultinfo;
import market.Mes.Tpm.Design.vo.PlnSrPlanStatistic;
import market.Mes.Tpm.mapper.MesTpmDMapper;
import market.Mes.Tpm.mapper.PlnSkinrollingPlanDetailMapper;
import market.Mes.Tpm.mapper.PlnSkinrollingPlanMapper;
import market.Mes.Tpm.vo.MesTpmD;
import market.Mes.Tpm.vo.MesTpmM;
import market.Mes.Tpm.vo.PlnSkinrollingPlan;
import market.Mes.Tpm.vo.PlnSkinrollingPlanDetail;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.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="MesApiSkinrollingPlanService")
public class MesApiSkinrollingPlanImpl extends BaseServiceImpl implements MesApiSkinrollingPlanService{


	//--------------------------------平整上料、下PDI Start----------------------------------------------------
	
		/**
		 * 平整上料接口 下PDI 验证
		 * @throws Exception 
		 * 其实都是单条操作
		 */
		public void MesTpmMAddValid(List<MesTpmM> oci) throws Exception{
			
			//循环 根据(平整计划号数组，虚拟物料号数组) 读取 平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】
			for(MesTpmM mcsm:oci){
				PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
				plpd.setSr_plan_no(mcsm.getSchedule_no());
				plpd.setObject_no_vt(mcsm.getMaterial_no_vt());
				//如果不存在，则报错退出“平整计划[xxxxx][xx]信息不存在！”
				List<PlnSkinrollingPlanDetail> lppd=this.getMapper(PlnSkinrollingPlanDetailMapper.class).find(plpd, 0, 0);
				if(lppd==null||lppd.size()==0){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				PlnMaterielDetail M1=new PlnMaterielDetail();
				//读取 根据计划明细表中的虚拟物料号(母卷号)，查找虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息 M1
				List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, " object_no_vt = '"+mcsm.getMaterial_no_vt()+"' AND NVL(invalid_flg,0) = '0'", null, 0, 1);
				if(lpmd==null||lpmd.size()==0){
					throw new Exception("虚拟物料["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				//判断虚拟母卷信息里的 物料号【OBJECT_NO】，如果为空，则报错退出
				M1=lpmd.get(0);
				if(M1.getObject_no()==null||"".equals(M1.getObject_no())){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]尚未挂料！");
				}
				
				//实际上只有一条
				for(PlnSkinrollingPlanDetail ppdc:lppd){
					//判断平整计划明细的状态【STATUS】
					if(ppdc.getStatus().compareTo("5BSR02")>0){
						throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]已上料，不能再次操作！");
					}
				}
			}
		}
		
		/**
		 * 平整上料接口 下PDI
		 * @throws Exception 
		 * 其实都是单条操作
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void MesTpmMAdd(List<MesTpmM> oci) throws Exception{
			
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			//循环 根据(平整计划号数组，虚拟物料号数组) 读取 平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】
			for(MesTpmM mcsm:oci){
				PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
				plpd.setSr_plan_no(mcsm.getSchedule_no());
				plpd.setObject_no_vt(mcsm.getMaterial_no_vt());
				//如果不存在，则报错退出“平整计划[xxxxx][xx]信息不存在！”
				List<PlnSkinrollingPlanDetail> lppd=this.getMapper(PlnSkinrollingPlanDetailMapper.class).find(plpd, 0, 0);
				if(lppd==null||lppd.size()==0){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				PlnMaterielDetail M1=new PlnMaterielDetail();
				//读取 根据计划明细表中的虚拟物料号(母卷号)，查找虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息 M1
				List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, " object_no_vt = '"+mcsm.getMaterial_no_vt()+"' AND NVL(invalid_flg,0) = '0'", null, 0, 1);
				if(lpmd==null||lpmd.size()==0){
					throw new Exception("虚拟物料["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				//判断虚拟母卷信息里的 物料号【OBJECT_NO】，如果为空，则报错退出
				M1=lpmd.get(0);
				if(M1.getObject_no()==null||"".equals(M1.getObject_no())){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]尚未挂料！");
				}
				
				//实际上只有一条
				for(PlnSkinrollingPlanDetail ppdc:lppd){
					//判断平整计划明细的状态【STATUS】
					if(ppdc.getStatus().compareTo("5BSR02")>0){
						throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]已上料，不能再次操作！");
					}
					PrcMaterielProcess pmp=new PrcMaterielProcess();
					pmp.setPrc_code("5BSR03");//新增的状态
					
					//这里的格式要和字符串日期的格式匹配，不然会抛出转换异常   
					SimpleDateFormat  dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
					if (mcsm.getMemo() != null && !"".equals(mcsm.getMemo())) {
						Date createdate = dateFormat.parse(mcsm.getMemo());
						pmp.setCreate_time(createdate);
					}else {
						pmp.setCreate_time(new Date());
					}
					
					
					pmp.setObject_no(M1.getObject_no());
					pmp.setCreate_man_name(userName);
					pmp.setRemark(mcsm.getMemo());
					//新增物料跟踪：
					this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
					
					//修改平整计划明细表
					PlnSkinrollingPlanDetail plpdu=new PlnSkinrollingPlanDetail();
					plpdu.setStatus("5BSR03");
					plpdu.setSerial_no(ppdc.getSerial_no());
					this.getMapper(PlnSkinrollingPlanDetailMapper.class).update(plpdu);
				}
				
				//根据母卷号，查询对应的所有子卷信息
				PlnMaterielDetail pmdq=new PlnMaterielDetail();
				pmdq.setMat_object_no_vt(lpmd.get(0).getObject_no_vt());
				List<PlnMaterielDetail> lpmdc=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmdq, "  nvl (invalid_flg, '0') <> 1 ", "", 0, 0);
				for(PlnMaterielDetail ppdc:lpmdc){
					//修改各子卷的 状态 STATUS= 0PSR03; 
					PlnMaterielDetail pmdu=new PlnMaterielDetail();
					pmdu.setSerial_no(ppdc.getSerial_no());
					pmdu.setStatus("0PSR03");
					this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
					
					//增加子卷的 虚拟物料履历表
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(ppdc.getObject_no_vt(),"0PSR03",userName,new Date());
				}
				
				//修改平整计划主表
				doUpdatePlnSkinrollingPlan(mcsm.getSchedule_no()); 
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				//修改(母卷)虚拟物料表【PLN_MATERIEL_DETAIL】
				pmdu.setSerial_no(lpmd.get(0).getSerial_no());
				pmdu.setStatus("0PSR03");
				this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
				//写虚拟物料履历，状态填:	0PSR03	母卷																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSR03",userName,new Date());
			}
		}
		
		/**
		 * 撤销平整上料接口 下PDI 验证
		 * @param 虚拟子卷号
		 * @throws Exception 
		 * 其实都是单条操作
		 */
		public void MesTpmMCancelValid(List<MesTpmM> oci) throws Exception{
			//循环 根据(平整计划号数组，虚拟物料号数组) 读取 平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】
			for(MesTpmM mcsm:oci){
				PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
				plpd.setSr_plan_no(mcsm.getSchedule_no());
				plpd.setObject_no_vt(mcsm.getMaterial_no_vt());
				//如果不存在，则报错退出“平整计划[xxxxx][xx]信息不存在！”
				List<PlnSkinrollingPlanDetail> lppd=this.getMapper(PlnSkinrollingPlanDetailMapper.class).find(plpd, 0, 0);
				if(lppd==null||lppd.size()==0){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				PlnMaterielDetail M1=new PlnMaterielDetail();
				//读取 根据计划明细表中的虚拟物料号(母卷号)，查找虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息 M1
				M1.setObject_no_vt(mcsm.getMaterial_no_vt());
				List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).find(M1, 0, 0);
				if(lpmd==null||lpmd.size()==0){
					throw new Exception("虚拟物料["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				//判断虚拟母卷信息里的 物料号【OBJECT_NO】，如果为空，则报错退出
				M1=lpmd.get(0);
				if(M1.getObject_no()==null||"".equals(M1.getObject_no())){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]尚未挂料！");
				}
				
				//实际上只有一条
				for(PlnSkinrollingPlanDetail ppdc:lppd){
					//判断平整计划明细的状态【STATUS】
					if(!"5BSR03".equals(ppdc.getStatus())){
						throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]上料后，经过其他操作，不允许撤销！");
					}
				}
			}
		}
		
		/**
		 * 撤销平整上料接口 下PDI
		 * @param 虚拟子卷号
		 * @throws Exception 
		 * 其实都是单条操作
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void MesTpmMCancel(List<MesTpmM> oci) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			//循环 根据(平整计划号数组，虚拟物料号数组) 读取 平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】
			for(MesTpmM mcsm:oci){
				PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
				plpd.setSr_plan_no(mcsm.getSchedule_no());
				plpd.setObject_no_vt(mcsm.getMaterial_no_vt());
				//如果不存在，则报错退出“平整计划[xxxxx][xx]信息不存在！”
				List<PlnSkinrollingPlanDetail> lppd=this.getMapper(PlnSkinrollingPlanDetailMapper.class).find(plpd, 0, 0);
				if(lppd==null||lppd.size()==0){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				PlnMaterielDetail M1=new PlnMaterielDetail();
				//读取 根据计划明细表中的虚拟物料号(母卷号)，查找虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息 M1
				M1.setObject_no_vt(mcsm.getMaterial_no_vt());
				List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).find(M1, 0, 0);
				if(lpmd==null||lpmd.size()==0){
					throw new Exception("虚拟物料["+mcsm.getMaterial_no_vt()+"]信息不存在！");
				}
				
				//判断虚拟母卷信息里的 物料号【OBJECT_NO】，如果为空，则报错退出
				M1=lpmd.get(0);
				if(M1.getObject_no()==null||"".equals(M1.getObject_no())){
					throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]尚未挂料！");
				}
				
				//实际上只有一条
				for(PlnSkinrollingPlanDetail ppdc:lppd){
					//判断平整计划明细的状态【STATUS】
					if(!"5BSR03".equals(ppdc.getStatus())){
						throw new Exception("平整计划["+mcsm.getSchedule_no()+"]["+mcsm.getMaterial_no_vt()+"]上料后，经过其他操作，不允许撤销！");
					}
					PrcMaterielProcess pmp=new PrcMaterielProcess();
					pmp.setObject_no(M1.getObject_no());
					pmp.setPrc_code("5BSR03");
					pmp.setRemark("回退上料");
					pmp.setKeep_sametime_status(0);
					ResultModel al=this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp);
					//撤销实物物料状态跟踪
					if(!"0".equals(al.getState())){
						throw new Exception("物料状态不是已上料状态，不允许撤销！");
					}
					//修改平整计划明细表
					PlnSkinrollingPlanDetail plpdu=new PlnSkinrollingPlanDetail();
					// V_最后状态
					plpdu.setStatus(((PrcMaterielProcess)al.getData()).getPrc_code());// "5BSR02"
					plpdu.setSerial_no(ppdc.getSerial_no());
					this.getMapper(PlnSkinrollingPlanDetailMapper.class).update(plpdu);
				}
				
				//根据母卷号，查询对应的所有子卷信息
				PlnMaterielDetail pmdq=new PlnMaterielDetail();
				pmdq.setMat_object_no_vt(lpmd.get(0).getObject_no_vt());
				List<PlnMaterielDetail> lpmdc=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmdq, "  nvl (invalid_flg, '0') <> 1 ", "", 0, 0);
				for(PlnMaterielDetail ppdc:lpmdc){
					//修改各子卷的 状态 STATUS= 0PSR02; 
					PlnMaterielDetail pmdu=new PlnMaterielDetail();
					pmdu.setSerial_no(ppdc.getSerial_no());
					pmdu.setStatus("0PSR02");
					this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
					//增加子卷的 虚拟物料履历表
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(ppdc.getObject_no_vt(),"0PSR02",userName,new Date());
				}
				//修改平整计划主表
				doUpdatePlnSkinrollingPlan(mcsm.getSchedule_no()); 
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				//修改(母卷)虚拟物料表【PLN_MATERIEL_DETAIL】
				pmdu.setSerial_no(lpmd.get(0).getSerial_no());
				pmdu.setStatus("0PSR02");
				this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
				//写虚拟物料履历，状态填:	0PSR02	母卷																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSR02",userName,new Date());
			}
		}
		//--------------------------------平整上料、下PDI End----------------------------------------------------
		
		//--------------------------------平整完成 Start----------------------------------------------------
		/**
		 * 平整完成
		 * 实物 生成接口  产生实物，未挂上单时调用
		 * @param OBJECT_NO 实物母卷号
		 * @param SUB_OBJECT_NO 实物子卷号
		 * @param RELATION_TYPE 关系类型     1:改号(原-现号码对应)；2:轧制(板坯-钢板对应)；3:剪切/切割(母-子对应)
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_PrcMatRelAdd(PrcMaterielRelation pmr) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			Date createTime = new Date();
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BSR10，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmr.getSub_object_no());
			pmp.setPrc_code("5BSR10");//新增的状态
			pmp.setCreate_time(createTime);
			pmp.setCreate_man_name(userName);
			pmp.setRemark("");
			//新增物料跟踪：
			this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			
			//调用公共函数2  建立物料母子对应关系 
			pmr.setCreate_man_name(userName);
			pmr.setCreate_time(createTime);
			pmr.setRelation_type(2);
			pmr.setPrc_code("5BSR10");
			pmr.setRemark("");
			this.getBean(MesApiServiceImpl.class).doBuildRelation(pmr);
		}
		
		/**
		 * 撤销平整
		 * 实物 撤销生成接口  产生实物，未挂上单时调用
		 * @param OBJECT_NO 实物母卷号
		 * @param SUB_OBJECT_NO 实物子卷号
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_PrcMatRelRemove(PrcMaterielRelation pmr) throws Exception{
			//回退子卷物料跟踪：
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmr.getSub_object_no());
			pmp.setPrc_code("5BSR10");
			pmp.setKeep_sametime_status(0);
			pmp.setRemark("撤销平整实物生成");
			ResultModel al=this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp);
			//撤销实物物料状态跟踪
			if(!"0".equals(al.getState())){
				throw new Exception("实物["+pmp.getObject_no()+"]状态已发生改变，不能撤销！");
			}
			
			//调用公共函数3  断开物料母子对应关系 
			PrcMaterielRelation pmrd=new PrcMaterielRelation();
			pmrd.setObject_no(pmr.getObject_no());
			pmr.setSub_object_no(pmr.getSub_object_no());
			this.getBean(MesApiServiceImpl.class).doBuildRelationRemove(pmr);
		}
		
		
	/**
	 * 平整完成 虚拟子卷 挂料接口
	 * 
	 * @param material_no   实物母卷号
	 * @param SUB_OBJECT_NO 实物子卷号
	 * @param object_no_vt  虚拟子卷号
	 * @param create_time   操作时间 注意 传入操作时间 新增履历表和实物进程表的创建时间，使用传入的操作时间
	 * 						厂内合同交工时,如果子卷工序完成把子卷库存信息的订单信息删除(2019-02-20)(此更改已屏蔽)
	 * @throws Exception
	 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_Relation(PlnMaterielDetail pmd) throws Exception{
			
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			Date createTime = new Date();
			// 工序点信息
			List<ComBaseInfo> info = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, "sort_code = '2002' AND validflag = '1'", null, 0, 0);
			ProcessPoint processpoint = new ProcessPoint(info);
			
			// 新增物料附加信息
			this.getBean(MesApiDesignInfoImpl.class).addChildMaterielExinfo(pmd.getObject_no(), pmd.getSub_object_no(), pmd.getObject_no_vt(), processpoint.smooth_code);
			// 调用物流接口，获取实物子卷信息。
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(pmd.getSub_object_no());
			List<YdmProductDetail> lypd=this.getMapper(YdmProductDetailMapper.class).find(ypd, 0, 0);
			if(lypd==null||lypd.size()==0){
				throw new Exception("库存中无["+pmd.getObject_no()+"]实物卷!");
			}
			
			// 平整工序代码processpoint.Skinrolling_code 2018-06-13
			// 读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟子卷信息
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no_vt = '"+pmd.getObject_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0, 1);
			if(lpmd==null||lpmd.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
			if(Utils.isEmpty(lypd.get(0).getSteelcode()) || (!lypd.get(0).getSteelcode().equals(lpmd.get(0).getSteel_code()))){
//				throw new Exception("实物卷["+pmd.getObject_no()+"]和虚拟卷["+pmd.getObject_no_vt()+"]的牌号不匹配，不能绑定");
			}
			// 2018-08-27 生产完成后厚度按实际情况取值,很有可能和计划不一致,故去此验证.
//			if(lypd.get(0).getThick().doubleValue()!=lpmd.get(0).getThick().doubleValue()){
//				throw new Exception("实物卷["+pmd.getObject_no()+"]和虚拟卷["+pmd.getObject_no_vt()+"]的厚不匹配，不能绑定");
//			}
			//------------------------2018-03-16------------------------------
			//100-100>5 考虑偏差
//			if((lypd.get(0).getWidth().doubleValue()-lpmd.get(0).getWidth().doubleValue()>dUpper)
//					||(lypd.get(0).getWidth().doubleValue()-lpmd.get(0).getWidth().doubleValue()<dLower)){
//				throw new Exception("实物卷["+pmd.getObject_no()+"]和虚拟卷["+pmd.getObject_no_vt()+"]的宽不匹配，不能绑定");
//			}
//			//----------------------------------------------------------------
//			if(lpmd.get(0).getObject_no()!=null&&!"".equals(lpmd.get(0).getObject_no())){
//				throw new Exception("实物卷["+pmd.getObject_no()+"]已绑定其他计划, 不能再次绑定！");
//			}
//			
			// 修改虚拟物料表
			PlnMaterielDetail pmdq=new PlnMaterielDetail();
			pmdq.setObject_no_vt(pmd.getObject_no_vt());
			pmdq.setSerial_no(lpmd.get(0).getSerial_no());
			pmdq.setObject_no(pmd.getSub_object_no());
			pmdq.setStatus("0PSR10");
			pmdq.setIs_finished("1");
//			pmdq.setPrev_process_code(processpoint.Skinrolling_code);
			this.getMapper(PlnMaterielDetailMapper.class).update(pmdq);
			// 新增虚拟子物料履历																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSR10",userName,createTime);
			
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BSR10，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getSub_object_no());
			pmp.setPrc_code("5BSR10");//新增的状态
			pmp.setCreate_time(createTime);
			pmp.setCreate_man_name(userName);
			pmp.setRemark("");
			// 新增物料跟踪：
			this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			
			// 2018-06-19 去掉 判断下一工序==null条件(判断虚拟子物料是否是其他子料的原料)
			// 修改改虚拟子物料对应的 订单进程表[PLN_ORDER_STATISTIC]
			PlnOrderStatistic pos = new PlnOrderStatistic();
			pos.setOrder_no(lpmd.get(0).getOrder_no());
			pos.setSr(lpmd.get(0).getSingle_weight());
			pos.setSr_cnt(1l);
			this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);

			// 生产订单进程表[PLN_PRODORDER_STATISTIC]信息
			PlnProdorderStatistic pps = new PlnProdorderStatistic();
			pps.setProd_order_no(lpmd.get(0).getProd_order_no());
			pps.setSr(lpmd.get(0).getSingle_weight());
			pps.setSr_cnt(1l);
			this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
			
			//修改虚拟物料对应的 平整计划进程表
			PlnSrPlanStatistic plps=new PlnSrPlanStatistic();
			plps.setSr_plan_no(lpmd.get(0).getSr_plan_no());
			plps.setSr(lpmd.get(0).getSingle_weight());
			plps.setSr_cnt(1l);
			this.getMapper(PlnSrPlanStatisticMapper.class).doUpdatePlaned(plps);
			
			//调用公共函数2  建立物料母子对应关系 
			PrcMaterielRelation pmr=new PrcMaterielRelation();
			pmr.setObject_no(pmd.getMaterial_no());
			pmr.setSub_object_no(pmd.getSub_object_no());
			pmr.setCreate_man_name(userName);
			pmr.setCreate_time(createTime);
			pmr.setRemark("");
			pmr.setRelation_type(2);
			this.getBean(MesApiServiceImpl.class).doBuildRelation(pmr);
		}
		
		/**
		 * 平整完成 不符合计划
		 * 实物生成, 新增虚拟子卷挂料
		 * @param sub_object_no 实物子卷号
		 * @param object_no 实物母卷号
		 * @param create_time 操作时间
		 * 注意 传入操作时间 新增履历表和实物进程表的创建时间，使用传入的操作时间
		 * 增加初始设计号 03-25
		 * @throws Exception 
		 * 2018-06-19
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public ResultModel SkinrollingFinsh_No_Plan(PlnMaterielDetail pmd) throws Exception {
			ResultModel rm = new ResultModel("200", "");
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			Date createTime = new Date();
			// 工序点信息
			List<ComBaseInfo> info = this.getMapper(ComBaseInfoMapper.class)
					.findWithCondition(null, "sort_code = '2002' AND validflag = '1'",
							null, 0, 0);
			ProcessPoint processpoint = new ProcessPoint(info);
			//调用物流接口，获取实物子卷信息。
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(pmd.getSub_object_no());
			List<YdmProductDetail> lypd=this.getMapper(YdmProductDetailMapper.class).find(ypd, 0, 0);
			if(lypd==null||lypd.size()==0){
				throw new Exception("库存中无["+pmd.getSub_object_no()+"]实物卷!");
			}
			// 获取母卷虚拟物料信息
			PlnMaterielDetail pmdcm=new PlnMaterielDetail();
			pmdcm.setObject_no(pmd.getObject_no());
			List<PlnMaterielDetail> pmdpp = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmdcm, " nvl(invalid_flg,0) = 0 ", "", 0, 1);
			if (pmdpp == null || pmdpp.size()==0) {
				rm.setState("500");
				rm.setMsgInfo("实物["+pmd.getObject_no()+"]的虚拟物料信息不存在!");
				return rm;
			}
			PlnProdorder pp=new PlnProdorder();
			pp.setProd_order_no(pmdpp.get(0).getProd_order_no());
			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;
			}
			
			// 获取下一个工序
			String temp = this.getBean(MesApiDesignInfoImpl.class)
					.getPath(lpp.get(0).getWhole_process_codes(), processpoint.smooth_code, pmdpp.get(0).getProc_repeat_index());
			String[] path =  temp.split("-");
			ArrayList<String> validpath = this.getBean(MesApiDesignInfoImpl.class).getProcessPath(path);
			
			// 新建虚拟物料表【PLN_MATERIEL_DETAIL】虚拟子卷信息
			PlnMaterielDetail pmdc=new PlnMaterielDetail();
			BeanUtils.copyProperties(pmdpp.get(0), pmdc);
			// 获取序列号
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, null, " serial_no desc ", 0, 1);
			long PMDNo=lpmd==null?1:(lpmd.size()==0?1:(lpmd.get(0).getSerial_no()+1));
			// 6位日期
			String sDate=new SimpleDateFormat("yyMMdd").format(new Date());
			// 6位流水号
			int objectnoVtNo=this.getMapper(PlnMaterielDetailMapper.class).getObjectVtMaxNoForDate(sDate)+1;
			pmdc.setSerial_no(PMDNo);
			// 年月日(6位)+6位流水
			pmdc.setObject_no_vt(sDate+String.format("%06d", objectnoVtNo));
			pmdc.setOrder_no(lpp.get(0).getOrder_no());
			pmdc.setObject_no(pmd.getSub_object_no());
			pmdc.setPline_code(lpp.get(0).getPline_code());
			pmdc.setStatus("0PSR10");
			pmdc.setIs_finished("1");
			pmdc.setPrev_dsn_process_code(processpoint.smooth_code);
			pmdc.setPrev_process_code(processpoint.smooth_code);
			pmdc.setIs_prev_dsn_finished("1");//母卷：0
			pmdc.setCreate_process_code(processpoint.smooth_code);
			if (Utils.isNotEmpty(validpath)) {
				pmdc.setNext_dsn_process_code(validpath.get(0));
				pmdc.setNext_process_code(validpath.get(0));
			}else {
				pmdc.setNext_dsn_process_code(null);
				pmdc.setNext_process_code(null);
			}
			pmdc.setIs_mat(0);//母卷：1
			pmdc.setOrder_no(lpp.get(0).getOrder_no());
			pmdc.setProd_order_no(lpp.get(0).getProd_order_no());
			pmdc.setPline_code(lpp.get(0).getPline_code());
//			pmdc.setLw_design_no(pmdpp.get(0).getLw_design_no());
//			pmdc.setAn_design_no(pmdpp.get(0).getAn_design_no());
//			pmdc.setCl_plan_no(pmdpp.get(0).getCl_plan_no());
//			pmdc.setSr_plan_no(pmdpp.get(0).getSr_plan_no());
			pmdc.setMat_object_no_vt(pmdpp.get(0).getObject_no_vt());
			pmdc.setIs_multi("0");
			pmdc.setSteel_code(lpp.get(0).getSteel_code());
			pmdc.setWidth(lypd.get(0).getWidth());
			pmdc.setThick(lypd.get(0).getThick());
			pmdc.setLength(lypd.get(0).getLength());
			pmdc.setDiameter_inn(lypd.get(0).getDiameter_inn());
			pmdc.setDiameter_out(lypd.get(0).getDiameter_out());
			pmdc.setSingle_weight(lypd.get(0).getFactweight());
			pmdc.setFixed_length_type_no(lpp.get(0).getFixed_length_type_no());
			pmdc.setCut_edge_type_no(lpp.get(0).getCut_edge_type_no());
			pmdc.setOrd_flg("2");
			pmdc.setCreate_man_name(userName);
			pmdc.setCreate_time(createTime);
			pmdc.setDeliver_date_earliest(lpp.get(0).getDeliver_date());
			pmdc.setDeliver_date_latest(lpp.get(0).getDeliver_date());
//			pmdc.setCreate_design_no(pmdpp.get(0).getSr_design_no());
//			pmdc.setSr_design_no(pmdpp.get(0).getSr_design_no());
			this.getMapper(PlnMaterielDetailMapper.class).insert(pmdc);
			

			// 新增物料附加信息
			this.getBean(MesApiDesignInfoImpl.class).addChildMaterielExinfo(pmd.getObject_no(), pmd.getSub_object_no(), pmdc.getObject_no_vt(), processpoint.smooth_code);
			// 子卷存在下一个工序
			if (Utils.isNotEmpty(validpath)) {
				// 调用设计信息接口
				List<DesignInfo> infos = new ArrayList<>();
				DesignInfo infor = new DesignInfo();
				infor.setCurrentProcIndex(0);
				infor.setObject_no(pmd.getSub_object_no());
				infor.setObject_no_vt(pmdc.getObject_no_vt());
				infor.setProcess(validpath);
				infor.setProd_order_no(pmdpp.get(0).getProd_order_no());
				infor.setSingle_wgt(lypd.get(0).getFactweight());
				infos.add(infor);
				ResultModel result = this.getBean(MesApiDesignInfoImpl.class).Design_Plan_Info(infos,null);
				if (!"200".equals(result.getState())) {
					throw new Exception("生产设计信息失败" + result.getMsgInfo());
				}
			}
			
			// 反写实绩的虚拟物料号 2018-09-20
			MesTpmD mcd = new MesTpmD();
			mcd.setCoil_no(pmd.getSub_object_no());
			mcd.setCoil_no_vt(pmdc.getObject_no_vt());
			this.getMapper(MesTpmDMapper.class).update(mcd);
			
			// 反写实物虚拟卷号 2018-11-08
			ypd.setObject_no_vt(pmdc.getObject_no_vt());
			this.getMapper(YdmProductDetailMapper.class).update(ypd);
			
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BSR10，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getSub_object_no());
			pmp.setPrc_code("5BSR10");
			pmp.setCreate_time(createTime);
			pmp.setCreate_man_name(userName);
			pmp.setRemark("");
			// 新增物料跟踪：
			this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			
			// 新增虚拟子物料履历																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmdc.getObject_no_vt(),"0PSR10",userName,createTime);
			
			// 修改改虚拟子物料对应的 订单进程表[PLN_ORDER_STATISTIC]
			PlnOrderStatistic pos = new PlnOrderStatistic();
			pos.setOrder_no(lpp.get(0).getOrder_no());
			pos.setSr(pmdc.getSingle_weight());
			pos.setSr_cnt(1l);
			this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);

			// 生产订单进程表[PLN_PRODORDER_STATISTIC]信息
			PlnProdorderStatistic pps = new PlnProdorderStatistic();
			pps.setProd_order_no(pmdc.getProd_order_no());
			pps.setSr(pmdc.getSingle_weight());
			pps.setSr_cnt(1l);
			this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
			
			//修改虚拟物料对应的 平整计划进程表
			PlnMaterielDetail pmdmq=new PlnMaterielDetail();
			pmdmq.setObject_no_vt(pmdc.getMat_object_no_vt());
			List<PlnMaterielDetail> lpmdm=this.getMapper(PlnMaterielDetailMapper.class).find(pmdmq, 0, 1);
			PlnSrPlanStatistic plps=new PlnSrPlanStatistic();
			plps.setSr_plan_no(lpmdm.get(0).getSr_plan_no());
			plps.setSr(pmdc.getSingle_weight());
			plps.setSr_cnt(1l);
			this.getMapper(PlnSrPlanStatisticMapper.class).doUpdatePlaned(plps);
			
			//调用公共函数2  建立物料母子对应关系 
			PrcMaterielRelation pmr=new PrcMaterielRelation();
			pmr.setObject_no(pmd.getObject_no());
			pmr.setSub_object_no(pmd.getSub_object_no());
			pmr.setCreate_man_name(userName);
			pmr.setCreate_time(createTime);
			pmr.setRemark("");
			pmr.setRelation_type(2);
			this.getBean(MesApiServiceImpl.class).doBuildRelation(pmr);
			return rm;
		}
		
		/**
		 * 撤销平整
		 * 虚拟子卷 挂料接口
		 * @param OBJECT_NO 实物子卷号
		 * @param object_no_vt 虚拟子卷号
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_RelationCancel(PlnMaterielDetail pmd) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			//调用物流接口，获取实物子卷信息。
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(pmd.getObject_no());
			List<YdmProductDetail> lypd=this.getMapper(YdmProductDetailMapper.class).find(ypd, 0, 0);
			if(lypd==null||lypd.size()==0){
				throw new Exception("库存中无["+pmd.getObject_no()+"]实物卷!");
			}
			 
			//读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟子卷信息
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no_vt = '"+pmd.getObject_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0, 1);
			if(lpmd==null||lpmd.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
			
			//读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟子卷的母卷信息
			List<PlnMaterielDetail> lpmdm=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no_vt = '"+lpmd.get(0).getMat_object_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0, 1);
			if(lpmdm==null||lpmdm.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
			
			if(lpmdm.get(0).getObject_no()==null||"".equals(lpmdm.get(0).getObject_no())){
				throw new Exception("实物卷["+pmd.getObject_no()+"]尚未绑定该计划，不能回退！");
			}
			
			// 下一个工序做了计划就不能回退
			if (Utils.isNotEmpty(pmd.getNext_dsn_process_code())) {
			this.getBean(MesApiDesignInfoImpl.class).JudgeIsOrNotPlan(lpmd.get(0));
			}
			
			// 物料充当脱单履历
//			this.getBean(MesApiDesignInfoImpl.class).createMatLog(lpmd.get(0), "厂内合同取消交工",userName, lpmd.get(0).getPrev_process_code() , "-1", pmd.getObject_no());
			
			//回退子卷物料跟踪：
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getObject_no());
			pmp.setPrc_code("5BSR10");
			pmp.setKeep_sametime_status(0);
			pmp.setRemark("撤销子卷挂料");
			ResultModel al=this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp);
			//撤销实物物料状态跟踪
			if(!"0".equals(al.getState())){
				throw new Exception("平整完成后，实物["+pmp.getObject_no()+"]状态已发生改变，不能撤销！");
			}
			
			//修改虚拟物料表
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(pmd.getObject_no_vt());
			pmdu.setSerial_no(lpmd.get(0).getSerial_no());
			pmdu.setStatus("0PSR03");
			pmdu.setIs_finished("0");
//			pmdu.setPrev_process_code(processpoint.Skinrolling_code);
			this.getMapper(PlnMaterielDetailMapper.class).doUpdateCancelRelation(pmdu);
			//新增虚拟子物料履历																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSR03",userName,new Date());
			//判断虚拟子物料是否有其他子料的原料
//			if(lpmd.get(0).getNext_dsn_process_code()==null||"".equals(lpmd.get(0).getNext_dsn_process_code())){
				//修改改虚拟子物料对应的 订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(lpmd.get(0).getOrder_no());
				pos.setAn(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				pos.setAn_cnt(-1l);
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePlaned(pos);
				//生产订单进程表[PLN_PRODORDER_STATISTIC]信息
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(lpmd.get(0).getProd_order_no());
				pps.setAn(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				pps.setAn_cnt(-1l);
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
//			}
			
			//修改虚拟物料对应的 平整计划进程表
			PlnSrPlanStatistic plps=new PlnSrPlanStatistic();
			plps.setSr_plan_no(lpmd.get(0).getSr_plan_no());
			plps.setSr(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
			plps.setSr_cnt(-1l);
			this.getMapper(PlnSrPlanStatisticMapper.class).doUpdatePlaned(plps);
			//调用公共函数3  断开物料母子对应关系 
			PrcMaterielRelation pmr=new PrcMaterielRelation();
			pmr.setObject_no(lpmdm.get(0).getObject_no());
			pmr.setSub_object_no(lpmd.get(0).getObject_no());
			this.getBean(MesApiServiceImpl.class).doBuildRelationRemove(pmr);
			
			// 厂内合同子卷生产完成时,清空库存信息订单信息
//			boolean flag_Ct = this.getBean(MesApiDesignInfoImpl.class).getContractType(lpmd.get(0).getProd_order_no());
//			boolean flag_Mf = this.getBean(MesApiDesignInfoImpl.class).matIsFinish(pmd.getObject_no());
//			if (flag_Mf && flag_Ct) {
//				YdmProductDetail ydm = new YdmProductDetail();
//				ydm.setCoilno(pmd.getObject_no());
//				ydm.setProdorderno(lpmd.get(0).getProd_order_no());
//				ydm.setOrderno(lpmd.get(0).getOrder_no());
//				ydm.setObject_no_vt(pmd.getObject_no_vt());
//				this.getMapper(YdmProductDetailMapper.class).update(ydm);;
//			}
			// 删除物料附加信息
			this.getMapper(PrcMaterielExtinfoMapper.class).delete(pmd.getObject_no());
			
			// 判断虚拟子卷信息，是否是额外的成品(ORD_FLG = 2), 如果是，则需释放虚拟子堆垛，调用接口:纵剪——虚拟子堆垛 释放接口
			if ("2".equals(lpmd.get(0).getOrd_flg())) {
				SkinrollingFinsh_Release(pmd);
			}
			
		}
		
		/**
		 * 平整完成
		 * 虚拟子卷 释放接口
		 * @param object_no_vt 虚拟子卷号
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_Release(PlnMaterielDetail pmd) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			// 工序点信息
			List<ComBaseInfo> info = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, "sort_code = '2002' AND validflag = '1'", null, 0, 0);
			ProcessPoint processpoint = new ProcessPoint(info);
			//读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟子卷信息
			PlnMaterielDetail pmdq=new PlnMaterielDetail();
			pmdq.setObject_no_vt(pmd.getObject_no_vt());
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).find(pmdq, 0, 0);
			if(lpmd==null||lpmd.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
			
			// ------------------------2018-06-19-------------------------
			// 判断虚拟子卷信息，是否是额外的成品(ORD_FLG = 2), 如果不成立， 则报错退出，“虚拟物料[xxx]不是额外成品，不能释放！”
			if (!"2".equals(lpmd.get(0).getOrd_flg())) {
				throw new Exception("虚拟物料["+pmd.getObject_no_vt()+"]不是额外成品，不能释放！");
			}
			// 判断虚拟子卷是否是平整的虚拟成品
			if(!processpoint.smooth_code.equals(lpmd.get(0).getPrev_dsn_process_code())){
				throw new Exception("虚拟子卷["+pmd.getObject_no_vt()+"]不是平整设计成品，无法释放！");
			}
//			PlnMaterielDetail pmdqd=new PlnMaterielDetail();
//			pmdqd.setMat_object_no_vt(pmd.getObject_no_vt());
//			int count = this.getMapper(PlnMaterielDetailMapper.class).count(pmdqd);
//			if (count>0) {
//				throw new Exception("虚拟子卷["+pmd.getObject_no_vt()+"]作为后一工序的原料，已经挂单，无法释放！");
//			}
			PlnMaterielDetail pmdqd=new PlnMaterielDetail();
			pmdqd.setMat_object_no_vt(pmd.getObject_no_vt());
			List<PlnMaterielDetail> child = this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "mat_object_no_vt = '"+pmd.getObject_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0, 0);
//			int count = this.getMapper(PlnMaterielDetailMapper.class).count(pmdqd);
			if (Utils.isNotEmpty(child)) {
				// 删除子卷
				this.getBean(MesApiDesignInfoImpl.class).deleteChildTrue(child);
//				throw new Exception("虚拟子卷["+pmd.getObject_no_vt()+"]作为后一工序的原料，已经挂单，无法释放！");
			}
			//------------------------------------------------------------
			
			// 判断虚拟子卷是否是其他物料母卷: IF(NEXT_DSN_PROCESS_CODE == NULL)
			// if(lpmd.get(0).getNext_dsn_process_code()==null||"".equals(lpmd.get(0).getNext_dsn_process_code())){
			
				//新增虚拟物料履历表【PRC_MATERIEL_VT_PROCESS】记录，状态标记为“0PSRXX”
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSRXX",userName,new Date());
				
				//将虚拟物料信息 从虚拟物料表【PLN_MATERIEL_DETAIL】移动到 无效虚拟物料表【PLN_MATERIEL_INVALID】.
				//(两个表结构一样，移动后，保持serial_no不变，删除原表记录)
				PlnMaterielInvalid pmi=new PlnMaterielInvalid();
				List<PlnMaterielInvalid> pmin = this.getMapper(PlnMaterielInvalidMapper.class).findWithCondition(null, " serial_no = '"+lpmd.get(0).getSerial_no()+"' or object_no_vt = '"+lpmd.get(0).getObject_no_vt()+"'", "", 0, 0);
				if (pmin !=null && pmin.size()>0) {
					this.getMapper(PlnMaterielInvalidMapper.class).delete(lpmd.get(0).getSerial_no());
				}
				BeanUtils.copyProperties(lpmd.get(0), pmi);
				this.getMapper(PlnMaterielInvalidMapper.class).insert(pmi);
				this.getMapper(PlnMaterielDetailMapper.class).doDeleteId(lpmd.get(0));
				
		}
		
		/**
		 * 平整完成
		 * 虚拟母卷统计
		 * @param object_no_vt 虚拟母卷号
		 * @param object_no 实物母卷号
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_Sum(PlnMaterielDetail pmd) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			Date createTime = new Date();
			String plan_no = "";
			// 工序点信息
			List<ComBaseInfo> infoi = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null,
					"sort_code = '2002' AND validflag = '1'", null, 0, 0);
			ProcessPoint processpoint = new ProcessPoint(infoi);
			
			// 修改物料附加信息
			this.getBean(MesApiDesignInfoImpl.class).updateMExInfo(pmd.getObject_no(), processpoint.smooth_code,true);
			
			//读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息
			PlnMaterielDetail pmdq=new PlnMaterielDetail();
			pmdq.setObject_no_vt(pmd.getObject_no_vt());
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(pmdq, "nvl(invalid_flg,0) <> 1", null, 0, 1);
			if(lpmd==null||lpmd.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
			if(lpmd.get(0).getObject_no()==null||"".equals(lpmd.get(0).getObject_no())){
				throw new Exception("计划母卷["+pmd.getObject_no_vt()+"]未上料, 不能平整！");
			}
			plan_no = lpmd.get(0).getSr_plan_no();
			// 新增母卷物料跟踪：调用 新增实物物料状态跟踪
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setPrc_code("5BSR10");//新增的状态
			pmp.setCreate_time(createTime);
			pmp.setObject_no(pmd.getObject_no());
			pmp.setCreate_man_name(userName);
			pmp.setRemark("");
			// 新增物料跟踪：
			this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			// 判断虚拟母卷对应的虚拟子卷是否 挂料完成
			List<PlnMaterielDetail> lpmdc=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "mat_object_no_vt = '"+lpmd.get(0).getObject_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0,0);
			int isum=0;
			if (lpmdc != null && lpmdc.size() > 0) {
				for (PlnMaterielDetail pmdcc : lpmdc) {
					PlnMaterielDetail pmdcu = new PlnMaterielDetail();
					pmdcu.setSerial_no(pmdcc.getSerial_no());
					pmdcu.setIs_finished("1");
					this.getMapper(PlnMaterielDetailMapper.class).update(pmdcu);
					if (pmdcc.getObject_no() == null || "".equals(pmdcc.getObject_no())) {
						// 统计 (OBJECT_NO=NULL的子卷数量)、虚拟子卷总数量
						isum += 1;
					}
				}
				//如果（OBJECT_NO为NULL的数量==子卷总数
				if(isum==lpmdc.size()){
					//读取物料关系表【PRC_MATERIEL_RELATION】(where OBJECT_NO=实物母卷号 AND RELATION_TYPE=3)的记录
					PrcMaterielRelation pmr=new PrcMaterielRelation();
					pmr.setObject_no(lpmd.get(0).getObject_no());
					pmr.setRelation_type(2);
					List<PrcMaterielRelation> lpmr=this.getMapper(PrcMaterielRelationMapper.class).find(pmr, 0, 0);
					if(lpmr==null||lpmr.size()==0){
						//如果 关系表记录为空（说明实物卷未切割，计划未执行）,直接报错退出“母卷xxxx尚未进行切割操作！”；
						throw new Exception("母卷["+pmr.getObject_no()+"]尚未进行切割操作！");
					}else{
						//修改虚拟物料表的母卷记录：OBJECT_NO=NULL;  NEED_EXTOPT=1;  Sr_plan_no=NULL
						PlnMaterielDetail pmdu=new PlnMaterielDetail();
						pmdu.setObject_no_vt(lpmd.get(0).getObject_no_vt());
						pmdu.setNeed_extopt("1");
						this.getMapper(PlnMaterielDetailMapper.class).doUpdateSkinrollingFinshSum(pmdu);
					}
				}else {
					// 挂单的时候一起修改了
//					this.getBean(MesApiDesignInfoImpl.class).updatePlnStatic(lpmd.get(0).getProd_order_no(), lpmd.get(0).getOrder_no(), lpmd.get(0).getNext_dsn_process_code(), lpmd.get(0).getSingle_weight(), true);
				}
			}
			
			//修改虚拟物料表OPT_TIMES+=1; STATUS= 0PSR10； IS_NEXT_FINISHED=1(2018-06-19)（同步修改S1的[OPT_TIMES]、[STATUS]
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setSerial_no(lpmd.get(0).getSerial_no());
			pmdu.setIs_next_finished("1");
			pmdu.setOpt_times((lpmd.get(0).getOpt_times()==null?0:lpmd.get(0).getOpt_times())+1);
			pmdu.setStatus("0PSR10");
			this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
			
			//新增虚拟物料履历表【PRC_MATERIEL_VT_PROCESS】记录
			//-------------2018-03-21-------这个地方的Sr_plan_no有点特殊的地方，所以没有直接调用InsertMatVtProc接口------------
			PlnMaterielDetail pmdcq=new PlnMaterielDetail();
			pmdcq.setObject_no_vt(lpmd.get(0).getObject_no_vt());
			List<PlnMaterielDetail> lpmdq = this.getMapper(PlnMaterielDetailMapper.class).find(pmdcq, 0, 0);
			//写虚拟物料履历，状态填:		子卷																			
			PrcMaterielVtProcess pmvpc=new PrcMaterielVtProcess();
			
			// 20191001
			long serial_no = 0l;
			List<PrcMaterielVtProcess> pmpQ = this.getMapper(PrcMaterielVtProcessMapper.class).findWithCondition(null, "", "serial_no desc", 0, 1);
			if (Utils.isNotEmpty(pmpQ)) {
				serial_no = pmpQ.get(0).getSerial_no()+1;
			}
			pmvpc.setSerial_no(serial_no);
			// end
			
			pmvpc.setObject_no_vt(lpmdq.get(0).getObject_no_vt());
			pmvpc.setObject_no(lpmdq.get(0).getObject_no());
			pmvpc.setObject_type(lpmdq.get(0).getObject_type());
			pmvpc.setPline_code(lpmdq.get(0).getPline_code());
			pmvpc.setStatus("0PSR10");
			pmvpc.setOrder_no(lpmdq.get(0).getOrder_no());
			pmvpc.setProd_order_no(lpmdq.get(0).getProd_order_no());
			pmvpc.setLw_design_no(lpmdq.get(0).getLw_design_no());
			pmvpc.setCl_design_no(lpmdq.get(0).getCl_design_no());
			pmvpc.setRl_design_no(lpmdq.get(0).getRl_design_no());
			pmvpc.setAn_design_no(lpmdq.get(0).getAn_design_no());
			pmvpc.setLw_plan_no(lpmdq.get(0).getLw_plan_no());
			pmvpc.setCl_plan_no(lpmdq.get(0).getCl_plan_no());
			pmvpc.setRl_plan_no(lpmdq.get(0).getRl_plan_no());
			pmvpc.setSr_plan_no(lpmdq.get(0).getSr_plan_no());
			pmvpc.setBatch_no(lpmdq.get(0).getBatch_no());
			pmvpc.setCreate_man_name(userName);
			pmvpc.setCreate_time(createTime);
			this.getMapper(PrcMaterielVtProcessMapper.class).doAdd(pmvpc);
			//----------------------------------------------------------------------
			
			//修改虚拟物料对应的 平整计划进程表
			//MesCrSlittingD mcsd=this.getMapper(MesCrSlittingDMapper.class).findById(lpmd.get(0).getObject_no());
			PlnSrPlanStatistic plps=new PlnSrPlanStatistic();
			plps.setMat_sr(lpmd.get(0).getSingle_weight());//mcsd.getCoil_act_wgt());
			plps.setMat_sr_cnt(1l);
			plps.setSr_plan_no(plan_no);
			this.getMapper(PlnSrPlanStatisticMapper.class).doUpdatePlaned(plps);
			
			//平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】：STATUS= 5BSR10
			PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
			plpd.setSr_plan_no(plan_no);
			plpd.setStatus("5BSR10");
			plpd.setObject_no_vt(pmd.getObject_no_vt());		
			this.getMapper(PlnSkinrollingPlanDetailMapper.class).doUpdateWSum(plpd);
			
			//平整计划主表【PLN_LENGTHWISE_PLAN】：调用公共函数1（平整计划号）;
			doUpdatePlnSkinrollingPlan(plan_no);
			
			// 将未挂单的子卷 置为无效
//			List<PlnMaterielDetail> child = this.getMapper(
//					PlnMaterielDetailMapper.class).findWithCondition(
//					null,
//					"mat_object_no_vt = '" + lpmd.get(0).getObject_no_vt()
//							+ "' and nvl(invalid_flg,0) =0 and object_no is null",
//					"", 0, 0);
//			if (Utils.isNotEmpty(child)) {
//				this.getBean(MesApiDesignInfoImpl.class).deleteChild(child);
//			}
		}
		
		/**
		 * 撤销平整
		 * 撤销虚拟母卷统计
		 * @param object_no_vt 虚拟母卷号
		 * @param object_no 实物母卷号
		 * @throws Exception 
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void SkinrollingFinsh_SumCancel(PlnMaterielDetail pmd) throws Exception{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			//读取虚拟物料表【PLN_MATERIEL_DETAIL】，获取虚拟母卷信息
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no_vt = '"+pmd.getObject_no_vt()+"' and nvl(invalid_flg,0) = '0'", null, 0, 1);
			if(lpmd==null||lpmd.size()==0){
				throw new Exception("虚拟物料无["+pmd.getObject_no_vt()+"]卷!");
			}
		
			// 工序点信息
						List<ComBaseInfo> infoi = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null,
								"sort_code = '2002' AND validflag = '1'", null, 0, 0);
						ProcessPoint processpoint = new ProcessPoint(infoi);
						
						// 修改物料附加信息
						this.getBean(MesApiDesignInfoImpl.class).updateMExInfo(pmd.getObject_no(), processpoint.smooth_code,false);
						
			// 将无效的虚拟物料子卷恢复
//			List<PlnMaterielDetail> child = this.getMapper(
//					PlnMaterielDetailMapper.class).findWithCondition(
//					null,
//					"mat_object_no_vt = '" + lpmd.get(0).getObject_no_vt()
//							+ "' and invalid_flg = '1'", "", 0, 0);
//			if (Utils.isNotEmpty(child)) {
//				this.getBean(MesApiDesignInfoImpl.class).recoveryChild(child);
//			}
			
			//读取虚拟物料履历表【PRC_MATERIEL_VT_PROCESS】，获取虚拟母卷最后一条
			PrcMaterielVtProcess pmvp=new PrcMaterielVtProcess();
			pmvp.setStatus("0PSR10");
			pmvp.setObject_no_vt(lpmd.get(0).getObject_no_vt());
			List<PrcMaterielVtProcess> lpmvp=this.getMapper(PrcMaterielVtProcessMapper.class).findWithCondition(pmvp, "", " create_time desc ", 0, 1);
			
			//循环 (MAT_OBJECT_NO_VT=虚拟母卷号) 的所有虚拟子卷记录
			PlnMaterielDetail pmdc=new PlnMaterielDetail();
			pmdc.setMat_object_no_vt(lpmd.get(0).getObject_no_vt());
			List<PlnMaterielDetail> lpmdc=this.getMapper(PlnMaterielDetailMapper.class).find(pmdc, 0, 0);
			for(PlnMaterielDetail pmdcc:lpmdc){
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				pmdu.setSerial_no(pmdcc.getSerial_no());
				pmdu.setIs_finished("0");
				//修改虚拟物料表【PLN_MATERIEL_DETAIL】：IS_FINISHED=0;
				this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
			}
			
			//修改虚拟物料表_母卷信息
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setSerial_no(lpmd.get(0).getSerial_no());
			pmdu.setNeed_extopt("0");
			pmdu.setIs_next_finished("0");
			pmdu.setSr_plan_no(lpmvp.get(0).getSr_plan_no());
			pmdu.setStatus_desc("0PSR03");
			
			if(lpmd.get(0).getObject_no()==null||"".equals(lpmd.get(0).getObject_no())){
				pmdu.setObject_no(lpmvp.get(0).getObject_no());
			}
			// 2018-06-23 opt_Times 之前一直没减,判断条件不对
			if(lpmd.get(0).getOpt_times()!=null){
				pmdu.setOpt_times(lpmd.get(0).getOpt_times()-1);
			}
			this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
			
			//新增虚拟物料履历表【PRC_MATERIEL_VT_PROCESS】记录
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(lpmd.get(0).getObject_no_vt(),"0PSR03",userName,new Date());
			
			//修改虚拟物料对应的 平整计划进程表
			PlnSrPlanStatistic plps=new PlnSrPlanStatistic();
			plps.setMat_sr(-lpmd.get(0).getSingle_weight());
			plps.setMat_sr_cnt(-1l);
			plps.setSr_plan_no(lpmd.get(0).getSr_plan_no());
			this.getMapper(PlnSrPlanStatisticMapper.class).doUpdatePlaned(plps);
			
			//回退实物母卷的物料跟踪 
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(lpmd.get(0).getObject_no());
			pmp.setPrc_code("5BSR10");
			pmp.setRemark("撤销母卷平整");
			pmp.setKeep_sametime_status(0);
			//防止物料号为null
			if (StringUtils.isEmpty(pmp.getObject_no())) {
				String condition = " and object_no_vt = '"+lpmd.get(0).getObject_no_vt()+"' and object_no is not null";
				List<PlnMatReplaceLog> pmrList = this.getMapper(PlnMatReplaceLogMapper.class).findWithCondition(null, condition, null, 0, 0);
				if (CollectionUtils.isNotEmpty(pmrList)) {
					pmp.setObject_no(pmrList.get(0).getObject_no());
				}
			}
			
			ResultModel rm = this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp);
			if(!"0".equals(rm.getState())){
				//throw new Exception("平整完成后，母卷状态已发生改变，不能撤销！");
			}
			
			//平整计划明细表【PLN_LENGTHWISE_PLAN_DETAIL】：STATUS= 5BSR03
			PlnSkinrollingPlanDetail plpd=new PlnSkinrollingPlanDetail();
			plpd.setSr_plan_no(lpmd.get(0).getSr_plan_no());
			plpd.setStatus("5BSR03");
			plpd.setObject_no_vt(pmd.getObject_no_vt());		
			this.getMapper(PlnSkinrollingPlanDetailMapper.class).doUpdateWSum(plpd);
			
			//平整计划主表【PLN_LENGTHWISE_PLAN】：调用公共函数1（平整计划号）;
			doUpdatePlnSkinrollingPlan(lpmd.get(0).getSr_plan_no());
			// ----------------------------2018-06-19-------------------------------
			// ---------------2018-10-16 挂单的时候一起修改
//			this.getBean(MesApiDesignInfoImpl.class).updatePlnStatic(lpmd.get(0).getProd_order_no(), lpmd.get(0).getOrder_no(), lpmd.get(0).getNext_dsn_process_code(), lpmd.get(0).getSingle_weight(), false);
		}
		
		/**
		 * 函数_通过加工计划产生设计
		 * @param pro_order_no 生产订单号
		 * @throws Exception 
		 * */
		public PlnSkinrollingDsnMain ProcessingPlanDsn(String pro_order_no) throws Exception{
			PlnSkinrollingDsnMain sr_design_no=null;
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			PlnProdorder pp=new PlnProdorder();
			pp.setProd_order_no(pro_order_no);
			List<PlnProdorder> lpp=this.getMapper(PlnProdorderMapper.class).find(pp, 0, 1);
			//根据加工计划号  读取生产订单对应的生产加工表[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()+"]对应的生产加工信息");
			}
			
			//保存设计主表数据：  平整设计号：DSR+YYMMDD+6流水号
			String sDate=new SimpleDateFormat("yyMMdd").format(new Date());
			PlnSkinrollingDsnMain pldm=new PlnSkinrollingDsnMain();
			List<PlnSkinrollingDsnMain> lpdmNo=this.getMapper(PlnSkinrollingDsnMainMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
			pldm.setSr_design_no("DSR"+sDate+String.format("%06d", 1));
			if(lpdmNo!=null&&lpdmNo.size()>0){
				pldm.setSerial_no(lpdmNo.get(0).getSerial_no()+1);
				if(sDate.equals(lpdmNo.get(0).getSr_design_no().substring(3,9))){
					pldm.setSr_design_no("DSR"+sDate+String.format("%06d", Integer.parseInt(lpdmNo.get(0).getSr_design_no().substring(9,15))+1 ));
				}
			}
			
			// 订单信息
			pldm.setProd_order_no(lpp.get(0).getProd_order_no());
			pldm.setOrder_no(lpp.get(0).getOrder_no());
			pldm.setPline_code(lpp.get(0).getPline_code());
			pldm.setSteel_code(lpp.get(0).getSteel_code());
			
			String isMulti="0";//是否拼料 根据订单数判断，多个订单填1，否则填0
			pldm.setIs_multi(isMulti);
			
			// 只有一条加工计划记录情况下
			if(lppp.size()==1){
				pldm.setThick(lppp.get(0).getThick());
				pldm.setWidth(lppp.get(0).getWidth());
				pldm.setLength(lppp.get(0).getLength());
				pldm.setDiameter_inn(lppp.get(0).getDiameter_inn());
				pldm.setDiameter_out(lppp.get(0).getDiameter_out());
				pldm.setSingle_weight(lppp.get(0).getEstimate_weight());
			}
			
			// 加工计划分切方式
//			pldm.setCut_style(lppp.get(0).getCut_style());
//			
//			pldm.setCrosscut_style(lppp.get(0).getCrosscut_style());
//			pldm.setFixed_length_type_no(lpp.get(0).getFixed_length_type_no());
//			pldm.setCut_edge_type_no(lpp.get(0).getCut_edge_type_no());
			pldm.setDeliver_date(lpp.get(0).getDeliver_date());
			
			pldm.setDesign_quantity(1);
//			pldm.setDesign_weight(ypd.getFactweight());
			
			pldm.setDeliver_date_earliest(lpp.get(0).getDeliver_date());
			pldm.setDeliver_date_latest(lpp.get(0).getDeliver_date());
			pldm.setCreate_man_name(userName);
			pldm.setCreate_time(new Date());
			pldm.setIs_finished(0L);
			pldm.setReserve_1(lppp.get(0).getProduce_no());
			this.getMapper(PlnSkinrollingDsnMainMapper.class).insert(pldm);
			sr_design_no=pldm;
			
			//倒叙取一条拼料信息
			List<PlnSkinrollingDsnMultinfo> lpdmM=this.getMapper(PlnSkinrollingDsnMultinfoMapper.class).findWithCondition(null, "", " serial_no desc ", 0,1);
			long DsnNo=1;
			if(lpdmM!=null&&lpdmM.size()>0){
				DsnNo=lpdmM.get(0).getSerial_no()+1;
			}
			
						//-----------------写拼料-----------------------------
						PlnSkinrollingDsnMultinfo pldmc=new PlnSkinrollingDsnMultinfo();
						pldmc.setSerial_no(DsnNo++);
						pldmc.setSr_design_no(pldm.getSr_design_no());
//						pldmc.setAn_seq(1);
//						pldmc.setSeg_seq(1);
						pldmc.setOrder_no(lpp.get(0).getOrder_no());
						pldmc.setProd_order_no(lpp.get(0).getProd_order_no());
						pldmc.setThick(lpp.get(0).getThick());
						pldmc.setWidth(lpp.get(0).getWidth());
						pldmc.setLength(lpp.get(0).getLength());
						pldmc.setDiameter_inn(lpp.get(0).getDiameter_inn());
						pldmc.setDiameter_out(lpp.get(0).getDiameter_out());
						pldmc.setWeight(pldm.getSingle_weight());
//						pldmc.setFixed_length_type_no(lpp.get(0).getFixed_length_type_no());
//						pldmc.setCut_edge_type_no(lpp.get(0).getCut_edge_type_no());
						pldmc.setSub_seq(1);
						this.getMapper(PlnSkinrollingDsnMultinfoMapper.class).insert(pldmc);
			
			return sr_design_no;
		}
		
		
		/**
		 * 公共函数1 修改平整计划主表状态
		 * @param 平整计划号
		 * @throws Exception
		 */
		@Transactional(value="tm", rollbackFor=Exception.class)
		public void doUpdatePlnSkinrollingPlan(String anPlanNo) throws Exception {
			PlnSkinrollingPlan plp = new PlnSkinrollingPlan();
			plp.setSr_plan_no(anPlanNo);
			String MIN_STATUS = "";
			boolean flag = false;
			PlnSkinrollingPlanDetail plpd = new PlnSkinrollingPlanDetail();
			plpd.setSr_plan_no(anPlanNo);
			List<PlnSkinrollingPlanDetail> lplpd = this.getMapper(PlnSkinrollingPlanDetailMapper.class)
					.findWithCondition(plpd, "status <> '0PSRXX'", "status asc", 0, 0);
			if (lplpd == null || lplpd.size() == 0) {
				throw new Exception("未找到平整计划明细数据!");
			}
			MIN_STATUS = lplpd.get(0).getStatus();

			// 计划状态						// 实物状态		
			// 0PSR01平整计划已编制			// 5BSR01平整计划已编制		
			// 0PSR02平整计划已下发			// 5BSR02平整计划已下发		
			// 0PSR03开始平整作业				// 5BSR03开始平整作业		
			// 0PAN79平整剪废				// 5BAN05正在平整作业		
			// 0PSR10平整作业完成				// 5BAN79平整剪废		
			// 0PSRXX平整计划已失效			// 5BSR10平整作业完成		
											// 5BAN83等待平整判定		
											// 5BAN90平整判定完成		
			
			// 如果明细记录中有卷开始作业,主表状态改为开始作业
			for (PlnSkinrollingPlanDetail plnL : lplpd) {
				if ("5BSR03".equals(plnL.getStatus())) {
					flag = true;
					break;
				}
			}
			if (flag) {
				plp.setStatus("0PSR03");
			} else if ("5BSR01".equals(MIN_STATUS)) {
				plp.setStatus("0PSR01");
			} else if (MIN_STATUS.compareTo("5BSR02") <= 0) {
				plp.setStatus("0PSR02");
			} else if (MIN_STATUS.compareTo("5BSR03") <= 0) {
				plp.setStatus("0PSR03");
			} else if (MIN_STATUS.compareTo("5BSR10") <= 0) {
				plp.setStatus("0PSR10");
			}
			this.getMapper(PlnSkinrollingPlanMapper.class).doUpdate(plp);
		}

	}

