/***文档注释***********************************************
 * 作者            ：bin.pan
 * 创建日期     ：2018-09-19
 * 描述            ：罩退拼料信息表 计划编制
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.anneal.Design.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Mes.impl.MesApiAnnealPlanImpl;
import market.Api.Mes.impl.MesApiDesignInfoImpl;
import market.Api.Mes.impl.MesApiServiceImpl;
import market.Api.Mes.impl.Utils;
import market.Api.Mes.service.MesApiAnnealPlanService;
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.anneal.Design.mapper.PlnAnPlanStatisticMapper;
import market.Mes.anneal.Design.mapper.PlnAnnealDsnMainMapper;
import market.Mes.anneal.Design.service.PlnAnnealDsnMultinfoService;
import market.Mes.anneal.Design.vo.PlnAnPlanStatistic;
import market.Mes.anneal.Design.vo.PlnAnnealDsnMain;
import market.Mes.anneal.Design.vo.PlnAnnealDsnMultinfo;
import market.Mes.anneal.mapper.PlnAnnealPlanDetailMapper;
import market.Mes.anneal.mapper.PlnAnnealPlanMapper;
import market.Mes.anneal.vo.PlnAnnealPlan;
import market.Mes.anneal.vo.PlnAnnealPlanDetail;

import org.apache.commons.lang3.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="PlnAnnealDsnMultinfoService")
public class PlnAnnealDsnMultinfoServiceImpl extends BaseServiceImpl<PlnAnnealDsnMultinfo,Object> implements PlnAnnealDsnMultinfoService {
	
	/**
	 * 选定物料加入罩退计划
	 * 传入PlnMaterielDetail object_vt_no 数组An_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();
		// 获取主计划号和序列号
		PlnAnnealPlan ppoo = getPlanNo();
		PlnAnnealPlan plpl = new PlnAnnealPlan();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String An_plan_no = "";
		List<PlnMaterielDetail> lpmd=oci.getObjectList();
		
		String tgMsg = null;
		
		// 循环处理传入的物料和计划号数组
		for (PlnMaterielDetail plnmd : lpmd) {
			if (Utils.isEmpty(plnmd.getMachine_code())) {
				rm.setState("500");
				rm.setMsgInfo("编制计划之前,请先选择机台!");
				return rm;
			}
			
			// 直接在这里获取库存卷是否已经有综判：不存在则直接跳过，并记录哪个卷跳过了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.getAn_plan_no()!=null && !"".equals(plnmd.getAn_plan_no())) {
				plpl.setAn_plan_no(plnmd.getAn_plan_no());
				plpl.setPlan_seq(plnmd.getPlan_seq());
				List<PlnAnnealPlan> lplp=this.getMapper(PlnAnnealPlanMapper.class).find(plpl, 0, 1);
				if(lplp==null||lplp.size()==0){
					rm.setState("500");
					rm.setMsgInfo("罩退计划["+plnmd.getAn_plan_no()+"]信息不存在!");
					return rm;
				}
				if(lplp.get(0).getStatus().trim().compareTo("0PAN01")>0){
					rm.setState("500");
					rm.setMsgInfo("罩退计划["+plnmd.getAn_plan_no()+"]已下发，不能加入!");
					return rm;
				}
				// 判断虚拟物料信息
				ResultModel rmValid = doPlanJudgeByObject_no_vt(plnmd.getObject_no_vt());
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				An_plan_no = plnmd.getAn_plan_no();
				// 新增罩退计划明细表记录
				doAddDetail(plnmd, userName, plnmd.getMachine_code(),oci.getMemo6());
			}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())) {
					PlnAnnealPlan pp = ppoo;
					An_plan_no = pp.getAn_plan_no();
					List<PlnAnnealPlan> plnleng = this.getMapper(PlnAnnealPlanMapper.class)
							.findWithCondition(null, "An_plan_no = '"+An_plan_no+"'", "", 0, 1);
					if (plnleng == null || plnleng.size()==0) {
						pp.setPlan_seq(pp.getSerial_no());
						pp.setStatus("0PAN01");
						pp.setPline_code(lpmd.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(PlnAnnealPlanMapper.class).insert(pp);
					}
					// 新增罩退计划明细表记录
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setAn_plan_no(pp.getAn_plan_no());
					plnmh.setObject_no_vt(plnmd.getObject_no_vt());
					doAddDetail(plnmh, userName, plnmd.getMachine_code(),oci.getMemo6());

				} else {
					// 新增罩退计划主记录
					PlnAnnealPlan pp = getPlanNo();
					An_plan_no = pp.getAn_plan_no();
					pp.setPlan_seq(pp.getSerial_no());
					pp.setStatus("0PAN01");
					pp.setPline_code(lpmd.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(PlnAnnealPlanMapper.class).insert(pp);

					// 新增罩退计划明细表
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setAn_plan_no(pp.getAn_plan_no());
					plnmh.setObject_no_vt(plnmd.getObject_no_vt());
					doAddDetail(plnmh, userName, plnmd.getMachine_code(),oci.getMemo6());
				}
			}
			// 修改其他信息
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(plnmd.getObject_no_vt());
			pmdu.setAn_plan_no(An_plan_no);
			doAddChildMethod(pmdu);

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

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

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

		PlnAnnealPlan plpl = new PlnAnnealPlan();
		plpl.setAn_plan_no(plnmd.getAn_plan_no());
		List<PlnAnnealPlan> lplp=this.getMapper(PlnAnnealPlanMapper.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<PlnAnnealPlanDetail> lppd=this.getMapper(PlnAnnealPlanDetailMapper.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;

		PlnAnnealPlanDetail plpdq=new PlnAnnealPlanDetail();
		plpdq.setAn_plan_no(plnmd.getAn_plan_no());
		lppd=this.getMapper(PlnAnnealPlanDetailMapper.class).findWithCondition(plpdq, "", " An_seq desc ", 0, 1);
		if(lppd==null||lppd.size()==0){
			isqe=1;
		}else{
			isqe=lppd.get(0).getAn_seq()+1;
		}

		PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
		plpd.setSerial_no(sNo++);
		plpd.setAn_plan_no(plnmd.getAn_plan_no());
		plpd.setAn_seq(isqe++);
		plpd.setObject_no_vt(pmd.getObject_no_vt());
		plpd.setAn_design_no(pmd.getAn_design_no());
		plpd.setStatus("5BAN01");
		plpd.setObject_no(pmd.getObject_no());
		plpd.setPline_code(pmd.getPline_code());
		plpd.setMachine_code(memo2==null?"M0004":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.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());

		PlnAnnealDsnMain pldm=new PlnAnnealDsnMain();
		pldm.setAn_design_no(pmd.getAn_design_no());
		List<PlnAnnealDsnMain> lpldm=this.getMapper(PlnAnnealDsnMainMapper.class).find(pldm, 0, 1);
		plpd.setRate(lpldm.get(0).getProd_rate());
//		plpd.setSub_count(lpldm.get(0).getSub_count().intValue());
		plpd.setPlan_hand_time(memo6);//计划交工时间
		this.getMapper(PlnAnnealPlanDetailMapper.class).insert(plpd);

		//虚拟物料表：修改母卷的 罩退计划号[An_PLANNO]字段
		PlnMaterielDetail pmdu=new PlnMaterielDetail();
		pmdu.setAn_plan_no(plnmd.getAn_plan_no());
		pmdu.setSerial_no(pmd.getSerial_no());
		pmdu.setMachine_code(memo2);
		pmdu.setStatus("0PAN01");
		this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
		
		// 修改虚拟物料子卷罩退计划号
		PlnMaterielDetail child =new PlnMaterielDetail();
		child.setAn_plan_no(plnmd.getAn_plan_no());
		child.setMat_object_no_vt(pmd.getObject_no_vt());
		child.setStatus("0PAN01");
		// child.setMachine_code(memo2);
		this.getMapper(PlnMaterielDetailMapper.class).updateAnByMat_No(child);

		//写虚拟物料履历，状态填:0PAn01 母卷																			
		this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmd.getObject_no_vt(), "0PAN01", 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) {
				// 写虚拟物料履历，状态填:0PAN01 子卷
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
						pln.getObject_no_vt(), "0PAN01", 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){
			//调用接口 新增实物物料状态跟踪	
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BAN80，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getObject_no());
			pmp.setPrc_code("5BAN01");//新增的状态
			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;
		//根据母卷的AN_DESIGN_NO , 读取【pln_dsn_order_info】
		PlnDsnOrderInfo pdoi = new PlnDsnOrderInfo();
		pdoi.setDesign_no(pmd.getAn_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.setAn_planed(s2.getWeight_in_one());
				pps.setAn_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.setAn_planed(s2.getWeight_in_one());
				pos.setAn_planed_cnt(s2.getQuantity_in_one());
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateAnPlaned(pos);
			}
		}
		// 在罩退计划进程表 [PLN_AN_PLAN_STATISTIC]， 没罩退计划号记录，就创建一条主记录
		PlnAnPlanStatistic plps=new PlnAnPlanStatistic();
		plps.setAn_plan_no(pmd.getAn_plan_no());
		int icount=this.getMapper(PlnAnPlanStatisticMapper.class).count(plps);
		if(icount==0){
			List<PlnAnPlanStatistic> lplps=this.getMapper(PlnAnPlanStatisticMapper.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(PlnAnPlanStatisticMapper.class).insert(plps);
		}else{
			this.getMapper(PlnAnPlanStatisticMapper.class).doUpdatePlaned(plps);
		}
		//修改罩退计划表： QUANTITY 数量 += 1;   TOTAANEIGHT 总重量 += 虚拟物料号单重
		PlnAnnealPlan plp=new PlnAnnealPlan();
		plp.setAn_plan_no(pmd.getAn_plan_no());
		plp.setMat_quantity(1);
		plp.setMat_weight(pmd.getSingle_weight());
		this.getMapper(PlnAnnealPlanMapper.class).doUpdatePlaned(plp);

		//调用【接口文档--公共函数SHEET--修改罩退计划主表状态
		this.getBean(MesApiAnnealPlanImpl.class).doUpdatePlnAnnealPlan(pmd.getAn_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.getAn_plan_no())
					&& pmdq.getAn_plan_no() != null) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经在计划["
						+ pmdq.getAn_plan_no() + "]内，无法再次编制！");
				return rm;
			}

			PlnAnnealPlanDetail ppd = new PlnAnnealPlanDetail();
			ppd.setObject_no_vt(pmdq.getObject_no_vt());
			int icount = this.getMapper(PlnAnnealPlanDetailMapper.class)
					.countWithCondition(ppd, " STATUS != '0PLWXX' ");
			if (icount > 0) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经在计划["	+ pmdq.getAn_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<PlnAnnealPlan> 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<PlnAnnealPlan> sAnPlanNo=oci.getObjectList();

		for(PlnAnnealPlan pc:sAnPlanNo){
			
			//循环处理对应计划中 分条明细（母卷）相关数据
			PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
			plpd.setAn_plan_no(pc.getAn_plan_no());
			List<PlnAnnealPlanDetail> lplpd=this.getMapper(PlnAnnealPlanDetailMapper.class).find(plpd, 0, 0);
			rm = doCheckCancelPlanChild(lplpd);
			if(!"200".equals(rm.getState())){
				throw new Exception(rm.getMsgInfo());
			}
			for (PlnAnnealPlanDetail 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<PlnAnnealPlanDetail> oci) throws Exception {
		List<PlnAnnealPlanDetail> lplpd=oci.getObjectList();
		ResultModel rm=doCheckCancelPlanChild(lplpd);
		return rm;
	}
	
	/**
	 * 选定物料移除计划
	 * @throws Exception 
	 * */
	public ResultModel doCheckCancelPlanChild(List<PlnAnnealPlanDetail> lplpd) throws Exception{
		ResultModel rm=new ResultModel();

		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<PlnAnnealPlanDetail> lp=new ArrayList<PlnAnnealPlanDetail>();
		for(PlnAnnealPlanDetail plpd:lplpd){
			PlnAnnealPlanDetail pq=new PlnAnnealPlanDetail();
			pq.setAn_plan_no(plpd.getAn_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnAnnealPlanDetail> lpq=this.getMapper(PlnAnnealPlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+lpq.get(0).getObject_no_vt()+"]的罩退计划["+lpq.get(0).getAn_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			
			// 撤销计划前需要先将pdi撤销：
			if(lpq.get(0).getSend_flag() != null && "1".equals(lpq.get(0).getSend_flag())){
				rm.setState("500");
				rm.setMsgInfo("卷["+lpq.get(0).getObject_no()+"]未撤销PDI，请先撤销PDI");
				return rm;
			}
			
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BAN01".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(PlnAnnealPlanDetail p:lp){
			//写虚拟物料履历，状态填:0PANXX		母卷																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(p.getObject_no_vt(), "0PANXX", 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) {
					// 写虚拟物料履历，状态填:0PAN01 子卷
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PANXX", userName,
							new Date());
				}
			}

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


			//根据虚拟母卷的【AN_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:(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.setAn_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setAn_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.setAn_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setAn_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateAnPlaned(pos);
			}

			// 查找罩退计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 罩退计划号】的记录条数。 
			PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
			plpd.setAn_plan_no(p.getAn_plan_no());
			List<PlnAnnealPlanDetail> lplpdq=this.getMapper(PlnAnnealPlanDetailMapper.class).find(plpd, 0, 0);

			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 罩退计划进程表相关罩退计划记录 [PLN_ORDER_STATISTIC]
				PlnAnPlanStatistic pps=new PlnAnPlanStatistic();
				pps.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnPlanStatisticMapper.class).doDelete(pps);

				//删除罩退计划主表数据
				PlnAnnealPlan pp=new PlnAnnealPlan();
				pp.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnnealPlanMapper.class).doDeleteAnPlanNo(pp);
			}else{
				//修改对应罩退计划主表【PLN_Anneal_PLAN】
				PlnAnnealPlan pp=new PlnAnnealPlan();
				pp.setAn_plan_no(p.getAn_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnAnnealPlanMapper.class).doUpdatePlaned(pp);

				//修改罩退计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnAnPlanStatistic pps=new PlnAnPlanStatistic();
				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.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnPlanStatisticMapper.class).doUpdatePlaned(pps);

				//调用【接口文档--公共函数SHEET--修改罩退计划主表状态
				this.getBean(MesApiAnnealPlanImpl.class).doUpdatePlnAnnealPlan(p.getAn_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}
	
	
	/**
	 * 选定物料移除计划
	 * @throws Exception 
	 * */
	public ResultModel doCancelPlanAuto(List<PlnAnnealPlanDetail> lplpd,String userName) throws Exception{
		
		ResultModel rm=new ResultModel();
		List<PlnAnnealPlanDetail> lp=new ArrayList<PlnAnnealPlanDetail>();
		for(PlnAnnealPlanDetail plpd:lplpd){
			PlnAnnealPlanDetail pq=new PlnAnnealPlanDetail();
			pq.setAn_plan_no(plpd.getAn_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnAnnealPlanDetail> lpq=this.getMapper(PlnAnnealPlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+lpq.get(0).getObject_no_vt()+"]的罩退计划["+lpq.get(0).getAn_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BAN01".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(PlnAnnealPlanDetail p:lp){
			//写虚拟物料履历，状态填:0PANXX		母卷																			
			this.getService(MesApiService.class).InsertMatVtProc(p.getObject_no_vt(), "0PANXX", 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) {
					// 写虚拟物料履历，状态填:0PAN01 子卷
					this.getService(MesApiService.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PANXX", userName,
							new Date());
				}
			}
			
			//修改虚拟物料表 修改各母卷 的 罩退计划号[AN_PLANNO]字段为空。																				
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(p.getObject_no_vt());
			pmdu.setAn_plan_no("");
			pmdu.setStatus("0PDC10");
			this.getMapper(PlnMaterielDetailMapper.class).doUpdateAnPlanNo(pmdu);
			
			// 修改虚拟物料子卷开平计划号
			PlnMaterielDetail child =new PlnMaterielDetail();
			child.setAn_plan_no(null);
			child.setMat_object_no_vt(p.getObject_no_vt());
			child.setStatus("0PDC10");
			// child.setMachine_code("");
			this.getMapper(PlnMaterielDetailMapper.class).updateAnByMat_No(child);
			
			if(p.getObject_no()!=null){
				//调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(p.getObject_no());
				pmp.setPrc_code("5BAN01");//新增的状态
				pmp.setRemark("计划已编制撤销");
				//撤销实物物料状态跟踪函数:
				String tmp=this.getService(MesApiService.class).ARRAYLIST(pmp).getState();
			}
			
			//删除罩退计划明细相关母卷数据
			this.getMapper(PlnAnnealPlanDetailMapper.class).doDelete(p);
			//========================================================================	
			
			
			//根据虚拟母卷的【AN_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.setAn_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setAn_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.setAn_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setAn_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdateAnPlaned(pos);
			}
			
			// 查找罩退计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 罩退计划号】的记录条数。 
			PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
			plpd.setAn_plan_no(p.getAn_plan_no());
			List<PlnAnnealPlanDetail> lplpdq=this.getMapper(PlnAnnealPlanDetailMapper.class).find(plpd, 0, 0);
			
			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 罩退计划进程表相关罩退计划记录 [PLN_ORDER_STATISTIC]
				PlnAnPlanStatistic pps=new PlnAnPlanStatistic();
				pps.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnPlanStatisticMapper.class).doDelete(pps);
				
				//删除罩退计划主表数据
				PlnAnnealPlan pp=new PlnAnnealPlan();
				pp.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnnealPlanMapper.class).doDeleteAnPlanNo(pp);
			}else{
				//修改对应罩退计划主表【PLN_Anneal_PLAN】
				PlnAnnealPlan pp=new PlnAnnealPlan();
				pp.setAn_plan_no(p.getAn_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnAnnealPlanMapper.class).doUpdatePlaned(pp);
				
				//修改罩退计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnAnPlanStatistic pps=new PlnAnPlanStatistic();
				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.setAn_plan_no(p.getAn_plan_no());
				this.getMapper(PlnAnPlanStatisticMapper.class).doUpdatePlaned(pps);
				
				//调用【接口文档--公共函数SHEET--修改罩退计划主表状态
				this.getService(MesApiAnnealPlanService.class).doUpdatePlnAnnealPlan(p.getAn_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}


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


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

			PlnAnnealPlan pp=new PlnAnnealPlan();
			pp.setAn_plan_no(pc.getAn_plan_no());
			List<PlnAnnealPlan> lp=this.getMapper(PlnAnnealPlanMapper.class).find(pp, 0, 0);
			if(lp==null||lp.size()==0){
				rm.setState("500");
				rm.setMsgInfo("罩退计划["+pc.getAn_plan_no()+"]信息不存在!");
				return rm;
			}
			if(!"0PAN01".equals(lp.get(0).getStatus())){
				rm.setState("500");
				rm.setMsgInfo("罩退计划["+pc.getAn_plan_no()+"]已下发，不能重复下发!");
				return rm;
			}
			//实际上包含了母卷跟子卷
			PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
			plpd.setAn_plan_no(pc.getAn_plan_no());
			pc.setChild(this.getMapper(PlnAnnealPlanDetailMapper.class).find(plpd, 0, 0));
		}

		for(PlnAnnealPlan pc:lplpC){
			//修改罩退计划表： 相关计划的状态给为：0PAN02																					
			pc.setStatus("0PAN02");
			this.getMapper(PlnAnnealPlanMapper.class).doUpdate(pc);

			//修改罩退计划明细表：计划内的的各母卷分别修改状态为：0PAN02																					
			for(PlnAnnealPlanDetail pcc:pc.getChild()){
				pcc.setStatus("5BAN02");
				//修改罩退计划明细表：计划内的的各母卷分别修改状态为：5BAN02		
				// 增加下发时间下发人
				pcc.setSender(userName);
				pcc.setSend_date(new Date());
				this.getMapper(PlnAnnealPlanDetailMapper.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("0PAN02");
				this.getMapper(PlnMaterielDetailMapper.class).doUpdateAnStatus(pmdu);
				
				// 修改虚拟物料子卷状态
				PlnMaterielDetail child =new PlnMaterielDetail();
				child.setAn_plan_no(pc.getAn_plan_no());
				child.setMat_object_no_vt(pcc.getObject_no_vt());
				child.setStatus("0PAN02");
				this.getMapper(PlnMaterielDetailMapper.class).updateAnByMat_No(child);
				
				//写虚拟物料履历，状态填:0PAN02		母卷跟子卷信息																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pcc.getObject_no_vt(), "0PAN02", userName, new Date());

				//2018-05-05新增实物跟踪 //调用生产计划模块物料跟踪接口 
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setPrc_code("5BAN02");//新增的状态
				pmp.setCreate_time(new Date());//来源.
				pmp.setObject_no(pcc.getObject_no());
				pmp.setCreate_man_name(userName);
				pmp.setRemark("计划已下发");
				//新增物料跟踪
				int tmp= 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<PlnAnnealPlan> oci) throws Exception {
		ResultModel rm=new ResultModel();
		List<PlnAnnealPlan> lplp=oci.getObjectList();

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

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

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

			// 详表 使用实物状态
			// 5BAN01 罩退计划已编制
			// 5BAN02 罩退计划已下发
			// 5BAN03 开始罩退作业
			// 5BAN09 罩退剪废
			// 5BAN80 罩退作业完成
			// 5BAN33 等待罩退判定
			// 5BAN40 罩退判定完成
			// 0PANXX 罩退计划已失效
			PlnAnnealPlanDetail plpt = new PlnAnnealPlanDetail();
			plpt.setAn_plan_no(pc.getAn_plan_no());
			List<PlnAnnealPlanDetail> plnpd = this.getMapper(PlnAnnealPlanDetailMapper.class).findWithCondition(plpt, "status <> '0PANXX'", null, 0, 0);
			if (plnpd != null && plnpd.size()>0) {
				for (PlnAnnealPlanDetail plnLe : plnpd) {
					if (!"5BAN02".equals(plnLe.getStatus())) {
						rm.setState("500");
						rm.setMsgInfo("罩退计划["+pc.getAn_plan_no()+"]内的某些母卷已开始作业,不能取消!");
						return rm;
					}
				}
			}

			//实际上包含了母卷跟子卷
			PlnAnnealPlanDetail plpd=new PlnAnnealPlanDetail();
			plpd.setAn_plan_no(pc.getAn_plan_no());
			pc.setChild(this.getMapper(PlnAnnealPlanDetailMapper.class).find(plpd, 0, 0));
		}


		for(PlnAnnealPlan pc:lplp){
			//修改罩退计划表： 相关计划的状态给为：0PAN01																					
			pc.setStatus("0PAN01");
			this.getMapper(PlnAnnealPlanMapper.class).doUpdate(pc);

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

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


}
