	package market.Qms.slmord.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import market.Com.mapper.ComBaseToleranceMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBaseTolerance;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.com.vo.ComBaseConfig;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscStdSpecBiasCMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecBiasMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecCMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscStdSpec;
import market.Qms.metallurgy.vo.ComMscStdSpecBias;
import market.Qms.metallurgy.vo.ComMscStdSpecBiasC;
import market.Qms.metallurgy.vo.ComMscStdSpecC;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdSicMapper;
import market.Qms.slmord.service.QualityDesignStdSicService;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdSic;
import market.Slm.ContractManagement.impl.SlmContractLineLvServiceImpl;
import market.Slm.ContractManagement.impl.SlmCustAlphaServiceImpl;
import market.Slm.ContractManagement.vo.SlmContractLineLv;
import market.Slm.ContractManagement.vo.SlmCustAlpha;
import market.Slm.ContractManagement.vo.SlmCustAlphaSpec;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;
import market.Slm.OrderManagement.vo.SlmProcessPlan;

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

@RemoteService(path="QualityDesignStdSicService")
public class QualityDesignStdSicServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignStdSicService{
	
	/**
	 * 获取交付、客户公差标准
	 * @param designKey
	 * @param psc
	 * @return
	 */
	public ResultModel GetStdBias(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.getSic().split(";"),"sic")+" And design_key='"+DataHelper.TrimSqlDangerMark(com.getObject().getDesign_key())+"'";
			List<SlmOrdDesignStdSic> listStdSic=this.getMapper(SlmOrdDesignStdSicMapper.class).findWithCondition(null, condition, null, 0, 0);
			if(!DataHelper.IsNullOrSpace(_designStd.getSic_k())){
				condition=DataHelper.CreateSqlCondition(designStd.getSic_k().split(";"),"sic");
				List<SlmOrdDesignStdSic> listSic=this.getMapper(SlmOrdDesignStdSicMapper.class).findWithCondition(null, condition, null, 0, 0);
				for (SlmOrdDesignStdSic slmOrdDesignStdSic : listSic) {
					boolean find=false;
					for (SlmOrdDesignStdSic _slmOrdDesignStdSic : listStdSic) {
						if(_slmOrdDesignStdSic.getBias_code().equals(slmOrdDesignStdSic.getBias_code())){
							_slmOrdDesignStdSic=slmOrdDesignStdSic;
							find=true;
							break;
						}
					}
					if(!find){//如果交付标准中没有包含客户要求的公差项则add
						listStdSic.add(slmOrdDesignStdSic);
					}
				}
			}
			model.setData(listStdSic);
		}catch(Exception ex){
			model.setState("500");
			model.setErrorInfo(ex.getMessage());
			model.setMsgInfo("获取标准信息失败");
		}
		return model;
	}
	
	/**
	 *  获取交付标准自动判定结果
	 * @param designKey 
	 * @param psc 产品码
	 * @param thick 厚度
	 * @param width 宽度
	 * @param length 长度
	 * @return
	 */
	public ResultModel GetDeliverJudgeResult(String designKey,String psc,Double thick,Double width,Double length){
		ResultModel model=new ResultModel("200","判定成功");
		ComBaseInfo result=new ComBaseInfo();
		try{
			if(DataHelper.IsNullOrSpace(designKey)||DataHelper.IsNullOrSpace(psc)||thick==null||width==null||length==null){
				model.setState("404");
				model.setMsgInfo("参数不对");
				return model;
			}
			//1、查询公差项标准
			CommonPage<SlmOrdDesignStd> com=new CommonPage<>();
			SlmOrdDesignStd designStd=new SlmOrdDesignStd();
			designStd.setDesign_key(designKey);
			designStd.setPsc(psc);
			com.setObject(designStd);
			ResultModel biasModel=GetStdBias(com);
			if(biasModel.getData()==null){
				throw new Exception("获取标准信息出错");
			}
			List<SlmOrdDesignStdSic> listStdSic=(List<SlmOrdDesignStdSic>)biasModel.getData();
			//2、对比公差标准
			for (SlmOrdDesignStdSic slmOrdDesignStdSic : listStdSic) {
				double max=Double.parseDouble(slmOrdDesignStdSic.getStd_result_max());
				double min=Double.parseDouble(slmOrdDesignStdSic.getStd_result_min());
				boolean pass=false;
				if(slmOrdDesignStdSic.getBias_code().equals("T0001")){//宽度
					pass=width.doubleValue()<=max&&width.doubleValue()>=min;
				}else if(slmOrdDesignStdSic.getBias_code().equals("T0002")){//厚
					pass=thick.doubleValue()<=max&&thick.doubleValue()>=min;
				}else	if(slmOrdDesignStdSic.getBias_code().equals("T0003")){//长
					pass=length.doubleValue()<=max&&length.doubleValue()>=min;
				}
				if(!pass){
					result.setBase_code("40740702");
					result.setBase_name("不合格");
					model.setData(result);
					return model;
				}
			}	
		}catch(Exception ex){
			model.setErrorInfo(ex.getMessage());
			model.setMsgInfo("计算出错");
			model.setState("500");
			return model;
		}
		result.setBase_code("40740701");
		result.setBase_name("合格");
		model.setData(result);
		return model;
	}
	
	
	/**
	 * 过滤交货状态
	 * @param listSic 公差索引
	 * @param deliveryCode 交货状态编号
	 */
	@Override
	public boolean FilterDeliveryState(List<ComMscStdSpec> listSic,String deliveryCode){
		for (int i = 0; i < listSic.size(); i++) {
			ComMscStdSpec sic=listSic.get(i);
			if(sic.getValidflag().equals("0")){
				listSic.remove(i);
				i--;
				continue;
			}
			
			if(sic.getDeliveryStateCode()!=null){
				if(!sic.getDeliveryStateCode().contains(deliveryCode)){
					listSic.remove(i);
					i--;
				}
			}
		}
		return listSic.size()>0;
	}
	
	/**
	 * 验证公差标准
	 * @param msg 返回信息
	 * @param msc 冶金规范码
	 * @param design 合同信息
	 * @param designKey 
	 * @param flag，0：国标，1：客户标准，2：内控标准
	 * @return
	 */
	@Override
	public boolean ValidationStdSIC(ReturnMsg msg,ComMsc msc,SlmContractProcessPlan design,String designKey,String flag){
		List<ComMscStdSpec> sics = new ArrayList<ComMscStdSpec>();
		ComMscStdSpecMapper specMapper = this.getMapper(ComMscStdSpecMapper.class);
		ComMscStdSpec _querySpec = new ComMscStdSpec();
		_querySpec.setMsc(msc.getMsc());
		if (msc.getValidflag().equals("C")) {
			sics = specMapper.findWithCondition(null,
					"MSC='" + msc.getMsc() + "' and VALIDFLAG!='0' and (STD_TYPE='G' or STD_TYPE='X') ", null, 0, 0);
		} else {
			sics = specMapper.findWithCondition(null,
					"MSC='" + msc.getMsc() + "' and (STD_TYPE='G' or STD_TYPE='X') and VALIDFLAG !='0'", null, 0, 0);
		}
		List<SlmContractLineLv> orderReq = new ArrayList<>();

		try {
			// 调用销售接口,查询指定公差项,如果msc0向下存在公差标准索引号。
			SlmProcessPlan plan = new SlmProcessPlan();
			plan.setPlan_no(design.getContract_line_no());
			ResultModel rm = this.getBean(SlmContractLineLvServiceImpl.class).doQueryByPlanNoWeb(plan);
			orderReq = (List<SlmContractLineLv>) rm.getData();

		} catch (Exception e) {
			msg.setMessage("质量调用销售的查询指定公差要求失败，请找销售确认 ！");
			return false;
		}

		// 交付标准
		// 交付标准
		if ("0".equals(flag)) {
			// 客户要求存在
			if (orderReq != null && orderReq.size() > 0) {
				// 标准不存在
				if (sics.size() < 1) {
					msg.setMessage("订单存在公差要求，但" + design.getMsc() + "向下未找到符合要求的标准索引信息，请确认！");
					return false;

				}
			}

		}

		if (sics.size() < 1 && (flag.equals("1") || flag.equals("0"))) {// 客户、国标标准中不存在标准索引则视为无标准要求
			msg.setMessage("公差标准无要求");
			return true;
		}

		// 过滤交货状态
		boolean pass = FilterDeliveryState(sics, design.getDelivery_state_code());
		if (!pass) {

			if ("0".equals(flag)) {
				if (orderReq != null && orderReq.size() > 0) {
					msg.setMessage("制造规范码[" + msc.getMsc() + "]没有找到符合订单交货状态[" + design.getDelivery_state_desc()
							+ "]的公差标准索引信息");
					return false;

				} else {
					msg.setMessage("公差标准无要求");
					return true;
				}

			} else if ("1".equals(flag)) {
				msg.setMessage(
						"制造规范码[" + msc.getMsc() + "]没有找到符合订单交货状态[" + design.getDelivery_state_desc() + "]的公差标准索引信息");
				return false;
			}

		}

		// 过滤精度类型
		for (int i = 0; i < sics.size(); i++) {
			if (design.getH_precisicon_type_code()==null || "".equals(design.getH_precisicon_type_code())) {
				msg.setMessage("订单的精度类型代码为空，无法过滤公差标准，请联系销售人员确认！");
				return false;
			}

			if (!sics.get(i).getPrecisiconTypeCode().equals(design.getH_precisicon_type_code())) {
				sics.remove(sics.get(i));
				i--;

			}

		}

		if (sics.size() > 1) {
			msg.setMessage("符合订单精度类型" + design.getH_precisicon_type_desc() + "的标准索引信息存在多条，请确认！");
			return false;

		}

		if (sics.size() < 1) {
			msg.setMessage("未找到符合订单精度类型" + design.getH_precisicon_type_desc() + "的标准索引信息，请确认！");
			return false;

		}

		// 公差标准明细
		ComMscStdSpecCMapper specCMapper = this.getMapper(ComMscStdSpecCMapper.class);
		List<String> listBiasCode = new ArrayList<>();// 存放公差项，用来确定唯一公差项
		// 获取公差索引号
		ComMscStdSpec sic = sics.get(0);

		List<ComMscStdSpecC> specs = new ArrayList<>();// 用于存放公差标准明细

		if (!DataHelper.IsNullOrSpace(sic.getSpecStdCode())) {
			// 索引号向下的标准
			specs = findStandSpec(specCMapper, sic);
			// 查询引用标准
			List<ComMscStdSpecBiasC> listBiasC = new ArrayList<>();

			ComMscStdSpecBias _bias = new ComMscStdSpecBias();
			_bias.setStdCode(sic.getSpecStdCode());
			_bias.setValidflag("1");
			List<ComMscStdSpecBias> listSpecBias = this.getMapper(ComMscStdSpecBiasMapper.class).find(_bias, 0, 0);

			if (listSpecBias != null && listSpecBias.size() > 0) {
				ComMscStdSpecBiasC _biasC = new ComMscStdSpecBiasC();
				_biasC.setValidflag("1");
				_biasC.setSic(listSpecBias.get(0).getSic());
				listBiasC = this.getMapper(ComMscStdSpecBiasCMapper.class).find(_biasC, 0, 0);
			}

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

				List<String> list_id = new ArrayList<String>();
				list_id.add("SIC0009");
				List<ComBaseConfig> con = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
						.getBaseConfigInfo(list_id).getData();

				if (con != null && con.size() > 0) {
					if (con.get(0).getConfig_val() != null && !"".equals(con.get(0).getConfig_val())) {

						if ("0".equals(con.get(0).getConfig_val())) {
							// 判断公差标准索引号的精度等级代码是否符合订单的精度等级代码
							specs = new ArrayList<>();
							if (!listSpecBias.get(0).getPrecisiconTypeCode()
									.equals(design.getH_precisicon_type_code())) {

								msg.setMessage("制造规范码[" + msc.getMsc() + "]公差引用标准索引号" + sic.getSpecStdCode()
										+ "未找到符合订单精度类型" + design.getH_precisicon_type_desc() + "的标准索引信息，请确认！");
								return false;

							}

							for (ComMscStdSpecBiasC comMscStdSpecBiasC : listBiasC) {
								ComMscStdSpecC spec = new ComMscStdSpecC();
								try {
									DataHelper.CopyEntity(comMscStdSpecBiasC, spec);
									spec.setSic(sic.getSic());
									specs.add(spec);
								} catch (Exception ex) {
									ex.printStackTrace();
									msg.setMessage("公差引用标准赋值到公差标准出错");
									return false;
								}
							}

						}

					}

				}

			} else if (listBiasC != null && listBiasC.size() > 0) {
				// 判断公差标准索引号的精度等级代码是否符合订单的精度等级代码
				if (!listSpecBias.get(0).getPrecisiconTypeCode().equals(design.getH_precisicon_type_code())) {

					msg.setMessage("制造规范码[" + msc.getMsc() + "]公差引用标准索引号" + sic.getSpecStdCode() + "未找到符合订单精度类型"
							+ design.getH_precisicon_type_desc() + "的标准索引信息，请确认！");
					return false;

				}
				specs = new ArrayList<>();
				// ComMscStdSpecBiasC 转换为 ComMscStdSpecC
				for (ComMscStdSpecBiasC comMscStdSpecBiasC : listBiasC) {
					ComMscStdSpecC spec = new ComMscStdSpecC();
					try {
						DataHelper.CopyEntity(comMscStdSpecBiasC, spec);
						spec.setSic(sic.getSic());
						specs.add(spec);
					} catch (Exception ex) {
						ex.printStackTrace();
						msg.setMessage("公差引用标准赋值到公差标准出错");
						return false;
					}
				}

			}
		} else {
			// 默认索引向下的标准
			specs = findStandSpec(specCMapper, sic);
		}

		// 查詢公差交付标准
		if (specs.size() < 1) {
			msg.setMessage("公差索引号：" + sic.getSic() + "不存在公差标准");
			return false;
		}
		List<ComMscStdSpecC> specAs = new ArrayList<ComMscStdSpecC>();
		// 过滤限制规格

		List<ComMscStdSpecC> specAl = FilterSpec(specs, design);
		if (specAl == null || specAl.size() < 1) {
			msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "'未找到符合订单规格的标准");
			return false;
		}

		if (flag.equals("0")) {
			// 得到订单的公差项目需求
			if (orderReq != null && orderReq.size() > 0) {
				for (SlmContractLineLv slmLv : orderReq) {

					List<ComMscStdSpecC> randomSpec = new ArrayList<ComMscStdSpecC>();

					for (int i = 0; i < specAl.size(); i++) {
						if (slmLv.getTol_code().equals(specAl.get(i).getBiasCode())) {
							randomSpec.add(specAl.get(i));
						}

					}

					if (randomSpec.size() < 1) {
						msg.setMessage(
								"订单存在[" + slmLv.getTol_name() + "]公差要求,但[" + specAl.get(0).getSic() + "]中未找到,请确认!");
						return false;
					}

					// 对比精度等级
					for (int i = 0; i < randomSpec.size(); i++) {
						if (randomSpec.get(i).getLvCode()!=null && !"".equals(randomSpec.get(i).getLvCode())) {
							if (!randomSpec.get(i).getLvCode().equals(slmLv.getLv_code())) {
								randomSpec.remove(i);
								i--;
							}
						}

					}

					if (randomSpec.size() < 1) {
						msg.setMessage("订单存在[" + slmLv.getTol_name() + "精度等级" + slmLv.getLv_desc() + "]公差要求,但["
								+ specAl.get(0).getSic() + "]中找不到符合订单要求的公差标准,请确认!");
						return false;

					}

					if (randomSpec.size() > 1) {
						msg.setMessage("订单存在[" + slmLv.getTol_name() + "精度等级" + slmLv.getLv_desc() + "]公差要求,但["
								+ specAl.get(0).getSic() + "]中找到多条符合订单要求的公差标准,请确认!");
						return false;

					}

					specAs.addAll(randomSpec);

				}

			} else {

				// 过滤切边方式
				for (int i = 0; i < specAl.size(); i++) {

					if ("T0002".equals(specAl.get(i).getBiasCode())) {
						// 切边方式问题

						if (!specAl.get(i).getCut_edge_type_no().equals(design.getCut_edge_type_code())) {
							specAl.remove(i);
							i--;
						}
					}

				}

				if (specAl == null || specAl.size() < 1) {
					msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "'未找到符合订单要求的标准");
					return false;
				}

				specAs.addAll(specAl);

			}
		} else if (flag.equals("1") && design.getCustomer_alpha_no() != null) {
			// 获取客户要求的公差标准
			CommonPage<SlmCustAlpha> sum = new CommonPage<SlmCustAlpha>();
			SlmCustAlpha al = new SlmCustAlpha();
			al.setCust_alpha_no(design.getCustomer_alpha_no());
			sum.setObject(al);
			ResultModel rl = this.getBean(SlmCustAlphaServiceImpl.class).doQueryWeb(sum);
			SlmCustAlpha alpha = (SlmCustAlpha) rl.getData();
			List<SlmCustAlphaSpec> specList = alpha.getSpecList();
			// 对比项目
			List<ComMscStdSpecC> randomSpec = null;
			for (SlmCustAlphaSpec slmCustAlphaSpec : specList) {
				randomSpec = new ArrayList<ComMscStdSpecC>();
				// 过滤项目
				for (int i = 0; i < specAl.size(); i++) {

					if (slmCustAlphaSpec.getBias_code().equals(specAl.get(i).getBiasCode())) {
						randomSpec.add(specAl.get(i));
					}

				}
				if (randomSpec.size() < 1) {
					msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "未找到客户的特殊要求公差项目:'"
							+ slmCustAlphaSpec.getBias_name() + "'");
					return false;

				}

				// 对比精度等级
				for (int i = 0; i < randomSpec.size(); i++) {
					if (randomSpec.get(i).getLvCode() != null && !"".equals(randomSpec.get(i).getLvCode())) {
						if (!randomSpec.get(i).getLvCode().equals(slmCustAlphaSpec.getLv_code())) {
							randomSpec.remove(i);
							i--;
						}
					}

				}

				if (randomSpec.size() < 1) {
					msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "未找到客户的特殊要求公差项目:'"
							+ slmCustAlphaSpec.getBias_name() + "'");
					return false;

				}

				// 对比标准值
				String tB = DataHelper.GetNull(slmCustAlphaSpec.getStd_min_val())
						+ DataHelper.GetNull(slmCustAlphaSpec.getStd_max_val());

				for (int i = 0; i < randomSpec.size(); i++) {
					String oB = DataHelper.GetNull(randomSpec.get(i).getStdMinVal())
							+ DataHelper.GetNull(randomSpec.get(i).getStdMaxVal());

					if (!tB.equals(oB)) {
						randomSpec.remove(i);
						i--;
					}
				}

				if (randomSpec.size() < 1) {
					msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "客户的特殊要求公差项目:'"
							+ slmCustAlphaSpec.getBias_name() + "'的标准值不符");
					return false;
				}

				if ("T0002".equals(slmCustAlphaSpec.getBias_code())) {

					for (int i = 0; i < randomSpec.size(); i++) {
						if (!randomSpec.get(i).getCut_edge_type_no().equals(design.getCut_edge_type_code())) {
							randomSpec.remove(i);
							i--;
						}

					}

					if (randomSpec.size() < 1) {
						msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "没有找到客户的特殊要求切边方式["
								+ design.getCut_edge_type_name() + "]公差项目[" + slmCustAlphaSpec.getBias_name() + "] ");
						return false;
					}

				}

				if (randomSpec.size() > 1) {
					msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "找到多条客户的特殊要求切边方式["
							+ design.getCut_edge_type_name() + "]公差项目[" + slmCustAlphaSpec.getBias_name() + "] ");
					return false;
				}

				specAs.addAll(randomSpec);

			}

		} else {
			specAs.addAll(specAl);

		}

		// 判断是否有重复项
		for (ComMscStdSpecC spec : specAs) {

			if (listBiasCode.contains(spec.getBiasCode())) {
				msg.setMessage("公差标准索引号:'" + specs.get(0).getSic() + "找到多条符合要求的公差项‘" + spec.getBiasName() + "’");
				return false;
			} else {
				listBiasCode.add(spec.getBiasCode());
			}

		}

		// 保存公差标准
		SlmOrdDesignStdSicMapper sicMapper = this.getMapper(SlmOrdDesignStdSicMapper.class);
		for (ComMscStdSpecC spec : specAs) {

			// 质量设计-交付标准-公差标准
			SlmOrdDesignStdSic stdSic = new SlmOrdDesignStdSic();
			stdSic.setSpecl_fl(flag);
			stdSic.setDesign_key(designKey);
			try {
				DataHelper.CopyEntity(spec, stdSic);
				// 是否特殊要求(0否;1是)
				if (flag.equals("1")) {
					stdSic.setUse_code(design.getUse_code());
					stdSic.setUse_desc(design.getUse_desc());
				}
				GetStdResult(stdSic, design);
				sicMapper.insert(stdSic);
			} catch (Exception ex) {
				// ex.printStackTrace();
				msg.setMessage("写入公差标准出错：" + ex.getMessage());
				return false;
			}
		}
		// 修改标准索引号
		UpdateDesignStdSic(designKey, msc.getPsc(), sic.getSic(), flag);
		msg.setMessage("公差标准验证通过");

		return true;
	}

	/**
	 * 查询材质标准信息
	 * @param specCMapper
	 * @param sic
	 * @return
	 */
	private List<ComMscStdSpecC> findStandSpec(ComMscStdSpecCMapper specCMapper, ComMscStdSpec sic) {
		List<ComMscStdSpecC> specs;
		ComMscStdSpecC _spec = new ComMscStdSpecC();
		_spec.setValidflag("1");
		_spec.setSic(sic.getSic());
		specs = specCMapper.find(_spec, 0, 0);
		return specs;
	}

	private void createOrderReq(List<SlmContractLineLv> orderReq,String codes) {
		List<ComBaseConfig> conFig;
		List<String> listconfig_id =new ArrayList<String>();
		listconfig_id.add(codes);
		conFig = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
				.getBaseConfigInfo(listconfig_id).getData();
		//厚度指定
		if (conFig!=null && conFig.size()>0) {
			
			if (conFig.get(0).getConfig_val()!=null) {
				String code=conFig.get(0).getConfig_val().split("_")[0];
				String name=conFig.get(0).getConfig_val().split("_")[1];
				
				if (code!=null && name!=null) {
					SlmContractLineLv lv=new SlmContractLineLv();
					lv.setLv_code(code);
					lv.setLv_desc(code);
					orderReq.add(lv);
					
				}
				
				
				
			}	
		}
		
	}
	
	/**
	 *  修改索引
	 * @param designKey
	 * @param psc 产品码
	 * @param sic 公差索引号
	 * @param flag 标准类型
	 */
	private void UpdateDesignStdSic(String designKey,String psc,String sic,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.setSic(sic);
		}else if(flag.equals("1")){
			std.setSic_k(sic);			
		}
		this.getMapper(SlmOrdDesignStdMapper.class).update(std);
	}
	
	
	/**
	 * 过滤限制规格
	 * @param listChem 
	 * @param orderLine
	 * @return
	 */
	@Override
	public List<ComMscStdSpecC> FilterSpec(List<ComMscStdSpecC> phys,SlmContractProcessPlan orderLine)
	{
		List<ComMscStdSpecC> _listSpec = new ArrayList<>();
		// 过滤限制规格 厚 400301， 宽 400302， 长 400306
		for (ComMscStdSpecC _stdPhy : phys) {
			if (!DataHelper.IsNullOrSpace(_stdPhy.getBiasCodeZ())) {// 为空则适用全部规格
				// 过滤限制规格一
				if (_stdPhy.getBiasCodeZ().equals("400301")) {
					boolean pass;
					if (_stdPhy.getZmax() == null) {
						pass = true;

					} else {
						
						pass = DataHelper.checkSignAndValue(_stdPhy.getZmaxSign(),
								Double.parseDouble(_stdPhy.getZmax()), DataHelper.GetString(orderLine.getThick()));
					}

					if (!pass) {
						continue;
					}

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

					if (!pass) {
						continue;
					}
				} else if (_stdPhy.getBiasCodeZ().equals("400302")) {
					boolean pass;

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

					if (!pass) {
						continue;
					}

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

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

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

					if (!pass) {
						continue;
					}

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

					if (!pass) {
						continue;
					}
				}
				// 验证限制规格二
				if (!DataHelper.IsNullOrSpace(_stdPhy.getBiasCodeC())) {
					if (_stdPhy.getBiasCodeC().equals("400301")) {
						boolean pass;
						if (_stdPhy.getCmin() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getCminSign(),
									Double.parseDouble(_stdPhy.getCmin()), DataHelper.GetString(orderLine.getThick()));
						}

						if (!pass) {
							continue;
						}

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

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getBiasCodeC().equals("400302")) {
						boolean pass;

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

						}
						if (!pass) {
							continue;
						}
						if (_stdPhy.getCmax() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getCmaxSign(),
									Double.parseDouble(_stdPhy.getCmax()), DataHelper.GetString(orderLine.getWidth()));
						}

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getBiasCodeC().equals("400306")) {
						boolean pass;
						if (_stdPhy.getCmin() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getCminSign(),
									Double.parseDouble(_stdPhy.getCmin()), DataHelper.GetString(orderLine.getLength()));
						}

						if (!pass) {
							continue;
						}

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

						if (!pass) {
							continue;
						}
					}
				}
				// 验证限制规格三
				if (!DataHelper.IsNullOrSpace(_stdPhy.getBiasCodeS())) {
					if (_stdPhy.getBiasCodeS().equals("400301")) {
						boolean pass;
						if (_stdPhy.getSmax() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSmaxSign(),
									Double.parseDouble(_stdPhy.getSmax()), DataHelper.GetString(orderLine.getThick()));
						}

						if (!pass) {
							continue;
						}

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

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getBiasCodeS().equals("400302")) {
						boolean pass;
						if (_stdPhy.getSmax() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSmaxSign(),
									Double.parseDouble(_stdPhy.getSmax()), DataHelper.GetString(orderLine.getWidth()));
						}

						if (!pass) {
							continue;
						}
						if (_stdPhy.getSmin() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSminSign(),
									Double.parseDouble(_stdPhy.getSmin()), DataHelper.GetString(orderLine.getWidth()));

						}

						if (!pass) {
							continue;
						}
					} else if (_stdPhy.getBiasCodeS().equals("400306")) {
						boolean pass;
						if (_stdPhy.getSmax() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSmaxSign(),
									Double.parseDouble(_stdPhy.getSmax()), DataHelper.GetString(orderLine.getLength()));

						}

						if (!pass) {
							continue;
						}
						if (_stdPhy.getSmin() == null) {
							pass = true;
						} else {
							pass = DataHelper.checkSignAndValue(_stdPhy.getSminSign(),
									Double.parseDouble(_stdPhy.getSmin()), DataHelper.GetString(orderLine.getLength()));

						}

						if (!pass) {
							continue;
						}
					}
				}
			}
			_listSpec.add(_stdPhy);
		}
		return _listSpec;
	}
	
//	/**
//	 * 确定计算后的标准值
//	 * @param stdSic
//	 * @param orderLine
//	 */
//	@Override
//	public void GetStdResult(SlmOrdDesignStdSic stdSic,SlmContractProcessPlan orderLine){
//		// 确定当前公差项应该取的参照值
//		double orderVal = stdSic.getBias_code().equals("T0001") ? orderLine.getThick()
//				: (stdSic.getBias_code().equals("T0002") ? orderLine.getWidth()
//						: (orderLine.getLength() == null ? 0 : orderLine.getLength()));
//		
//		ComBaseTolerance combase=this.getMapper(ComBaseToleranceMapper.class).findById(stdSic.getBias_code());
//		if (combase.getIs_cacl().equals("0")) {
//			return;
//		}
//		
//		DecimalFormat deFormat = new DecimalFormat("######0.00");
//		Double resultMin = null, resultMax = null;
//		if ("1".equals(stdSic.getStd_min_formula_v())) {// 取最小值:下限取最大、上限取最小
//			Double minVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_val()) ? null
//					: Double.parseDouble(stdSic.getStd_min_val());
//			Double minPercentVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_percent()) ? null
//					: Double.parseDouble(
//							deFormat.format(Double.parseDouble(stdSic.getStd_min_percent()) / 100 * orderVal));
//			Double minFormulaVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_formula()) ? null
//					: Double.parseDouble(stdSic.getStd_min_formula());
//			if (minVal != null) {
//				resultMin = minVal;
//			}
//			if (minPercentVal != null) {
//				if (resultMin == null) {
//					resultMin = minPercentVal;
//				} else {
//					resultMin = resultMin.doubleValue() > minPercentVal.doubleValue() ? resultMin
//							: minPercentVal.doubleValue();
//				}
//			}
//			if (minFormulaVal != null) {
//				if (resultMin == null) {
//					resultMin = minFormulaVal;
//				} else {
//					resultMin = resultMin.doubleValue() > minFormulaVal.doubleValue() ? resultMin
//							: minFormulaVal.doubleValue();
//				}
//			}
//
//			Double maxVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_val()) ? null
//					: Double.parseDouble(stdSic.getStd_max_val());
//			Double maxPercentVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_percent()) ? null
//					: Double.parseDouble(
//							deFormat.format(Double.parseDouble(stdSic.getStd_max_percent()) / 100 * orderVal));
//			Double maxFormulaVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_formula()) ? null
//					: Double.parseDouble(stdSic.getStd_max_formula());
//			if (maxVal != null) {
//				resultMax = maxVal;
//			}
//			if (maxPercentVal != null) {
//				if (resultMax == null) {
//					resultMax = maxPercentVal;
//				} else {
//					resultMax = resultMax.doubleValue() < maxPercentVal.doubleValue() ? resultMax
//							: maxPercentVal.doubleValue();
//				}
//			}
//			if (maxFormulaVal != null) {
//				if (resultMax == null) {
//					resultMax = maxFormulaVal;
//				} else {
//					resultMax = resultMax.doubleValue() < maxFormulaVal.doubleValue() ? resultMax
//							: maxFormulaVal.doubleValue();
//				}
//			}
//		} else { // 取最大值:下限取最小、上限取最大
//			Double maxVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_val()) ? null
//					: Double.parseDouble(stdSic.getStd_max_val());
//			Double maxPercentVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_percent()) ? null
//					: Double.parseDouble(
//							deFormat.format(Double.parseDouble(stdSic.getStd_max_percent()) / 100 * orderVal));
//			Double maxFormulaVal = DataHelper.IsNullOrSpace(stdSic.getStd_max_formula()) ? null
//					: Double.parseDouble(stdSic.getStd_max_formula());
//			if (maxVal != null) {
//				resultMax = maxVal;
//			}
//			if (maxPercentVal != null) {
//				if (resultMax == null) {
//					resultMax = maxPercentVal;
//				} else {
//					resultMax = resultMax.doubleValue() > maxPercentVal.doubleValue() ? resultMax
//							: maxPercentVal.doubleValue();
//				}
//			}
//			if (maxFormulaVal != null) {
//				if (resultMax == null) {
//					resultMax = maxFormulaVal;
//				} else {
//					resultMax = resultMax.doubleValue() > maxFormulaVal.doubleValue() ? resultMax
//							: maxFormulaVal.doubleValue();
//				}
//			}
//
//			Double minVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_val()) ? null
//					: Double.parseDouble(stdSic.getStd_min_val());
//			Double minPercentVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_percent()) ? null
//					: Double.parseDouble(
//							deFormat.format(Double.parseDouble(stdSic.getStd_min_percent()) / 100 * orderVal));
//			Double minFormulaVal = DataHelper.IsNullOrSpace(stdSic.getStd_min_formula()) ? null
//					: Double.parseDouble(stdSic.getStd_min_formula());
//			if (maxVal != null) {
//				resultMin = minVal;
//			}
//			if (minPercentVal != null) {
//				if (resultMin == null) {
//					resultMin = minPercentVal;
//				} else {
//					resultMin = resultMin.doubleValue() < minPercentVal.doubleValue() ? resultMin
//							: minPercentVal.doubleValue();
//				}
//			}
//			if (minFormulaVal != null) {
//				if (resultMin == null) {
//					resultMin = minFormulaVal;
//				} else {
//					resultMin = resultMin.doubleValue() < minFormulaVal.doubleValue() ? resultMin
//							: minFormulaVal.doubleValue();
//				}
//			}
//		}
//
//		resultMin = resultMin == null ? orderVal : (orderVal + resultMin);
//		resultMax = resultMax == null ? orderVal : (orderVal + resultMax);
//		stdSic.setStd_result_min(resultMin.toString());
//		stdSic.setStd_result_max(resultMax.toString());
//	}
	@Override
	public void GetStdResult(SlmOrdDesignStdSic stdSic, SlmContractProcessPlan orderLine) {
		// 确定当前公差项应该取的参照值
		double orderVal = stdSic.getBias_code().equals("T0001") ? orderLine.getThick()
				: (stdSic.getBias_code().equals("T0002") ? orderLine.getWidth()
						: (orderLine.getLength() == null ? 0 : orderLine.getLength()));
		
		ComBaseTolerance combase=this.getMapper(ComBaseToleranceMapper.class).findById(stdSic.getBias_code());
		if (combase.getIs_cacl().equals("0")) {
			return;
		}
		
		DecimalFormat deFormat = new DecimalFormat("######0.000");
		//用于保存最后的值
		Double resultMin = null, resultMax = null;
		//判断是否有多组值
		Double minVal=null;
		Double maxVal=null;
		if ((stdSic.getStd_max_val()!=null && !"".equals(stdSic.getStd_max_val())) ||(stdSic.getStd_min_val()!=null && !"".equals(stdSic.getStd_min_val()))) {
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_min_val()) ) {
				if (stdSic.getStd_min_val().contains("-")) {
					minVal=Double.parseDouble(stdSic.getStd_min_val());
				}else {
					minVal=Double.parseDouble(("-"+stdSic.getStd_min_val()));
				}
			}
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_max_val()) ) {
				if (stdSic.getStd_max_val().contains("+")) {
					maxVal=Double.parseDouble(stdSic.getStd_max_val());
				}else {
					maxVal=Double.parseDouble("+"+stdSic.getStd_max_val());
				}
			}		
		}
		if ((stdSic.getStd_max_in()!=null && !"".equals(stdSic.getStd_max_in())) ||(stdSic.getStd_min_in()!=null && !"".equals(stdSic.getStd_min_in()))) {
			Double std_max_in=null;
			Double std_min_in=null;
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_max_in()) ) {
				std_max_in=DataHelper.IsNullOrSpace(stdSic.getStd_max_in()) ? null : Double.parseDouble(stdSic.getStd_min_val());
				if (DataHelper.IsNullOrSpace(stdSic.getStd_max_in())) {
					if (!stdSic.getStd_max_in().contains("+")) {
						std_max_in=Double.parseDouble("+"+stdSic.getStd_max_in());
					}else {
						std_max_in=Double.parseDouble(stdSic.getStd_max_in());
					}
				}
			}
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_min_in()) ) {
			     std_min_in=DataHelper.IsNullOrSpace(stdSic.getStd_min_in()) ? null : Double.parseDouble(stdSic.getStd_min_in());
				if (DataHelper.IsNullOrSpace(stdSic.getStd_min_in())) {
					if (!stdSic.getStd_min_in().contains("-")) {
						std_min_in=Double.parseDouble("-"+stdSic.getStd_min_in());
					}else {
						std_min_in=Double.parseDouble(stdSic.getStd_min_in());
					}
				}
			}
			if (std_max_in!=null) {
				if (maxVal!=null) {
					if (stdSic.getStd_min_formula_v()==null || "1".equals(stdSic.getStd_min_formula_v())) {// 取最小值:下限取最大、上限取最小
						if (maxVal>std_max_in) {
							maxVal=std_max_in;
						}
					}else {
						if (maxVal<std_max_in) {
							maxVal=std_max_in;
						}
					}
				}else {
					maxVal=std_max_in;
				}
			}
			if (std_min_in!=null) {
				if (minVal!=null) {
					if (stdSic.getStd_min_formula_v()==null || "1".equals(stdSic.getStd_min_formula_v())) {// 取最小值:下限取最大、上限取最小
						if (minVal<std_min_in) {
							minVal=std_max_in;
						}
					}else {
						if (minVal>std_max_in) {
							minVal=std_max_in;
						}
					}
				}else {
					minVal=std_min_in;
				}
			}
			
			
		}
		if ((stdSic.getStd_max_percent()!=null && !"".equals(stdSic.getStd_max_percent())) ||(stdSic.getStd_min_percent()!=null && !"".equals(stdSic.getStd_min_percent()))) {
			Double std_max_percent=null;
			Double std_min_percent=null;
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_max_formula()) ) {
				std_max_percent=DataHelper.IsNullOrSpace(stdSic.getStd_max_formula()) ? null : Double.parseDouble(stdSic.getStd_max_formula());
				if (!DataHelper.IsNullOrSpace(stdSic.getStd_max_formula())) {
					if (!stdSic.getStd_max_formula().contains("+")) {
						std_max_percent=Double.parseDouble("+"+deFormat.format(Double.parseDouble(stdSic.getStd_max_percent()) / 100 * orderVal));
					}else {
						std_max_percent=Double.parseDouble(deFormat.format(Double.parseDouble(stdSic.getStd_max_percent()) / 100 * orderVal));
					}
				}
			}
			if (!DataHelper.IsNullOrSpace(stdSic.getStd_min_formula()) ) {
				std_min_percent=DataHelper.IsNullOrSpace(stdSic.getStd_min_formula()) ? null : Double.parseDouble(stdSic.getStd_min_formula());
				if (!DataHelper.IsNullOrSpace(stdSic.getStd_min_formula())) {
					if (!stdSic.getStd_min_formula().contains("-")) {
						std_min_percent=Double.parseDouble("-"+deFormat.format(Double.parseDouble(stdSic.getStd_min_percent()) / 100 * orderVal));
					}else {
						std_min_percent=Double.parseDouble(deFormat.format(Double.parseDouble(stdSic.getStd_min_percent()) / 100 * orderVal));
					}
				}
			}
			if (std_max_percent!=null) {
				if (maxVal!=null) {
					if (stdSic.getStd_min_formula_v()==null || "1".equals(stdSic.getStd_min_formula_v())) {// 取最小值:下限取最大、上限取最小
						if (maxVal>std_max_percent) {
							maxVal=std_max_percent;
						}
					}else {
						if (maxVal<std_max_percent) {
							maxVal=std_max_percent;
						}
					}
				}else {
					maxVal=std_max_percent;
				}
			}
			if (std_min_percent!=null) {
				if (minVal!=null) {
					if (stdSic.getStd_min_formula_v()==null || "1".equals(stdSic.getStd_min_formula_v())) {// 取最小值:下限取最大、上限取最小
						if (minVal<std_min_percent) {
							minVal=std_min_percent;
						}
					}else {
						if (maxVal>std_min_percent) {
							minVal=std_min_percent;
						}
					}
				}else {
					minVal=std_min_percent;
				}
			}
		}
	
		resultMin = minVal == null ? orderVal : (orderVal + minVal);
		resultMax = maxVal == null ? orderVal : (orderVal + maxVal);
		stdSic.setStd_result_min(deFormat.format(resultMin));
		stdSic.setStd_result_max(deFormat.format(resultMax));
	}

	
}
