/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-12-25
 * 描述            ：质量封锁/放行信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.slmord.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptException;

import market.Api.Lms.impl.LmsQmsApiServiceImpl;
import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.mapper.YdmProductInlistMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Lms.Warehouse.vo.YdmProductInlist;
import market.Mes.Lengthwise.Plan.impl.PlnMaterielDetailServiceImpl;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.vo.ComMscStdChem;
import market.Qms.qcm.vo.QcmJhyElements;
import market.Qms.slmord.mapper.QcmJudgeBlockadeInfoMapper;
import market.Qms.slmord.service.QcmJudgeBlockadeInfoService;
import market.Qms.slmord.vo.QcmJudgeBlockadeInfo;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "QcmJudgeBlockadeInfoService")
public class QcmJudgeBlockadeInfoServiceImpl extends BaseServiceImpl<QcmJudgeBlockadeInfo, Object>
		implements QcmJudgeBlockadeInfoService {

	/**
	 * 异常材处置界面，查询待处置信息
	 * @param sum
	 * @return
	 */
	public ResultModel getInformation(CommonPage<QcmJudgeBlockadeInfo> sum) {
		ResultModel rm = new ResultModel();
		Integer pageSize = sum.getPageSize() * (sum.getPageIndex() - 1);
		Integer pageIndex = sum.getPageSize() * (sum.getPageIndex() - 1) + sum.getPageSize();
		ReturnMsg<List<QcmJudgeBlockadeInfo>> msg = new ReturnMsg<>();
		QcmJudgeBlockadeInfo info = sum.getObject();
		try {			
			if (info == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入处置信息");
				return rm;
			}
			String conn = "";
			if(!DataHelper.IsNullOrSpace(info.getBic_type())){
				conn += " and t.BIC_TYPE='"+info.getBic_type()+"'";
			}
			if(!DataHelper.IsNullOrSpace(info.getMaterial_no())){
				conn += " and t.MATERIAL_NO like'%"+info.getMaterial_no()+"%'";
			}
			if(!DataHelper.IsNullOrSpace(info.getPline_code())){
				conn += " and t.PLINE_CODE ='"+info.getPline_code()+"'";
			}
			if(!DataHelper.IsNullOrSpace(info.getDis_result_desc())){
				conn += " and t.DIS_RESULT_DESC ='"+info.getDis_result_desc()+"'";
			}

			msg.setPageIndex(pageIndex);
			msg.setPageSize(pageSize);
			List<QcmJudgeBlockadeInfo> in = this.getMapper(QcmJudgeBlockadeInfoMapper.class).getInformation(conn, pageSize, pageIndex);
			conn = conn.replace("t.", "");
			int total = this.getMapper(QcmJudgeBlockadeInfoMapper.class).countWithCondition(null, conn);
			msg.setResult(in);
			msg.setTotal(total);
			rm.setData(msg);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			rm.setMsgInfo(e.getMessage());
			return rm;
		}

		return rm;
	}
	
	/**
	 * 异常材处理：处置记录查询
	 * @param sum
	 * @return
	 */
	public ResultModel getAbnormalRecordInfo(CommonPage<QcmJudgeBlockadeInfo> sum) {
		ResultModel rm=new ResultModel();
		QcmJudgeBlockadeInfo qjb=sum.getObject();
		String conn="dis_status='1' and dis_result_code is not null";
		PageModel<QcmJudgeBlockadeInfo> pm=new PageModel<>(sum.getPageIndex(), sum.getPageSize());
		try {
			if (!DataHelper.IsNullOrSpace(qjb.getMaterial_no())) {
				conn+=" and material_no like '%"+qjb.getMaterial_no()+"%'";
			}
			if (!DataHelper.IsNullOrSpace(qjb.getBic_type())) {
				conn+=" and bic_type='"+qjb.getBic_type()+"'";
			}
			if (!DataHelper.IsNullOrSpace(qjb.getPline_name())) {
				conn+=" and pline_name like '%"+qjb.getPline_name()+"%'";
			}
			if (StringUtils.isNotEmpty(qjb.getPline_code())) {
				conn += " and pline_code = '"+qjb.getPline_code()+"'";
			}			
			List<QcmJudgeBlockadeInfo> listqjb = this.getMapper(QcmJudgeBlockadeInfoMapper.class).findWithCondition(null, conn, "UPDATE_TIME desc", (pm.getPageNumber()-1)*pm.getPageSize(), (pm.getPageNumber()-1)*pm.getPageSize()+pm.getPageSize());
			pm.setRows(listqjb);
			pm.setTotal(this.getMapper(QcmJudgeBlockadeInfoMapper.class).countWithCondition(null, conn));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception e) {
		   rm.setState("500");
		}
		return rm;
	}
   
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInformationSure(CommonPage<QcmJudgeBlockadeInfo> sum) throws Exception {
		ResultModel rm = new ResultModel();
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			if (sum == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			
			List<QcmJudgeBlockadeInfo> info = sum.getObjectList();
			if (info == null || info.size() <= 0) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			boolean pass = judgeType(info, rm);
			if (!pass) {
				return rm;
			}
			//更新异常处置表
			for (QcmJudgeBlockadeInfo qjb : info) {
				qjb.setDis_status("1");
				qjb.setUpdate_name(username);
				qjb.setUpdate_time(new Date());
				qjb.setDis_result_code(qjb.getDis_result_code());
				qjb.setDis_result_desc(qjb.getDis_result_desc());
				qjb.setDis_memo(qjb.getDis_memo());
				this.getMapper(QcmJudgeBlockadeInfoMapper.class).update(qjb);
			}
			//处理结果逻辑
			for (QcmJudgeBlockadeInfo in : info) {
				//改判
				if ("40741001".equals(in.getDis_result_code())) {
					
				}
				//解锁
				else if ("40741002".equals(in.getDis_result_code())) {
					YdmProductDetail ydm=new YdmProductDetail();
					ydm.setCoilno(in.getMaterial_no());;
					ydm.setInspection_lot(in.getInspection_lot());
					ydm.setState("0");
					//表检(上表)
					if ("U".equals(in.getBic_type())) {
						ydm.setSfu_is_lock("0");
					}
					//表检(下表)
					if ("F".equals(in.getBic_type())) {
						ydm.setSfd_is_lock("0");
					}
					//表检(边部)
					if ("E".equals(in.getBic_type())) {
						ydm.setSfe_is_lock("0");
					}
					//公差
					if ("S".equals(in.getBic_type())) {
						ydm.setSic_is_lock("0");
					}
					//材质
					if ("P".equals(in.getBic_type())) {
						ydm.setPic_is_lock("0");
					}
					this.getMapper(YdmProductDetailMapper.class).update(ydm);
				}
				//放行
				else if ("40741003".equals(in.getDis_result_code())) {
					YdmProductDetail ydm=new YdmProductDetail();
					ydm.setCoilno(in.getMaterial_no());;
					ydm.setInspection_lot(in.getInspection_lot());
					ydm.setState("0");
					//表检(上表)
					if ("U".equals(in.getBic_type())) {
						ydm.setSfu_is_lock("2");
						ydm.setUp_surface_result("40740601");
					}
					//表检(下表)
					if ("F".equals(in.getBic_type())) {
						ydm.setSfd_is_lock("2");
						ydm.setUd_surface_result("40740601");
					}
					//表检(边部)
					if ("E".equals(in.getBic_type())) {
						ydm.setSfe_is_lock("2");
						ydm.setMarginal_result("40740601");
					}
					//公差
					if ("S".equals(in.getBic_type())) {
						ydm.setSic_is_lock("2");
						ydm.setMeasure_result("40740301");
					}
					//材质
					if ("P".equals(in.getBic_type())) {
						ydm.setPic_is_lock("2");
						ydm.setPhysresult("40740201");
					}
					this.getMapper(YdmProductDetailMapper.class).update(ydm);
				}
				//判废
				else if ("40741004".equals(in.getDis_result_code())) {
					YdmProductDetail ydm=new YdmProductDetail();
					ydm.setCoilno(in.getMaterial_no());;
					ydm.setInspection_lot(in.getInspection_lot());
					ydm.setState("0");
					//表检(上表)
					if ("U".equals(in.getBic_type())) {
						ydm.setSfu_is_lock("0");
						ydm.setUp_surface_result("40740604");
					}
					//表检(下表)
					if ("F".equals(in.getBic_type())) {
						ydm.setSfd_is_lock("0");
						ydm.setUd_surface_result("40740604");
					}
					//表检(边部)
					if ("E".equals(in.getBic_type())) {
						ydm.setSfe_is_lock("0");
						ydm.setMarginal_result("40740604");
					}
					//公差
					if ("S".equals(in.getBic_type())) {
						ydm.setSic_is_lock("0");
						ydm.setMeasure_result("40740304");
					}
					//材质
					if ("P".equals(in.getBic_type())) {
						ydm.setPic_is_lock("0");
						ydm.setPhysresult("40740204");
					}
					this.getMapper(YdmProductDetailMapper.class).update(ydm);
					//脱单
					CommonPage<PlnMaterielDetail> oci = new CommonPage<>();
					List<PlnMaterielDetail> listPln = new ArrayList<>();
					PlnMaterielDetail pln = new PlnMaterielDetail();
					pln.setObject_no(in.getMaterial_no());
					listPln.add(pln);
					oci.setObjectList(listPln);
					this.getService(PlnMaterielDetailServiceImpl.class).doMaterialReplace(oci);
				}
				//判次
				else if ("40741005".equals(in.getDis_result_code())) {
					YdmProductDetail ydm=new YdmProductDetail();
					ydm.setCoilno(in.getMaterial_no());;
					ydm.setInspection_lot(in.getInspection_lot());
					ydm.setState("0");
					//表检(上表)
					if ("U".equals(in.getBic_type())) {
						ydm.setSfu_is_lock("0");
						ydm.setUp_surface_result("40740605");
					}
					//表检(下表)
					if ("F".equals(in.getBic_type())) {
						ydm.setSfd_is_lock("0");
						ydm.setUd_surface_result("40740605");
					}
					//表检(边部)
					if ("E".equals(in.getBic_type())) {
						ydm.setSfe_is_lock("0");
						ydm.setMarginal_result("40740605");
					}
					//公差
					if ("S".equals(in.getBic_type())) {
						ydm.setSic_is_lock("0");
						ydm.setMeasure_result("40740303");
					}
					//材质
					if ("P".equals(in.getBic_type())) {
						ydm.setPic_is_lock("0");
						ydm.setPhysresult("40740202");
					}
					this.getMapper(YdmProductDetailMapper.class).update(ydm);
					// 脱单
					CommonPage<PlnMaterielDetail> oci = new CommonPage<>();
					List<PlnMaterielDetail> listPln = new ArrayList<>();
					PlnMaterielDetail pln = new PlnMaterielDetail();
					pln.setObject_no(in.getMaterial_no());
					listPln.add(pln);
					oci.setObjectList(listPln);
					this.getService(PlnMaterielDetailServiceImpl.class).doMaterialReplace(oci);
				}
				
				//判断物料之前是否已经入库
				YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).doFind(in.getMaterial_no(), null);
				if (ypd!=null) {					
					YdmProductInlist ypi = new YdmProductInlist();
					ypi.setCoilno(ypd.getCoilno());
					ypi.setStorageattr(ypd.getStorageattr());
					int count = this.getMapper(YdmProductInlistMapper.class).count(ypi);
					if (count > 0) {
						//存在入库记录，调整库存状态为0：在库
						YdmProductDetail y = new YdmProductDetail();
						y.setCoilno(ypd.getCoilno());
						y.setState("0");
						this.getMapper(YdmProductDetailMapper.class).update(y);
					}else {
						//不存在入库记录，则调整库存状态为-1，待入库
						YdmProductDetail y = new YdmProductDetail();
						y.setCoilno(ypd.getCoilno());
						y.setState("-1");
						this.getMapper(YdmProductDetailMapper.class).update(y);
					}
				}
				
				
			}
			rm.setState("200");
			rm.setMsgInfo("异常材料处理确认成功");
			
			
			

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("异常材料处理确认失败");
			throw new Exception(rm.getMsgInfo());

		}

		return rm;
	}
	/**
	 * 根据产品码psc获取改判后的标准信息(材质)---2019.1.9
	 */
//   public void getStdInfo(String psc,String DeliveryStateCode,String thick,String width,String length,String object_no_prod,String order_no,String Inspection_lot,QcmJudgeBlockadeInfo in){
//		List<String> liststr=new ArrayList<>();
//		List<String> listp=new ArrayList<>();
//	   try {
//		//根据psc查询msc集合
//			List<ComPscRMsc> listmsc = this.getMapper(ComPscRMscMapper.class).findWithCondition(null, "psc='"+psc+"' and validflag='1'", null, 0, 0);
//		    for (ComPscRMsc c : listmsc) {
//		    	//过滤msc
//				List<ComMsc> listm = this.getMapper(ComMscMapper.class).findWithCondition(null, "msc='"+c.getMsc()+"' and (MSC_SEQ='' or MSC_SEQ is null)", null, 0, 0);
//		        if (listm!=null&&listm.size()>0) {
//		        	for (ComMsc cm : listm) {
//		        		liststr.add(cm.getMsc());
//					}
//				}
//		    }
//		    //根据过滤后的msc查询pic
//		    for (String s : liststr) {
//				List<ComMscStdPic> listpic = this.getMapper(ComMscStdPicMapper.class).findWithCondition(null, "msc='"+s+"'", null, 0, 0);
//			    if (listpic!=null&&listpic.size()>0) {
//					for (ComMscStdPic cp : listpic) {
//						listp.add(cp.getPic());
//					}
//				}
//		    }
//		    for (String  pic : listp) {
//				List<ComMscStdPhy> listphy = this.getMapper(ComMscStdPhyMapper.class).findWithCondition(null, "pic='"+pic+"' and validflag='1'", null, 0, 0);
//				List<ComMscStdPhy> list=new ArrayList<>();
//				//根据交货状态和规格去过滤的到的项目
//				for (int i = 0; i < listphy.size(); i++) {
//					if (listphy.get(i).getDeliveryStateCode()!=null && listphy.get(i).getDeliveryStateCode().contains(DeliveryStateCode)) {
//						list.add(listphy.get(i));
//					}
//					
//				}
//				SlmContractProcessPlan orderLine=new SlmContractProcessPlan();
//				//厚度
//				if (thick==null || "".equals(thick)) {
//					orderLine.setThick(null);
//				}else {
//					orderLine.setThick(Double.valueOf(thick));
//				}
//				//宽度
//				if (width==null || "".equals(width)) {
//					orderLine.setWidth(Double.valueOf(width));
//				}else {
//					orderLine.setWidth(Double.valueOf(width));
//				}
//				//长度
//				if (length==null || "".equals(length)) {
//					orderLine.setLength(null);
//				}else {
//					orderLine.setLength(Double.valueOf(length));
//				}
//				list=this.getBean(QualityDesignStdPicServiceImpl.class).FilterSpec(list, orderLine);
//		        //获取订单和检验号向下的子板信息
////				ResultModel rm = this.getBean(MesApiServiceImpl.class).getPlateInfoByOrderInspection(order_no, Inspection_lot);
//				 List<PrcMaterielExtinfo> listpr= this.getMapper(PrcMaterielExtinfoMapper.class).findWithCondition(null, "object_no_pseudo='"+object_no_prod+"'", null, 0, 0);
//				 if (listpr!=null&&listpr.size()>0&&!DataHelper.IsNullOrSpace(listpr.get(0).getPic_id())) {
//					List<QcmJudgePhysicalUtmD> listud = this.getMapper(QcmJudgePhysicalUtmDMapper.class).findWithCondition(null, "PIC_ID='"+listpr.get(0).getPic_id()+"'", null, 0, 0);
//				    for (QcmJudgePhysicalUtmD ud : listud) {
//				    	String memo="";
//				    	//根据判定记录号获取实测值
//				    	List<QcmJhyPhyresult> listph=new ArrayList<>();
//						List<QcmJhySampleConsignD> listd = this.getMapper(QcmJhySampleConsignDMapper.class).findWithCondition(null, "smp_no='"+ud.getSmp_no()+"'", null, 0, 0);
//					    if (listd!=null&&listd.size()>0) {
//					    	for (QcmJhySampleConsignD d : listd) {
//					    		List<QcmJhyPhyresult> listphyr= this.getMapper(QcmJhyPhyresultMapper.class).findWithCondition(null, "specimen_no='"+d.getSpecimen_no()+"'", null, 0, 0);
//							    for (QcmJhyPhyresult p : listphyr) {
//							    	listph.add(p);
//								}
//					    	}
//						}
//					   //判定（比较标准和实测值）
//					    for (ComMscStdPhy st : list) {
//					    	String conn1=st.getPhyCodeL()+st.getPhyCodeM()+st.getPhyCodeS()+st.getItemCodeD()+st.getItemCodeS()+st.getItemCodeT();
//							for (QcmJhyPhyresult py : listph) {
//							String conn2=py.getPhy_code_l()+py.getPhy_code_m()+py.getPhy_code_s()+py.getItem_code_d()+py.getItem_code_s()+py.getItem_code_t();
//							   if (!DataHelper.IsNullOrSpace(conn1)&&!DataHelper.IsNullOrSpace(conn2)) {
//								if (conn1.equals(conn2)) {
//									List<String> lis=judge_val(st, py);
//									if (lis!=null&&lis.size()>0) {
//										for (String s : lis) {
//											 memo=memo+s+",";
//										}
//									}
//								}
//							}
//							}
//						}
//					    // 如果memo不等于空则存在不合格项目
//					    if (!DataHelper.IsNullOrSpace(memo)) {
//					    QcmJudgeBlockadeInfo qjb=new QcmJudgeBlockadeInfo();
//					    int count=0;
//						SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
//						String date=sdf.format(new Date());
//						 List<QcmJudgeBlockadeInfo> listqcbk = this.getMapper(QcmJudgeBlockadeInfoMapper.class).findWithCondition(null, "BIC like '%"+date+"%'", "BIC desc", 0, 0);
//						if(listqcbk!=null&&listqcbk.size()>0){
//							String s=listqcbk.get(0).getBic();
//							count=Integer.parseInt(s.substring(s.length()-4, s.length()));
//						}
//						count++;
//						String bic=sdf.format(new Date())+String.format("%04d",count);
//						qjb.setBic(bic);
////						String memo="";
////						if (lists!=null&&lists.size()>0) {
////							for (String str : lists) {
////								memo=memo+str+",";
////							}
////						}
//						if (!DataHelper.IsNullOrSpace(memo)&&",".equals(memo.substring(memo.length()-1,memo.length()))) {
//							qjb.setBic_memo(memo.substring(0,memo.length()-1));
//						}else {
//							qjb.setBic_memo(memo);
//						}
//						qjb.setBic_type("P");
//						qjb.setHeat_no(in.getHeat_no());
//						qjb.setBatch_no(in.getBatch_no());
//						qjb.setBoard_no(in.getBatch_no());
//						qjb.setInspection_lot(in.getInspection_lot());
//						qjb.setMaterial_no(in.getMaterial_no());
//						qjb.setObject_no_pseudo(in.getObject_no_pseudo());
//						qjb.setDis_result_code(in.getDis_result_code());
//						qjb.setDis_result_desc(in.getDis_result_desc());
//						qjb.setDis_memo(in.getDis_memo());
//						qjb.setContract_line_no(in.getContract_line_no());
//						qjb.setPsc(in.getPsc());
////				    	qjb.setProd_code(qjs.getProd_code());
////				    	qjb.setProd_name(qjs.getProd_name());
////				    	qjb.setStd_code(qjs.getStd_code());
////				    	qjb.setStd_name(qjs.getStd_name());
////				    	qjb.setSteel_code(qjs.getSteel_code());
////				    	qjb.setSteel_name(qjs.getSteel_name());
//				    	qjb.setCreate_time(new Date());
//				    	qjb.setCreate_name(this.getUserName());
//				    	qjb.setPline_code(in.getPline_code());
//				    	qjb.setPline_name(in.getPline_name());
//				    	if (in.getWidth()!=null&&!(in.getWidth()).equals("")) {
//				    		in.setWidth(in.getWidth().longValue());	
//						}
//				    	if (in.getThick()!=null&&!(in.getThick()).equals("")) {
//				    		qjb.setThick(in.getThick().longValue());
//						}
//				    	if (in.getLength()!=null&&!(in.getLength()).equals("")) {
//				    		qjb.setLength(in.getLength().longValue());	
//						}
//					//更新物料进程表标识
////					List<PrcMaterielExtinfo> listprc = this.getMapper(PrcMaterielExtinfoMapper.class).findWithCondition(null, "OBJECT_NO_PSEUDO='"+prc.getObject_no_pseudo()+"'", null, 0, 0);
////				    if (listprc!=null&&listprc.size()>0&&!DataHelper.IsNullOrSpace(listprc.get(0).getObject_no_pseudo())) {
//				    	qjb.setOld_prod_code(in.getProd_code());
//						qjb.setOld_prod_name(in.getProd_name());
//						qjb.setOld_psc(in.getPsc());
//						qjb.setHt_code(in.getHt_code());
//						qjb.setOld_std_code(in.getStd_code());
//						qjb.setOld_std_name(in.getStd_name());
//						qjb.setOld_steel_code(in.getSteel_code());
//						qjb.setOld_steel_name(in.getSteel_name());
//				    	qjb.setDelivery_state_code(in.getDelivery_state_code());
//				    	qjb.setDelivery_state_desc(in.getDelivery_state_desc());
//				    	PrcMaterielExtinfo pme=new PrcMaterielExtinfo();
//				    	pme.setPic_is_lock("1");
//				    	pme.setObject_no_pseudo(in.getObject_no_pseudo());
//				    	pme.setInspection_lot(in.getInspection_lot());
//				    	//锁定物料
//				    	this.getBean(MesApiServiceImpl.class).UpdateExtinfoByOrder(pme);
//				    	//新增异常材料信息
//						this.getMapper(QcmJudgeBlockadeInfoMapper.class).insert(qjb);
//						//清空产品信息
//						this.getMapper(QcmJudgeBlockadeInfoMapper.class).doEmptyOrderInfo(bic);
////					}else {
////						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
////					}
//					    }
//				    }
//				 }
//		    }
//	} catch (Exception e) {
//		// TODO: handle exception
//		System.out.println(e.getMessage());
//	}
//   }
//   public List<String> judge_val(ComMscStdPhy st,QcmJhyPhyresult py){
//	   List<String> lists=new ArrayList<>();//异常材料处理用，用于不合格项目的备注
//	   try {
//		   String bool0="";
//		   String bool1="";
//		   String bool2="";
//		   String bool3="";
//		   String bool4="";
//		   String bool5="";
//		   Boolean pass=true;
//		   //最小值判定
//		if (!DataHelper.IsNullOrSpace(st.getJudgeBasis())&&st.getJudgeBasis().equalsIgnoreCase("A")&&"1".equals(st.getIsjudge())) {
//			    String min = this.getBean(QcmJhySampleConsignDServiceImpl.class).compareVal(py.getVal1(),py.getVal2(),py.getVal3(),st.getJudgeBasis());
//			    bool0=getJudgeColor(st.getStdminSign(),st.getStdmin(),min,null,null);
//			    if ("2".equals(bool0)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//					lists.add(py.getPhy_name_l()+":"+min);
//				}
//			    bool1=getJudgeColor(st.getStdmaxSign(),st.getStdmaxSign(),min,null,null);
//			    if ("2".equals(bool1)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//					lists.add(py.getPhy_name_l()+":"+min);
//				}
//			
//			    if ("2".equals(bool0)||"2".equals(bool1)||"2".equals(bool2)||"2".equals(bool3)||"2".equals(bool4)||"2".equals(bool5)) {
//					pass=false;
//				}
//		}
//		//最大值判定
//		if (!DataHelper.IsNullOrSpace(st.getJudgeBasis())&&st.getJudgeBasis().equalsIgnoreCase("B")&&"1".equals(st.getIsjudge())) {
//		    String min = this.getBean(QcmJhySampleConsignDServiceImpl.class).compareVal(py.getVal1(),py.getVal2(),py.getVal3(),st.getJudgeBasis());
//		    bool0=getJudgeColor(st.getStdminSign(),st.getStdmin(),min,null,null);
//		    if ("2".equals(bool0)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//				lists.add(py.getPhy_name_l()+":"+min);
//			}
//		    bool1=getJudgeColor(st.getStdmaxSign(),st.getStdmaxSign(),min,null,null);
//		    if ("2".equals(bool1)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//				lists.add(py.getPhy_name_l()+":"+min);
//			}
//		
//		    if ("2".equals(bool0)||"2".equals(bool1)||"2".equals(bool2)||"2".equals(bool3)||"2".equals(bool4)||"2".equals(bool5)) {
//				pass=false;
//			}
//		}
//		//平均值判定
//		if (!DataHelper.IsNullOrSpace(st.getJudgeBasis())&&st.getJudgeBasis().equalsIgnoreCase("C")&&"1".equals(st.getIsjudge())) {
//		    String min = this.getBean(QcmJhySampleConsignDServiceImpl.class).compareVal(py.getVal1(),py.getVal2(),py.getVal3(), st.getJudgeBasis());
//		    bool0=getJudgeColor(st.getStdminSign(),st.getStdmin(),min,null,null);
//		    if ("2".equals(bool0)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//				lists.add(py.getPhy_name_l()+":"+min);
//			}
//		    bool1=getJudgeColor(st.getStdmaxSign(),st.getStdmaxSign(),min,null,null);
//		    if ("2".equals(bool1)&&!lists.contains(py.getPhy_name_l()+":"+min)) {
//				lists.add(py.getPhy_name_l()+":"+min);
//			}
//		
//		    if ("2".equals(bool0)||"2".equals(bool1)||"2".equals(bool2)||"2".equals(bool3)||"2".equals(bool4)||"2".equals(bool5)) {
//				pass=false;
//			}
//		}
//		//逐项值判定
//		if (DataHelper.IsNullOrSpace(st.getJudgeBasis())||st.getJudgeBasis().equalsIgnoreCase("D")) {
//			if ("1".equals(st.getIsjudge())) {
//			bool0=getJudgeColor(st.getStdminSign(),st.getStdmin(),py.getVal1(),py.getVal2(),py.getVal3());
//			String str=this.getBean(QcmJhyPhyresultServiceImpl.class).connectVal(py.getPhy_name_l(), py.getVal1(),py.getVal2(),py.getVal3());
//			if ("2".equals(bool0)&&!lists.contains(str)) {
//				lists.add(str);
//			}
//		    bool1=getJudgeColor(st.getStdmaxSign(),st.getStdmax(),py.getVal1(),py.getVal2(),py.getVal3());
//		    if ("2".equals(bool1)&&!lists.contains(str)) {
//				lists.add(str);
//			}
//		    if ("2".equals(bool0)||"2".equals(bool1)||"2".equals(bool2)||"2".equals(bool3)||"2".equals(bool4)||"2".equals(bool5)) {
//				pass=false;
//			}
//		}
//		}
//	   } catch (Exception e) {
//		// TODO: handle exception
//	}
//	   return lists;
//   }
   /**
	 * 为前端获取对比结果好标颜色
	 * 0 为null
	 * 1 为合格
	 * 2为不合格
	 * @param sign 符号
	 * @param qsd  标准值
	 * @param val1   实测值
	 * @param val2
	 * @param val3
	 * @return
	 */
	public String getJudgeColor(String sign,String qsd,String val1,String val2,String val3){
		boolean min1=true;
		boolean min3=true;
		boolean min2=true;
		boolean v1=true;
		boolean v2=true;
		boolean v3=true;
		boolean q=true;
		if (DataHelper.IsNullOrSpace(qsd)) {
			return "0";
		}
		if (!DataHelper.IsNullOrSpace(qsd)) {
			 q=isNumeric(qsd);
		}
		
		if (!DataHelper.IsNullOrSpace(val1)) {
			 v1=isNumeric(val1);
		}
		if (!DataHelper.IsNullOrSpace(val2)) {
			v2=isNumeric(val2);
		}
		if (!DataHelper.IsNullOrSpace(val3)) {
			v3=isNumeric(val3);
		}
		if (!q) {
			if (!DataHelper.IsNullOrSpace(val1)) {
				min1=qsd.equals(val1);
			}
			if (!DataHelper.IsNullOrSpace(val2)) {
				 min2=qsd.equals(val2);
			}
			if (!DataHelper.IsNullOrSpace(val3)) {
				min3=qsd.equals(val3);
			}
			if (DataHelper.IsNullOrSpace(val1)&&DataHelper.IsNullOrSpace(val2)&&DataHelper.IsNullOrSpace(val3)) {
				return "0";
			}
		    if (min1&&min2&&min3) {
				return "1";
			}else {
				return "2";
			}
		}
		////111111111111111
		if (!v1) {
			if (v2) {
				if (v3) {
					 min1=qsd.equals(val1);
					 min2=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val2);
					 min3=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val3);
				}else {
					 min1=qsd.equals(val1);
					 min2=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val2);
					 min3=qsd.equals(val3);
				}
			}else {
				if (v3) {
					 min1=qsd.equals(val1);
					 min2=qsd.equals(val2);
					 min3=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val3);
				}else {
					 min1=qsd.equals(val1);
					 min2=qsd.equals(val2);
					 min3=qsd.equals(val3);
				}
			}
			    if (min1&&min2&&min3) {
					return "1";
				}else {
					return "2";
				}
		}
		//2222222222222222222222222
		if (!v2) {
			if (v1) {
				if (v3) {
					 min2=qsd.equals(val2);
					 min1=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val1);
					 min3=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val3);
				}else {
					 min2=qsd.equals(val2);
					 min1=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val1);
					 min3=qsd.equals(val3);
				}
			}else {
				if (v3) {
					 min1=qsd.equals(val1);
					 min2=qsd.equals(val2);
					 min3=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val3);
				}else {
					 min1=qsd.equals(val1);
					 min2=qsd.equals(val2);
					 min3=qsd.equals(val3);
				}
			}
			    if (min1&&min2&&min3) {
					return "1";
				}else {
					return "2";
				}
		}
		
		//3333333333333333333333333
				if (!v3) {
					if (v1) {
						if (v2) {
							 min3=qsd.equals(val3);
							 min1=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val1);
							 min2=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val2);
						}else {
							 min2=qsd.equals(val2);
							 min1=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val1);
							 min3=qsd.equals(val3);
						}
					}else {
						if (v2) {
							 min1=qsd.equals(val1);
							 min3=qsd.equals(val3);
							 min2=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val2);
						}else {
							 min1=qsd.equals(val1);
							 min2=qsd.equals(val2);
							 min3=qsd.equals(val3);
						}
					}
					    if (min1&&min2&&min3) {
							return "1";
						}else {
							return "2";
						}
				}
		if (qsd!=null) {
				min1=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val1);
			    min2=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val2);
			    min3=DataHelper.checkSignAndValue(sign, Double.parseDouble(qsd), val3);
			    if (min1&&min2&&min3) {
					return "1";
				}else {
					return "2";
				}
           }else {
				return "0";
           }
	}
	/**
	 * 判断字符串是否为数字-------蒋崇波
	 * @param str
	 * @return
	 */
	public boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]+([.]{1}[0-9]+){0,1}"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
		}
	/**
	 * 数据验证
	 * 
	 * @param info
	 * @param rm
	 * @return
	 */
	private boolean judgeType(List<QcmJudgeBlockadeInfo> info, ResultModel rm) {

		HashSet<String> bic_type = new HashSet<>();
		HashSet<String> old_psc = new HashSet<>();
		HashSet<String> inspection_lot = new HashSet<>();
		for (QcmJudgeBlockadeInfo i : info) {
			bic_type.add(i.getBic_type());
			old_psc.add(i.getOld_psc());
			inspection_lot.add(i.getInspection_lot());
		}

		if (bic_type.size() > 1) {
			rm.setState("500");
			rm.setMsgInfo("封锁类型不一致，请确认");
			return false;
		}

		if (old_psc.size() > 1) {
			rm.setState("500");
			rm.setMsgInfo("原产品码不一致，请确认");
			return false;
		}

		if (inspection_lot.size() > 1) {
			rm.setState("500");
			rm.setMsgInfo("检验号不一致，请确认");
			return false;
		}

		return true;
	}
	
	
	/**
	 * 供判定异常结果处理，插入方法
	 * @param sum
	 * @return
	 * @throws Exception
	 */

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertMation(CommonPage<QcmJudgeBlockadeInfo> sum) throws Exception {
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		try {

			if (sum == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<QcmJudgeBlockadeInfo> info = sum.getObjectList();
			if (info == null && info.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			// 数据验证
			/*
			 * boolean pass=judgeData(info,rm); if (!pass) { return rm; }
			 */
			for (QcmJudgeBlockadeInfo i : info) {
				// 封锁记录号
				String bic = createRecordNo();
				i.setCreate_time(new Date());
				i.setCreate_name(userName);
				this.getMapper(QcmJudgeBlockadeInfoMapper.class).insert(i);

			}

			rm.setState("200");
			rm.setMsgInfo("保存成功");
			rm.setData(info);

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("插入数据失败");
			throw new Exception(rm.getMsgInfo());

		}

		return rm;
	}
// 	/**
//		 * 成分异常材料处置	2019.1.7	
//		 */
//		public void MaterialAbnormalTreatment(QcmJudgeBlockadeInfo qjsr,List<String> lists,String result_code,String result_name,String result_memo){
//			try {
//				//更新材料异常处理表
//				 QcmJudgeBlockadeInfo qjb=new QcmJudgeBlockadeInfo();
//				 //生成质量封锁记录号
//				 //调用计划模块接口获取订单和检验号下面所有的子板信息
//				 ResultModel rm = this.getBean(MesApiServiceImpl.class).getPlateInfoByOrderInspection(qjsr.getContract_line_no(), qjsr.getInspection_lot());
//				 List<PrcMaterielExtinfo> listp = (List<PrcMaterielExtinfo>) rm.getData();
//				 for (PrcMaterielExtinfo prc : listp) {
//				 int count=0;
//					SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
//					String date=sdf.format(new Date());
//					 List<QcmJudgeBlockadeInfo> listqcbk = this.getMapper(QcmJudgeBlockadeInfoMapper.class).findWithCondition(null, "BIC like '%"+date+"%'", "BIC desc", 0, 0);
//					if(listqcbk!=null&&listqcbk.size()>0){
//						String s=listqcbk.get(0).getBic();
//						count=Integer.parseInt(s.substring(s.length()-4, s.length()));
//					}
//					count++;
//					String bic=sdf.format(new Date())+String.format("%04d",count);
//					qjb.setBic(bic);
//					String memo="";
//					if (lists!=null&&lists.size()>0) {
//						for (String str : lists) {
//							memo=memo+str+",";
//						}
//					}
//					if (!DataHelper.IsNullOrSpace(memo)&&",".equals(memo.substring(memo.length()-1,memo.length()))) {
//						qjb.setBic_memo(memo);
//					}else {
//						qjb.setBic_memo(memo);
//					}
//					qjb.setBic_type("C");
//					qjb.setHeat_no(qjsr.getHeat_no());
//					qjb.setBatch_no(qjsr.getBatch_no());
//					qjb.setBoard_no(qjsr.getBoard_no());
//					qjb.setInspection_lot(qjsr.getInspection_lot());
//					qjb.setMaterial_no(prc.getObject_no());
//					qjb.setObject_no_pseudo(prc.getObject_no_pseudo());
//					qjb.setDis_result_code(result_code);
//					qjb.setDis_result_desc(result_name);
//					qjb.setDis_memo(result_memo);
//					qjb.setContract_line_no(qjsr.getContract_line_no());
//					qjb.setPsc(qjsr.getPsc());
////			    	qjb.setProd_code(qjs.getProd_code());
////			    	qjb.setProd_name(qjs.getProd_name());
////			    	qjb.setStd_code(qjs.getStd_code());
////			    	qjb.setStd_name(qjs.getStd_name());
////			    	qjb.setSteel_code(qjs.getSteel_code());
////			    	qjb.setSteel_name(qjs.getSteel_name());
//			    	qjb.setCreate_time(new Date());
//			    	qjb.setCreate_name(this.getUserName());
//			    	qjb.setPline_code(qjsr.getPline_code());
//			    	qjb.setPline_name(qjsr.getPline_name());
//			    	if (prc.getWidth()!=null&&!(prc.getWidth()).equals("")) {
//			    		qjb.setWidth(prc.getWidth().longValue());	
//					}
//			    	if (prc.getThick()!=null&&!(prc.getThick()).equals("")) {
//			    		qjb.setThick(prc.getThick().longValue());
//					}
//			    	if (prc.getLength()!=null&&!(prc.getLength()).equals("")) {
//			    		qjb.setLength(prc.getLength().longValue());	
//					}
//				//更新物料进程表标识
//				List<PrcMaterielExtinfo> listprc = this.getMapper(PrcMaterielExtinfoMapper.class).findWithCondition(null, "OBJECT_NO_PSEUDO='"+prc.getObject_no_pseudo()+"'", null, 0, 0);
//			    if (listprc!=null&&listprc.size()>0&&!DataHelper.IsNullOrSpace(listprc.get(0).getObject_no_pseudo())) {
//			    	qjb.setOld_prod_code(listprc.get(0).getProd_code());
//					qjb.setOld_prod_name(listprc.get(0).getProd_name());
//					qjb.setOld_psc(listprc.get(0).getPsc());
//					qjb.setHt_code(listprc.get(0).getHt_code());
//					qjb.setOld_std_code(listprc.get(0).getStd_code());
//					qjb.setOld_std_name(listprc.get(0).getStd_name());
//					qjb.setOld_steel_code(listprc.get(0).getSteel_code());
//					qjb.setOld_steel_name(listprc.get(0).getSteel_name());
//			    	qjb.setDelivery_state_code(listprc.get(0).getDelivery_state_code());
//			    	qjb.setDelivery_state_desc(listprc.get(0).getDelivery_state_desc());
//			    	PrcMaterielExtinfo pme=new PrcMaterielExtinfo();
//			    	pme.setSic_is_lock("1");
//			    	pme.setObject_no_pseudo(prc.getObject_no_pseudo());
//			    	pme.setInspection_lot(prc.getInspection_lot());
//			    	this.getBean(MesApiServiceImpl.class).UpdateExtinfoByOrder(pme);
//					this.getMapper(QcmJudgeBlockadeInfoMapper.class).insert(qjb);
//				}else {
//					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//				}
//				 }
//			} catch (Exception e) {
//				// TODO: handle exception
//				System.out.println(e.getMessage());
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//			}
//		}
     /**
 	 * 获取对比结果 (成分)
 	 * @param stdCic 标准数据
 	 * @param listEle 检验数据
 	 * @return 
 	 */
 	public List<String> CompareResult(List<ComMscStdChem> stdCic,List<QcmJhyElements> listEle)  throws ScriptException{
 		List<String> chemMemo=new ArrayList<>();
 		for (ComMscStdChem cic : stdCic) {
 			if(cic.getIsjudge().equals("0")){
 				continue;
 			}
 			if(cic.getChemType().equals("B")){ //复合元素
 				String express=cic.getChemFormula();
 				for (QcmJhyElements qcmJhyElements : listEle) {
 					express=express.replaceAll(qcmJhyElements.getChem_name(), qcmJhyElements.getChem_value());
 				}
 				 boolean isLetter = false;//定义一个boolean值，用来表示是否包含字母
 			    for (int j = 0; j < express.length(); j++) {
 			    	 if (!Character.isLetter(express.charAt(j))) {//如果计算式还包含字母则表示计算式不正确，不计算值
 			    		isLetter=true;
 					}
				}
 			    if (!isLetter) {
 			    	double result=DataHelper.GetFormulaResult(express);
	 				boolean pass=DataHelper.checkSignAndValue(cic.getStdminSign(),DataHelper.GetDouble(cic.getStdmin()),DataHelper.GetString(result));
	 				if(!pass){
	 					if (!chemMemo.contains(cic.getChemName()+":"+result)) {
	 						chemMemo.add(cic.getChemName()+":"+result);
						}
	 				}
	 				boolean pass1=DataHelper.checkSignAndValue(cic.getStdmaxSign(),DataHelper.GetDouble(cic.getStdmax()),DataHelper.GetString(result));
	 				if(!pass1){
	 					if (!chemMemo.contains(cic.getChemName()+":"+result)) {
	 						chemMemo.add(cic.getChemName()+":"+result);
						}
	 				}
				}
 			}

 			for (QcmJhyElements ele : listEle) {
 				if(ele.getChem_code().equals(cic.getChemCode())){
 					boolean pass=DataHelper.checkSignAndValue(cic.getStdmaxSign(),DataHelper.GetDouble(cic.getStdmax()),ele.getChem_value());
 					if(!pass){
 						if (!chemMemo.contains(cic.getChemName()+":"+ele.getChem_value())) {
 							chemMemo.add(cic.getChemName()+":"+ele.getChem_value());
						}
 					}
 					boolean pass1=DataHelper.checkSignAndValue(cic.getStdminSign(),DataHelper.GetDouble(cic.getStdmin()),ele.getChem_value());
 					if(!pass1){
 						if (!chemMemo.contains(cic.getChemName()+":"+ele.getChem_value())) {
 							chemMemo.add(cic.getChemName()+":"+ele.getChem_value());
						}
 					}
 				}
 			}
 		}
 		return chemMemo;
 	}
 	/**
 	 * 查询异常材料处置下拉框结果集
 	 * @return
 	 */
 	public ResultModel getBlocadeDownResult() {
 		ResultModel rm=new ResultModel();
 		try {
			rm.setData(this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, "sort_code='407410'", "base_code", 0, 0));
		    rm.setState("200");
 		} catch (Exception e) {
			// TODO: handle exception
		    rm.setState("500");
            rm.setErrorInfo(e.getMessage());
		}
 		return rm;
 	}
	/**
	 * 创建封锁记录号
	 * 
	 * @return
	 */
	public String createRecordNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		String date = sdf.format(new Date());
		int count=0;
		List<QcmJudgeBlockadeInfo> info = this.getMapper(QcmJudgeBlockadeInfoMapper.class).findWithCondition(null,
				"BIC like '%" + date + "%'", "BIC desc", 0, 0);
		if (info != null && info.size() > 0) {
			String s=info.get(0).getBic();
			count=Integer.parseInt(s.substring(s.length()-4, s.length()));
		}
		count++;
		String code=sdf.format(new Date())+String.format("%04d",count);
		return code;
	}
	/***
	 * 清空物料检验信息
	 * @param ydmList
	 * @return
	 * @throws Exception
	 */
	public ResultModel EmptyJudInfo (List<YdmProductDetail> ydmList)  throws Exception {
		ResultModel rm = new ResultModel();
		try {
			for(YdmProductDetail ydm: ydmList) {
				if(DataHelper.IsNullOrSpace(ydm.getCoilno())) {
					rm.setMsgInfo("请传入物料号!");
					rm.setState("500");
					return rm;
				}
			}
			// 调用物流接口清空物料库存表中质量相关信息
			CommonPage<YdmProductDetail> cp = new CommonPage<>();
			cp.setObjectList(ydmList);
			this.getBean(LmsQmsApiServiceImpl.class).removeDetailQmsInfo(cp);
		} catch (Exception e) {
			e.printStackTrace();
			rm.setErrorInfo(e.getMessage());
			rm.setState("403");
			return rm;
		}
		return rm;
	}


	/*	*//**
			 * 验证插入数据的准确性
			 * 
			 * @param info
			 * @param rm
			 * @return
			 *//*
				 * 
				 * private boolean judgeData(List<QcmJudgeBlockadeInfo> info, ResultModel rm) {
				 * 
				 * 
				 * 
				 * 
				 * 
				 * return true; }
				 */

}
