/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-04-22
 * 描述            ：酸洗拼料信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Pickle.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.MesApiPicklePlanServiceImpl;
import market.Api.Mes.impl.MesApiServiceImpl;
import market.Api.Mes.impl.Utils;
import market.Api.Mes.service.MesApiPicklePlanService;
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.Pickle.Design.mapper.PlnPcPlanStatisticMapper;
import market.Mes.Pickle.Design.mapper.PlnPickleDsnMainMapper;
import market.Mes.Pickle.Design.service.PlnPickleDsnMultinfoService;
import market.Mes.Pickle.Design.vo.PlnPcPlanStatistic;
import market.Mes.Pickle.Design.vo.PlnPickleDsnMain;
import market.Mes.Pickle.Design.vo.PlnPickleDsnMultinfo;
import market.Mes.Pickle.Performance.mapper.MesPlmMMapper;
import market.Mes.Pickle.Performance.vo.MesPlmM;
import market.Mes.Pickle.Plan.mapper.PlnPicklePlanDetailMapper;
import market.Mes.Pickle.Plan.mapper.PlnPicklePlanMapper;
import market.Mes.Pickle.Plan.vo.PlnPicklePlan;
import market.Mes.Pickle.Plan.vo.PlnPicklePlanDetail;

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="PlnPickleDsnMultinfoService")
public class PlnPickleDsnMultinfoServiceImpl extends BaseServiceImpl<PlnPickleDsnMultinfo,Object> implements PlnPickleDsnMultinfoService {

	/**
	 * 选定物料加入酸洗计划
	 * 传入PlnMaterielDetail object_vt_no 数组Pc_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();
		// 获取主计划号和序列号
		PlnPicklePlan ppoo = getPlanNo();
		PlnPicklePlan plpl = new PlnPicklePlan();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Pc_plan_no = "";
		List<PlnMaterielDetail> lpmd=oci.getObjectList();
		// 循环处理传入的物料和计划号数组
		for (PlnMaterielDetail plnmd : lpmd) {
			if (Utils.isEmpty(plnmd.getMachine_code())) {
				plnmd.setMachine_code("M0010");
			}
			// 判断是否传入计划号
			if (plnmd.getPc_plan_no()!=null && !"".equals(plnmd.getPc_plan_no())) {
				plpl.setPc_plan_no(plnmd.getPc_plan_no());
				plpl.setPlan_seq(plnmd.getPlan_seq());
				List<PlnPicklePlan> lplp=this.getMapper(PlnPicklePlanMapper.class).find(plpl, 0, 1);
				if(lplp==null||lplp.size()==0){
					rm.setState("500");
					rm.setMsgInfo("酸洗计划["+plnmd.getPc_plan_no()+"]信息不存在!");
					return rm;
				}
				if(lplp.get(0).getStatus().trim().compareTo("0PPC01")>0){
					rm.setState("500");
					rm.setMsgInfo("酸洗计划["+plnmd.getPc_plan_no()+"]已下发，不能加入!");
					return rm;
				}
				// 判断虚拟物料信息
				ResultModel rmValid = doPlanJudgeByObject_no_vt(plnmd.getObject_no_vt());
				if(!"200".equals(rmValid.getState())){
					throw new Exception(rmValid.getMsgInfo());
				}
				Pc_plan_no = plnmd.getPc_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())) {
					PlnPicklePlan pp = ppoo;
					Pc_plan_no = pp.getPc_plan_no();
					List<PlnPicklePlan> plnleng = this.getMapper(PlnPicklePlanMapper.class)
							.findWithCondition(null, "pc_plan_no = '"+Pc_plan_no+"'", "", 0, 1);
					if (plnleng == null || plnleng.size()==0) {
						pp.setPlan_seq(pp.getSerial_no());
						pp.setStatus("0PPC01");
						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(PlnPicklePlanMapper.class).insert(pp);
					}
					// 新增酸洗计划明细表记录
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setPc_plan_no(pp.getPc_plan_no());
					plnmh.setObject_no_vt(plnmd.getObject_no_vt());
					doAddDetail(plnmh, userName, plnmd.getMachine_code(), oci.getMemo6());

				} else {
					// 新增酸洗计划主记录
					PlnPicklePlan pp = getPlanNo();
					Pc_plan_no = pp.getPc_plan_no();
					pp.setPlan_seq(pp.getSerial_no());
					pp.setStatus("0PPC01");
					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(PlnPicklePlanMapper.class).insert(pp);

					// 新增酸洗计划明细表
					PlnMaterielDetail plnmh = new PlnMaterielDetail();
					plnmh.setPc_plan_no(pp.getPc_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.setPc_plan_no(Pc_plan_no);
			doAddChildMethod(pmdu);

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

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

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

		PlnPicklePlan plpl = new PlnPicklePlan();
		plpl.setPc_plan_no(plnmd.getPc_plan_no());
		List<PlnPicklePlan> lplp=this.getMapper(PlnPicklePlanMapper.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<PlnPicklePlanDetail> lppd=this.getMapper(PlnPicklePlanDetailMapper.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;

		PlnPicklePlanDetail plpdq=new PlnPicklePlanDetail();
		plpdq.setPc_plan_no(plnmd.getPc_plan_no());
		lppd=this.getMapper(PlnPicklePlanDetailMapper.class).findWithCondition(plpdq, "", " Pc_seq desc ", 0, 1);
		if(lppd==null||lppd.size()==0){
			isqe=1;
		}else{
			isqe=lppd.get(0).getPc_seq()+1;
		}

		PlnPicklePlanDetail plpd=new PlnPicklePlanDetail();
		plpd.setSerial_no(sNo++);
		plpd.setPc_plan_no(plnmd.getPc_plan_no());
		plpd.setPc_seq(isqe++);
		plpd.setObject_no_vt(pmd.getObject_no_vt());
		plpd.setPc_design_no(pmd.getPc_design_no());
		plpd.setStatus("5BPC01");
		plpd.setObject_no(pmd.getObject_no());
		plpd.setPline_code(pmd.getPline_code());
		plpd.setMachine_code(memo2==null?"M0010":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.setSub_count(pmd.getSub_count() == null ? 1 : pmd.getSub_count());
		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());

		PlnPickleDsnMain pldm=new PlnPickleDsnMain();
		pldm.setPc_design_no(pmd.getPc_design_no());
		List<PlnPickleDsnMain> lpldm=this.getMapper(PlnPickleDsnMainMapper.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(PlnPicklePlanDetailMapper.class).insert(plpd);

		//虚拟物料表：修改母卷的 酸洗计划号[Pc_PLPCNO]字段
		PlnMaterielDetail pmdu=new PlnMaterielDetail();
		pmdu.setPc_plan_no(plnmd.getPc_plan_no());
		pmdu.setSerial_no(pmd.getSerial_no());
		pmdu.setMachine_code(memo2);
		pmdu.setStatus("0PPC01");
		this.getMapper(PlnMaterielDetailMapper.class).update(pmdu);
		
		// 修改虚拟物料子卷酸洗计划号
		PlnMaterielDetail child =new PlnMaterielDetail();
		child.setPc_plan_no(plnmd.getPc_plan_no());
		child.setMat_object_no_vt(pmd.getObject_no_vt());
		child.setStatus("0PPC01");
		// child.setMachine_code(memo2);
		this.getMapper(PlnMaterielDetailMapper.class).updatePcByMat_No(child);

		//写虚拟物料履历，状态填:0PPc01 母卷																			
		this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmd.getObject_no_vt(), "0PPC01", 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) {
				// 写虚拟物料履历，状态填:0PPC01 子卷
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
						pln.getObject_no_vt(), "0PPC01", 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){
			//调用接口 新增实物物料状态跟踪	
			//调用 新增实物物料状态跟踪:  传入（实物子卷号,5BPC80，操作时间，操作人，""）
			PrcMaterielProcess pmp=new PrcMaterielProcess();
			pmp.setObject_no(pmd.getObject_no());
			pmp.setPrc_code("5BPC01");//新增的状态
			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;
		//根据母卷的PC_DESIGN_NO , 读取【pln_dsn_order_info】
		PlnDsnOrderInfo pdoi = new PlnDsnOrderInfo();
		pdoi.setDesign_no(pmd.getPc_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.setPc_planed(s2.getWeight_in_one());
				pps.setPc_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.setPc_planed(s2.getWeight_in_one());
				pos.setPc_planed_cnt(s2.getQuantity_in_one());
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePcPlaned(pos);
			}
		}
		// 在酸洗计划进程表 [PLN_PC_PLAN_STATISTIC]， 没酸洗计划号记录，就创建一条主记录
		PlnPcPlanStatistic plps=new PlnPcPlanStatistic();
		plps.setPc_plan_no(pmd.getPc_plan_no());
		int icount=this.getMapper(PlnPcPlanStatisticMapper.class).count(plps);
		if(icount==0){
			List<PlnPcPlanStatistic> lplps=this.getMapper(PlnPcPlanStatisticMapper.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(PlnPcPlanStatisticMapper.class).insert(plps);
		}else{
			this.getMapper(PlnPcPlanStatisticMapper.class).doUpdatePlaned(plps);
		}
		//修改酸洗计划表： QUANTITY 数量 += 1;   TOTAANEIGHT 总重量 += 虚拟物料号单重
		PlnPicklePlan plp=new PlnPicklePlan();
		plp.setPc_plan_no(pmd.getPc_plan_no());
		plp.setMat_quantity(1);
		plp.setMat_weight(pmd.getSingle_weight());
		this.getMapper(PlnPicklePlanMapper.class).doUpdatePlaned(plp);

		//调用【接口文档--公共函数SHEET--修改酸洗计划主表状态
		this.getBean(MesApiPicklePlanServiceImpl.class).doUpdatePlnPicklePlan(pmd.getPc_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;
		}
		
		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;
			}
		}
		// 实际上只有一条
		for (PlnMaterielDetail pmdq : lpmdq) {
			if (!"".equals(pmdq.getPc_plan_no())
					&& pmdq.getPc_plan_no() != null) {
				rm.setState("500");
				rm.setMsgInfo("虚拟物料[" + Object_no_vt + "]已经在计划["
						+ pmdq.getPc_plan_no() + "]内，无法再次编制！");
				return rm;
			}

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

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

		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<PlnPicklePlanDetail> lp=new ArrayList<PlnPicklePlanDetail>();
		for(PlnPicklePlanDetail plpd:lplpd){
			PlnPicklePlanDetail pq=new PlnPicklePlanDetail();
			pq.setPc_plan_no(plpd.getPc_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnPicklePlanDetail> lpq=this.getMapper(PlnPicklePlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+plpd.getObject_no_vt()+"]的酸洗计划["+plpd.getPc_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BPC01".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(PlnPicklePlanDetail p:lp){
			//写虚拟物料履历，状态填:0PPCXX		母卷																			
			this.getBean(MesApiServiceImpl.class).InsertMatVtProc(p.getObject_no_vt(), "0PPCXX", 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) {
					// 写虚拟物料履历，状态填:0PPC01 子卷
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PPCXX", userName,
							new Date());
				}
			}

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


			//根据虚拟母卷的【PC_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.setPc_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setPc_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.setPc_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setPc_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePcPlaned(pos);
			}

			// 查找酸洗计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 酸洗计划号】的记录条数。 
			PlnPicklePlanDetail plpd=new PlnPicklePlanDetail();
			plpd.setPc_plan_no(p.getPc_plan_no());
			List<PlnPicklePlanDetail> lplpdq=this.getMapper(PlnPicklePlanDetailMapper.class).find(plpd, 0, 0);

			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 酸洗计划进程表相关酸洗计划记录 [PLN_ORDER_STATISTIC]
				PlnPcPlanStatistic pps=new PlnPcPlanStatistic();
				pps.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPcPlanStatisticMapper.class).doDelete(pps);

				//删除酸洗计划主表数据
				PlnPicklePlan pp=new PlnPicklePlan();
				pp.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPicklePlanMapper.class).doDeletePcPlanNo(pp);
			}else{
				//修改对应酸洗计划主表【PLN_Rolling_PLAN】
				PlnPicklePlan pp=new PlnPicklePlan();
				pp.setPc_plan_no(p.getPc_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnPicklePlanMapper.class).doUpdatePlaned(pp);

				//修改酸洗计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnPcPlanStatistic pps=new PlnPcPlanStatistic();
				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.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPcPlanStatisticMapper.class).doUpdatePlaned(pps);

				//调用【接口文档--公共函数SHEET--修改酸洗计划主表状态
				this.getBean(MesApiPicklePlanServiceImpl.class).doUpdatePlnPicklePlan(p.getPc_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}
	
	
	/**
	 * 选定物料移除计划
	 * @throws Exception 
	 * */
	public ResultModel doCancelPlanAuto(List<PlnPicklePlanDetail> lplpd , String userName) throws Exception{
		
		ResultModel rm=new ResultModel();
		List<PlnPicklePlanDetail> lp=new ArrayList<PlnPicklePlanDetail>();
		for(PlnPicklePlanDetail plpd:lplpd){
			PlnPicklePlanDetail pq=new PlnPicklePlanDetail();
			pq.setPc_plan_no(plpd.getPc_plan_no());
			pq.setObject_no_vt(plpd.getObject_no_vt());
			List<PlnPicklePlanDetail> lpq=this.getMapper(PlnPicklePlanDetailMapper.class).find(pq, 0, 1);
			if(lpq==null||lpq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟母卷["+plpd.getObject_no_vt()+"]的酸洗计划["+plpd.getPc_plan_no()+"]不存在，无法删除！");
				return rm;
			}
			//状态STATUS>5BAN01, 如果成立，则报错退出
			if("5BPC01".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(PlnPicklePlanDetail p:lp){
			//写虚拟物料履历，状态填:0PPCXX		母卷																			
			this.getService(MesApiService.class).InsertMatVtProc(p.getObject_no_vt(), "0PPCXX", 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) {
					// 写虚拟物料履历，状态填:0PPC01 子卷
					this.getService(MesApiService.class).InsertMatVtProc(
							pln.getObject_no_vt(), "0PPCXX", userName,
							new Date());
				}
			}
			
			//修改虚拟物料表 修改各母卷 的 酸洗计划号[PC_PLANNO]字段为空。																				
			PlnMaterielDetail pmdu=new PlnMaterielDetail();
			pmdu.setObject_no_vt(p.getObject_no_vt());
			pmdu.setPc_plan_no("");
			pmdu.setStatus("0PDC10");
			this.getMapper(PlnMaterielDetailMapper.class).doUpdatePcPlanNo(pmdu);
			
			// 修改虚拟物料子卷开平计划号
			PlnMaterielDetail child =new PlnMaterielDetail();
			child.setPc_plan_no(null);
			child.setMat_object_no_vt(p.getObject_no_vt());
			child.setStatus("0PDC10");
			// child.setMachine_code("");
			this.getMapper(PlnMaterielDetailMapper.class).updatePcByMat_No(child);
			
			if(p.getObject_no()!=null){
				//调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(p.getObject_no());
				pmp.setPrc_code("5BPC01");//新增的状态
				pmp.setRemark("计划已编制撤销");
				//撤销实物物料状态跟踪函数:
				String tmp=this.getService(MesApiService.class).ARRAYLIST(pmp).getState();
			}
			
			//删除酸洗计划明细相关母卷数据
			this.getMapper(PlnPicklePlanDetailMapper.class).doDelete(p);
			//========================================================================	
			
			
			//根据虚拟母卷的【PC_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.setPc_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pps.setPc_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.setPc_planed(s2.getWeight_in_one()==null?0:(-s2.getWeight_in_one()));
				pos.setPc_planed_cnt(s2.getQuantity_in_one()==null?0:(-s2.getQuantity_in_one()));
				this.getMapper(PlnOrderStatisticMapper.class).doUpdatePcPlaned(pos);
			}
			
			// 查找酸洗计划明细表【PLN_CUTTOLENGTH_PLAN_DETAIL】  where AN_plan_no=【母卷对应 酸洗计划号】的记录条数。 
			PlnPicklePlanDetail plpd=new PlnPicklePlanDetail();
			plpd.setPc_plan_no(p.getPc_plan_no());
			List<PlnPicklePlanDetail> lplpdq=this.getMapper(PlnPicklePlanDetailMapper.class).find(plpd, 0, 0);
			
			if(lplpdq==null||lplpdq.size()==0){//如果 记录数=0，
				//删除 酸洗计划进程表相关酸洗计划记录 [PLN_ORDER_STATISTIC]
				PlnPcPlanStatistic pps=new PlnPcPlanStatistic();
				pps.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPcPlanStatisticMapper.class).doDelete(pps);
				
				//删除酸洗计划主表数据
				PlnPicklePlan pp=new PlnPicklePlan();
				pp.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPicklePlanMapper.class).doDeletePcPlanNo(pp);
			}else{
				//修改对应酸洗计划主表【PLN_Rolling_PLAN】
				PlnPicklePlan pp=new PlnPicklePlan();
				pp.setPc_plan_no(p.getPc_plan_no());
				pp.setMat_quantity(-1);
				pp.setMat_weight(lpmd.get(0).getSingle_weight()==null?null:(-lpmd.get(0).getSingle_weight()));
				this.getMapper(PlnPicklePlanMapper.class).doUpdatePlaned(pp);
				
				//修改酸洗计划号记录 [PLN_AN_PLAN_STATISTIC]，填写字段
				PlnPcPlanStatistic pps=new PlnPcPlanStatistic();
				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.setPc_plan_no(p.getPc_plan_no());
				this.getMapper(PlnPcPlanStatisticMapper.class).doUpdatePlaned(pps);
				
				//调用【接口文档--公共函数SHEET--修改酸洗计划主表状态
				this.getService(MesApiPicklePlanService.class).doUpdatePlnPicklePlan(p.getPc_plan_no());
			}	
		}
		rm.setState("200");
		return rm;
	}


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


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

			PlnPicklePlan pp=new PlnPicklePlan();
			pp.setPc_plan_no(pc.getPc_plan_no());
			List<PlnPicklePlan> lp=this.getMapper(PlnPicklePlanMapper.class).find(pp, 0, 0);
			if(lp==null||lp.size()==0){
				rm.setState("500");
				rm.setMsgInfo("酸洗计划["+pc.getPc_plan_no()+"]信息不存在!");
				return rm;
			}
			if(!"0PPC01".equals(lp.get(0).getStatus())){
				rm.setState("500");
				rm.setMsgInfo("酸洗计划["+pc.getPc_plan_no()+"]已下发，不能重复下发!");
				return rm;
			}
			//实际上包含了母卷跟子卷
			PlnPicklePlanDetail plpd=new PlnPicklePlanDetail();
			plpd.setPc_plan_no(pc.getPc_plan_no());
			pc.setChild(this.getMapper(PlnPicklePlanDetailMapper.class).find(plpd, 0, 0));
		}

		for(PlnPicklePlan pc:lplpC){
			// 修改酸洗计划表： 相关计划的状态给为：0PPC02																					
			pc.setStatus("0PPC02");
			this.getMapper(PlnPicklePlanMapper.class).doUpdate(pc);

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

				//2018-05-05新增实物跟踪 //调用生产计划模块物料跟踪接口 
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setPrc_code("5BPC02");//新增的状态
				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<PlnPicklePlan> oci) throws Exception {
		ResultModel rm=new ResultModel();
		List<PlnPicklePlan> lplp=oci.getObjectList();

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

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

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

			// 详表 使用实物状态
			// 5BPC01 酸洗计划已编制
			// 5BPC02 酸洗计划已下发
			// 5BPC03 开始酸洗作业
			// 5BPC09 酸洗剪废
			// 5BPC80 酸洗作业完成
			// 5BPC33 等待酸洗判定
			// 5BPC40 酸洗判定完成
			// 0PPCXX 酸洗计划已失效
			PlnPicklePlanDetail plpt = new PlnPicklePlanDetail();
			plpt.setPc_plan_no(pc.getPc_plan_no());
			List<PlnPicklePlanDetail> plnpd = this.getMapper(PlnPicklePlanDetailMapper.class).findWithCondition(plpt, "status <> '0PPCXX'", null, 0, 0);
			if (plnpd != null && plnpd.size()>0) {
				for (PlnPicklePlanDetail plnLe : plnpd) {
					if (!"5BPC02".equals(plnLe.getStatus())) {
						rm.setState("500");
						rm.setMsgInfo("酸洗计划["+pc.getPc_plan_no()+"]内的某些母卷已开始作业,不能取消!");
						return rm;
					}
					//20201222 已下发到酸洗2级的数据，不能取消计划
					if ("1".equals(plnLe.getSend_flag())) {
						rm.setState("500");
						rm.setMsgInfo("酸洗计划["+pc.getPc_plan_no()+":"+pc.getObject_no()+"]已下发到酸洗2级,不能取消!");
						return rm;
					}
				}
			}

			//实际上包含了母卷跟子卷
			PlnPicklePlanDetail plpd=new PlnPicklePlanDetail();
			plpd.setPc_plan_no(pc.getPc_plan_no());
			pc.setChild(this.getMapper(PlnPicklePlanDetailMapper.class).find(plpd, 0, 0));
		}


		for(PlnPicklePlan pc:lplp){
			//修改酸洗计划表： 相关计划的状态给为：0PPC01																					
			pc.setStatus("0PPC01");
			this.getMapper(PlnPicklePlanMapper.class).doUpdate(pc);

			//修改酸洗计划明细表：计划内的的各母卷分别修改状态为：5BPC01																					
			for(PlnPicklePlanDetail pcc:pc.getChild()){
				//20200106 取消下发编制计划时，判断酸洗是否下发pdi
				MesPlmM mpm = new MesPlmM();
				mpm.setSchedule_no(pcc.getPc_plan_no());
				mpm.setMaterial_no_vt(pcc.getObject_no_vt());
				int count = this.getMapper(MesPlmMMapper.class).count(mpm);
				if (count > 0) {
					rm.setState("200");
					rm.setMsgInfo("物料【"+pcc.getObject_no()+"】已经下发PDI，不能取消");
					return rm;
				}
				
				pcc.setStatus("5BPC01");
				//修改酸洗计划明细表：计划内的的各母卷分别修改状态为：5BPC01
				// 增加下发时间下发人
				pcc.setSender("");
				pcc.setSend_date(null);
				this.getMapper(PlnPicklePlanDetailMapper.class).doUpdateStatus(pcc);
				
				//虚拟物料表：修改母卷状态
				PlnMaterielDetail pmdu=new PlnMaterielDetail();
				pmdu.setStatus("0PPC01");
				pmdu.setObject_no_vt(pcc.getObject_no_vt());
				this.getMapper(PlnMaterielDetailMapper.class).doUpdatePcStatus(pmdu);
				
				// 修改虚拟物料子卷酸洗计划号
				PlnMaterielDetail child =new PlnMaterielDetail();
				child.setPc_plan_no(pcc.getPc_plan_no());
				child.setMat_object_no_vt(pcc.getObject_no_vt());
				child.setStatus("0PPC01");
				this.getMapper(PlnMaterielDetailMapper.class).updatePcByMat_No(child);
				
				//写虚拟物料履历，状态填:0PPC01		母卷跟子卷信息																			
				this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pcc.getObject_no_vt(), "0PPC01", userName, new Date());
				
				// 撤销实物进程跟踪调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp=new PrcMaterielProcess();
				pmp.setObject_no(pcc.getObject_no());
				pmp.setPrc_code("5BPC02");//新增的状态
				pmp.setRemark("计划取消下发");
				pmp.setKeep_sametime_status(0);
				//撤销实物物料状态跟踪函数:
				String tmp=this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();
			}
		}

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

}

