package market.Qms.slmord.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import market.Com.mapper.ComBaseSampleMapper;
import market.Com.mapper.ComBaseSampleRProductMapper;
import market.Com.mapper.ComBaseSampleRStdMapper;
import market.Com.mapper.ComBaseSampleRSteelMapper;
import market.Com.vo.ComBaseSample;
import market.Com.vo.ComBaseSampleRProduct;
import market.Com.vo.ComBaseSampleRStd;
import market.Com.vo.ComBaseSampleRSteel;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscPhySampleMapper;
import market.Qms.metallurgy.mapper.ComMscStdPhyMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscPhySample;
import market.Qms.metallurgy.vo.ComMscStdPhy;
import market.Qms.metallurgy.vo.ComMscStdPic;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicSmpMapper;
import market.Qms.slmord.service.QualityDesignStdPicService;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdPic;
import market.Qms.slmord.vo.SlmOrdDesignStdPicSmp;
import market.Slm.ContractManagement.impl.SlmCustAlphaServiceImpl;
import market.Slm.ContractManagement.vo.SlmCustAlpha;
import market.Slm.ContractManagement.vo.SlmCustAlphaPhy;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path = "QualityDesignStdPicService")
public class QualityDesignStdPicServiceImpl extends BaseServiceImpl<Object, Object>
		implements QualityDesignStdPicService {

	/**
	 * 获取交付、客户公差标准
	 * 
	 * @param designKey
	 * @param psc
	 * @return
	 */
	@Override
	public ResultModel GetStdPhyItem(CommonPage<SlmOrdDesignStd> com) {
		ResultModel model = new ResultModel("200", "获取数据成功");
		try {
			SlmOrdDesignStd _designStd = new SlmOrdDesignStd();
			_designStd.setDesign_key(com.getObject().getDesign_key());
			_designStd.setPsc(com.getObject().getPsc());
			SlmOrdDesignStd designStd = this.getMapper(SlmOrdDesignStdMapper.class)
					.findById(com.getObject().getDesign_key(), com.getObject().getPsc());
			/*String condition = DataHelper.CreateSqlCondition(designStd.getPic().split(";"), "pic") + " And design_key='"
					+ DataHelper.TrimSqlDangerMark(com.getObject().getDesign_key()) + "'";
		*/	List<SlmOrdDesignStdPic> listStdPic = new ArrayList<>();
			if (designStd.getPic()!=null && !"".equals(designStd.getPic())) {
				listStdPic = this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null,
						"pic='"+designStd.getPic()+"' and design_key='"+com.getObject().getDesign_key()+"'", null, 0, 0);
			}
			
			/*List<SlmOrdDesignStdPic> listStdPic = this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null,
					condition, null, 0, 0);*/
			if (!DataHelper.IsNullOrSpace(designStd.getPic_k())) {
			 String condition = "pic='"+designStd.getPic_k()+"' and DESIGN_KEY='"+com.getObject().getDesign_key()+"'";
				List<SlmOrdDesignStdPic> listPic = this.getMapper(SlmOrdDesignStdPicMapper.class)
						.findWithCondition(null, condition, null, 0, 0);
				for (SlmOrdDesignStdPic slmOrdDesignStdPic : listPic) {
					boolean find = false;
					for (SlmOrdDesignStdPic _slmOrdDesignStdPic : listStdPic) {
						if (_slmOrdDesignStdPic.getPhy_code_s().equals(slmOrdDesignStdPic.getPhy_code_s())) {
							_slmOrdDesignStdPic = slmOrdDesignStdPic;
							find = true;
							break;
						}
					}
					if (!find) {// 如果交付标准中没有包含客户要求的公差项则add
						listStdPic.add(slmOrdDesignStdPic);
					}
				}
			}
			
			//添加内控材质试样信息
			List<SlmOrdDesignStdPic> listPicN=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, "DESIGN_KEY='"+com.getObject().getDesign_key()+"' and SPECL_FL='2'", null, 0, 0);
			if (listPicN!=null && listPicN.size()>0) {
				listStdPic.addAll(listPicN);
			}
			model.setData(listStdPic);
		} catch (Exception ex) {
			model.setState("500");
			model.setErrorInfo(ex.getMessage());
			model.setMsgInfo("获取标准信息失败");
		}
		return model;
	}

	/**
	 * 过滤交货状态
	 * 
	 * @param listPic      材质索引
	 * @param deliveryCode 交货状态编号
	 */
	@Override
	public boolean FilterDeliveryState(List<ComMscStdPic> listPic, String deliveryCode) {
		for (int i = 0; i < listPic.size(); i++) {
			ComMscStdPic pic = listPic.get(i);
			if (pic.getValidflag().equals("0")) {
				listPic.remove(i);
				i--;
				continue;
			}
			if (pic.getDeliveryStateCode() != null) {
				if (!pic.getDeliveryStateCode().contains(deliveryCode)) {
					listPic.remove(i);
					i--;
				}
			}
		}
		return listPic.size() > 0;
	}

	/**
	 * 验证材质标准
	 * 
	 * @param msg       返回信息
	 * @param msc       冶金规范码
	 * @param orderLine 合同信息
	 * @param designKey
	 * @param           flag，0：国标，1：客户标准，2：内控标准
	 * @return
	 */
	@Override
	public boolean ValidationStdPIC(ReturnMsg msg, ComMsc msc, SlmContractProcessPlan orderLine, String designKey,
			String flag) {
		ComMscStdPicMapper mapper = this.getMapper(ComMscStdPicMapper.class);
		// 如果冶金规范码已经通过审批，即状态等于C 材质标准索引ComMscStdPic
		List<ComMscStdPic> ApprovalPics = new ArrayList<>();
		ComMscStdPic _mscStdPic = new ComMscStdPic();
		_mscStdPic.setMsc(msc.getMsc());
		if (msc.getValidflag().equals("C")) {
			//_mscStdPic.setValidflag("3");
			//ApprovalPics = mapper.find(_mscStdPic, 0, 0);
			ApprovalPics=mapper.findWithCondition(null, "MSC='" + msc.getMsc() + "' and VALIDFLAG!='0' and (STD_TYPE='G' or STD_TYPE='X') ", null, 0, 0);
		} else {
			ApprovalPics = mapper.findWithCondition(null, "MSC='" + msc.getMsc() + "' and (STD_TYPE='G' or STD_TYPE='X') and VALIDFLAG!='0'", null, 0, 0);
			
		}
	
		if (ApprovalPics.size() < 1 && (flag.equals("1") || flag.equals("0"))) {// 客户、国标标准中不存在标准索引则视为无标准要求
			msg.setMessage("无材质要求");
			return true;
		}
		// 过滤交货状态
		boolean pass = FilterDeliveryState(ApprovalPics, orderLine.getDelivery_state_code());
		if (!pass) {
			if ("0".equals(flag)) {
				if (!pass) {
					msg.setMessage("材质标准无要求");
					return true;
				}
				
				
			}else if ("1".equals(flag)) {

				if (!pass) {
					msg.setMessage("制造规范码[" + msc.getMsc() + "]没有找到符合订单交货状态的材质标准索引信息");
					return false;
				}
				
			}
			
		}

		if (ApprovalPics.size() > 1) {

			msg.setMessage("找到多条符合订单交货状态的材质标准索引");
			return false;
		}

		// 获取索引
		String PIC = ApprovalPics.get(0).getPic();

		// 获取索引对应的试样组信息
		ComMscStdPhy _stdPhy = new ComMscStdPhy();
		_stdPhy.setValidflag("1");
		_stdPhy.setPic(PIC);
		List<ComMscStdPhy> phys = this.getMapper(ComMscStdPhyMapper.class).find(_stdPhy, 0, 0);
		if (phys == null) {
			msg.setMessage("材质索引号:" + PIC + "找不到有效的材质标准");
			return false;
		}

		// 过滤规格
		List<ComMscStdPhy> phyAs = FilterSpec(phys, orderLine);
		if (phyAs.size() < 1) {
			msg.setMessage("材质索引：" + PIC + "，找不到符合合同规格的材质标准");
			return false;
		}

		// 对比项目
		List<ComMscStdPhy> phyAs1 = new ArrayList<ComMscStdPhy>();
		StringBuffer newCodes;
		StringBuffer oldCodes;
		for (int i = 0; i < phyAs.size(); i++) {
			newCodes = new StringBuffer();
			newCodes.append(phyAs.get(i).getPhyCodeL() + phyAs.get(i).getPhyCodeM() + phyAs.get(i).getPhyCodeS()
					+ phyAs.get(i).getItemCodeD() + phyAs.get(i).getItemCodeS() + phyAs.get(i).getItemCodeT()+phyAs.get(i).getSampleStyleCode());

			if (i == 0) {
				phyAs1.add(phyAs.get(i));
				continue;
			}

			for (int j = 0; j < phyAs1.size(); j++) {
				oldCodes = new StringBuffer();
				oldCodes.append(phyAs1.get(j).getPhyCodeL() + phyAs1.get(j).getPhyCodeM() + phyAs1.get(j).getPhyCodeS()
						+ phyAs1.get(j).getItemCodeD() + phyAs1.get(j).getItemCodeS() + phyAs1.get(j).getItemCodeT()+phyAs1.get(j).getSampleStyleCode());

				if (newCodes.toString().equals(oldCodes.toString())) {
					msg.setMessage("检验项目：" + phyAs.get(i).getPhyNameS() + "，存在多条符合订单要求的记录，请确认！");
					return false;

				}

				if (j == phyAs1.size() - 1) {
					phyAs1.add(phyAs.get(i));
					break;
				}

			}

		}
		
		
		
		List<ComMscStdPhy> tarPhy = new ArrayList<ComMscStdPhy>();
		// 如果是特殊要求的mscn 判断是指定mscn还是转化的mscn
		if ("1".equals(flag) && orderLine.getCustomer_alpha_no() != null) {
			// 获取客户要求的公差标准
			CommonPage<SlmCustAlpha> sum = new CommonPage<SlmCustAlpha>();
			SlmCustAlpha al = new SlmCustAlpha();
			al.setCust_alpha_no(orderLine.getCustomer_alpha_no());
			sum.setObject(al);
			ResultModel rm = this.getBean(SlmCustAlphaServiceImpl.class).doQueryWeb(sum);
			SlmCustAlpha alpha = (SlmCustAlpha) rm.getData();
			List<SlmCustAlphaPhy> phyCust = alpha.getPhyList();
			String comItem = null;

			for (SlmCustAlphaPhy cust : phyCust) {
				List<ComMscStdPhy> randomPhy = new ArrayList<ComMscStdPhy>();
				String custItem = cust.getPhy_code_l() + cust.getPhy_code_m() + cust.getPhy_code_s()
						+ cust.getItem_code_d() + cust.getItem_code_s() + cust.getItem_code_t()
						+ cust.getSampleStyleCode();
				// 将项目放入零时集合中
				for (int i = 0; i < phyAs1.size(); i++) {
					comItem = phyAs1.get(i).getPhyCodeL() + phyAs1.get(i).getPhyCodeM() + phyAs1.get(i).getPhyCodeS()
							+ phyAs1.get(i).getItemCodeD() + phyAs1.get(i).getItemCodeS() + phyAs1.get(i).getItemCodeT()
							+ phyAs1.get(i).getSampleStyleCode();
					if (custItem.equals(comItem)) {
						randomPhy.add(phyAs1.get(i));

					}

				}

				if (randomPhy.size() < 1) {
					msg.setMessage("材质索引号：" + PIC + "未找到订单要求的试样类型为[" + cust.getSampleStyleName()+ "]的材质项目["
							+ cust.getPhy_name_s() + "]");
					return false;
				}

				// 过滤标准值
				String rNue = DataHelper.GetNull(cust.getStdmin_sign()) + DataHelper.GetNull(cust.getStdmin())
						+ DataHelper.GetNull(cust.getStdmax_sign()) + DataHelper.GetNull(cust.getStdmax());
				for (int i = 0; i < randomPhy.size(); i++) {
					String tNue = DataHelper.GetNull(randomPhy.get(i).getStdminSign())
							+ DataHelper.GetNull(randomPhy.get(i).getStdmin())
							+ DataHelper.GetNull(randomPhy.get(i).getStdmaxSign())
							+ DataHelper.GetNull(randomPhy.get(i).getStdmax());
					if (!rNue.equals(tNue)) {
						randomPhy.remove(i);
						i--;
					}

				}

				if (randomPhy.size() < 1) {
					msg.setMessage("材质索引号：" + PIC + "向下的试样类型为[" + cust.getSampleStyleName()+ "]的材质项目["
							+ cust.getPhy_name_s() + "]的标准值信息与客户指定不符");
					return false;
				}

				if (randomPhy.size() > 1) {
					msg.setMessage("材质索引号：" + PIC + "向下的试样类型为[" + cust.getSampleStyleName() + "]的材质项目["
							+ cust.getPhy_name_s() + "]的标准值信息相符的数据找到多条");
					return false;
				}
				tarPhy.addAll(randomPhy);
			}

			

		}else {
			tarPhy.addAll(phyAs1);
		}


		// 去重
		List<ComMscStdPhy> phyAss = new ArrayList<ComMscStdPhy>();

		for (int i = 0; i < tarPhy.size(); i++) {
			String oldProjects = phyAs1.get(i).getPhyCodeM() + phyAs1.get(i).getItemCodeD()
					+ phyAs1.get(i).getItemCodeT() + phyAs1.get(i).getItemCodeS();

			if (i == 0) {
				phyAss.add(phyAs1.get(i));
				continue;
			}

			for (int j = 0; j < phyAss.size(); j++) {
				String newProjects = phyAss.get(j).getPhyCodeM() + phyAss.get(j).getItemCodeD()
						+ phyAss.get(j).getItemCodeT() + phyAss.get(j).getItemCodeS();

				if (oldProjects.equals(newProjects)) {

					break;

				}

				if (j == phyAss.size() - 1) {
					phyAss.add(phyAs1.get(i));
				}

			}

		}

		// 遍历集合
		StringBuffer phyCode;
		for (ComMscStdPhy phy : phyAss) {
			phyCode = new StringBuffer();
			phyCode.append((phy.getPhyCodeM() == null ? "null" : phy.getPhyCodeM()) + "_");
			phyCode.append((phy.getItemCodeD() == null ? "null" : phy.getItemCodeD()) + "_");
			phyCode.append((phy.getItemCodeT() == null ? "null" : phy.getItemCodeT()) + "_");
			phyCode.append((phy.getItemCodeS() == null ? "null" : phy.getItemCodeS()));
			List<ComMscPhySample> listSam = this.getMapper(ComMscPhySampleMapper.class).findWithCondition(null,
					"PIC='" + phy.getPic() + "'and SAMPLE_KEY='" + phyCode + "'", null, 0, 0);
			if (listSam == null || listSam.size() < 1) {

				msg.setMessage("材质索引号：" + phy.getPic() + "的试样组：" + phy.getPhyNameM() + "没有找到适用的取样要求");
				return false;
			}

			// 验证材质标准的检验项目
			List<ComMscPhySample> smps = new ArrayList<ComMscPhySample>();
			// 验证取样要求
			for (ComMscPhySample comMscPhySample : listSam) {
				ComMscPhySample smp = checkSmple(comMscPhySample, orderLine);
				//ComMscPhySample smp = checkSmp(comMscPhySample, orderLine);
				if (smp == null) {
					continue;
				} else {
					if (smp.getSmp_index().equals("")) {
						continue;
					}
				}
				smp.setDesign_key(designKey);
				smps.add(smp);

			}

			if (smps.size() > 1) {

				msg.setMessage("材质索引号：" + phy.getPic() + "的试样组：" + phy.getPhyNameM() + "找到多条适用的取样要求");
				return false;

			}

			if (smps.size() < 1) {

				msg.setMessage("材质索引号:" + phy.getPic() + "没有取样要求(牌号：" + orderLine.getSteel_name() + ",宽度："
						+ orderLine.getWidth() + ",厚度：" + orderLine.getThick() + ")");
				return false;

			}

			for (ComMscPhySample smp : smps) {
				SlmOrdDesignStdPicSmpMapper smpMapper = this.getMapper(SlmOrdDesignStdPicSmpMapper.class);
				SlmOrdDesignStdPicSmp _smp = new SlmOrdDesignStdPicSmp();
				try {
					DataHelper.CopyEntity(smp, _smp);
					// 查询基础表获取取样频率
					ComBaseSample smple = this.getMapper(ComBaseSampleMapper.class).findById(smp.getSmp_index());
					_smp.setRate(smple.getRate());
					if (smple.getSample_count()!=null ) {
						_smp.setSample_count(smple.getSample_count().intValue());
					}
					if (smple.getDesc_count()!=null) {
						_smp.setDesc_count(smple.getDesc_count().intValue());
					}	
					_smp.setDesc_position(smple.getDesc_position());
					_smp.setDesc_rate(smple.getDesc_rate());
					_smp.setDesign_key(designKey);
					smpMapper.insert(_smp);
				} catch (Exception ex) {
					msg.setMessage("材质取样要求验证出错：" + ex.getMessage());
					return false;

				}
			}

		}

		SlmOrdDesignStdPicMapper picMapper = this.getMapper(SlmOrdDesignStdPicMapper.class);
		for (ComMscStdPhy phy : tarPhy) {
			phy.setDesign_key(designKey);
			phy.setSpecl_fl(flag);
			SlmOrdDesignStdPic _stdPic = new SlmOrdDesignStdPic();
			try {
				DataHelper.CopyEntity(phy, _stdPic);
				// 是否特殊要求(0否;1是)
				if (flag.equals("1")) {
					_stdPic.setUse_code(orderLine.getUse_code());
					_stdPic.setUse_desc(orderLine.getUse_desc());
				}
				_stdPic.setDelivery_state_desc(orderLine.getDelivery_state_desc());
				_stdPic.setDelivery_state_code(orderLine.getDelivery_state_code());
				picMapper.insert(_stdPic);
			} catch (Exception ex) {
				msg.setMessage("写入材质标准出错：" + ex.getMessage());
				return false;
			}
		}
		// 修改标准索引号
		UpdateDesignStdPic(designKey, msc.getPsc(), PIC, flag);
		msg.setMessage("材质标准验证通过");
		return true;
	}

	private List<ComMscStdPhy> FilterDeliveryPhyState(List<ComMscStdPhy> phys, SlmContractProcessPlan orderLine) {

		for (int i = 0; i < phys.size(); i++) {
			if (phys.get(i).getDeliveryStateCode() != null
					&& !phys.get(i).getDeliveryStateCode().contains(orderLine.getDelivery_state_code())) {
				phys.remove(i);
				i--;
			}

		}

		return phys;
	}

	/**
	 * 修改索引
	 * 
	 * @param designKey
	 * @param psc       产品码
	 * @param pic       材质索引号
	 * @param flag      标准类型
	 */
	private void UpdateDesignStdPic(String designKey, String psc, String pic, String flag) {
		SlmOrdDesignStdMapper stdMapper = this.getMapper(SlmOrdDesignStdMapper.class);
		SlmOrdDesignStd designStd = stdMapper.findById(designKey, psc);// 根据designKey与psc找到当前标准索引信息
		SlmOrdDesignStd std = new SlmOrdDesignStd();
		std.setDesign_key(designKey);
		std.setPsc(psc);
		if (flag.equals("0")) {
			/* std.setPic(designStd.getPic()==null?pic:designStd.getPic()+";"+pic); */
			std.setPic(pic);
		} else if (flag.equals("1")) {
			/*
			 * std.setPic_k(designStd==null?pic:designStd.getPic_k()+";"+pic);
			 */ std.setPic_k(pic);
		}
		this.getMapper(SlmOrdDesignStdMapper.class).update(std);
	}

	/**
	 * 过滤取样要求
	 * 
	 * @param smp
	 * @param orderLine
	 * @return
	 */
	@Override
	public ComMscPhySample checkSmp(ComMscPhySample smp, SlmContractProcessPlan orderLine) {
		String width = orderLine.getWidth().toString();
		String thick = orderLine.getThick().toString();
		ComMscPhySample returnData = null;
		if (DataHelper.checkStr(width, smp.getMin_d_sign(), smp.getMin_d())
				&& DataHelper.checkStr(width, smp.getMax_d_sign(), smp.getMax_d())
				&& DataHelper.checkStr(thick, smp.getMin_h_sign(), smp.getMin_h())
				&& DataHelper.checkStr(thick, smp.getMax_h_sign(), smp.getMax_h())) {
			returnData = smp;
		} else if ((smp.getMin_d()==null||"".equals(smp.getMin_d())) && (smp.getMax_d()==null || "".equals(smp.getMax_d()) )&& (smp.getMin_h()==null ||"".equals(smp.getMin_h()))
				&& (smp.getMax_h()==null ||"".equals(smp.getMax_h()))) {
			returnData = smp;
		}
		return returnData;
	}

	/**
	 * 过滤限制规格
	 * 
	 * @param listChem
	 * @param orderLine
	 * @return
	 */
	@Override
	public List<ComMscStdPhy> FilterSpec(List<ComMscStdPhy> phys, SlmContractProcessPlan orderLine) {
		List<ComMscStdPhy> _listChem = new ArrayList<>();
		// 过滤限制规格 厚 400301， 宽 400302， 长 400306
		for (ComMscStdPhy _stdPhy : phys) {
			if (!DataHelper.IsNullOrSpace(_stdPhy.getSizeCode())) {// 为空则适用全部规格
				// 过滤限制规格一
				if (_stdPhy.getSizeCode().equals("400302")) {
					boolean pass;
					if (_stdPhy.getSizeMax() == null) {
						pass = true;
					} else {
						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign(),
								Double.parseDouble(_stdPhy.getSizeMax()), DataHelper.GetString(orderLine.getWidth()));
					}

					if (!pass) {
						continue;
					}

					if (_stdPhy.getSizeMin() == null) {
						pass = true;
					} else {
						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign(),
								Double.parseDouble(_stdPhy.getSizeMin()), DataHelper.GetString(orderLine.getWidth()));
					}

					if (!pass) {
						continue;
					}
				} else if (_stdPhy.getSizeCode().equals("400301")) {
					boolean pass;

					if (_stdPhy.getSizeMax() == null) {
						pass = true;
					} else {
						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign(),
								Double.parseDouble(_stdPhy.getSizeMax()), DataHelper.GetString(orderLine.getThick()));
					}

					if (!pass) {
						continue;
					}

					if (_stdPhy.getSizeMin() == null) {
						pass = true;
					} else {
						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign(),
								Double.parseDouble(_stdPhy.getSizeMin()), DataHelper.GetString(orderLine.getThick()));
					}

					if (!pass) {
						continue;
					}
				} else if (_stdPhy.getSizeCode().equals("400306")) {
					boolean pass;

					if (_stdPhy.getSizeMax() == null) {
						pass = true;
					} else {

						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign(),
								Double.parseDouble(_stdPhy.getSizeMax()), DataHelper.GetString(orderLine.getLength()));
					}

					if (!pass) {
						continue;
					}

					if (_stdPhy.getSizeMin() == null) {
						pass = true;
					} else {
						pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign(),
								Double.parseDouble(_stdPhy.getSizeMin()), DataHelper.GetString(orderLine.getLength()));
					}

					if (!pass) {
						continue;
					}
				}
				// 验证限制规格二
				if (!DataHelper.IsNullOrSpace(_stdPhy.getSizeCode2())) {
					if (_stdPhy.getSizeCode2().equals("400302")) {
						boolean pass;

						if (_stdPhy.getSizeMax2() == null) {
							pass = true;

						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign2(),
									Double.parseDouble(_stdPhy.getSizeMax2()),
									DataHelper.GetString(orderLine.getWidth()));
						}

						if (!pass) {
							continue;
						}

						if (_stdPhy.getSizeMin2() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign2(),
									Double.parseDouble(_stdPhy.getSizeMin2()),
									DataHelper.GetString(orderLine.getWidth()));
						}

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getSizeCode2().equals("400301")) {
						boolean pass;

						if (_stdPhy.getSizeMax2() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign2(),
									Double.parseDouble(_stdPhy.getSizeMax2()),
									DataHelper.GetString(orderLine.getThick()));
						}

						if (!pass) {
							continue;
						}

						if (_stdPhy.getSizeMin2() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign2(),
									Double.parseDouble(_stdPhy.getSizeMin2()),
									DataHelper.GetString(orderLine.getThick()));
						}

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getSizeCode2().equals("400306")) {
						boolean pass;

						if (_stdPhy.getSizeMax2() == null) {
							pass = true;

						} else {

							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMaxSign2(),
									Double.parseDouble(_stdPhy.getSizeMax2()),
									DataHelper.GetString(orderLine.getLength()));
						}

						if (!pass) {
							continue;
						}

						if (_stdPhy.getSizeMin2() == null) {
							pass = true;
						} else {

							pass = DataHelper.checkSignAndValue(_stdPhy.getSizeMinSign2(),
									Double.parseDouble(_stdPhy.getSizeMin2()),
									DataHelper.GetString(orderLine.getLength()));
						}

						if (!pass) {
							continue;
						}
					}
				}
			}
			_listChem.add(_stdPhy);
		}
		return _listChem;
	}

	public ResultModel getSample(CommonPage<SlmOrdDesignStdPicSmp> oci) {
		
		ResultModel rm=new ResultModel();
		try {
		
			SlmOrdDesignStdPicSmp smp=oci.getObject();
			
			if (smp==null || smp.getDesign_key()==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入designkey");
				return rm;
			}
			
			//得到psc
			if (oci.getMemo1()==null || "".equals(oci.getMemo1())) {
				rm.setState("500");
				rm.setMsgInfo("请传入psc");
				return rm;
			}
			
		//根据design_key+psc查询质量设计材质结果表
		SlmOrdDesignStd std=this.getMapper(SlmOrdDesignStdMapper.class).findById(smp.getDesign_key(),oci.getMemo1());
		//获取pic,以及pic_k取并集
		List<SlmOrdDesignStdPic> picks=new ArrayList<>();
		List<SlmOrdDesignStdPic> pics=new ArrayList<>();
		if (std!=null) {
			if (std.getPic_k()!=null && !"".equals(std.getPic_k())) {
				picks=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, "DESIGN_KEY='"+smp.getDesign_key()+"' and PIC='"+std.getPic_k()+"'", null, 0, 0);
			}
			
			
			if (std.getPic()!=null && !"".equals(std.getPic())) {
				pics=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, "DESIGN_KEY='"+smp.getDesign_key()+"' and PIC='"+std.getPic()+"'", null, 0, 0);
			}
		}
		
		List<SlmOrdDesignStdPic> target=new ArrayList<SlmOrdDesignStdPic>();
		if (picks!=null && picks.size()>0) {
			for (SlmOrdDesignStdPic pick : picks) {
				String big=DataHelper.Get00000(pick.getPhy_code_m())+DataHelper.Get00000(pick.getItem_code_d())+DataHelper.Get00000(pick.getItem_code_t())+DataHelper.Get00000(pick.getItem_code_s());
				if (target!=null && target.size()>0) {
					for (int i = 0; i < target.size(); i++) {
						String tel=DataHelper.Get00000(target.get(i).getPhy_code_m())+DataHelper.Get00000(target.get(i).getItem_code_d())+DataHelper.Get00000(target.get(i).getItem_code_t())+DataHelper.Get00000(target.get(i).getItem_code_s());
						if (big.equals(tel)) {
							break;
						}
					}
				}else {
					target.add(pick);
				}
				
			}
			
		}
		target.addAll(picks);
		for (SlmOrdDesignStdPic pic : pics) {
			String big=DataHelper.Get00000(pic.getPhy_code_m())+DataHelper.Get00000(pic.getItem_code_d())+DataHelper.Get00000(pic.getItem_code_t())+DataHelper.Get00000(pic.getItem_code_s());
			if (target!=null && target.size()>0) {
				for (int i = 0; i < target.size(); i++) {
					String tel=DataHelper.Get00000(target.get(i).getPhy_code_m())+DataHelper.Get00000(target.get(i).getItem_code_d())+DataHelper.Get00000(target.get(i).getItem_code_t())+DataHelper.Get00000(target.get(i).getItem_code_s());
					if (big.equals(tel)) {
						break;
					}
					
					if (i ==target.size()-1) {
						target.add(pic);
					}
					
				}
				
			}else {
				target.add(pic);
			}
			
		}
		
		List<SlmOrdDesignStdPicSmp> smps=new ArrayList<>();
		for (SlmOrdDesignStdPic pic : target) {
			String sample_key=DataHelper.GetNull(pic.getPhy_code_m())+"_"+DataHelper.GetNull(pic.getItem_code_d())+"_"+DataHelper.GetNull(pic.getItem_code_t())+"_"+DataHelper.GetNull(pic.getItem_code_s());
			List<SlmOrdDesignStdPicSmp> ran=this.getMapper(SlmOrdDesignStdPicSmpMapper.class).findWithCondition(null, "DESIGN_KEY='"+smp.getDesign_key()+"' and PIC='"+pic.getPic()+"' and SAMPLE_KEY='"+sample_key+"'", null, 0, 0);
			for (SlmOrdDesignStdPicSmp slmOrdDesignStdPicSmp : ran) {
				slmOrdDesignStdPicSmp.setItem_name_d(pic.getItem_name_d());
				slmOrdDesignStdPicSmp.setItem_name_s(pic.getItem_name_s());
				slmOrdDesignStdPicSmp.setItem_name_t(pic.getItem_name_s());
				smps.add(slmOrdDesignStdPicSmp);
				
			}
			
		
		}
	
		
		rm.setState("200");	
		rm.setData(smps);
			
			
			
		} catch (Exception e) {
			rm.setState("500");	
			rm.setMsgInfo("查询材质取样要求失败！");
			return rm;
		
		
		
		
		}
		
		
		
		return rm;
	}

	public boolean judgePhy(List<SlmCustAlphaPhy> phyT, List<ComMscStdPic> listPic, SlmContractProcessPlan orderLine) {
		// 查询材质项目
		List<ComMscStdPhy> phyStandard = this.getMapper(ComMscStdPhyMapper.class).findWithCondition(null,
				"PIC='" + listPic.get(0).getPic() + "'", null, 0, 0);

		HashSet<String> stands = new HashSet<String>();
		for (int j = 0; j < phyStandard.size(); j++) {
			String standProject = phyStandard.get(j).getPhyCodeL() + phyStandard.get(j).getPhyCodeM()
					+ phyStandard.get(j).getPhyCodeS() + phyStandard.get(j).getItemCodeD()
					+ phyStandard.get(j).getItemCodeS() + phyStandard.get(j).getItemCodeT()
					+ phyStandard.get(j).getSampleStyleCode();
			stands.add(standProject);

		}
		// 判断项目个数是否满足
		if (stands.size() != phyT.size()) {
			return false;
		}

		// 判断项目是否存在
		boolean judge = FilterSpecificationsPhy(stands, phyT);

		if (!judge) {
			return false;
		}
		// 判断客户要求的材质项目是否符合规格
		List<String> tarl = new ArrayList<String>();
		for (int i = 0; i < phyStandard.size(); i++) {
			/*
			 * ComMscStdPhy comPhy =
			 * this.getBean(QualityDesignMscNServiceImpl.class).FilterPhy(phyStandard.get(i)
			 * , orderLine);
			 */

			List<ComMscStdPhy> listPhy = new ArrayList<ComMscStdPhy>();
			listPhy.add(phyStandard.get(i));

			if (this.FilterSpec(listPhy, orderLine) != null && this.FilterSpec(listPhy, orderLine).size() > 0) {
				// 过滤标准
				judge = this.FilterStandPhy(phyStandard.get(i), phyT);

				if (!judge) {
					return false;
				}

				// 过滤交货状态
				if (phyStandard.get(i).getDeliveryStateCode() != null
						&& !phyStandard.get(i).getDeliveryStateCode().contains(orderLine.getDelivery_state_code())) {
					return false;
				}

				tarl.add(phyStandard.get(i).getPhyCodeL() + phyStandard.get(i).getPhyCodeM()
						+ phyStandard.get(i).getPhyCodeS() + phyStandard.get(i).getItemCodeD()
						+ phyStandard.get(i).getItemCodeS() + phyStandard.get(i).getItemCodeT());

			}

		}

		if (tarl.size() == phyT.size()) {
			stands.clear();
			stands.addAll(tarl);
			if (stands.size() != phyT.size()) {
				return false;
			}

		} else {
			return false;
		}

		return true;
	}

	/**
	 * 过滤材质项目
	 * 
	 * @param stands
	 * @param phyT
	 * @return
	 */
	private boolean FilterSpecificationsPhy(HashSet<String> stands, List<SlmCustAlphaPhy> phyT) {

		for (SlmCustAlphaPhy slmCustAlphaPhy : phyT) {
			String project = slmCustAlphaPhy.getPhy_code_l() + slmCustAlphaPhy.getPhy_code_m()
					+ slmCustAlphaPhy.getPhy_code_s() + slmCustAlphaPhy.getItem_code_d()
					+ slmCustAlphaPhy.getItem_code_s() + slmCustAlphaPhy.getItem_code_t()
					+ slmCustAlphaPhy.getSampleStyleCode();
			if (!stands.contains(project)) {
				return false;

			}

		}

		return true;
	}

	/**
	 * 判断标准规格，是否满足订单要求的规格
	 * 
	 * @param comMscStdPhy
	 * @param phyT
	 * @return
	 */

	public boolean FilterStandPhy(ComMscStdPhy comMscStdPhy, List<SlmCustAlphaPhy> phyT) {

		String old = comMscStdPhy.getPhyCodeL() + comMscStdPhy.getPhyCodeM() + comMscStdPhy.getPhyCodeS()
				+ comMscStdPhy.getItemCodeD() + comMscStdPhy.getItemCodeS() + comMscStdPhy.getItemCodeT();
		for (SlmCustAlphaPhy phy : phyT) {
			String newProject = phy.getPhy_code_l() + phy.getPhy_code_m() + phy.getPhy_code_s() + phy.getItem_code_d()
					+ phy.getItem_code_s() + phy.getItem_code_t();

			if (old.equals(newProject)) {
				// 判断项目规格标准是否满足要求
				boolean pass;
				if (comMscStdPhy.getStdmin() == null) {
					pass = true;
				} else {
					pass = DataHelper.checkSignAndValue(comMscStdPhy.getStdminSign(),
							Double.parseDouble(comMscStdPhy.getStdmin()), phy.getStdmin());

				}

				if (!pass) {
					return false;
				}

				if (comMscStdPhy.getStdmax() == null) {
					pass = true;
				} else {
					pass = DataHelper.checkSignAndValue(comMscStdPhy.getStdmaxSign(),
							Double.parseDouble(comMscStdPhy.getStdmax()), phy.getStdmax());

				}

				if (!pass) {
					return false;
				}

			}

		}

		return true;
	}
	
	
	/**
	 * 过滤取样要求
	 * by zhangjiao  3/14
	 * @param smp
	 * @param orderLine
	 * @return
	 */
	
	public ComMscPhySample checkSmple(ComMscPhySample smp, SlmContractProcessPlan orderLine) {
		//过滤适应产品名称
		
		ComMscPhySample returnData = null;
		List<ComBaseSampleRProduct> products=this.getMapper(ComBaseSampleRProductMapper.class).findWithCondition(null, "SMP_INDEX='"+smp.getSmp_index()+"'", null, 0, 0);
		if (products!=null && products.size()>0) {
			for (int i = 0; i <products.size(); i++) {
				if (!products.get(i).getProd_code().equals(orderLine.getProd_code())) {
					products.remove(i);
					i--;
				}
				
			}
			
			if (products==null || products.size()<1) {
				return returnData;
			}
		}
		
		
		//过滤牌号
		List<ComBaseSampleRSteel> steels=this.getMapper(ComBaseSampleRSteelMapper.class).findWithCondition(null, "SMP_INDEX='"+smp.getSmp_index()+"' and VALIDFLAG='1'", null, 0, 0);
		if (steels!=null && steels.size()>0) {
			for (int i = 0; i <steels.size(); i++) {
				if (!steels.get(i).getSteel_code().equals(orderLine.getSteel_code())) {
					steels.remove(i);
					i--;
				}
				
			}
			
			if (steels==null || steels.size()<1) {
				return returnData;
			}
		}
		
		//过滤标准
		List<ComBaseSampleRStd> stand=this.getMapper(ComBaseSampleRStdMapper.class).findWithCondition(null, "SMP_INDEX='"+smp.getSmp_index()+"' and VALIDFLAG='1'", null, 0, 0);
		if (stand!=null && stand.size()>0) {
			for (int i = 0; i <stand.size(); i++) {
				if (!stand.get(i).getStd_code().equals(orderLine.getStd_code())) {
					steels.remove(i);
					i--;
				}
				
			}
			
			if (stand==null || stand.size()<1) {
				return returnData;
			}
		}
		
		//过滤厚度宽度
		String width = orderLine.getWidth().toString();
		String thick = orderLine.getThick().toString();
		boolean pass;
		if (smp.getMin_d()==null||"".equals(smp.getMin_d())) {
			pass=true;
		}else{
			pass=DataHelper.checkStr(width, smp.getMin_d_sign(), smp.getMin_d());
					
		}
		if (!pass) {
			return returnData;
		}
		
		
		if (smp.getMax_d()==null || "".equals(smp.getMax_d())) {
			pass=true;
		}else{
			pass=DataHelper.checkStr(width, smp.getMax_d_sign(), smp.getMax_d());
		}
		
		if (!pass) {
			return returnData;
		}
		
		if (smp.getMin_h()==null||"".equals(smp.getMin_h())) {
			pass=true;
		}else{
			pass=DataHelper.checkStr(thick, smp.getMin_h_sign(), smp.getMin_h());
					
		}
		if (!pass) {
			return returnData;
		}
		
		if (smp.getMax_h()==null||"".equals(smp.getMax_h())) {
			pass=true;
		}else{
			pass=DataHelper.checkStr(thick, smp.getMax_h_sign(), smp.getMax_h());
					
		}
		if (!pass) {
			return returnData;
		}
		
		returnData = smp;
		return returnData;
	}

}
