/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-25
 * 描述            ：轧制拼料信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Rolling.Design.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Mes.impl.MesApiDesignInfoImpl;
import market.Api.Mes.impl.MesApiRollingPlanImpl;
import market.Api.Mes.impl.MesApiServiceImpl;
import market.Api.Mes.impl.Utils;
import market.Api.Mes.service.MesApiRollingPlanService;
import market.Api.Mes.service.MesApiService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Lengthwise.Design.mapper.PlnDsnOrderInfoMapper;
import market.Mes.Lengthwise.Design.vo.PlnDsnOrderInfo;
import market.Mes.Lengthwise.Plan.mapper.PlnMaterielDetailMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.MaterielTrack.vo.PrcMaterielProcess;
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.Rolling.Design.mapper.PlnRlPlanStatisticMapper;
import market.Mes.Rolling.Design.mapper.PlnRollingDsnMainMapper;
import market.Mes.Rolling.Design.service.PlnRollingDsnMultinfoService;
import market.Mes.Rolling.Design.vo.PlnRlPlanStatistic;
import market.Mes.Rolling.Design.vo.PlnRollingDsnMain;
import market.Mes.Rolling.Design.vo.PlnRollingDsnMultinfo;
import market.Mes.Rolling.Plan.mapper.PlnRollingPlanDetailMapper;
import market.Mes.Rolling.Plan.mapper.PlnRollingPlanMapper;
import market.Mes.Rolling.Plan.vo.PlnRollingPlan;
import market.Mes.Rolling.Plan.vo.PlnRollingPlanDetail;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
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="PlnRollingDsnMultinfoService")
public class PlnRollingDsnMultinfoServiceImpl extends BaseServiceImpl<PlnRollingDsnMultinfo,Object> implements PlnRollingDsnMultinfoService {

	/**
	 * 选定物料加入轧制计划
	 * 传入PlnMaterielDetail object_vt_no 数组Rl_plan_no数组
	 * memo1 计划轧制时间
	 * memo3 = 1 :母卷批量编制到同一个计划 ; 默认为空:一个卷一个计划。
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doCheckPlanAdd(CommonPage<PlnMaterielDetail> oci) throws Exception{
		ResultModel rm=new ResultModel();		
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<PlnMaterielDetail> pmdList=oci.getObjectList();
		String tgMsg = null;
		// 获取主计划号和序列号
		PlnRollingPlan plpl = new PlnRollingPlan();
		String Rl_plan_no = "";
		// 循环处理传入的物料和计划号数组
		for (PlnMaterielDetail plnmd : pmdList) {
			if (Utils.isEmpty(plnmd.getMachine_code())) {
				plnmd.setMachine_code("M0008");
			}
			
			// 直接在这里获取库存卷是否已经有综判：不存在则直接跳过，并记录哪个卷跳过了20231106刘侣
			List<PlnMaterielDetail> lpmdq=this.getMapper(PlnMaterielDetailMapper.class).findWithCondition(null, "object_no_vt = '"+plnmd.getObject_no_vt()+"' and nvl(invalid_flg,0) =0", null, 0, 1);
			YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(lpmdq.get(0).getObject_no());
			if (StringUtils.isEmpty(ypd.getJudgeresult())) {
				// throw new Exception("物料【"+ypd.getCoilno()+"】还未进行综合判定，不能编制计划");
				tgMsg += (ypd.getCoilno() + "; ");
				continue;
			}
			// end
			
			// 判断是否传入计划号
			if (plnmd.getRl_plan_no()!=null && !"".equals(plnmd.getRl_plan_no())) {
				plpl.setRl_plan_no(plnmd.getRl_plan_no());
				plpl.setPlan_seq(plnmd.getPlan_seq());
				List<PlnRollingPlan> lplp=this.getMapper(PlnRollingPlanMapper.class).find(plpl, 0, 1);
				if(lplp==null||lplp.size()==0){
					rm.setState("500");
					rm.setMsgInfo("轧制计划["+plnmd.getRl_plan_no()+"]信息不存在!");
					return rm;
				}
				if(lplp.get(0).getStatus().trim().compareTo("0PRL01")>0){
					rm.setState("500");
					rm.setMsgInfo("轧制计划["+plnmd.getRl_plan_no()+"]已下发，不能加入!");
					return rm;
				}
				// 判断虚拟物料信息
				ResultModel rmValid = doPlanJudgeByObject_no_vt(plnmd.getObject_no_vt());
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				Rl_plan_no = plnmd.getRl_plan_no();
				// 新增轧制计划明细表记录
				doAddDetail(plnmd, userName, plnmd.getMachine_code(),oci.getMemo6(), oci.getMemo7());
			}else {	//第一次进行编制计划
				// 判断虚拟物料信息
				ResultModel rmValid = doPlanJudgeByObject_no_vt(plnmd.getObject_no_vt());
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				if ("1".equals(oci.getMemo3())) {
					PlnRollingPlan pp = getPlanNo();
					Rl_plan_no = pp.getRl_plan_no();
					List<PlnRollingPlan> plnleng = this.getMapper(PlnRollingPlanMapper.class).findWithCondition(null, "rl_plan_no = '"+Rl_plan_no+"'", "", 0, 1);
					if (plnleng == null || plnleng.size()==0) {
						pp.setPlan_seq(pp.getSerial_no());
						pp.setStatus("0PRL01");
						pp.setPline_code(pmdList.get(0).getPline_code());
						pp.setMachine_code(plnmd.getMachine_code());

						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						Date date = sdf.parse(oci.getMemo1());
						pp.setStart_time(date);
						pp.setCreate_man_name(userName);
						pp.setCreate_time(new Date());
						this.getMapper(PlnRollingPlanMapper.class).insert(pp);
					} else {
						// 第一次计划编制的时候就已经存在轧制计划号：抛出异常；20230104 刘侣
						throw new Exception("轧机计划号：" + pp.getRl_plan_no() + "已存在，请重新操作一次！！！");
					}
					// 新增轧制计划明细表记录
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setRl_plan_no(pp.getRl_plan_no());
					plnmh.setObject_no_vt(plnmd.getObject_no_vt());
					doAddDetail(plnmh, userName, plnmd.getMachine_code(),oci.getMemo6(), oci.getMemo7());

				} else {
					// 新增轧制计划主记录
					PlnRollingPlan pp = getPlanNo();
					Rl_plan_no = pp.getRl_plan_no();
					pp.setPlan_seq(pp.getSerial_no());
					pp.setStatus("0PRL01");
					pp.setPline_code(pmdList.get(0).getPline_code());
					pp.setMachine_code(plnmd.getMachine_code());

					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date date = sdf.parse(oci.getMemo1());
					pp.setStart_time(date);
					pp.setCreate_man_name(userName);
					pp.setCreate_time(new Date());
					this.getMapper(PlnRollingPlanMapper.class).insert(pp);

					// 新增轧制计划明细表
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setRl_plan_no(pp.getRl_plan_no());
					plnmh.setObject_no_vt(plnmd.getObject_no_vt());
					doAddDetail(plnmh, userName, plnmd.getMachine_code(),oci.getMemo6(), oci.getMemo7());
				}
			}
			// 修改其他信息
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(plnmd.getObject_no_vt());
			pmdu.setRl_plan_no(Rl_plan_no);
			doAddChildMethod(pmdu);

			//写虚拟物料履历，状态填:0PRL01 母卷																		
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(plnmd.getObject_no_vt(), "0PRL01", userName, new Date());

		}		
		rm.setState("200");
		rm.setData(Rl_plan_no);
		if (tgMsg != null) {
			rm.setMsgInfo("卷: " + tgMsg + " 没综判未成功，其他计划编制成功");
		}
		return rm;
	}

	/**
	 * 获取计划号和序列号
	 * @return 
	 */
	public  PlnRollingPlan getPlanNo(){
		String Dt=new SimpleDateFormat("yyMMdd").format(new Date());
		//轧制计划表： 轧制计划号(RL+15位流水); 其他字段参考																					
		PlnRollingPlan pp=new PlnRollingPlan();
		//倒叙找到最后一条数据
		List<PlnRollingPlan> lpp=this.getMapper(PlnRollingPlanMapper.class).findWithCondition(null, "", "rl_plan_no desc, serial_no desc ", 0, 1);
		if(lpp==null||lpp.size()==0){
			pp.setSerial_no(1l);
			pp.setRl_plan_no("RL"+Dt+String.format("%03d", 1)); //Rl180531001 
		}else{
			List<PlnRollingPlan> prpList=this.getMapper(PlnRollingPlanMapper.class).findWithCondition(null, "", "serial_no desc ", 0, 1);
			pp.setSerial_no(prpList.get(0).getSerial_no()+1);
			if(Dt.equals(lpp.get(0).getRl_plan_no().substring(2,8))){
				//截取后3位  RL200623 + 002
				pp.setRl_plan_no("RL"+Dt+String.format("%03d", Integer.parseInt(lpp.get(0).getRl_plan_no().substring(8,11))+1));
			}else{
				pp.setRl_plan_no("RL"+Dt+String.format("%03d", 1));
			}
		}
		return pp;
	}
	
	/**
	 * 轧制计划明细表：对选中的母卷增加明细记录
	 * 修改虚拟物料信息  写虚拟履历
	 * @param plnmd 虚拟物料号、计划号
	 * @param userName
	 */
	public void doAddDetail(PlnMaterielDetail plnmd, String userName,String memo2, String memo6, String memo7){

		PlnRollingPlan plpl = new PlnRollingPlan();
		plpl.setRl_plan_no(plnmd.getRl_plan_no());
		List<PlnRollingPlan> lplp=this.getMapper(PlnRollingPlanMapper.class).find(plpl, 0, 1);

		PlnMaterielDetail pmd=new PlnMaterielDetail();
		pmd.setObject_no_vt(plnmd.getObject_no_vt());
		List<PlnMaterielDetail> lpmdq=this.getMapper(PlnMaterielDetailMapper.class).find(pmd, 0, 0);
		pmd = lpmdq.get(0);
		List<PlnRollingPlanDetail> lppd=this.getMapper(PlnRollingPlanDetailMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
		long sNo=0;
		if(lppd==null||lppd.size()==0){
			sNo=1;
		}else{
			sNo=lppd.get(0).getSerial_no()+1;
		}
		long isqe=0;

		PlnRollingPlanDetail plpdq=new PlnRollingPlanDetail();
		plpdq.setRl_plan_no(plnmd.getRl_plan_no());
		lppd=this.getMapper(PlnRollingPlanDetailMapper.class).findWithCondition(plpdq, "", " Rl_seq desc ", 0, 1);
		if(lppd==null||lppd.size()==0){
			isqe=1;
		}else{
			isqe=lppd.get(0).getRl_seq()+1;
		}

		PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
		plpd.setSerial_no(sNo++);
		plpd.setRl_plan_no(plnmd.getRl_plan_no());
		plpd.setRl_seq(isqe++);
		plpd.setObject_no_vt(pmd.getObject_no_vt());
		plpd.setRl_design_no(pmd.getRl_design_no());
		plpd.setStatus("5BRL01");
		plpd.setObject_no(pmd.getObject_no());
		plpd.setPline_code(pmd.getPline_code());
		plpd.setMachine_code(memo2==null?"M0008":memo2);
		plpd.setSteel_code(pmd.getSteel_code());
		plpd.setMat_width(pmd.getWidth());
		plpd.setMat_thick(pmd.getThick());
		plpd.setMat_diameter_inn(pmd.getDiameter_inn());
		plpd.setMat_diameter_out(pmd.getDiameter_out());
		plpd.setMat_length(pmd.getLength());
		plpd.setThick(StringUtils.isEmpty(memo7)?null:Double.parseDouble(memo7));//目标厚度
		plpd.setMat_single_weight(pmd.getSingle_weight());
		plpd.setPlan_opt_time(lplp.get(0).getStart_time());
		plpd.setCreate_man_name(userName);
		plpd.setCreate_time(new Date());
		plpd.setSub_count(pmd.getSub_count() == null ? 1 : pmd.getSub_count());
		PlnRollingDsnMain pldm=new PlnRollingDsnMain();
		pldm.setRl_design_no(pmd.getRl_design_no());
		List<PlnRollingDsnMain> lpldm=this.getMapper(PlnRollingDsnMainMapper.class).find(pldm, 0, 1);
		plpd.setRate(lpldm.get(0).getProd_rate());
		plpd.setPlan_hand_time(memo6);//计划交工时间
		this.getMapper(PlnRollingPlanDetailMapper.class).insert(plpd);
		
		//新增操作日志
		Date now = new Date();
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		MesOprLog mol = new MesOprLog("生产管理", "轧制计划编制", "编制计划", userName, opr_time, pmd.getObject_no(), null, null, null, null);
		this.getMapper(MesOprLogMapper.class).insert(mol);

		//虚拟物料表：修改母卷的 轧制计划号[Rl_PLRLNO]字段
		PlnMaterielDetail pmdu=new PlnMaterielDetail();
		pmdu.setRl_plan_no(plnmd.getRl_plan_no());
		pmdu.setSerial_no(pmd.getSerial_no());
		pmdu.setMachine_code(memo2);
		pmdu.setStatus("0PRL01");
		this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
		
		// 修改虚拟物料子卷轧制计划号
		PlnMaterielDetail child =new PlnMaterielDetail();
		child.setRl_plan_no(plnmd.getRl_plan_no());
		child.setMat_object_no_vt(pmd.getObject_no_vt());
		child.setStatus("0PRL01");
		// child.setMachine_code(memo2);
		this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
		
		//写虚拟物料履历，状态填:0PRl01 母卷																			
		this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmd.getObject_no_vt(), "0PRL01", userName, new Date());
		// 查询子卷
		List<PlnMaterielDetail> listchild = this.getMapper(
				PlnMaterielDetailMapper.class).findWithCondition(null,
				" mat_object_no_vt = '" + pmd.getObject_no_vt() + "'", null, 0,0);
		if (Utils.isNotEmpty(listchild)) {
			for (PlnMaterielDetail pln : listchild) {
				// 写虚拟物料履历，状态填:0PRL01 子卷
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
						pln.getObject_no_vt(), "0PRL01", userName, new Date());
			}
		}
	}

	/**
	 * doAddWeb的子方法
	 * @throws Exception 
	 * */
	private void doAddChildMethod(PlnMaterielDetail pmd) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		//判断虚拟母卷对应的实物号是否挂料,如果  object_no<>null

		pmd=this.getMapper(PlnMaterielDetailMapper.class).find(pmd, 0, 1).get(0);

		if(pmd.getObject_no()!=null){
			//调用接口 新增实物物料状态跟踪	
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BRL80，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getObject_no());
			pmp.setPrc_code("5BRL01");//新增的状态
			pmp.setCreate_time(new Date());
			pmp.setCreate_man_name(userName);
			pmp.setRemark("");
			//新增物料跟踪：
			this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
		}
		double One_Weight=0;
		int One_Quantity=0;
		//根据母卷的RL_DESIGN_NO , 读取【pln_dsn_order_info】
		PlnDsnOrderInfo pdoi = new PlnDsnOrderInfo();
		pdoi.setDesign_no(pmd.getRl_design_no());
		List<PlnDsnOrderInfo> lpoi=this.getMapper(PlnDsnOrderInfoMapper.class).find(pdoi, 0, 0);
		if(lpoi!=null&&lpoi.size()>0){
			for(PlnDsnOrderInfo s2:lpoi){
				One_Weight+=(s2.getWeight_in_one()== null ? 0:s2.getWeight_in_one());
				One_Quantity+=(s2.getQuantity_in_one()== null ? 0:s2.getQuantity_in_one());

				//根据S2的 生产订单号，修改对应 生产订单进程表[PLN_PRODORDER_STATISTIC]
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(s2.getProd_order_no());
				pps.setRl_planed(s2.getWeight_in_one());
				pps.setRl_planed_cnt(s2.getQuantity_in_one());
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);

				//根据S2的 订单号，修改  订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(s2.getOrder_no());
				pos.setRl_planed(s2.getWeight_in_one());
				pos.setRl_planed_cnt(s2.getQuantity_in_one());
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateRlPlaned(pos);
			}
		}
		// 在轧制计划进程表 [PLN_RL_PLAN_STATISTIC]， 没轧制计划号记录，就创建一条主记录
		PlnRlPlanStatistic plps=new PlnRlPlanStatistic();
		plps.setRl_plan_no(pmd.getRl_plan_no());
		int icount=this.getMapper(PlnRlPlanStatisticMapper.class).count(plps);
		if(icount==0){
			List<PlnRlPlanStatistic> lplps=this.getMapper(PlnRlPlanStatisticMapper.class).findWithCondition(null, null, " to_number(serial_no) desc ", 0, 1);
			if(lplps!=null&&lplps.size()>0){
				plps.setSerial_no((Long.parseLong(lplps.get(0).getSerial_no().trim())+1)+"");
			}else{
				plps.setSerial_no("1");
			}
		}
		//修改 轧制计划进程表 [PLN_AN_PLAN_STATISTIC] 
		plps.setMat_planed(pmd.getSingle_weight());
		plps.setMat_planed_cnt(1);
		plps.setPlaned(One_Weight);
		plps.setPlaned_cnt(One_Quantity);
		if(icount==0){
			this.getMapper(PlnRlPlanStatisticMapper.class).insert(plps);
		}else{
			this.getMapper(PlnRlPlanStatisticMapper.class).doUpdatePlaned(plps);
		}
		//修改轧制计划表： QUANTITY 数量 += 1;   TOTAANEIGHT 总重量 += 虚拟物料号单重
		PlnRollingPlan plp=new PlnRollingPlan();
		plp.setRl_plan_no(pmd.getRl_plan_no());
		plp.setMat_quantity(1);
		plp.setMat_weight(pmd.getSingle_weight());
		this.getMapper(PlnRollingPlanMapper.class).doUpdatePlaned(plp);

		//调用【接口文档--公共函数SHEET--修改轧制计划主表状态
		this.getBean(MesApiRollingPlanImpl.class).doUpdatePlnRollingPlan(pmd.getRl_plan_no());
	}
	
	/**
	 * 编制计划 判断虚拟物料号
	 * @param Object_no_vt
	 * @return
	 */
	public ResultModel doPlanJudgeByObject_no_vt(String Object_no_vt) {
		ResultModel rm = new ResultModel();
		// 查询虚拟卷
		PlnMaterielDetail pmd = new PlnMaterielDetail();
		pmd.setObject_no_vt(Object_no_vt);
		List<PlnMaterielDetail> lpmdq = this.getMapper(
				PlnMaterielDetailMapper.class).find(pmd, 0, 0);

		if (lpmdq == null || lpmdq.size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]信息不存在!");
			return rm;
		}
		
		List<PlnProdorder> ppoL = this.getMapper(PlnProdorderMapper.class).findWithCondition(null, " PROD_ORDER_NO='"+lpmdq.get(0).getProd_order_no()+"'", null, 0, 0);
		if (ppoL == null || ppoL.size() <= 0) {
			rm.setState("500");
			rm.setMsgInfo("生产订单：" + ppoL.get(0).getOrder_no() + " 不存在");
			return rm;
		}
		// 验证生产订单状态是否为订单保留：
		if ("0PAA60".equals(ppoL.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+ppoL.get(0).getProd_order_no()+"]为 ‘订单保留’ 状态，不允许编制计划");
			return rm;
		}
		if ("0PAA70".equals(ppoL.get(0).getStatus())) {
			rm.setState("500");
			rm.setMsgInfo("生产订单["+ppoL.get(0).getProd_order_no()+"]为 ‘订单作废’ 状态，不允许编制计划");
			return rm;
		}
		
		if (Utils.isNotEmpty(lpmdq.get(0).getObject_no())) {
			YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(lpmdq.get(0).getObject_no());
			if (Utils.isNotEmpty(ypd) && !"0".equals(ypd.getState())) {
				rm.setState("500");
				rm.setMsgInfo("请先将物料[" + lpmdq.get(0).getObject_no() + "]入库再编制计划！");
				return rm;
			}
			
			// 计划编制增加是否综判判断：20231010刘侣
			if (StringUtils.isEmpty(ypd.getJudgeresult())) {
				rm.setState("500");
				rm.setMsgInfo("物料【"+ypd.getCoilno()+"】还未进行综合判定，不能编制计划");
				return rm;
			}
			// end
		}
		// 实际上只有一条
		for (PlnMaterielDetail pmdq : lpmdq) {
			if (!"".equals(pmdq.getRl_plan_no())
					&& pmdq.getRl_plan_no() != null) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经在计划["
						+ pmdq.getRl_plan_no() + "]内，无法再次编制！");
				return rm;
			}

			PlnRollingPlanDetail ppd = new PlnRollingPlanDetail();
			ppd.setObject_no_vt(pmdq.getObject_no_vt());
			int icount = this.getMapper(PlnRollingPlanDetailMapper.class)
					.countWithCondition(ppd, " STATUS != '0PLWXX' ");
			if (icount > 0) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经在计划["	+ pmdq.getRl_plan_no() + "]内，无法再次编制！");
				return rm;
			}
			// 若虚拟物料表的 (OPT_TIMES >=1 AND NEED_EXTOPT =0) ,则报错退出
			if (pmdq.getOpt_times() != null && pmdq.getOpt_times() >= 1
					&& "1".equals(pmdq.getNeed_extopt())) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经生产完毕，不能编制计划！");
				return rm;
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 轧制计划取消
	 * 传入轧制计划号数组
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doCancelPlanWeb(CommonPage<PlnRollingPlan> oci) throws Exception {
		ResultModel rm=new ResultModel();
		Date now = new Date();
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<PlnRollingPlan> sRlPlanNo=oci.getObjectList();

		for(PlnRollingPlan pc:sRlPlanNo){
			//循环处理对应计划中 分条明细（母卷）相关数据
			PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
			plpd.setRl_plan_no(pc.getRl_plan_no());
			List<PlnRollingPlanDetail> lplpd=this.getMapper(PlnRollingPlanDetailMapper.class).find(plpd, 0, 0);
			rm = doCheckCancelPlanChild(lplpd);
			if(!"200".equals(rm.getState())){
				throw new Exception(rm.getMsgInfo());
			}
			for (PlnRollingPlanDetail ppd : lplpd) {
				MesOprLog mol = new MesOprLog("生产管理", "轧制计划编制", "取消计划", userName, opr_time, ppd.getObject_no(), null, null, null, null);
				this.getMapper(MesOprLogMapper.class).insert(mol);
			}
		}
		return rm;
	}

	
	/**
	 * 传入 横剪计划号、虚拟母卷号
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doCheckCancelPlanWeb(CommonPage<PlnRollingPlanDetail> oci) throws Exception {
		List<PlnRollingPlanDetail> lplpd=oci.getObjectList();
		ResultModel rm=doCheckCancelPlanChild(lplpd);
		return rm;
	}
	
	/**
	 * 选定物料移除计划
	 * @throws Exception 
	 * */
	public ResultModel doCheckCancelPlanChild(List<PlnRollingPlanDetail> lplpd) throws Exception{
		ResultModel rm=new ResultModel();

		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<PlnRollingPlanDetail> lp=new ArrayList<PlnRollingPlanDetail>();
		for(PlnRollingPlanDetail plpd:lplpd){
			PlnRollingPlanDetail pq=new PlnRollingPlanDetail();
			pq.setRl_plan_no(plpd.getRl_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnRollingPlanDetail> lpq=this.getMapper(PlnRollingPlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+plpd.getObject_no_vt()+"]的轧制计划["+plpd.getRl_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BRL01".compareTo(lpq.get(0).getStatus())<0){
				rm.setState("500");
				rm.setMsgInfo("母卷["+lpq.get(0).getObject_no_vt()+"]的轧制计划已下发，无法删除！");
				return rm;
			}
			lp.add(lpq.get(0));
		}

		for(PlnRollingPlanDetail p:lp){
			//写虚拟物料履历，状态填:0PRLXX		母卷																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(p.getObject_no_vt(), "0PRLXX", userName, new Date());
			
			// 查询子卷
			List<PlnMaterielDetail> listchild = this.getMapper(
					PlnMaterielDetailMapper.class).findWithCondition(null,
					" mat_object_no_vt = '" + p.getObject_no_vt() + "'", null,
					0, 0);
			if (Utils.isNotEmpty(listchild)) {
				for (PlnMaterielDetail pln : listchild) {
					// 写虚拟物料履历，状态填:0PRL01 子卷
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PRLXX", userName,
							new Date());
				}
			}

			//修改虚拟物料表 修改各母卷 的 轧制计划号[RL_PLANNO]字段为空。																				
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(p.getObject_no_vt());
			pmdu.setRl_plan_no("");
			pmdu.setStatus("0PDC10");
			this.getMapper(PlnMaterielDetailMapper.class).doUpdateRlPlanNo(pmdu);
			
			// 修改虚拟物料子卷开平计划号
			PlnMaterielDetail child =new PlnMaterielDetail();
			child.setRl_plan_no(null);
			child.setMat_object_no_vt(p.getObject_no_vt());
			child.setStatus("0PDC10");
			// child.setMachine_code("");
			this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
			
			if(p.getObject_no()!=null){
				//调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(p.getObject_no());
				pmp.setPrc_code("5BRL01");//新增的状态
				pmp.setRemark("计划已编制撤销");
				//撤销实物物料状态跟踪函数:
				this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();
			}
			
			//删除轧制计划明细相关母卷数据
			this.getMapper(PlnRollingPlanDetailMapper.class).doDelete(p);
			//========================================================================	


			//根据虚拟母卷的【RL_DESIGN_NO】, 读取【pln_dsn_order_info】中的所有数据(DESIGN_NO= 母卷CREATE_DESIGN_NO)
			PlnMaterielDetail pmdc=new PlnMaterielDetail();
			pmdc.setObject_no_vt(p.getObject_no_vt());
			List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).find(pmdc, 0, 0);

			//准备临时变量:  One_Weight=0;   One_Quantity=0;
			double One_Weight=0;
			int One_Quantity=0;

			PlnDsnOrderInfo pdoi=new PlnDsnOrderInfo();
			pdoi.setDesign_no(lpmd.get(0).getCreate_design_no());
			List<PlnDsnOrderInfo> lpdoi=this.getMapper(PlnDsnOrderInfoMapper.class).find(pdoi, 0, 0);
			for(PlnDsnOrderInfo s2:lpdoi){
				//One_Weight+=S2.WEIGHT_IN_ONE;   One_Quantity+=S2.QUANTITY_IN_ONE;
				One_Weight+=(s2.getWeight_in_one() == null ? 0.0 : s2.getWeight_in_one());
				One_Quantity+=(s2.getQuantity_in_one() == null ? 0 : s2.getQuantity_in_one());
				//根据S2的 生产订单号，修改对应 生产订单进程表
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(s2.getProd_order_no());
				pps.setRl_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setRl_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);

				//根据S2的 订单号，修改  订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(s2.getOrder_no());
				pos.setRl_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setRl_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateRlPlaned(pos);
			}

			// 查找轧制计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 轧制计划号】的记录条数。 
			PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
			plpd.setRl_plan_no(p.getRl_plan_no());
			List<PlnRollingPlanDetail> lplpdq=this.getMapper(PlnRollingPlanDetailMapper.class).find(plpd, 0, 0);

			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 轧制计划进程表相关轧制计划记录 [PLN_ORDER_STATISTIC]
				PlnRlPlanStatistic pps=new PlnRlPlanStatistic();
				pps.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRlPlanStatisticMapper.class).doDelete(pps);

				//删除轧制计划主表数据
				PlnRollingPlan pp=new PlnRollingPlan();
				pp.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRollingPlanMapper.class).doDeleteRlPlanNo(pp);
			}else{
				//修改对应轧制计划主表【PLN_Rolling_PLAN】
				PlnRollingPlan pp=new PlnRollingPlan();
				pp.setRl_plan_no(p.getRl_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnRollingPlanMapper.class).doUpdatePlaned(pp);

				//修改轧制计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnRlPlanStatistic pps=new PlnRlPlanStatistic();
				pps.setMat_planed(lpmd.get(0).getSingle_weight()==null?0:(-lpmd.get(0).getSingle_weight()));
				pps.setMat_planed_cnt(-1);
				pps.setPlaned(-One_Weight);
				pps.setPlaned_cnt(-One_Quantity);
				pps.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRlPlanStatisticMapper.class).doUpdatePlaned(pps);

				//调用【接口文档--公共函数SHEET--修改轧制计划主表状态
				this.getBean(MesApiRollingPlanImpl.class).doUpdatePlnRollingPlan(p.getRl_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 选定物料移除计划
	 * @throws Exception 
	 * */
	public ResultModel doCancelPlanAuto(List<PlnRollingPlanDetail> lplpd,String userName) throws Exception{
		
		ResultModel rm=new ResultModel();
		List<PlnRollingPlanDetail> lp=new ArrayList<PlnRollingPlanDetail>();
		for(PlnRollingPlanDetail plpd:lplpd){
			PlnRollingPlanDetail pq=new PlnRollingPlanDetail();
			pq.setRl_plan_no(plpd.getRl_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnRollingPlanDetail> lpq=this.getMapper(PlnRollingPlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+plpd.getObject_no_vt()+"]的轧制计划["+plpd.getRl_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BRL01".compareTo(lpq.get(0).getStatus())<0){
				rm.setState("500");
				rm.setMsgInfo("母卷["+lpq.get(0).getObject_no_vt()+"]的轧制计划已下发，无法删除！");
				return rm;
			}
			lp.add(lpq.get(0));
		}
		
		for(PlnRollingPlanDetail p:lp){
			//写虚拟物料履历，状态填:0PRLXX		母卷																			
			this.getService(MesApiService.class).InsertMatVtProc(p.getObject_no_vt(), "0PRLXX", userName, new Date());
			
			// 查询子卷
			List<PlnMaterielDetail> listchild = this.getMapper(
					PlnMaterielDetailMapper.class).findWithCondition(null,
							" mat_object_no_vt = '" + p.getObject_no_vt() + "'", null,
							0, 0);
			if (Utils.isNotEmpty(listchild)) {
				for (PlnMaterielDetail pln : listchild) {
					// 写虚拟物料履历，状态填:0PRL01 子卷
					this.getService(MesApiService.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PRLXX", userName,
							new Date());
				}
			}
			
			//修改虚拟物料表 修改各母卷 的 轧制计划号[RL_PLANNO]字段为空。																				
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(p.getObject_no_vt());
			pmdu.setRl_plan_no("");
			pmdu.setStatus("0PDC10");
			this.getMapper(PlnMaterielDetailMapper.class).doUpdateRlPlanNo(pmdu);
			
			// 修改虚拟物料子卷开平计划号
			PlnMaterielDetail child =new PlnMaterielDetail();
			child.setRl_plan_no(null);
			child.setMat_object_no_vt(p.getObject_no_vt());
			child.setStatus("0PDC10");
			// child.setMachine_code("");
			this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
			
			if(p.getObject_no()!=null){
				//调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(p.getObject_no());
				pmp.setPrc_code("5BRL01");//新增的状态
				pmp.setRemark("计划已编制撤销");
				//撤销实物物料状态跟踪函数:
				this.getService(MesApiService.class).ARRAYLIST(pmp).getState();
			}
			
			//删除轧制计划明细相关母卷数据
			this.getMapper(PlnRollingPlanDetailMapper.class).doDelete(p);
			//========================================================================	
			
			
			//根据虚拟母卷的【RL_DESIGN_NO】, 读取【pln_dsn_order_info】中的所有数据(DESIGN_NO= 母卷CREATE_DESIGN_NO)
			PlnMaterielDetail pmdc=new PlnMaterielDetail();
			pmdc.setObject_no_vt(p.getObject_no_vt());
			List<PlnMaterielDetail> lpmd = this.getMapper(PlnMaterielDetailMapper.class).find(pmdc, 0, 0);
			
			//准备临时变量:  One_Weight=0;   One_Quantity=0;
			double One_Weight=0;
			int One_Quantity=0;
			
			PlnDsnOrderInfo pdoi=new PlnDsnOrderInfo();
			pdoi.setDesign_no(lpmd.get(0).getCreate_design_no());
			List<PlnDsnOrderInfo> lpdoi=this.getMapper(PlnDsnOrderInfoMapper.class).find(pdoi, 0, 0);
			for(PlnDsnOrderInfo s2:lpdoi){
				//One_Weight+=S2.WEIGHT_IN_ONE;   One_Quantity+=S2.QUANTITY_IN_ONE;
				One_Weight+=s2.getWeight_in_one();
				One_Quantity+=s2.getQuantity_in_one();
				//根据S2的 生产订单号，修改对应 生产订单进程表
				PlnProdorderStatistic pps=new PlnProdorderStatistic();
				pps.setProd_order_no(s2.getProd_order_no());
				pps.setRl_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setRl_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnProdorderStatisticMapper.class).doUpdatePlaned(pps);
				
				//根据S2的 订单号，修改  订单进程表[PLN_ORDER_STATISTIC]
				PlnOrderStatistic pos=new PlnOrderStatistic();
				pos.setOrder_no(s2.getOrder_no());
				pos.setRl_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setRl_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateRlPlaned(pos);
			}
			
			// 查找轧制计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 轧制计划号】的记录条数。 
			PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
			plpd.setRl_plan_no(p.getRl_plan_no());
			List<PlnRollingPlanDetail> lplpdq=this.getMapper(PlnRollingPlanDetailMapper.class).find(plpd, 0, 0);
			
			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 轧制计划进程表相关轧制计划记录 [PLN_ORDER_STATISTIC]
				PlnRlPlanStatistic pps=new PlnRlPlanStatistic();
				pps.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRlPlanStatisticMapper.class).doDelete(pps);
				
				//删除轧制计划主表数据
				PlnRollingPlan pp=new PlnRollingPlan();
				pp.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRollingPlanMapper.class).doDeleteRlPlanNo(pp);
			}else{
				//修改对应轧制计划主表【PLN_Rolling_PLAN】
				PlnRollingPlan pp=new PlnRollingPlan();
				pp.setRl_plan_no(p.getRl_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnRollingPlanMapper.class).doUpdatePlaned(pp);
				
				//修改轧制计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnRlPlanStatistic pps=new PlnRlPlanStatistic();
				pps.setMat_planed(lpmd.get(0).getSingle_weight()==null?0:(-lpmd.get(0).getSingle_weight()));
				pps.setMat_planed_cnt(-1);
				pps.setPlaned(-One_Weight);
				pps.setPlaned_cnt(-One_Quantity);
				pps.setRl_plan_no(p.getRl_plan_no());
				this.getMapper(PlnRlPlanStatisticMapper.class).doUpdatePlaned(pps);
				
				//调用【接口文档--公共函数SHEET--修改轧制计划主表状态
				this.getService(MesApiRollingPlanService.class).doUpdatePlnRollingPlan(p.getRl_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}


	/**
	 * 轧制计划下发 
	 * objectList
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSendPlanWeb(CommonPage<PlnRollingPlan> oci) throws Exception {
		ResultModel rm=new ResultModel();
		List<PlnRollingPlan> lplp=oci.getObjectList();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		//去重操作
		List<PlnRollingPlan> lplpC=new ArrayList<PlnRollingPlan>();
		for(PlnRollingPlan plp:lplp){
			if(lplpC.size()==0){
				lplpC.add(plp);
			}else{
				boolean flag=true;
				for(PlnRollingPlan plpc:lplpC){
					if(plpc.getRl_plan_no().equals(plp.getRl_plan_no())){
						flag=false;
						break;
					}
				}
				//未重复
				if(flag){
					lplpC.add(plp);
				}
			}
		}


		for(PlnRollingPlan pc:lplpC){
			// 判断物料是否在库
			this.getBean(MesApiDesignInfoImpl.class).JudgeIsOrNotZk(pc.getObject_no());

			PlnRollingPlan pp=new PlnRollingPlan();
			pp.setRl_plan_no(pc.getRl_plan_no());
			List<PlnRollingPlan> lp=this.getMapper(PlnRollingPlanMapper.class).find(pp, 0, 0);
			if(lp==null||lp.size()==0){
				rm.setState("500");
				rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]信息不存在!");
				return rm;
			}
			if(!"0PRL01".equals(lp.get(0).getStatus())){
				rm.setState("500");
				rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]已下发，不能重复下发!");
				return rm;
			}
			//实际上包含了母卷跟子卷
			PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
			plpd.setRl_plan_no(pc.getRl_plan_no());
			pc.setChild(this.getMapper(PlnRollingPlanDetailMapper.class).find(plpd, 0, 0));
		}

		for(PlnRollingPlan pc:lplpC){
			//修改轧制计划表： 相关计划的状态给为：0PRL02																					
			pc.setStatus("0PRL02");
			this.getMapper(PlnRollingPlanMapper.class).doUpdate(pc);

			//修改轧制计划明细表：计划内的的各母卷分别修改状态为：0PRL02																					
			for(PlnRollingPlanDetail pcc:pc.getChild()){
				pcc.setStatus("5BRL02");
				//修改轧制计划明细表：计划内的的各母卷分别修改状态为：5BRL02		
				// 增加下发时间下发人
				pcc.setSender(userName);
				pcc.setSend_date(new Date());
				this.getMapper(PlnRollingPlanDetailMapper.class).doUpdateStatus(pcc);

				//虚拟物料表：修改母卷的状态
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				pmdu.setObject_no_vt(pcc.getObject_no_vt());
				
				List<PlnMaterielDetail> pmddd = this.getMapper(PlnMaterielDetailMapper.class).find(pmdu, 0, 1);
				
				List<PlnProdorder> ppoL = this.getMapper(PlnProdorderMapper.class).findWithCondition(null, " PROD_ORDER_NO='"+pmddd.get(0).getProd_order_no()+"'", null, 0, 0);
				if (ppoL == null || ppoL.size() <= 0) {
					throw new Exception("生产订单：" + ppoL.get(0).getOrder_no() + " 不存在");
				}
				// 验证生产订单状态是否为订单保留：
				if ("0PAA60".equals(ppoL.get(0).getStatus())) {
					throw new Exception("生产订单["+ppoL.get(0).getProd_order_no()+"]为 ‘订单保留’ 状态，不允许下发");
				}
				if ("0PAA70".equals(ppoL.get(0).getStatus())) {
					throw new Exception("生产订单["+ppoL.get(0).getProd_order_no()+"]为 ‘订单作废’ 状态，不允许下发");
				}
				
				// 验证生产订单是否锁定
				boolean flag_l = this.getBean(MesApiDesignInfoImpl.class).pOrdIsLocked(pmddd.get(0).getProd_order_no());
				if (flag_l) {
					throw new Exception("生产订单已经锁定[订单变更中],不允许下发计划到车间!");
				}
				pmdu.setStatus("0PRL02");
				this.getMapper(PlnMaterielDetailMapper.class).doUpdateRlStatus(pmdu);
				
				// 修改虚拟物料子卷状态
				PlnMaterielDetail child =new PlnMaterielDetail();
				child.setRl_plan_no(pc.getRl_plan_no());
				child.setMat_object_no_vt(pcc.getObject_no_vt());
				child.setStatus("0PRL02");
				this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
				
				//写虚拟物料履历，状态填:0PRL02		母卷跟子卷信息																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pcc.getObject_no_vt(), "0PRL02", userName, new Date());

				//2018-05-05新增实物跟踪 //调用生产计划模块物料跟踪接口 
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setPrc_code("5BRL02");//新增的状态
				pmp.setCreate_time(new Date());//来源.
				pmp.setObject_no(pcc.getObject_no());
				pmp.setCreate_man_name(userName);
				pmp.setRemark("计划已下发");
				//新增物料跟踪
				this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
				//if(tmp==-1){
				//throw new Exception("物料跟踪失败");
				//}
			}
		}

		rm.setState("200");
		return rm;
	}

	/**
	 * 轧制计划取消下发
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSendPlanCancelWeb(CommonPage<PlnRollingPlan> oci) throws Exception {
		ResultModel rm=new ResultModel();
		List<PlnRollingPlan> lplp=oci.getObjectList();

		// 去重操作
		List<PlnRollingPlan> lplpC = new ArrayList<PlnRollingPlan>();
		for (PlnRollingPlan plp : lplp) {
			if (lplpC.size() == 0) {
				lplpC.add(plp);
			} else {
				boolean flag = true;
				for (PlnRollingPlan plpc : lplpC) {
					if (plpc.getRl_plan_no().equals(plp.getRl_plan_no())) {
						flag = false;
						break;
					}
				}
				// 未重复
				if (flag) {
					lplpC.add(plp);
				}
			}
		}

		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

		for(PlnRollingPlan pc:lplpC){
			PlnRollingPlan pp=new PlnRollingPlan();
			pp.setRl_plan_no(pc.getRl_plan_no());
			List<PlnRollingPlan> lp=this.getMapper(PlnRollingPlanMapper.class).find(pp, 0, 0);
			if(lp==null||lp.size()==0){
				rm.setState("500");
				rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]信息不存在!");
				return rm;
			}
			if("0PRL01".equals(lp.get(0).getStatus())){
				rm.setState("500");
				rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]尚未下发，不能取消!");
				return rm;
			}
			if("0PRL03".equals(lp.get(0).getStatus())){
				rm.setState("500");
				rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]内的某些母卷已开始作业,不能取消!");
				return rm;
			}

			// 详表 使用实物状态
			// 5BRL01 轧制计划已编制
			// 5BRL02 轧制计划已下发
			// 5BRL03 开始轧制作业
			// 5BRL09 轧制剪废
			// 5BRL80 轧制作业完成
			// 5BRL33 等待轧制判定
			// 5BRL40 轧制判定完成
			// 0PRLXX 轧制计划已失效
			PlnRollingPlanDetail plpt = new PlnRollingPlanDetail();
			plpt.setRl_plan_no(pc.getRl_plan_no());
			List<PlnRollingPlanDetail> plnpd = this.getMapper(PlnRollingPlanDetailMapper.class).findWithCondition(plpt, "status <> '0PRLXX'", null, 0, 0);
			if (plnpd != null && plnpd.size()>0) {
				for (PlnRollingPlanDetail plnLe : plnpd) {
					if (!"5BRL02".equals(plnLe.getStatus())) {
						rm.setState("500");
						rm.setMsgInfo("轧制计划["+pc.getRl_plan_no()+"]内的某些母卷已开始作业,不能取消!");
						return rm;
					}
					
					// 如果下发过pdi，也不能取消下发：20230418 刘侣
					if (!("0".equals(plnLe.getSend_flag()) || plnLe.getSend_flag() == null)) {
						throw new Exception("物料 " + plnLe.getObject_no() + " 已下发pdi，不能取消下发。");
					}
					// end
					
				}
			}

			//实际上包含了母卷跟子卷
			PlnRollingPlanDetail plpd=new PlnRollingPlanDetail();
			plpd.setRl_plan_no(pc.getRl_plan_no());
			pc.setChild(this.getMapper(PlnRollingPlanDetailMapper.class).find(plpd, 0, 0));
		}


		for(PlnRollingPlan pc:lplp){
			//修改轧制计划表： 相关计划的状态给为：0PRL01																					
			pc.setStatus("0PRL01");
			this.getMapper(PlnRollingPlanMapper.class).doUpdate(pc);

			//修改轧制计划明细表：计划内的的各母卷分别修改状态为：5BRL01		
			if(pc.getChild()!=null){
			for(PlnRollingPlanDetail pcc:pc.getChild()){
				
				
				
				pcc.setStatus("5BRL01");
				//修改轧制计划明细表：计划内的的各母卷分别修改状态为：5BRL01
				// 增加下发时间下发人
				pcc.setSender("");
				pcc.setSend_date(null);
				this.getMapper(PlnRollingPlanDetailMapper.class).doUpdateStatus(pcc);
				
				//虚拟物料表：修改母卷状态
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				pmdu.setStatus("0PRL01");
				pmdu.setObject_no_vt(pcc.getObject_no_vt());
				this.getMapper(PlnMaterielDetailMapper.class).doUpdateRlStatus(pmdu);
				
				// 修改虚拟物料子卷轧制计划号
				PlnMaterielDetail child =new PlnMaterielDetail();
				child.setRl_plan_no(pcc.getRl_plan_no());
				child.setMat_object_no_vt(pcc.getObject_no_vt());
				child.setStatus("0PRL01");
				this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
				
				//写虚拟物料履历，状态填:0PRL01		母卷跟子卷信息																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pcc.getObject_no_vt(), "0PRL01", userName, new Date());
				
				// 撤销实物进程跟踪调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(pcc.getObject_no());
				pmp.setPrc_code("5BRL02");//新增的状态
				pmp.setRemark("计划取消下发");
				pmp.setKeep_sametime_status(0);
				//撤销实物物料状态跟踪函数:
				this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();
			}
			}
		}

		rm.setState("200");
		return rm;
	}
	
	/**
	 * 轧制计划取消下发(单条操作)
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSendPlanCancelWeb2(CommonPage<PlnRollingPlanDetail> oci) throws Exception {
		ResultModel rm=new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		PlnRollingPlanDetail prpd = oci.getObject();
		
		PlnRollingPlan prp = new PlnRollingPlan();
		prp.setRl_plan_no(prpd.getRl_plan_no());
		List<PlnRollingPlan> prpList=this.getMapper(PlnRollingPlanMapper.class).find(prp, 0, 0);
		if(prpList==null||prpList.size()==0){
			rm.setState("500");
			rm.setMsgInfo("轧制计划["+prp.getRl_plan_no()+"]信息不存在!");
			return rm;
		}
		if("0PRL01".equals(prpList.get(0).getStatus())){
			rm.setState("500");
			rm.setMsgInfo("轧制计划["+prp.getRl_plan_no()+"]尚未下发，不能取消!");
			return rm;
		}
		/*if("0PRL03".equals(prpList.get(0).getStatus())){
			rm.setState("500");
			rm.setMsgInfo("轧制计划["+prp.getRl_plan_no()+"]内的某些母卷已开始作业,不能取消!");
			return rm;
		}*/
		
		PlnRollingPlanDetail plpt = new PlnRollingPlanDetail();
		plpt.setRl_plan_no(prpd.getRl_plan_no());
		plpt.setObject_no(prpd.getObject_no());
		List<PlnRollingPlanDetail> prpdList = this.getMapper(PlnRollingPlanDetailMapper.class).findWithCondition(plpt, "status <> '0PRLXX'", null, 0, 0);
		if (prpdList != null && prpdList.size()>0) {
			for (PlnRollingPlanDetail pd : prpdList) {
				
				// 如果下发过pdi，也不能取消下发：20230418 刘侣
				if (!("0".equals(pd.getSend_flag()) || pd.getSend_flag() == null)) {
					throw new Exception("物料 " + prp.getObject_no() + " 已下发pdi，不能取消下发。");
				}
				// end
				
				if (!"5BRL02".equals(pd.getStatus())) {
					rm.setState("500");
					rm.setMsgInfo("轧制计划["+prp.getRl_plan_no()+"]的物料["+prp.getObject_no()+"]已开始作业,不能取消!");
					return rm;
				}
				pd.setStatus("5BRL01");
				//修改轧制计划明细表：计划内的的各母卷分别修改状态为：5BRL01
				// 增加下发时间下发人
				pd.setSender("");
				pd.setSend_date(null);
				this.getMapper(PlnRollingPlanDetailMapper.class).doUpdateStatus(pd);
				
				//虚拟物料表：修改母卷状态
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				pmdu.setStatus("0PRL01");
				pmdu.setObject_no_vt(pd.getObject_no_vt());
				this.getMapper(PlnMaterielDetailMapper.class).doUpdateRlStatus(pmdu);
				
				// 修改虚拟物料子卷轧制计划号
				PlnMaterielDetail child =new PlnMaterielDetail();
				child.setRl_plan_no(pd.getRl_plan_no());
				child.setMat_object_no_vt(pd.getObject_no_vt());
				child.setStatus("0PRL01");
				this.getMapper(PlnMaterielDetailMapper.class).updateRlByMat_No(child);
				
				//写虚拟物料履历，状态填:0PRL01		母卷跟子卷信息																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pd.getObject_no_vt(), "0PRL01", userName, new Date());
				
				// 撤销实物进程跟踪调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(pd.getObject_no());
				pmp.setPrc_code("5BRL02");//新增的状态
				pmp.setRemark("计划取消下发");
				pmp.setKeep_sametime_status(0);
				//撤销实物物料状态跟踪函数:
				this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();	
				
				//调整轧制计划号 + "-01"
				pd.setRl_plan_no(pd.getRl_plan_no()+"-01");
				this.getMapper(PlnRollingPlanDetailMapper.class).update(pd);
			}
		}
		
		PlnRollingPlan newPrp = new PlnRollingPlan();
		BeanUtils.copyProperties(newPrp, prpList.get(0));
		newPrp.setRl_plan_no(prpList.get(0).getRl_plan_no()+"-1");
		this.getMapper(PlnRollingPlanMapper.class).doDeleteRlPlanNo(newPrp);//先删除
		newPrp.setStatus("0PRL01");
		//流水号+1
		List<PlnRollingPlan> lpp=this.getMapper(PlnRollingPlanMapper.class).findWithCondition(null, null, " serial_no desc ", 0, 1);		
		newPrp.setSerial_no(lpp.get(0).getSerial_no()+1);
		this.getMapper(PlnRollingPlanMapper.class).insert(newPrp);
		
		rm.setState("200");
		rm.setMsgInfo("取消下发成功");
		return rm;
	}

}
