package com.steering.pss.sale.order.qualityDesign.server.iserver;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;

import javax.script.ScriptException;

import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.idao.DifferenceValueDao;
import com.steering.pss.sale.order.qualityDesign.model.ComBaseAdditionalEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignAddAskDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignCicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignDicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignGicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignPicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignSicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdCicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdDicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdGicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdPicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdWicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignWicDiffEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrderAddAskEntity;

/**
 * 差值
 * 
 * @author Administrator
 * 
 */
public class DifferenceValue {
	public void checkDifference(Connection con, String designKey, String ordPk,
			String ordLnPk, String stdCode, String addAskNo, String dimater,
			String height, String length) throws SQLException, ScriptException {
		DifferenceValueDao differenceValueDao = new DifferenceValueDao();
		/**
		 * 成分
		 */

		// 客户成分标准
		List<SlmOrdDesignStdCicEntity> alphaChems = differenceValueDao.getChem(
				con, designKey, "1");
		// 如果存在客户标准,则计算差值
		if (alphaChems != null) {
			// 交付成分标准
			List<SlmOrdDesignStdCicEntity> stdChems = differenceValueDao
					.getChem(con, designKey, "0");
			// 循环每一项客户成分要求
			for (SlmOrdDesignStdCicEntity alphaChem : alphaChems) {
				// 成分差值类
				SlmOrdDesignCicDiffEntity cicDiffEntity = new SlmOrdDesignCicDiffEntity();
				cicDiffEntity.setDesignKey(designKey);
				cicDiffEntity.setOrdPk(ordPk);
				cicDiffEntity.setOrdLnPk(ordLnPk);
				cicDiffEntity.setItemCode(alphaChem.getChemCode());
				cicDiffEntity.setItemName(alphaChem.getChemName());
				cicDiffEntity.setStdmax1(alphaChem.getStdmax());
				cicDiffEntity.setStdmaxSign1(alphaChem.getStdmaxSign());
				cicDiffEntity.setStdmin1(alphaChem.getStdmin());
				cicDiffEntity.setStdminSign1(alphaChem.getStdminSign());
				cicDiffEntity.setCic1(alphaChem.getCic());
				// 过滤
				SlmOrdDesignStdCicEntity stdChem = DifferenceValue.fiterChem(
						stdChems, alphaChem.getChemCode());
				if (stdChem != null) {
					cicDiffEntity.setStdmax(stdChem.getStdmax());
					cicDiffEntity.setStdmaxSign(stdChem.getStdmaxSign());
					cicDiffEntity.setStdmin(stdChem.getStdmin());
					cicDiffEntity.setStdminSign(stdChem.getStdminSign());
					cicDiffEntity.setCic(stdChem.getCic());
					cicDiffEntity = DifferenceValue
							.calculateChem(cicDiffEntity);
				} else {
					// 如果有客户要求，没有标准，则代表不同
					cicDiffEntity.setDiffFlag("1");
				}
				// 保存数据
				differenceValueDao.addChem(con, cicDiffEntity);
			}
		}

		/**
		 * 理化
		 */

		// 客户
		List<SlmOrdDesignStdPicEntity> alphaPhys = differenceValueDao.getPhy(
				con, designKey, "1");
		if (alphaPhys != null) {
			// 交付
			List<SlmOrdDesignStdPicEntity> stdPhys = differenceValueDao.getPhy(
					con, designKey, "0");
			for (SlmOrdDesignStdPicEntity alphaPhy : alphaPhys) {
				String code = alphaPhy.getPhyCodeMax() + ","
						+ alphaPhy.getPhyCodeMin() + ","
						+ alphaPhy.getPhyCode() + "," + alphaPhy.getItemCodeF()
						+ "," + alphaPhy.getItemCodeS()
						+ alphaPhy.getItemCodeF();
				String name = alphaPhy.getPhyNameMax() + ","
						+ alphaPhy.getPhyNameMin() + ","
						+ alphaPhy.getPhyName() + "," + alphaPhy.getItemNameF()
						+ "," + alphaPhy.getItemNameS()
						+ alphaPhy.getItemNameF();
				SlmOrdDesignPicDiffEntity picDiffEntity = new SlmOrdDesignPicDiffEntity();
				picDiffEntity.setDesignKey(designKey);
				picDiffEntity.setOrdPk(ordPk);
				picDiffEntity.setOrdLnPk(ordLnPk);
				picDiffEntity.setItemCode(code);
				picDiffEntity.setItemName(name);
				picDiffEntity.setStdmax1(alphaPhy.getStdmax());
				picDiffEntity.setStdmaxSign1(alphaPhy.getStdmaxSign());
				picDiffEntity.setStdmin1(alphaPhy.getStdmin());
				picDiffEntity.setStdminSign1(alphaPhy.getStdminSign());
				picDiffEntity.setPic1(alphaPhy.getPic());
				SlmOrdDesignStdPicEntity stdPhy = DifferenceValue.fiterPhy(
						stdPhys, code);
				if (stdPhy != null) {
					picDiffEntity.setStdmax(stdPhy.getStdmax());
					picDiffEntity.setStdmaxSign(stdPhy.getStdmaxSign());
					picDiffEntity.setStdmin(stdPhy.getStdmin());
					picDiffEntity.setStdminSign(stdPhy.getStdminSign());
					picDiffEntity.setPic(stdPhy.getPic());
				} else {
					picDiffEntity.setDiffFlag("1");
				}
				picDiffEntity = DifferenceValue.calculatePhy(picDiffEntity);
				// 保存
				differenceValueDao.addPhy(con, picDiffEntity);
			}
		}
		/**
		 * 水压
		 */

		SlmOrdDesignStdWicEntity alphaWater = differenceValueDao.getWater(con,
				designKey, "1");
		if (alphaWater != null) {
			SlmOrdDesignStdWicEntity stdWater = differenceValueDao.getWater(
					con, designKey, "0");
			SlmOrdDesignWicDiffEntity wicDiffEntity = new SlmOrdDesignWicDiffEntity();
			wicDiffEntity.setDesignKey(designKey);
			wicDiffEntity.setOrdPk(ordPk);
			wicDiffEntity.setOrdLnPk(ordLnPk);
			wicDiffEntity.setFormula1(alphaWater.getFormula());
			wicDiffEntity.setMaxPress1(alphaWater.getMaxPress());
			wicDiffEntity.setPressureY1(alphaWater.getPressureY());
			wicDiffEntity.setWic1(alphaWater.getWic());
			wicDiffEntity.setSaveTime1(alphaWater.getSaveTime());
			wicDiffEntity.setPressure1(alphaWater.getPressure());
			if (stdWater != null) {
				wicDiffEntity.setFormula(stdWater.getFormula());
				wicDiffEntity.setMaxPress(stdWater.getMaxPress());
				wicDiffEntity.setPressureY(stdWater.getPressureY());
				wicDiffEntity.setWic(stdWater.getWic());
				wicDiffEntity.setSaveTime(stdWater.getSaveTime());
				wicDiffEntity.setPressure(stdWater.getPressure());
				wicDiffEntity.setDiffValue(String.valueOf(Double
						.parseDouble(alphaWater.getSaveTime())
						- Double.parseDouble(stdWater.getSaveTime())));
				wicDiffEntity.setDiffFlag("0");
			} else {
				wicDiffEntity.setDiffFlag("1");
			}
			// 保存
			differenceValueDao.addWater(con, wicDiffEntity);
		}
		/**
		 * 探伤
		 */

		// 客户探伤
		List<SlmOrdDesignStdDicEntity> alphaDetes = differenceValueDao.getDete(
				con, designKey, "1");

		// 差值
		if (alphaDetes != null) {
			// 交付探伤
			List<SlmOrdDesignStdDicEntity> stdDetes = differenceValueDao
					.getDete(con, designKey, "0");
			for (SlmOrdDesignStdDicEntity alphaDete : alphaDetes) {
				SlmOrdDesignDicDiffEntity dicDiffEntity = new SlmOrdDesignDicDiffEntity();
				dicDiffEntity.setDesignKey(designKey);
				dicDiffEntity.setOrdPk(ordPk);
				dicDiffEntity.setOrdLnPk(ordLnPk);
				dicDiffEntity.setPhyCode(alphaDete.getPhyCode());
				dicDiffEntity.setPhyName(alphaDete.getPhyName());
				dicDiffEntity.setCoverage1(alphaDete.getCoverage());
				dicDiffEntity.setDescRate1(alphaDete.getDescRate());
				dicDiffEntity.setDescVal1(alphaDete.getDescVal());
				dicDiffEntity.setDic1(alphaDete.getDic());
				dicDiffEntity.setGrooveDir1(alphaDete.getGrooveDir());
				dicDiffEntity.setGroovePos1(alphaDete.getGroovePos());
				dicDiffEntity.setLvCode1(alphaDete.getLvCode());
				dicDiffEntity.setLvDesc1(alphaDete.getLvDesc());
				dicDiffEntity.setPositionCode1(alphaDete.getPositionCode());
				dicDiffEntity.setPositionDesc1(alphaDete.getPositionDesc());
				dicDiffEntity.setRate1(alphaDete.getRate());
				dicDiffEntity.setStdDesc1(alphaDete.getStdDesc());
				SlmOrdDesignStdDicEntity fiterDete = DifferenceValue.fiterDete(
						stdDetes, alphaDete);
				if (fiterDete != null) {
					dicDiffEntity.setCoverage(fiterDete.getCoverage());
					dicDiffEntity.setDescRate(fiterDete.getDescRate());
					dicDiffEntity.setDescVal(fiterDete.getDescVal());
					dicDiffEntity.setDic(fiterDete.getDic());
					dicDiffEntity.setGrooveDir(fiterDete.getGrooveDir());
					dicDiffEntity.setGroovePos(fiterDete.getGroovePos());
					dicDiffEntity.setLvCode(fiterDete.getLvCode());
					dicDiffEntity.setLvDesc(fiterDete.getLvDesc());
					dicDiffEntity.setPositionCode(fiterDete.getPositionCode());
					dicDiffEntity.setPositionDesc(fiterDete.getPositionDesc());
					dicDiffEntity.setRate(fiterDete.getRate());
					dicDiffEntity.setStdDesc(fiterDete.getStdDesc());
					if (!fiterDete.getLvCode().equals(alphaDete.getLvCode())) {
						dicDiffEntity.setDiffFlag("1");
					} else {
						dicDiffEntity.setDiffFlag("0");
					}
				} else {
					dicDiffEntity.setDiffFlag("1");
				}
				differenceValueDao.addDete(con, dicDiffEntity);
			}
		}
		/**
		 * 加工
		 */

		// 客户加工
		List<SlmOrdDesignStdGicEntity> alphaGproes = differenceValueDao
				.getGprocess(con, designKey, "1");

		// 加工差值
		if (alphaGproes != null) {
			// 交付加工
			List<SlmOrdDesignStdGicEntity> stdGproes = differenceValueDao
					.getGprocess(con, designKey, "0");
			for (SlmOrdDesignStdGicEntity alphaGpro : alphaGproes) {
				SlmOrdDesignGicDiffEntity gicDiffEntity = new SlmOrdDesignGicDiffEntity();
				gicDiffEntity.setDesignKey(designKey);
				gicDiffEntity.setOrdPk(ordPk);
				gicDiffEntity.setOrdLnPk(ordLnPk);
				gicDiffEntity.setGic1(alphaGpro.getGic());
				gicDiffEntity.setProcessCode(alphaGpro.getProcessCdoe());
				gicDiffEntity.setProcessName(alphaGpro.getProcessDesc());
				gicDiffEntity.setStationCode(alphaGpro.getStationCode());
				gicDiffEntity.setStationName(alphaGpro.getStationDesc());
				gicDiffEntity.setValueNum1(alphaGpro.getValueNum());
				gicDiffEntity.setValueText1(alphaGpro.getValueText());
				SlmOrdDesignStdGicEntity fiterGic = DifferenceValue
						.fiterGprocess(stdGproes, alphaGpro);
				if (fiterGic != null) {
					gicDiffEntity.setValueNum(fiterGic.getValueNum());
					gicDiffEntity.setValueText(fiterGic.getValueText());
					gicDiffEntity.setGic(fiterGic.getGic());
					// 如果客户的文本值和数值 与 标准不一致
					if (!(fiterGic.getValueNum() + fiterGic.getValueText())
							.equals((alphaGpro.getValueNum() + alphaGpro
									.getValueText()))) {
						gicDiffEntity.setDiffFlag("1");
					} else {
						gicDiffEntity.setDiffFlag("0");
					}
				} else {
					gicDiffEntity.setDiffFlag("1");
				}
				differenceValueDao.addGprocess(con, gicDiffEntity);
			}
		}
		/**
		 * 公差
		 */

		// 客户
		List<SlmOrdDesignStdSicEntity> alphaSpeces = differenceValueDao
				.getSpec(con, designKey, "1");
		// 差值
		if (alphaSpeces != null) {
			// 交付
			List<SlmOrdDesignStdSicEntity> stdSpeces = differenceValueDao
					.getSpec(con, designKey, "0");
			for (SlmOrdDesignStdSicEntity alphaSpec : alphaSpeces) {
				SlmOrdDesignSicDiffEntity sicDiffEntity = new SlmOrdDesignSicDiffEntity();
				sicDiffEntity.setDesignKey(designKey);
				sicDiffEntity.setOrdPk(ordPk);
				sicDiffEntity.setOrdLnPk(ordLnPk);
				sicDiffEntity.setSic1(alphaSpec.getSic());
				sicDiffEntity.setPhyCode(alphaSpec.getBiasCode());
				sicDiffEntity.setPhyName(alphaSpec.getBiasName());
				sicDiffEntity.setStdMaxVal1(alphaSpec.getStdMaxVal());
				sicDiffEntity.setStdMinVal1(alphaSpec.getStdMinVal());
				sicDiffEntity.setStdMaxPercent1(alphaSpec.getStdMaxPercent());
				sicDiffEntity.setStdMinPercent1(alphaSpec.getStdMinPercent());
				sicDiffEntity.setStdMaxFormula1(alphaSpec.getStdMaxFormula());
				sicDiffEntity.setStdMinFormula1(alphaSpec.getStdMinFormula());
				SlmOrdDesignStdSicEntity fiterSpec = DifferenceValue.fiterSpec(
						stdSpeces, alphaSpec);
				if (fiterSpec != null) {
					sicDiffEntity.setSic(fiterSpec.getSic());
					sicDiffEntity.setStdMaxVal(fiterSpec.getStdMaxVal());
					sicDiffEntity.setStdMinVal(fiterSpec.getStdMinVal());
					sicDiffEntity
							.setStdMaxPercent(fiterSpec.getStdMaxPercent());
					sicDiffEntity
							.setStdMinPercent(fiterSpec.getStdMinPercent());
					sicDiffEntity
							.setStdMaxFormula(fiterSpec.getStdMaxFormula());
					sicDiffEntity
							.setStdMinFormula(fiterSpec.getStdMinFormula());
					// 计算差值(等处理)
					sicDiffEntity = DifferenceValue.calculateSpecValue(
							sicDiffEntity, dimater, height, length);
					sicDiffEntity.setDiffFlag("1");
				} else {
					sicDiffEntity.setDiffFlag("1");
				}
				// 保存公差差值
				differenceValueDao.addSpec(con, sicDiffEntity);
			}
		}
		/**
		 * 附加要求
		 */
		// 标准
		List<ComBaseAdditionalEntity> stdAddAskes = differenceValueDao
				.getStdAddAsk(con, stdCode);
		// 客户
		List<SlmOrderAddAskEntity> alphaAddAskes = differenceValueDao
				.getAlphaAddAsk(con, addAskNo);
		if (alphaAddAskes != null) {
			for (SlmOrderAddAskEntity alphaAddAsk : alphaAddAskes) {
				SlmOrdDesignAddAskDiffEntity addDiffEntity = new SlmOrdDesignAddAskDiffEntity();
				addDiffEntity.setDesignKey(designKey);
				addDiffEntity.setOrdPk(ordPk);
				addDiffEntity.setOrdLnPk(ordLnPk);
				addDiffEntity.setAddAskNo(addAskNo);
				addDiffEntity.setAskItme(alphaAddAsk.getAskItme());
				addDiffEntity.setAskItmeDesc(alphaAddAsk.getAskItmeDesc());
				addDiffEntity.setAskSubItme(alphaAddAsk.getAskSubItme());
				addDiffEntity
						.setAskSubItmeDesc(alphaAddAsk.getAskSubItmeDesc());
				addDiffEntity.setAskSubItmeVal1(alphaAddAsk.getAskSubItmeVal());
				addDiffEntity.setAskSubItmeValDesc1(alphaAddAsk
						.getAskSubItmeValDesc());
				ComBaseAdditionalEntity fiterAsk = DifferenceValue.fiterAddAsk(
						stdAddAskes, alphaAddAsk);
				if (fiterAsk != null) {
					addDiffEntity.setAskSubItmeVal(fiterAsk.getAskSubItmeVal());
					addDiffEntity.setAskSubItmeValDesc(fiterAsk
							.getAskSubItmeValDesc());
					addDiffEntity.setStdCode(fiterAsk.getStdCode());
					addDiffEntity.setStdName(fiterAsk.getStdName());
					if (fiterAsk.getAskSubItmeVal().equals(
							alphaAddAsk.getAskSubItmeVal())) {
						addDiffEntity.setDiffFlag("0");
					} else {
						addDiffEntity.setDiffFlag("1");
					}
				} else {
					addDiffEntity.setDiffFlag("1");
				}
				// 保存
				differenceValueDao.addAddAsk(con, addDiffEntity);
			}
		}
	}

	/**
	 * 计算公差差值
	 * 
	 * @param sicDiffEntity
	 * @return
	 * @throws ScriptException
	 */
	public static SlmOrdDesignSicDiffEntity calculateSpecValue(
			SlmOrdDesignSicDiffEntity sicDiffEntity, String dimater,
			String height, String length) throws ScriptException {
		// 客户最小值
		double stdMinVal1 = -100000;
		if (!sicDiffEntity.getStdMinVal1().equals("")) {
			stdMinVal1 = Double.parseDouble(sicDiffEntity.getStdMinVal1());
		}
		double stdMinPercent1 = -100000;
		if (!sicDiffEntity.getStdMinPercent1().equals("")) {
			stdMinPercent1 = Double.parseDouble(sicDiffEntity
					.getStdMinPercent1()) * Double.parseDouble(height);
		}
		double stdMinFormula1 = -100000;
		if (!sicDiffEntity.getStdMinFormula1().equals("")) {
			String strStdMinFormula1 = sicDiffEntity.getStdMinFormula1();
			strStdMinFormula1 = strStdMinFormula1.replace("t", height);
			strStdMinFormula1 = strStdMinFormula1.replace("D", dimater);
			strStdMinFormula1 = strStdMinFormula1.replace("L", length);
			stdMinFormula1 = QualityDesignTool.countFormula(strStdMinFormula1);
		}
		double stdMinValue1 = -100000;
		if (stdMinVal1 > stdMinValue1) {
			stdMinValue1 = stdMinVal1;
		}
		if (stdMinPercent1 > stdMinValue1) {
			stdMinValue1 = stdMinPercent1;
		}
		if (stdMinFormula1 > stdMinValue1) {
			stdMinValue1 = stdMinFormula1;
		}
		// 标准最小值
		double stdMinVal = -100000;
		if (!sicDiffEntity.getStdMinVal().equals("")) {
			stdMinVal = Double.parseDouble(sicDiffEntity.getStdMinVal());
		}
		double stdMinPercent = -100000;
		if (!sicDiffEntity.getStdMinPercent().equals("")) {
			stdMinPercent = Double
					.parseDouble(sicDiffEntity.getStdMinPercent())
					* Double.parseDouble(height);
		}
		double stdMinFormula = -100000;
		if (!sicDiffEntity.getStdMinFormula().equals("")) {
			String strStdMinFormula = sicDiffEntity.getStdMinFormula();
			strStdMinFormula = strStdMinFormula.replace("t", height);
			strStdMinFormula = strStdMinFormula.replace("D", dimater);
			strStdMinFormula = strStdMinFormula.replace("L", length);
			stdMinFormula = QualityDesignTool.countFormula(strStdMinFormula);
		}
		double stdMinValue = -100000;
		if (stdMinVal > stdMinValue) {
			stdMinValue = stdMinVal;
		}
		if (stdMinPercent > stdMinValue) {
			stdMinValue = stdMinPercent;
		}
		if (stdMinFormula > stdMinValue) {
			stdMinValue = stdMinFormula;
		}
		DecimalFormat df = new DecimalFormat("#.00");
		// 下偏差值
		sicDiffEntity.setDiffValueMin(String.valueOf(df.format(stdMinValue1
				- stdMinValue)));

		// 客户最大值
		double stdMaxVal1 = 100000;
		if (!sicDiffEntity.getStdMaxVal1().equals("")) {
			stdMaxVal1 = Math.abs(Double.parseDouble(sicDiffEntity
					.getStdMaxVal1()));
		}
		double stdMaxPercent1 = 100000;
		if (!sicDiffEntity.getStdMaxPercent1().equals("")) {
			stdMaxPercent1 = Math.abs(Double.parseDouble(sicDiffEntity
					.getStdMaxPercent1()) * Double.parseDouble(height));
		}
		double stdMaxFormula1 = 100000;
		if (!sicDiffEntity.getStdMaxFormula1().equals("")) {
			String strStdMaxFormula1 = sicDiffEntity.getStdMaxFormula1();
			strStdMaxFormula1 = strStdMaxFormula1.replace("t", height);
			strStdMaxFormula1 = strStdMaxFormula1.replace("D", dimater);
			strStdMaxFormula1 = strStdMaxFormula1.replace("L", length);
			stdMaxFormula1 = Math.abs(QualityDesignTool
					.countFormula(strStdMaxFormula1));
		}
		double stdMaxValue1 = 100000;
		if (stdMaxVal1 < stdMaxValue1) {
			stdMaxValue1 = stdMaxVal1;
		}
		if (stdMaxPercent1 < stdMaxValue1) {
			stdMaxValue1 = stdMaxPercent1;
		}
		if (stdMaxFormula1 < stdMaxValue1) {
			stdMaxValue1 = stdMaxFormula1;
		}

		// 标准最大值
		double stdMaxVal = 100000;
		if (!sicDiffEntity.getStdMaxVal().equals("")) {
			stdMaxVal = Math.abs(Double.parseDouble(sicDiffEntity
					.getStdMaxVal()));
		}
		double stdMaxPercent = 100000;
		if (!sicDiffEntity.getStdMaxPercent().equals("")) {
			stdMaxPercent = Math.abs(Double.parseDouble(sicDiffEntity
					.getStdMaxPercent()) * Double.parseDouble(height));
		}
		double stdMaxFormula = 100000;
		if (!sicDiffEntity.getStdMaxFormula().equals("")) {
			String strStdMaxFormula = sicDiffEntity.getStdMaxFormula();
			strStdMaxFormula = strStdMaxFormula.replace("t", height);
			strStdMaxFormula = strStdMaxFormula.replace("D", dimater);
			strStdMaxFormula = strStdMaxFormula.replace("L", length);
			stdMaxFormula = Math.abs(QualityDesignTool
					.countFormula(strStdMaxFormula));
		}
		double stdMaxValue = 100000;
		if (stdMaxVal < stdMaxValue) {
			stdMaxValue = stdMaxVal;
		}
		if (stdMaxPercent < stdMaxValue) {
			stdMaxValue = stdMaxPercent;
		}
		if (stdMaxFormula < stdMaxValue) {
			stdMaxValue = stdMaxFormula;
		}
		// 上偏差值
		sicDiffEntity.setDiffValueMax(String.valueOf(df.format(stdMaxValue1
				- stdMaxValue)));
		return sicDiffEntity;
	}

	/**
	 * 过滤公差
	 * 
	 * @param stdSpeces
	 * @param alphaSpec
	 * @return
	 */
	public static SlmOrdDesignStdSicEntity fiterSpec(
			List<SlmOrdDesignStdSicEntity> stdSpeces,
			SlmOrdDesignStdSicEntity alphaSpec) {
		if (stdSpeces == null) {
			return null;
		}
		for (SlmOrdDesignStdSicEntity stdSpec : stdSpeces) {
			if (stdSpec.getBiasCode().equals(alphaSpec.getBiasCode())) {
				return stdSpec;
			}
		}
		return null;
	}

	/**
	 * 过滤探伤标准
	 * 
	 * @param stdDetes
	 * @param alphaDete
	 * @return
	 */
	public static SlmOrdDesignStdDicEntity fiterDete(
			List<SlmOrdDesignStdDicEntity> stdDetes,
			SlmOrdDesignStdDicEntity alphaDete) {
		if (stdDetes == null)
			return null;
		for (SlmOrdDesignStdDicEntity stdDete : stdDetes) {
			if (stdDete.getPhyCode().equals(alphaDete.getPhyCode())
					&& stdDete.getGroovePos().equals(alphaDete.getGroovePos())) {
				return stdDete;
			}
		}
		return null;
	}

	/**
	 * 过滤加工标准
	 * 
	 * @param stdGproces
	 * @param gproc
	 * @return
	 */
	public static SlmOrdDesignStdGicEntity fiterGprocess(
			List<SlmOrdDesignStdGicEntity> stdGproces,
			SlmOrdDesignStdGicEntity gproc) {
		if (stdGproces == null) {
			return null;
		}
		for (SlmOrdDesignStdGicEntity stdGproc : stdGproces) {
			if (stdGproc.getProcessCdoe().equals(gproc.getProcessCdoe())
					&& stdGproc.getStationCode().equals(gproc.getStationCode())) {
				return stdGproc;
			}
		}
		return null;
	}

	/**
	 * 过滤成分
	 * 
	 * @param stdChems
	 * @param chemCode
	 * @return
	 */
	public static SlmOrdDesignStdCicEntity fiterChem(
			List<SlmOrdDesignStdCicEntity> stdChems, String chemCode) {
		if (stdChems == null) {
			return null;
		}
		for (SlmOrdDesignStdCicEntity chem : stdChems) {
			if (chem.getChemCode().equals(chemCode)) {
				return chem;
			}
		}
		return null;
	}

	/**
	 * 过滤理化
	 * 
	 * @param stdPhys
	 * @param code
	 * @return
	 */
	public static SlmOrdDesignStdPicEntity fiterPhy(
			List<SlmOrdDesignStdPicEntity> stdPhys, String code) {
		if (stdPhys == null) {
			return null;
		}
		for (SlmOrdDesignStdPicEntity stdPhy : stdPhys) {
			String phyCode = stdPhy.getPhyCodeMax() + ","
					+ stdPhy.getPhyCodeMin() + "," + stdPhy.getPhyCode() + ","
					+ stdPhy.getItemCodeF() + "," + stdPhy.getItemCodeS()
					+ stdPhy.getItemCodeF();
			if (phyCode.equals(code)) {
				return stdPhy;
			}
		}
		return null;
	}

	/**
	 * 计算成分差值
	 * 
	 * @param cicDiffEntity
	 */
	public static SlmOrdDesignCicDiffEntity calculateChem(
			SlmOrdDesignCicDiffEntity cicDiffEntity) {
		// 标准上限值
		String stdMax = cicDiffEntity.getStdmax();
		// 标准下限值
		String stdMin = cicDiffEntity.getStdmin();
		// 客户上限值
		String stdMax1 = cicDiffEntity.getStdmax1();
		// 客户下限值
		String stdMin1 = cicDiffEntity.getStdmin1();
		if (stdMax.equals("") && stdMax1.equals("") && stdMin.equals("")
				&& stdMin1.equals("")) {
			cicDiffEntity.setDiffFlag("0");
			return cicDiffEntity;
		}
		int flagNum = 0;
		double diffValue = 0;
		// 上限差值
		// 如果两个上限值都不为空,则计算上限差值
		if (!stdMax.equals("") && !stdMax1.equals("")) {
			// 如果两个值都是数值
			if (DifferenceValue.isNumeric(stdMax)
					&& DifferenceValue.isNumeric(stdMax1)) {
				diffValue = Double.parseDouble(stdMax1)
						- Double.parseDouble(stdMax);
				cicDiffEntity.setDifferentValueMax(String.valueOf(diffValue));
				if (Math.abs(diffValue) > 0) {
					cicDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			} else {
				if (!stdMax.equals(stdMax1)) {
					cicDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			}
		} else {
			if (stdMax.equals("") && stdMax1.equals("")) {
				flagNum++;
			} else {
				cicDiffEntity.setDiffFlag("1");
			}
		}
		// 下限差值
		if (!stdMin.equals("") && !stdMin1.equals("")) {
			if (DifferenceValue.isNumeric(stdMin)
					&& DifferenceValue.isNumeric(stdMin1)) {
				diffValue = Double.parseDouble(stdMin1)
						- Double.parseDouble(stdMin);
				cicDiffEntity.setDifferentValueMin(String.valueOf(diffValue));
				if (Math.asin(diffValue) > 0) {
					cicDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			} else {
				if (!stdMin.equals(stdMin1)) {
					cicDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			}
		} else {
			if (stdMin.equals("") && stdMin1.equals("")) {
				flagNum++;
			} else {
				cicDiffEntity.setDiffFlag("1");
			}
		}
		if (flagNum == 2) {
			cicDiffEntity.setDiffFlag("0");
		}
		return cicDiffEntity;
	}

	/**
	 * 计算理化差值
	 * 
	 * @param cicDiffEntity
	 */
	public static SlmOrdDesignPicDiffEntity calculatePhy(
			SlmOrdDesignPicDiffEntity picDiffEntity) {
		String stdMax = picDiffEntity.getStdmax();
		String stdMin = picDiffEntity.getStdmin();
		String stdMax1 = picDiffEntity.getStdmax1();
		String stdMin1 = picDiffEntity.getStdmin1();
		if (stdMax.equals("") && stdMax1.equals("") && stdMin.equals("")
				&& stdMin1.equals("")) {
			picDiffEntity.setDiffFlag("0");
			return picDiffEntity;
		}
		int flagNum = 0;
		double diffValue = 0;
		// 上限差值
		// 如果两个上限值都不为空,则计算上限差值
		if (!stdMax.equals("") && !stdMax1.equals("")) {
			// 如果两个值都是数值
			if (DifferenceValue.isNumeric(stdMax)
					&& DifferenceValue.isNumeric(stdMax1)) {
				diffValue = Double.parseDouble(stdMax1)
						- Double.parseDouble(stdMax);
				picDiffEntity.setDifferentValueMax(String.valueOf(diffValue));
				if (Math.abs(diffValue) > 0) {
					picDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			} else {
				if (!stdMax.equals(stdMax1)) {
					picDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			}
		} else {
			if (stdMax.equals("") && stdMax1.equals("")) {
				flagNum++;
			} else {
				picDiffEntity.setDiffFlag("1");
			}
		}
		// 下限差值
		if (!stdMin.equals("") && !stdMin1.equals("")) {
			if (DifferenceValue.isNumeric(stdMin)
					&& DifferenceValue.isNumeric(stdMin1)) {
				diffValue = Double.parseDouble(stdMin1)
						- Double.parseDouble(stdMin);
				picDiffEntity.setDifferentValueMin(String.valueOf(diffValue));
				if (Math.asin(diffValue) > 0) {
					picDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			} else {
				if (!stdMin.equals(stdMin1)) {
					picDiffEntity.setDiffFlag("1");
				} else {
					flagNum++;
				}
			}
		} else {
			if (stdMin.equals("") && stdMin1.equals("")) {
				flagNum++;
			} else {
				picDiffEntity.setDiffFlag("1");
			}
		}
		if (flagNum == 2) {
			picDiffEntity.setDiffFlag("0");
		}
		return picDiffEntity;
	}

	/**
	 * 验证是否为DOUBLE
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		try {
			Double.parseDouble(str);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 过滤附加要求
	 * 
	 * @param stdAddAskes
	 * @param addAskEntity
	 * @return
	 */
	public static ComBaseAdditionalEntity fiterAddAsk(
			List<ComBaseAdditionalEntity> stdAddAskes,
			SlmOrderAddAskEntity addAskEntity) {
		if (stdAddAskes == null) {
			return null;
		}
		for (ComBaseAdditionalEntity stdAddAsk : stdAddAskes) {
			if (stdAddAsk.getAskItme().equals(addAskEntity.getAskItme())
					&& stdAddAsk.getAskSubItme().equals(
							addAskEntity.getAskSubItme())) {
				return stdAddAsk;
			}
		}
		return null;
	}
}
