/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-06
 * 描述            ：改判申请记录表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/

package market.Qms.qcm.service.impl;

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

import market.Api.Lms.impl.LmsQmsApiServiceImpl;
import market.Com.mapper.ComBaseMachineMapper;
import market.Com.mapper.ComBasePlineMapper;
import market.Com.mapper.ComBaseSampleMapper;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.ComBasePline;
import market.Com.vo.ComBaseSample;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Tpm.vo.MesTpmD;
import market.Mes.anneal.impl.MesCrAnnealMServiceImpl;
import market.Qms.common.DataHelper;
import market.Qms.metallurgy.mapper.ComMscStdPhyMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.mapper.ComPscRMscMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscStdPhy;
import market.Qms.metallurgy.vo.ComMscStdPic;
import market.Qms.qcm.mapper.QcmRejudgeApplyMapper;
import market.Qms.qcm.service.QcmRejudgeApplyService;
import market.Qms.qcm.vo.ComMscExemption;
import market.Qms.qcm.vo.QcmJhyPhyresult;
import market.Qms.qcm.vo.QcmJhySampleFromPlan;
import market.Qms.qcm.vo.QcmRejudgeApply;
import market.Qms.qcm.vo.QcmRejudgeApplyItem;
import market.Qms.slmord.impl.QcmJudgeBlockadeInfoServiceImpl;
import market.Qms.slmord.impl.QualityDesignStdPicServiceImpl;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicSmpMapper;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdPic;
import market.Qms.slmord.vo.SlmOrdDesignStdPicSmp;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

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 = "QcmRejudgeApplyService")
public class QcmRejudgeApplyServiceImpl extends BaseServiceImpl<QcmRejudgeApply, Object>
		implements QcmRejudgeApplyService {

	/**
	 * 改判判定检验
	 */
	public ResultModel getJudge(CommonPage<QcmRejudgeApply> sum) {
		ResultModel rm = new ResultModel();
		List<QcmRejudgeApply> listJudges = sum.getObjectList();

		if (listJudges != null && listJudges.size() > 0) {

			String old_inspection_lot = listJudges.get(0).getOld_inspection_lot();
			// String old_psc=listJudges.get(0).getOld_psc();

			for (QcmRejudgeApply qcmRejudgeApply : listJudges) {

				if (old_inspection_lot == null) {
					rm.setState("500");
					rm.setMsgInfo("原检验号不存在，请确认");
					return rm;
				}

				/*if (!old_inspection_lot.equals(qcmRejudgeApply.getOld_inspection_lot())) {

					rm.setState("500");
					rm.setMsgInfo("选择的物料信息不属于一个检验号");
					return rm;

				}*/

				/*
				 * if (!old_psc.equals(qcmRejudgeApply.getOld_psc())) {
				 * rm.setState("500"); rm.setMsgInfo("选择的物料信息不属于一个产品码"); return
				 * rm; }
				 */

			}

		} else {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}

		rm.setState("200");
		rm.setData(listJudges);

		return rm;

	}

	/**
	 * 改判申请确认
	 * 
	 * @throws Exception
	 */

	/*
	 * @Override
	 * 
	 * @Transactional(value = "tm", rollbackFor = Exception.class) public
	 * ResultModel judgeSure(CommonPage<QcmRejudgeApply> sum) throws Exception {
	 * ResultModel rm = new ResultModel(); List<QcmRejudgeApply> listJudges =
	 * sum.getObjectList(); String username = this.getUserName();
	 * 
	 * try {
	 * 
	 * if (listJudges != null && listJudges.size() > 0) {
	 * 
	 * // 遍历集合，写表操作 for (QcmRejudgeApply qcmRejudgeApply : listJudges) {
	 * 
	 * // 改判申请记录号（R+两位年/月/日+3位流水号）
	 * 
	 * int counts = 0; List<QcmRejudgeApply> qlist =
	 * this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, null,
	 * "REJUDGE_ID desc", 0, 0); if (qlist != null && qlist.size() >= 1) {
	 * String sn = qlist.get(0).getRejudge_id(); counts =
	 * Integer.parseInt(sn.substring(sn.length() - 3, sn.length())); } counts++;
	 * SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); String rejudge_id
	 * = "R" + sdf.format(new Date()) + String.format("%03d", counts);
	 * qcmRejudgeApply.setRejudge_id(rejudge_id);
	 * 
	 * qcmRejudgeApply.setCreate_name(username);
	 * qcmRejudgeApply.setCreate_time(new Date());
	 * qcmRejudgeApply.setRejudge_status("1");
	 * qcmRejudgeApply.setCfm_name(username); qcmRejudgeApply.setCfm_time(new
	 * Date());
	 * this.getMapper(QcmRejudgeApplyMapper.class).insert(qcmRejudgeApply);
	 * //锁定物流状态 try { if (!"1".equals(qcmRejudgeApply.getFlag())) { ResultModel
	 * r=this.getBean(LmsApiServiceImpl.class).UpdateYdmState(qcmRejudgeApply.
	 * getMaterial_no(), "6"); if (!"200".equals(r.getState())) { throw new
	 * Exception(r.getState()); } }
	 * 
	 * } catch (Exception e) { rm.setState("500"); rm.setMsgInfo("钢材状态失败");
	 * throw new Exception(rm.getMsgInfo()); }
	 * 
	 * 
	 * }
	 * 
	 * 
	 * 
	 * rm.setState("200"); rm.setMsgInfo("改判申请发布成功"); rm.setData(listJudges);
	 * 
	 * } else {
	 * 
	 * rm.setState("500"); rm.setMsgInfo("请传入参数"); return rm;
	 * 
	 * }
	 * 
	 * } catch (Exception e) { rm.setState("500"); rm.setMsgInfo("改判申请确认失败");
	 * rm.setErrorInfo(e.getMessage()); throw new Exception(rm.getMsgInfo());
	 * 
	 * }
	 * 
	 * return rm; }
	 */
	@Override
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel judgeSure(CommonPage<QcmRejudgeApply> sum) throws Exception {
		ResultModel rm = new ResultModel();
		List<QcmRejudgeApply> listJudges = sum.getObjectList();
		String username = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();

		try {

			// 数据验证
			if (listJudges != null && listJudges.size() > 0) {
				boolean pass = validationData(listJudges, rm, sum.getMemo1());
				if (!pass) {
					return rm;
				}
			}else{
				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;
			}
			//数据处理，获取目标厚宽长
			//原检验号相同、目标厚宽长相同、产品码、交货状态、牌号等一致生成一个改判记录号
			TreeSet<String> set=new TreeSet<String>();
			for (QcmRejudgeApply app : listJudges) {
				String same=DataHelper.Get00000(app.getOld_inspection_lot())+"_"+DataHelper.Get00000(app.getOld_psc())+"_"+DataHelper.Get00000(app.getOld_std_code())
				+"_"+DataHelper.Get00000(app.getOld_std_name())+"_"+DataHelper.Get00000(app.getOld_prod_code())+"_"+DataHelper.Get00000(app.getOld_prod_name())+"_"+DataHelper.Get00000(app.getOld_steel_code())
				+"_"+DataHelper.Get00000(app.getOld_steel_name())+"_"+DataHelper.Get00000(app.getOld_delivery_state_code())+"_"+DataHelper.Get00000(app.getOld_delivery_state_desc())
				+"_"+DataHelper.Get00000(app.getPlan_thick())+"_"+DataHelper.Get00000(app.getPlan_width())+"_"+DataHelper.Get00000(app.getPlan_length());
				set.add(same);
			}
			//获取改判申请记录id
			// 改判申请记录号（R+两位年/月/日+3位流水号）
			int counts = 0;
			List<QcmRejudgeApply> qlist = this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, null,
					"REJUDGE_ID desc", 0, 0);
			if (qlist != null && qlist.size() >= 1) {
				String sn = qlist.get(0).getRejudge_id();
				counts = Integer.parseInt(sn.substring(sn.length() - 3, sn.length()));
			}
			counts++;
			for (String key : set) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
				String rejudge_id = "R" + sdf.format(new Date()) + String.format("%03d", counts++);
				for (QcmRejudgeApply app : listJudges) {
					String same=DataHelper.Get00000(app.getOld_inspection_lot())+"_"+DataHelper.Get00000(app.getOld_psc())+"_"+DataHelper.Get00000(app.getOld_std_code())
					+"_"+DataHelper.Get00000(app.getOld_std_name())+"_"+DataHelper.Get00000(app.getOld_prod_code())+"_"+DataHelper.Get00000(app.getOld_prod_name())+"_"+DataHelper.Get00000(app.getOld_steel_code())
					+"_"+DataHelper.Get00000(app.getOld_steel_name())+"_"+DataHelper.Get00000(app.getOld_delivery_state_code())+"_"+DataHelper.Get00000(app.getOld_delivery_state_desc())
					+"_"+DataHelper.Get00000(app.getPlan_thick())+"_"+DataHelper.Get00000(app.getPlan_width())+"_"+DataHelper.Get00000(app.getPlan_length());
				
					if (key.equals(same)) {
						app.setRejudge_id(rejudge_id);
						app.setCreate_name(username);
						app.setCreate_time(new Date());
						app.setRejudge_status("0");
						
						// 根据机台信息，查询产线信息
						if (!"1".equals(sum.getMemo1())) {
							ComBaseMachine mation = this.getMapper(ComBaseMachineMapper.class)
									.findById(app.getMachine_code());
							if (mation != null && mation.getPline_code() != null) {
								app.setPline_code(mation.getPline_code());
								ComBasePline pline = this.getMapper(ComBasePlineMapper.class)
										.findById(mation.getPline_code());
								if (pline != null) {
									app.setPline_name(pline.getPline_name());
								}
							}
						}
						
						this.getMapper(QcmRejudgeApplyMapper.class).insert(app);
						// 锁定物流状态
						try {
							this.getBean(LmsQmsApiServiceImpl.class).doUpdateLmsQmsState(app.getMaterial_no(),
									"6");
						} catch (Exception e) {
							rm.setState("500");
							rm.setMsgInfo("锁定钢材["+app.getMaterial_no()+"]状态失败");
							throw new Exception(rm.getMsgInfo());

						}
					}
				}
				
			}

			
			

			rm.setState("200");
			rm.setMsgInfo("改判申请确认成功！");
			return rm;

			

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("改判申请确认失败");
			rm.setErrorInfo(e.getMessage());
			throw new Exception(rm.getMsgInfo());

		}

		
	}

	/**
	 * 改判申请取人数据验证
	 * 
	 * @param listJudges
	 * @param rm
	 * @param memo1
	 * @return
	 */
	private boolean validationData(List<QcmRejudgeApply> listJudges, ResultModel rm, String memo1) {
		if (listJudges == null || listJudges.size() < 1) {
			rm.setState("500");
			rm.setMsgInfo("请输入需要改判申请的数据！");
			return false;
		}

		// 获取原检验号
		for (QcmRejudgeApply app : listJudges) {
			if (app.getMaterial_no() == null || "".equals(app.getMaterial_no())) {
				rm.setState("500");
				rm.setMsgInfo("请传入物料号，请确认！");
				return false;
			}
			if (app.getOld_inspection_lot() == null || "".equals(app.getOld_inspection_lot())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原检验号，不能发起改判！");
				return false;
			}
			if (app.getOld_psc() == null || "".equals(app.getOld_psc())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原产品码，不能发起改判！");
				return false;
			}
			if (app.getOld_std_code() == null || "".equals(app.getOld_std_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原订货标准代码，不能发起改判！");
				return false;
			}
			if (app.getOld_std_name() == null || "".equals(app.getOld_std_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原订货标准名称，不能发起改判！");
				return false;
			}
			if (app.getOld_delivery_state_code() == null || "".equals(app.getOld_delivery_state_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原交货状态代码，不能发起改判！");
				return false;
			}
			if (app.getOld_delivery_state_desc() == null || "".equals(app.getOld_delivery_state_desc())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原交货状态名称，不能发起改判！");
				return false;
			}
			if (app.getOld_prod_code() == null || "".equals(app.getOld_prod_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原产品名称代码，不能发起改判！");
				return false;
			}
			if (app.getOld_prod_name() == null || "".equals(app.getOld_prod_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原产品名称描述，不能发起改判！");
				return false;
			}
			if (app.getOld_steel_code() == null || "".equals(app.getOld_steel_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原产品牌号代码，不能发起改判！");
				return false;
			}
			if (app.getOld_steel_name() == null || "".equals(app.getOld_steel_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无原产品牌号描述，不能发起改判！");
				return false;
			}
			// 目标信息验证
			if (app.getPsc() == null || "".equals(app.getPsc())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品码，不能发起改判！");
				return false;
			}
			if (app.getStd_code() == null || "".equals(app.getStd_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标订货标准代码，不能发起改判！");
				return false;
			}
			if (app.getStd_name() == null || "".equals(app.getStd_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标订货标准名称，不能发起改判！");
				return false;
			}
			if (app.getDelivery_state_code() == null || "".equals(app.getDelivery_state_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标交货状态代码，不能发起改判！");
				return false;
			}
			if (app.getDelivery_state_desc() == null || "".equals(app.getDelivery_state_desc())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标交货状态名称，不能发起改判！");
				return false;
			}
			if (app.getProd_code() == null || "".equals(app.getProd_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品名称代码，不能发起改判！");
				return false;
			}
			if (app.getProd_name() == null || "".equals(app.getProd_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品名称描述，不能发起改判！");
				return false;
			}
			if (app.getSteel_code() == null || "".equals(app.getSteel_code())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品牌号代码，不能发起改判！");
				return false;
			}
			if (app.getSteel_name() == null || "".equals(app.getSteel_name())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品牌号描述，不能发起改判！");
				return false;
			}
			if (app.getPlan_thick() == null || "".equals(app.getPlan_thick())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品厚度，不能发起改判！");
				return false;
			}
			if (app.getPlan_width() == null || "".equals(app.getPlan_width())) {
				rm.setState("500");
				rm.setMsgInfo("物料[" + app.getMaterial_no() + "]无目标产品宽度，不能发起改判！");
				return false;
			}

		}
		String delivery_code = listJudges.get(0).getOld_delivery_state_code();
		String psc = listJudges.get(0).getPsc();
		for (QcmRejudgeApply app : listJudges) {
			if (!app.getOld_delivery_state_code().equals(delivery_code)) {
				rm.setState("500");
				rm.setMsgInfo("改判批存在不同的原交货状态，不能发起改判！");
				return false;
			}
			if (!app.getPsc().equals(psc)) {
				rm.setState("500");
				rm.setMsgInfo("改判批存在不同的目标产品码，不能发起改判！");
				return false;
			}

			if (!"1".equals(memo1)) {
				// 根据机台信息，查询产线信息
				if (app.getMachine_code()==null || "".equals(app.getMachine_code())) {
					rm.setState("500");
					rm.setMsgInfo("物料["+app.getMaterial_no()+"]的机台代码为空，不能发起改判！");
					return false;
				}
				ComBaseMachine mation = this.getMapper(ComBaseMachineMapper.class).findById(app.getMachine_code());
				if (mation != null) {
					if (mation.getPline_code() == null || "".equals(mation.getPline_code())) {
						rm.setState("500");
						rm.setMsgInfo("物料["+app.getMaterial_no()+"]的机台代码["+app.getMachine_code()+"]对产线代码不存在，不能发起改判！");
						return false;
					}
					ComBasePline pline = this.getMapper(ComBasePlineMapper.class).findById(mation.getPline_code());
					if (pline != null) {
						app.setPline_code(mation.getPline_code());
						app.setPline_name(pline.getPline_name());
					}else{
						rm.setState("500");
						rm.setMsgInfo("物料["+app.getMaterial_no()+"]的机台代码["+app.getMachine_code()+"]对产线不存在，不能发起改判！");
						return false;
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("物料["+app.getMaterial_no()+"]的机台代码["+app.getMachine_code()+"]对应机台不存在，不能发起改判！");
					return false;
				}

			}else{
				if (app.getPline_code() == null || "".equals(app.getPline_code())) {
					rm.setState("500");
					rm.setMsgInfo("物料["+app.getMaterial_no()+"]的产线代码不存在，不能发起改判！");
					return false;
				}
				if (app.getPline_name() == null || "".equals(app.getPline_name())) {
					rm.setState("500");
					rm.setMsgInfo("物料["+app.getMaterial_no()+"]的产线名称不存在，不能发起改判！");
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * 改判申请取消
	 * 
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel deleteApply(CommonPage<QcmRejudgeApply> sum) throws Exception {

		ResultModel rm = new ResultModel();
		List<QcmRejudgeApply> applys = sum.getObjectList();

		try {

			if (applys == null) {

				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;

			}

			for (QcmRejudgeApply qcmRejudgeApply : applys) {

				QcmRejudgeApply app=this.getMapper(QcmRejudgeApplyMapper.class).findById(qcmRejudgeApply.getRejudge_id(),
						qcmRejudgeApply.getMaterial_no());
				if (app!=null) {
					if ("0".equals(app.getRejudge_status())) {
						this.getMapper(QcmRejudgeApplyMapper.class).delete(app.getRejudge_id(),
								app.getMaterial_no());
						// 恢复物料在库状态
						try {
							this.getBean(LmsQmsApiServiceImpl.class).doUpdateLmsQmsState(app.getMaterial_no(), "0");
						} catch (Exception e) {
							e.printStackTrace();
							throw new Exception("刪除失败");
						}
					}
				}
				
			}

			rm.setData(applys);
			rm.setMsgInfo("刪除成功");
			rm.setState("200");

		} catch (Exception e) {
			rm.setMsgInfo("删除失败" + e.getMessage());
			rm.setState("500");
			throw new Exception();

		}

		return rm;
	}

	/**
	 * 改判申请信息查询
	 */

	public ResultModel querryInfo(CommonPage<QcmRejudgeApply> sum) {

		ResultModel rm = new ResultModel();
		try {
			QcmRejudgeApply qa = sum.getObject();
			if (qa == null) {
				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;

			}

			String str = "1=1";
			if (qa.getMaterial_no() != null && !qa.getMaterial_no().equals("")) {

				str += "and MATERIAL_NO like '%" + qa.getMaterial_no() + "%' and REJUDGE_STATUS='0'";

			}

			if (sum.getStartTime() != null && sum.getEndTime() != null) {
				str += "and (CFM_TIME>='" + sum.getStartTime() + "' and CFM_TIME<='" + sum.getEndTime() + "')";
			}

			if (qa.getMaterial_no() == null && sum.getStartTime() == null && sum.getEndTime() == null
					&& qa.getMaterial_no().equals("") && sum.getStartTime().equals("") && sum.getEndTime().equals("")) {
				str += "and CFM_TIME >= to_date(' " + "sysdate - 30"
						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";
			}

			rm.setData(this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, str, null, 0, 0));
			rm.setState("200");

		} catch (Exception e) {
			rm.setMsgInfo("查询失败" + e.getMessage());
			rm.setState("500");
			return rm;
		}

		return rm;

	}

	/**
	 * 改判确认申请查询
	 */

	public ResultModel querryJudge(CommonPage<QcmRejudgeApply> sum) {

		ResultModel rm = new ResultModel();
		try {
			QcmRejudgeApply qa = sum.getObject();
			if (qa == null) {
				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;

			}

			String str = "1=1";
			if (qa.getMaterial_no() != null && !qa.getMaterial_no().equals("")) {

				str += "and MATERIAL_NO like '%" + qa.getMaterial_no() + "%' and REJUDGE_STATUS='0'";

			}
			if (sum.getStartTime() != null && !sum.getStartTime().equals("")) {

				str += " and CFM_TIME >= to_date('" + sum.getStartTime()
						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";

			}
			if (sum.getEndTime() != null && sum.getEndTime().equals("")) {
				str += " and CFM_TIME <= to_date('" + sum.getEndTime()
						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";

			}
			if (qa.getMaterial_no() == null && sum.getStartTime() == null && sum.getEndTime() == null
					&& qa.getMaterial_no().equals("") && sum.getStartTime().equals("") && sum.getEndTime().equals("")) {
				str += "and CFM_TIME >= to_date(' " + "sysdate - 30"
						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";
			}

			rm.setData(
					this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, str, "REJUDGE_ID desc", 0, 0));
			rm.setState("200");

		} catch (Exception e) {
			rm.setMsgInfo("查询失败" + e.getMessage());
			rm.setState("500");
			return rm;
		}

		return rm;

	}
	
	
	/**
	 * 改判确认申请查询
	 */

	public ResultModel querryJudgeApply(CommonPage<QcmRejudgeApply> sum) {

		ResultModel rm = new ResultModel();
		try {
			QcmRejudgeApply qa = sum.getObject();
			if (qa == null) {
				rm.setMsgInfo("请传入参数");
				rm.setState("500");
				return rm;

			}

			List<QcmRejudgeApply> apps=this.getMapper(QcmRejudgeApplyMapper.class).findByTime(sum.getStartTime(),sum.getEndTime(),qa.getSteel_name(),qa.getContract_line_desc(),qa.getRejudge_status());
//			String str = "1=1";
//			if (qa.getMaterial_no() != null && !qa.getMaterial_no().equals("")) {
//
//				str += "and MATERIAL_NO like '%" + qa.getMaterial_no() + "%' and REJUDGE_STATUS='0'";
//
//			}
//			if (sum.getStartTime() != null && !sum.getStartTime().equals("")) {
//
//				str += " and CFM_TIME >= to_date('" + sum.getStartTime()
//						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";
//
//			}
//			if (sum.getEndTime() != null && sum.getEndTime().equals("")) {
//				str += " and CFM_TIME <= to_date('" + sum.getEndTime()
//						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";
//
//			}
//			if (qa.getMaterial_no() == null && sum.getStartTime() == null && sum.getEndTime() == null
//					&& qa.getMaterial_no().equals("") && sum.getStartTime().equals("") && sum.getEndTime().equals("")) {
//				str += "and CFM_TIME >= to_date(' " + "sysdate - 30"
//						+ "','yyyy/mm/dd HH24:mi:ss') and REJUDGE_STATUS='0'";
//			}

			rm.setData(apps);
			rm.setState("200");

		} catch (Exception e) {
			rm.setMsgInfo("查询失败" + e.getMessage());
			rm.setState("500");
			return rm;
		}

		return rm;

	}
	
	
	
	
	

	/**
	 * 获得原检验信息
	 */

	public ResultModel querryOldJudge(CommonPage<QcmRejudgeApply> sum) {
		ResultModel rm = new ResultModel();
		QcmRejudgeApply qd = sum.getObject();

		try {

			if (qd == null) {
				rm.setMsgInfo("请选择参数");
				rm.setState("500");
				return rm;

			}

			List<HashMap> qds = this.getMapper(QcmRejudgeApplyMapper.class)
					.findByInspection(qd.getOld_inspection_lot());
			rm.setData(qds);
			rm.setState("200");

		} catch (Exception e) {
			rm.setState("500");
			rm.setErrorInfo("查询失败" + e.getMessage());
		}

		return rm;
	}

	/**
	 * 查询检验信息
	 * 
	 * @param sum
	 * @return
	 */

	public ResultModel querryJudgeInfo(CommonPage<QcmRejudgeApply> sum) {
		ResultModel rm = new ResultModel();
		QcmRejudgeApply qd = sum.getObject();

		try {
			if (qd == null) {
				rm.setMsgInfo("请选择参数");
				rm.setState("500");
				return rm;

			}

			// 判断销售订单号是否为空
			if (qd.getContract_line_no() != null && qd.getDesign_key() != null
					&& !qd.getDesign_key().trim().equals("")) {

				String psc = qd.getPsc();
				String design_key = qd.getDesign_key();

				SlmOrdDesignStd std = this.getMapper(SlmOrdDesignStdMapper.class).findById(design_key, psc);
				// 判断是否免检
				ResultModel mianJian = this.getMapper(ComMscExemptionServiceImpl.class).judgeExemptedByDk(design_key);
				ComMscExemption cp = (ComMscExemption) mianJian.getData();
				if (cp != null) {
					if ("1".equals(cp.getIs_pic_exmt())) {
						rm.setState("200");
						rm.setMsgInfo("材质项目已经免检，可直接进行改判！");
						return rm;
					}
				}

				if (std != null) {

					String PIC = std.getPic();
					String PIC_K = std.getPic_k();
					List<SlmOrdDesignStdPic> pic = this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(
							null, "DESIGN_KEY='" + design_key + "' and PIC='" + PIC + "'", null, 0, 0);
					List<SlmOrdDesignStdPic> pic_k = this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(
							null, "DESIGN_KEY='" + design_key + "' and PIC='" + PIC_K + "'", null, 0, 0);
					// 遍历集合，获取检验项目和标准值
					List<SlmOrdDesignStdPic> targetProject = new ArrayList<SlmOrdDesignStdPic>();
					for (SlmOrdDesignStdPic pk : pic_k) {

						// 获取检验项目的值
						String testProject = pk.getPhy_code_l() + pk.getPhy_code_m() + pk.getPhy_code_s()
								+ pk.getItem_code_d() + pk.getItem_code_s() + pk.getItem_code_t()
								+ pk.getSampleStyleCode();

						for (int i = 0; i < pic.size(); i++) {

							String oldProject = pic.get(i).getPhy_code_l() + pic.get(i).getPhy_code_m()
									+ pic.get(i).getPhy_code_s() + pic.get(i).getItem_code_d()
									+ pic.get(i).getItem_code_s() + pic.get(i).getItem_code_t()
									+ pic.get(i).getSampleStyleCode();

							if (testProject.equals(oldProject)) {
								targetProject.add(pk);
								break;

							} else if (i == pic.size() - 1) {
								targetProject.add(pk);
								// targetProject.add(pic.get(i));
								break;

							} else {
								continue;
							}

						}

					}

					// 反向验证
					for (SlmOrdDesignStdPic pi : pic) {
						String oldProject = pi.getPhy_code_l() + pi.getPhy_code_m() + pi.getPhy_code_s()
								+ pi.getItem_code_d() + pi.getItem_code_s() + pi.getItem_code_t()
								+ pi.getSampleStyleCode();

						for (int i = 0; i < pic_k.size(); i++) {
							// 获取检验项目的值
							String testProject = pic_k.get(i).getPhy_code_l() + pic_k.get(i).getPhy_code_m()
									+ pic_k.get(i).getPhy_code_s() + pic_k.get(i).getItem_code_d()
									+ pic_k.get(i).getItem_code_s() + pic_k.get(i).getItem_code_t()
									+ pic_k.get(i).getSampleStyleCode();

							if (oldProject.equals(testProject)) {
								break;
							}
							if (i == pic_k.size() - 1) {
								targetProject.add(pi);
							}
						}

					}
					for (int i = 0; i < targetProject.size(); i++) {
						if ("0".equals(targetProject.get(i).getIscheck())) {
							targetProject.remove(i);
							i--;
						}
					}

					List<QcmRejudgeApplyItem> items = new ArrayList<QcmRejudgeApplyItem>();
					for (SlmOrdDesignStdPic tp : targetProject) {
						String sample_key = DataHelper.GetNull(tp.getPhy_code_m()) + "_"
								+ DataHelper.GetNull(tp.getItem_code_d()) + "_"
								+ DataHelper.GetNull(tp.getItem_code_t()) + "_"
								+ DataHelper.GetNull(tp.getItem_code_s());
						List<SlmOrdDesignStdPicSmp> sm = this.getMapper(SlmOrdDesignStdPicSmpMapper.class)
								.findWithCondition(null, "DESIGN_KEY='" + tp.getDesign_key() + "' and PIC='"
										+ tp.getPic() + "' and SAMPLE_KEY='" + sample_key + "'", "RATE desc", 0, 0);

						QcmRejudgeApplyItem item = new QcmRejudgeApplyItem();
						item.setItem_code_d(tp.getItem_code_d());
						item.setItem_code_s(tp.getItem_code_s());
						item.setItem_code_t(tp.getItem_code_t());
						item.setItem_name_d(tp.getItem_name_d());
						item.setItem_name_s(tp.getItem_name_s());
						item.setItem_name_t(tp.getItem_name_t());
						item.setPhy_code_l(tp.getPhy_code_l());
						item.setPhy_code_m(tp.getPhy_code_m());
						item.setPhy_code_s(tp.getPhy_code_s());
						item.setPhy_name_l(tp.getPhy_name_l());
						item.setPhy_name_m(tp.getPhy_name_m());
						item.setPhy_name_s(tp.getPhy_name_s());
						item.setSample_style_code(tp.getSampleStyleCode());
						item.setSample_style_name(tp.getSampleStyleName());
						item.setStdmax(tp.getStdmax());
						item.setStdmax_sign(tp.getStdmax_sign());
						item.setStdmin(tp.getStdmin());
						item.setStdmin_sign(tp.getStdmin_sign());
						if (sm != null && sm.size() > 0) {
							item.setDesc_rate(sm.get(0).getDesc_rate());
							item.setRate(sm.get(0).getRate());

						}

						items.add(item);

						/*
						 * item.setRate(slmRate.getRate());
						 * item.setDesc_rate(slmRate.getDesc_rate());
						 * tp.setRate(slmRate.getRate());
						 * tp.setDesc_rate(slmRate.getDesc_rate()); rms.add(tp);
						 */

					}

					rm.setData(items);
					rm.setState("200");

				}

			} else if (qd.getPsc() != null && !"".equals(qd.getPsc())) {

				// 获取msc0
				List<ComMsc> mscs = this.getMapper(ComPscRMscMapper.class).findByPsc(qd.getPsc());
				// 遍历集合获取MSC

				if (mscs == null || mscs.size() < 1) {
					/*rm.setErrorInfo("产品码[" + qd.getPsc() + "]关联的基础制造规范码不存在或已经作废!");
					rm.setState("500");*/
					return rm;
				}

				List<ComMscStdPic> pic = this.getMapper(ComMscStdPicMapper.class).findWithCondition(null,
						"MSC='" + mscs.get(0).getMsc() + "'", null, 0, 0);

				if (pic==null || pic.size()<1) {
					/*rm.setErrorInfo("产品码[" + qd.getPsc() + "]关联的基础制造规范码不存在或已经作废!");
					rm.setState("500");*/
					return rm;
				}
				// 过滤交货状态
				for (int i = 0; i < pic.size(); i++) {
					if (pic.get(i).getDeliveryStateCode() != null
							&& !pic.get(i).getDeliveryStateCode().contains(qd.getDelivery_state_code())) {
						pic.remove(i);
						i--;
					}

				}

				// 验证
				if (pic.size() > 1) {

					/*rm.setErrorInfo("找到多条符合条件的标准索引信息，标准维护错误，请确认!");
					rm.setState("500");
					return rm;
*/
				} else if (pic.size() < 1) {
					/*rm.setState("500");
					return rm;*/

				} else {

					List<ComMscStdPhy> phy = this.getMapper(ComMscStdPhyMapper.class).findWithCondition(null,
							"PIC='" + pic.get(0).getPic() + "'", null, 0, 0);

					if (phy.size() < 1) {

						/*rm.setErrorInfo("未找到符合条件的标准信息！");
						rm.setState("500");*/
						return rm;

					}
					// 过滤规格
					SlmContractProcessPlan orderLine = new SlmContractProcessPlan();
					if (qd.getPlan_thick()==null || "".equals(qd.getPlan_thick())) {
						return rm;
					}
					if (qd.getPlan_width()==null || "".equals(qd.getPlan_width())) {
						return rm;
					}
					orderLine.setThick(Double.valueOf(qd.getPlan_thick()));
					orderLine.setWidth(Double.valueOf(qd.getPlan_width()));
					if (qd.getPlan_length() == null || "".equals(qd.getPlan_length())) {
						orderLine.setLength(null);
					} else {
						orderLine.setLength(Double.valueOf(qd.getPlan_length()));
					}

					List<ComMscStdPhy> phys = new ArrayList<ComMscStdPhy>();
					// 调用材质过滤接口
					phys = this.getBean(QualityDesignStdPicServiceImpl.class).FilterSpec(phy, orderLine);

					// 过滤不需要检验的项目
					for (int i = 0; i < phys.size(); i++) {
						if ("0".equals(phys.get(i).getIscheck())) {
							phys.remove(i);
							i--;
						}

					}
					List<QcmRejudgeApplyItem> items = new ArrayList<QcmRejudgeApplyItem>();

					for (ComMscStdPhy comMscStdPhy : phys) {
						String sample_key = DataHelper.GetNull(comMscStdPhy.getPhyCodeM()) + "_"
								+ DataHelper.GetNull(comMscStdPhy.getItemCodeD()) + "_"
								+ DataHelper.GetNull(comMscStdPhy.getItemCodeT()) + "_"
								+ DataHelper.GetNull(comMscStdPhy.getItemCodeS());
						List<ComBaseSample> samples = this.getMapper(ComBaseSampleMapper.class)
								.findBySample(comMscStdPhy.getPic(), sample_key);
						QcmRejudgeApplyItem item = new QcmRejudgeApplyItem();
						item.setItem_code_d(comMscStdPhy.getItemCodeD());
						item.setItem_code_s(comMscStdPhy.getItemCodeS());
						item.setItem_code_t(comMscStdPhy.getItemCodeT());
						item.setItem_name_d(comMscStdPhy.getItemNameD());
						item.setItem_name_s(comMscStdPhy.getItemNameS());
						item.setItem_name_t(comMscStdPhy.getItemNameT());
						item.setPhy_code_l(comMscStdPhy.getPhyCodeL());
						item.setPhy_code_m(comMscStdPhy.getPhyCodeM());
						item.setPhy_code_s(comMscStdPhy.getPhyCodeS());
						item.setPhy_name_l(comMscStdPhy.getPhyNameL());
						item.setPhy_name_m(comMscStdPhy.getPhyNameM());
						item.setPhy_name_s(comMscStdPhy.getPhyNameS());
						item.setSample_style_code(comMscStdPhy.getSampleStyleCode());
						item.setSample_style_name(comMscStdPhy.getSampleStyleName());
						item.setStdmax(comMscStdPhy.getStdmax());
						item.setStdmax_sign(comMscStdPhy.getStdmaxSign());
						item.setStdmin(comMscStdPhy.getStdmin());
						item.setStdmin_sign(comMscStdPhy.getStdminSign());
						if (samples != null && samples.size() > 0) {
							item.setRate(samples.get(0).getRate());
							item.setDesc_rate(samples.get(0).getDesc_rate());
						}

						items.add(item);

					}

					rm.setData(items);
					rm.setState("200");
				}

			}

		} catch (Exception e) {
			rm.setState("500");
			rm.setErrorInfo("查询失败" + e.getMessage());
		}

		return rm;
	}

	/**
	 * 获取引用
	 * 
	 * @param sum
	 * @return
	 */

	public ResultModel getQuote(CommonPage<QcmJhyPhyresult> sum) {
		ResultModel rm = new ResultModel();
		List<QcmJhyPhyresult> results = sum.getObjectList();
		List<QcmJhyPhyresult> targets = new ArrayList<QcmJhyPhyresult>();

		try {

			if (results == null && results.size() < 1) {
				rm.setMsgInfo("请选择参数");
				rm.setState("500");
				return rm;

			}

			// 检验大项代码、试样组代码、检验项目代码相同
			String phy_code_l = sum.getMemo1();
			String phy_code_m = sum.getMemo2();
			String phy_code_s = sum.getMemo3();

			// 遍历集合查询符合条件的集合
			for (QcmJhyPhyresult qcmJhyPhyresult : results) {

				if (qcmJhyPhyresult.getPhy_code_l().equals(phy_code_l)
						&& qcmJhyPhyresult.getPhy_code_m().equals(phy_code_m)
						&& qcmJhyPhyresult.getPhy_code_s().equals(phy_code_s)) {
					targets.add(qcmJhyPhyresult);
					continue;
				}

			}

			rm.setState("200");
			rm.setData(targets);

		} catch (Exception e) {
			rm.setErrorInfo("未查询到匹配的的数据" + e.getMessage());
			rm.setState("500");

		}

		return rm;
	}

	/**
	 * 重新改判确认
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel reTestResure(CommonPage<QcmRejudgeApply> sum) throws Exception {

		ResultModel rm = new ResultModel();

		try {

			//String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			if (sum==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			List<QcmRejudgeApply> ap = sum.getObjectList();
			if (ap==null || ap.size()<1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要改判确认的数据");
				return rm;
			}
			
			// 获取改判的检验项目
			List<QcmRejudgeApplyItem> items = ap.get(0).getListItems();
			if (items==null ||items.size()<1) {
				rm.setState("500");
				rm.setMsgInfo("请传入改判项目信息");
				return rm;
			}
			List<QcmRejudgeApply> apps=new ArrayList<QcmRejudgeApply>();
			for (QcmRejudgeApply app : ap) {
				List<QcmRejudgeApply> ran=this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, "REJUDGE_ID='"+app.getRejudge_id()+"'", null, 0, 0);
				for (QcmRejudgeApply r : ran) {
					if (!"0".equals(r.getRejudge_status())) {
						rm.setState("500");
						rm.setMsgInfo("不能确认不属于待确认状态的改判记录！");
						return rm;
					}
				}
				apps.addAll(ran);
			}
			String app_id=apps.get(0).getRejudge_id();
			for (QcmRejudgeApply app : apps) {
				if (!app_id.equals(app.getRejudge_id())) {
					rm.setState("500");
					rm.setMsgInfo("改判记录号不一致，不能同时进行改判确认");
					return rm;
				}
			}
			// 数据验证
			for (QcmRejudgeApplyItem it : items) {
				// 判断取样频率是否为空
				if (it.getRate() == null || "".equals(it.getRate())) {
					rm.setState("500");
					rm.setMsgInfo("试样组[" + it.getPhy_name_m() + "]未关联取样要求");
					return rm;

				}

			}
			// 获取原检验号的工序信息
			/*
			 * for (QcmRejudgeApply app : apps) {}
			 * 
			 * // 如果检验项目为空，直接改变改判申请表里面的状态 if (items == null || items.size() < 1)
			 * { for (QcmRejudgeApply app : apps) { app.setRejudge_status("2");
			 * 
			 * this.getMapper(QcmRejudgeApplyMapper.class).update(app); }
			 * 
			 * rm.setState("200"); rm.setMsgInfo("改判确认成功"); return rm; }
			 */

			// 调用生产接口，获取检验号
			TreeSet<String> inspection_lots = new TreeSet<String>();
			for (QcmRejudgeApply app : apps) {
				CommonPage<MesTpmD> cp = new CommonPage<MesTpmD>();
				MesTpmD md = new MesTpmD();
				md.setSteel_code(app.getSteel_code());
				md.setCoil_no(app.getMaterial_no());
				md.setAct_wdt(Double.valueOf(app.getPlan_width()));
				md.setAct_thk(Double.valueOf(app.getPlan_thick()));
				cp.setObject(md);
				cp.setMemo1(app.getPline_code());
				try {
					ResultModel ir = this.getBean(MesCrAnnealMServiceImpl.class).getInspectLotWeb(cp);
					if ("500".equals(ir.getState())) {
						throw new Exception("调用生产生成检验号失败");
					}
					String inspection_lot = (String) ir.getData();
					if (inspection_lot == null || "".equals(inspection_lot)) {
						throw new Exception("未生成检验号");
					}
					app.setInspection_lot(inspection_lot);
					inspection_lots.add(inspection_lot);
				} catch (Exception e) {
					throw new Exception("调用生产生成检验号失败");
				}

			}

			List<QcmJhySampleFromPlan> plans = new ArrayList<QcmJhySampleFromPlan>();
			for (QcmRejudgeApply app : apps) {
				QcmJhySampleFromPlan plan = new QcmJhySampleFromPlan();
				app.setInspection_lot(app.getInspection_lot());
				plan.setInspection_lot(app.getInspection_lot());
				plan.setDesign_key(app.getDesign_key());
				plan.setPline_code(app.getPline_code());
				plan.setBoard_no(app.getBoard_no());
				plan.setMaterial_no(app.getMaterial_no());
				plan.setPline_name(app.getPline_name());
				plan.setBatch_no(app.getBatch_no());
				plan.setHeat_no(app.getHeat_no());
				plan.setContract_line_no(app.getContract_line_no());
				plan.setOrder_no(app.getContract_line_desc());
				plan.setJudge("1");
				plan.setMsc_pline(app.getMsc_pline());
				plan.setPsc(app.getPsc());
				plans.add(plan);

			}

			// 检验项目输入项目
			for (QcmJhySampleFromPlan qn : plans) {
				qn.setListItems(items);
			}

			CommonPage<QcmJhySampleFromPlan> pages = new CommonPage<QcmJhySampleFromPlan>();
			for (QcmJhySampleFromPlan qn : plans) {
				List<QcmJhySampleFromPlan> onlyOne = new ArrayList<QcmJhySampleFromPlan>();
				onlyOne.add(qn);
				pages.setObjectList(plans);
				ResultModel results = this.getBean(QcmJhySampleConsignDItemServiceImpl.class)
						.AutoGenerationSampleInfo(pages);
				if (!results.getState().equals("200")) {
					rm.setState("500");
					rm.setMsgInfo(results.getMsgInfo());
					rm.setErrorInfo(results.getErrorInfo());
					throw new Exception(rm.getMsgInfo());
				}

			}

			// 写入结果表
			/*
			 * List<QcmRejudgeApplyItem> listItems = apps.get(0).getListItems();
			 * for (QcmRejudgeApplyItem item : listItems) { String targetProject
			 * = item.getItem_code_d() + item.getItem_code_s() +
			 * item.getItem_code_t() + item.getPhy_code_l() +
			 * item.getPhy_code_m() + item.getPhy_code_s(); //
			 * 根据检验号和试样类型查询检验委托明细表获取取样编号 List<QcmJhySampleConsignD> qd =
			 * this.getMapper(QcmJhySampleConsignDMapper.class).
			 * findWithCondition(null, "INSPECTION_LOT='" + inspection_lot +
			 * "'", null, 0, 0); long seq = 1; for (QcmJhySampleConsignD
			 * qcmJhySampleConsignD : qd) {
			 * 
			 * String specimen_no = qcmJhySampleConsignD.getSpecimen_no(); //
			 * 根据取样编号查项目表 List<QcmJhySampleConsignDItem> qi =
			 * this.getMapper(QcmJhySampleConsignDItemMapper.class)
			 * .findWithCondition(null, "SPECIMEN_NO='" + specimen_no + "'",
			 * null, 0, 0);
			 * 
			 * for (QcmJhySampleConsignDItem q : qi) { String oldProject =
			 * q.getItem_code_d() + q.getItem_code_s() + q.getItem_code_t() +
			 * q.getPhy_code_l() + q.getPhy_code_m() + q.getPhy_code_s(); if
			 * (targetProject.equals(oldProject)) { if (item.getRe_phy_name_s()
			 * != null) { // 写入项目表 QcmJhyPhyresult result = new
			 * QcmJhyPhyresult(); DataHelper.CopyEntity(q, result);
			 * result.setSeq(seq++); result.setTest_qty(Long.valueOf("1"));
			 * result.setCreate_name(userName); result.setCreate_time(new
			 * Date()); result.setVal1(item.getRe_v1());
			 * result.setVal2(item.getRe_v2()); result.setVal3(item.getRe_v3());
			 * this.getMapper(QcmJhyPhyresultMapper.class).insert(result);
			 * 
			 * }
			 * 
			 * } } } }
			 */
			// 改变申请表状态
			List<YdmProductDetail> ydmList = new ArrayList<YdmProductDetail>();
			for (QcmRejudgeApply app : apps) {
				app.setRejudge_status("1");

				this.getMapper(QcmRejudgeApplyMapper.class).update(app);
				// 原检验信息
				YdmProductDetail ydm = new YdmProductDetail();
				ydm.setCoilno(app.getMaterial_no());
				ydmList.add(ydm);
				// 修改库存表信息
				YdmProductDetail ypd = new YdmProductDetail();
				ypd.setCoilno(app.getMaterial_no());
				ypd.setPsc(app.getPsc());
				ypd.setInspection_lot(app.getInspection_lot());
				ypd.setDelivery_state_code(app.getDelivery_state_code());
				ypd.setDelivery_state_desc(app.getDelivery_state_desc());
				ypd.setProd_code(app.getProd_code());
				ypd.setProd_name(app.getProd_name());
				ypd.setStd_code(app.getStd_code());
				ypd.setStd_name(app.getStd_name());
				ypd.setSteelcode(app.getSteel_code());
				ypd.setSteel_name(app.getSteel_name());
				this.getBean(LmsQmsApiServiceImpl.class).doUpdateLmsQmsInfo(ypd);

			}
			try {
				// 清空原检验信息
				ResultModel rs = this.getBean(QcmJudgeBlockadeInfoServiceImpl.class).EmptyJudInfo(ydmList);
				if ("403".equals(rs.getState()) || "500".equals(rs.getState())) {
					throw new Exception("清空物料检验信息失败");
				}
			} catch (Exception e) {
				throw new Exception("清空物料检验信息失败");
			}

			// 修改库存的牌号信息

			rm.setState("200");
			rm.setMsgInfo("改判确认成功");

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}

		return rm;

	}
	
	
	/**
	 * 根据申报号查询改判物料信息
	 */
	public ResultModel getApplyMaterial(CommonPage<QcmRejudgeApply> sum) {
		ResultModel rm=new ResultModel();
		List<QcmRejudgeApply> targets=new ArrayList<QcmRejudgeApply>();
		try {
			
			if (sum!=null) {
				QcmRejudgeApply app=sum.getObject();
				String rejudge_id=null;
				if (app!=null) {
					rejudge_id=app.getRejudge_id();
				}
				if (rejudge_id!=null && !"".equals(rejudge_id)) {
					List<QcmRejudgeApply> apps=this.getMapper(QcmRejudgeApplyMapper.class).findWithCondition(null, "REJUDGE_ID='"+rejudge_id+"'", null, 0, 0);
					targets.addAll(apps);
					}
			}
		} catch (Exception e) {
			rm.setData(targets);
			rm.setState("500");
			return rm;
		}
		rm.setData(targets);
		rm.setState("200");
		return rm;
	}
}
