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

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.steering.pss.sale.order.model.ComMscEntity;
import com.steering.pss.sale.order.model.DesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.idao.IMscSpecDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscSpecDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.model.ComBaseProductEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscSpecImp;

public class IMscSpec implements MscSpecImp {
	/**
	 * 验证公差标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @return boolean
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String checkSpec(Connection con, String msc, DesignEntity design,
			String flag) throws Exception {
		String message = "";
		String designKey = design.getDesignKey();
		String modelCode = design.getModelCode();
		String dimater = design.getDimater();
		String height = design.getHeight();
		String specStyle = design.getSpecStyle();
		String specName = design.getSpecName();
		String useCode = design.getUserCode();
		String useDesc = design.getUserDesc();
		String lenMax = design.getLengthMax();
		String lenMin = design.getLengthMin();
		MscSpecDaoImp specDaoImp = FactoryDao.getSpecDaoImp();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		// 查询冶金规范码信息
		ComMscEntity comMscEntity = mscDaoImp.getMsc(con, msc);
		List<String> sics = new ArrayList<String>();
		if (comMscEntity.getValidflag().equals("C")) {
			sics = specDaoImp.getExamineSic(con, msc, modelCode);
		} else {
			sics = specDaoImp.getvalidSic(con, msc, modelCode);
		}
		// 找不到公差索引号,则没有公差标准
		if (sics == null || sics.size() == 0) {
			return message;
		}
		for (String sic : sics) {
			List<SlmOrdDesignStdSicEntity> specs = specDaoImp.getSpecBySic(con,
					sic);
			if (specs == null || specs.size() == 0) {
				message = "公差索引号" + sic + "不存在公差标准";
				continue;
			}

			// 过滤公差标准
			List<SlmOrdDesignStdSicEntity> specAs = null;
			if (specStyle.equals("A")) {
				// 钢管
				// 径壁比
				String dh = String.valueOf(Double.parseDouble(dimater)
						/ Double.parseDouble(height));
				// 壁径比
				String hd = String.valueOf(Double.parseDouble(height)
						/ Double.parseDouble(dimater));
				// 内径
				String in = String.valueOf(Double.parseDouble(dimater) - 2
						* Double.parseDouble(height));
				specAs = IMscSpec.fiterSpecA(specs, dimater, height, dh, hd,
						in, lenMax, lenMin);
			} else if (specStyle.equals("B")) {
				// 管坯
				specAs = IMscSpec.fiterSpecB(specs, dimater);
			} else {
				// 其他规格
				specAs = IMscSpec.fiterSpecD(specs, dimater);
			}
			Object[] info = IMscSpec.fiterSpec(specAs, specs, dimater, height,
					specName, specStyle, flag, design.getProducCode(), con);
			message = (String) info[1];
			specAs = (ArrayList<SlmOrdDesignStdSicEntity>) info[0];
			if (message.length() > 0) {
				message = "公差索引号:" + sic + "," + message;
				continue;
			}
			// 验证公差项标准值是否大于15
			// if (flag.equals("0")) {
			// String sicFlag = specDaoImp
			// .getSicFlag(con, design.getOrdLnPk());
			// if (sicFlag.equals("0")) {
			// boolean flagValue = IMscSpec.checkSpecHeightValue(specAs,
			// height);
			// if (!flagValue) {
			// message = "公差索引号:" + sic + ",壁厚公差值小于15,请确认!";
			// continue;
			// }
			// }
			//
			// }
			// 保存公差标准
			for (SlmOrdDesignStdSicEntity spec : specAs) {
				// 是否特殊要求(0否;1是)
				spec.setSpeclFl(flag);
				spec.setDesginKey(designKey);
				if (flag.equals("1")) {
					spec.setUseCode(useCode);
					spec.setUseDesc(useDesc);
				}
				specDaoImp.saveSpec(con, spec);
			}
			// 保存索引号
			if (flag.equals("0")) {
				specDaoImp.updateDesignStd(con, sic, designKey);
			}
			message = "";
			break;
		}
		return message;
	}

	/**
	 * 过滤公差标准(钢管)
	 * 
	 * @param specs
	 *            公差标准集合
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<DesignStdSicEntity>
	 * @throws Exception
	 */
	public static List<SlmOrdDesignStdSicEntity> fiterSpecA(
			List<SlmOrdDesignStdSicEntity> specs, String dimater,
			String height, String DHProportion, String HDProportion,
			String innerProportion, String lengthMax, String lengthMin)
			throws Exception {
		if (specs == null || specs.size() == 0) {
			return null;
		}
		List<SlmOrdDesignStdSicEntity> specAs = new ArrayList<SlmOrdDesignStdSicEntity>();
		for (int i = 0; i < specs.size(); i++) {
			SlmOrdDesignStdSicEntity spec = specs.get(i);
			if (spec.getBiasCodeZ().equals("")
					&& spec.getBiasCodeC().equals("")
					&& spec.getBiasCodeS().equals("")) {
				specAs.add(spec);
				continue;
			}
			// 针对公式约束项进行处理
			// 约束1下限值
			String zmin = spec.getZmin();
			if (!zmin.equals("")
					&& (zmin.contains("t") || zmin.contains("D") || zmin
							.contains("L"))) {
				zmin = zmin.replace("t", height);
				zmin = zmin.replace("D", dimater);
				zmin = zmin.replace("t", lengthMin);
				double zminValue = QualityDesignTool.countFormula(zmin);
				spec.setZmin(String.valueOf(zminValue));
			}
			// 约束1上限值
			String zmax = spec.getZmax();
			if (!zmax.equals("")
					&& (zmax.contains("t") || zmax.contains("D") || zmax
							.contains("L"))) {
				zmax = zmax.replace("t", height);
				zmax = zmax.replace("D", dimater);
				zmax = zmax.replace("t", lengthMin);
				double zmaxValue = QualityDesignTool.countFormula(zmax);
				spec.setZmax(String.valueOf(zmaxValue));
			}
			// 约束2下限值
			String smin = spec.getSmin();
			if (!smin.equals("")
					&& (smin.contains("t") || smin.contains("D") || smin
							.contains("L"))) {
				smin = smin.replace("t", height);
				smin = smin.replace("D", dimater);
				smin = smin.replace("t", lengthMin);
				double sminValue = QualityDesignTool.countFormula(smin);
				spec.setSmin(String.valueOf(sminValue));
			}
			// 约束2上限值
			String smax = spec.getSmax();
			if (!smax.equals("")
					&& (smax.contains("t") || smax.contains("D") || smax
							.contains("L"))) {
				smax = smax.replace("t", height);
				smax = smax.replace("D", dimater);
				smax = smax.replace("t", lengthMin);
				double smaxValue = QualityDesignTool.countFormula(smax);
				spec.setSmax(String.valueOf(smaxValue));
			}
			// 约束3下限值
			String cmin = spec.getCmin();
			if (!cmin.equals("")
					&& (cmin.contains("t") || cmin.contains("D") || cmin
							.contains("L"))) {
				cmin = cmin.replace("t", height);
				cmin = cmin.replace("D", dimater);
				cmin = cmin.replace("t", lengthMin);
				double cminValue = QualityDesignTool.countFormula(cmin);
				spec.setCmin(String.valueOf(cminValue));
			}
			// 约束3上限值
			String cmax = spec.getCmax();
			if (!cmax.equals("")
					&& (cmax.contains("t") || cmax.contains("D") || cmax
							.contains("L"))) {
				cmax = cmax.replace("t", height);
				cmax = cmax.replace("D", dimater);
				cmax = cmax.replace("t", lengthMin);
				double cmaxValue = QualityDesignTool.countFormula(cmax);
				spec.setCmax(String.valueOf(cmaxValue));
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (spec.getBiasCodeZ().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			specAs.add(spec);
		}
		return specAs;
	}

	/**
	 * 过滤公差标准 (管坯)
	 * 
	 * @param specs
	 * @param dimater
	 * @return
	 */
	public static List<SlmOrdDesignStdSicEntity> fiterSpecB(
			List<SlmOrdDesignStdSicEntity> specs, String dimater) {
		List<SlmOrdDesignStdSicEntity> specAs = new ArrayList<SlmOrdDesignStdSicEntity>();
		for (int i = 0; i < specs.size(); i++) {
			SlmOrdDesignStdSicEntity spec = specs.get(i);
			if (spec.getBiasCodeZ().equals("")
					&& spec.getBiasCodeC().equals("")
					&& spec.getBiasCodeS().equals("")) {
				specAs.add(spec);
				continue;
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (spec.getBiasCodeZ().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}

			if (spec.getBiasCodeC().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}

			if (spec.getBiasCodeS().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}

			specAs.add(spec);
		}

		return specAs;
	}

	/**
	 * 过滤公差标准 (管坯)
	 *
	 * @param specs
	 * @param dimater
	 * @return
	 */
	public static List<SlmOrdDesignStdSicEntity> fiterSpecD(
			List<SlmOrdDesignStdSicEntity> specs, String dimater) {
		List<SlmOrdDesignStdSicEntity> specAs = new ArrayList<SlmOrdDesignStdSicEntity>();
		for (int i = 0; i < specs.size(); i++) {
			SlmOrdDesignStdSicEntity spec = specs.get(i);
			if(!spec.getZminSign().equals("") || !spec.getZmaxSign().equals("")
					|| !spec.getSminSign().equals("") || !spec.getCmaxSign().equals("")
					|| !spec.getSminSign().equals("") || !spec.getSmaxSign().equals("")){
				continue;
			}
			specAs.add(spec);
		}

		return specAs;
	}

	/**
	 * 过滤公差标准
	 * 
	 * @param specs
	 *            公差标准集合
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param DHProportion
	 *            径壁比
	 * @param HDProportion
	 *            壁径比
	 * @param innerProportion
	 *            内径
	 * @param lengthMax
	 *            长度上限
	 * @param lengthMin
	 *            长度下限
	 * @return List<DesignStdSicEntity>
	 */
	public List<DesignStdSicEntity> fiterSpec(List<DesignStdSicEntity> specs,
			String dimater, String height, String DHProportion,
			String HDProportion, String innerProportion, String lengthMax,
			String lengthMin) {
		if (specs == null || specs.size() == 0) {
			return null;
		}
		List<DesignStdSicEntity> specAs = new ArrayList<DesignStdSicEntity>();
		for (int i = 0; i < specs.size(); i++) {
			DesignStdSicEntity spec = specs.get(i);
			if (spec.getBiasCodeZ().equals("")
					&& spec.getBiasCodeC().equals("")
					&& spec.getBiasCodeS().equals("")) {
				specAs.add(spec);
				continue;
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (spec.getBiasCodeZ().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getZminSign(), spec.getZmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeZ().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getZminSign(),
						spec.getZmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getZmaxSign(), spec.getZmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getCminSign(), spec.getCmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeC().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getCminSign(),
						spec.getCmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getCmaxSign(), spec.getCmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400301")) {
				if (!(QualityDesignTool.checkStr(dimater, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(dimater,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400302")) {
				if (!(QualityDesignTool.checkStr(height, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(height,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400303")) {
				if (!(QualityDesignTool.checkStr(DHProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400304")) {
				if (!(QualityDesignTool.checkStr(HDProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400305")) {
				if (!(QualityDesignTool.checkStr(innerProportion,
						spec.getSminSign(), spec.getSmin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMax, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(lengthMax,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			if (spec.getBiasCodeS().equals("400306")) {
				if (!(QualityDesignTool.checkStr(lengthMin, spec.getSminSign(),
						spec.getSmin()))
						|| (!QualityDesignTool.checkStr(lengthMin,
								spec.getSmaxSign(), spec.getSmax()))) {
					continue;
				}
			}
			specAs.add(spec);
		}
		return specAs;
	}

	/**
	 * 验证公差标准
	 * 
	 * @param specs
	 * @param specAlls
	 * @param dimater
	 * @param height
	 * @param specName
	 * @param specStyle
	 * @return
	 * @throws SQLException 
	 */
	public static Object[] fiterSpec(List<SlmOrdDesignStdSicEntity> specs,
			List<SlmOrdDesignStdSicEntity> specAlls, String dimater,
			String height, String specName, String specStyle, String flag,
			String producCode, Connection con) throws SQLException {
		Object[] arr = new Object[2];
		if (specs == null || specs.size() == 0 || specAlls == null
				|| specAlls.size() == 0) {
			arr[0] = null;
			if (specStyle.equals("A") || specStyle.equals("B")) {
				arr[1] = "满足外径(" + dimater + ")、壁厚(" + height + ")等约束公差标准不存在";
			} else {
				arr[1] = "满足规格(" + specName + ")约束公差标准不存在";
			}
			return arr;
		}
		List<SlmOrdDesignStdSicEntity> specYs = new ArrayList<SlmOrdDesignStdSicEntity>();
		List<String> specYKeys = new ArrayList<String>();
		List<SlmOrdDesignStdSicEntity> specNs = new ArrayList<SlmOrdDesignStdSicEntity>();
		List<String> specNKeys = new ArrayList<String>();
		for (SlmOrdDesignStdSicEntity spec : specs) {
			String keyCode = spec.getBiasCode();
			String keyName = spec.getBiasName();
			if (spec.getBiasCodeZ().equals("")
					&& spec.getBiasCodeC().equals("")
					|| spec.getBiasCodeS().equals("")) {
				if (specNKeys.contains(keyCode)) {
					arr[0] = null;
					arr[1] = "公差项(" + keyName + ")存在多条公差标准,并且不存在公差约束项!";
				}
				specNKeys.add(keyCode);
				specNs.add(spec);
			} else {
				if (specYKeys.contains(keyCode)) {
					arr[0] = null;
					arr[1] = "公差项(" + keyName + ")存在多条满足外径(" + dimater
							+ ")、壁厚(" + height + ")公差标准!";
					return arr;
				}
				specYKeys.add(keyCode);
				specYs.add(spec);
			}
		}
		for (int i = 0; i < specNs.size(); i++) {
			SlmOrdDesignStdSicEntity spec = specNs.get(i);
			String keyCode = spec.getBiasCode();
			if (!specYKeys.contains(keyCode)) {
				specYKeys.add(keyCode);
				specYs.add(spec);
			}
		}
		// 公差项必须维护壁厚
		ComBaseProductEntity product = new IMscSpecDao().getProduct(con, producCode);
		boolean productTypeA = product.getProducType().equals("A") && (product.getProducHead().equals("404901")
				|| product.getProducHead().equals("404902") || product.getProducHead().equals("404903"));
		boolean productTypeB = product.getProducType().equals("B");
		if (flag.equals("0") && (productTypeA || productTypeB)) {
			if (!specYKeys.contains("404019")) {
				arr[0] = null;
				arr[1] = "公差标准满足外径(" + dimater + "),壁厚(" + height
						+ ")的公差项没有管体壁厚项,公差项目必须维护管体壁厚公差项!";
				return arr;
			}

		}
//		List<String> keyCodes = IMscSpec.getSpecSize(specYs);
//		List<String> keyAllCodes = IMscSpec.getSpecSize(specAlls);
//		if (keyCodes.size() != keyAllCodes.size()) {
//			for (int i = 0; i < keyAllCodes.size(); i++) {
//				String keyCode = keyAllCodes.get(i);
//				if (!keyCodes.contains(keyCode)) {
//					arr[0] = null;
//					arr[1] = "公差维护项目:" + keyAllCodes.size() + "项,但满足外径("
//							+ dimater + ")、壁厚(" + height + ") "
//							+ keyCodes.size() + "项,公差项("
//							+ keyCode.substring(keyCode.indexOf("&") + 1)
//							+ ")不满足约束!";
//					return arr;
//				}
//			}
//		} else {
//			for (int i = 0; i < keyCodes.size(); i++) {
//				if (!keyCodes.get(i).equals(keyAllCodes.get(i))) {
//					arr[0] = null;
//					arr[1] = "公差维护项目:"
//							+ keyAllCodes.size()
//							+ "项,但满足外径("
//							+ dimater
//							+ ")、壁厚("
//							+ height
//							+ ")"
//							+ keyCodes.size()
//							+ "项,公差项("
//							+ keyCodes.get(i).substring(
//									keyCodes.get(i).indexOf("&") + 1)
//							+ ")不满足约束!";
//					return arr;
//				}
//			}
//		}
		arr[0] = specYs;
		arr[1] = "";
		return arr;
	}

	/**
	 * 公差标准维护项目
	 * 
	 * @param specs
	 * @return
	 */
	public static List<String> getSpecSize(List<SlmOrdDesignStdSicEntity> specs) {
		if (specs == null || specs.size() == 0) {
			return null;
		}
		List<String> keys = new ArrayList<String>();
		for (SlmOrdDesignStdSicEntity spec : specs) {
			String key = spec.getBiasCode() + "&" + spec.getBiasName();
			if (!keys.contains(key)) {
				keys.add(key);
			}
		}
		// 升序排序
		Collections.sort(keys);
		return keys;
	}

	public static boolean checkSpecHeightValue(
			List<SlmOrdDesignStdSicEntity> sicEntitys, String height) {
		SlmOrdDesignStdSicEntity sicEntity = null;
		for (SlmOrdDesignStdSicEntity sicE : sicEntitys) {
			if (sicE.getBiasCode().equals("404002")
					|| sicE.getBiasCode().equals("404019")) {
				sicEntity = sicE;
			}
		}
		if (sicEntity == null) {
			return true;
		}
		double value1 = 0;
		int count = 0;
		// 数值
		if (!sicEntity.getStdMinVal().equals("")
				&& !sicEntity.getStdMaxVal().equals("")) {
			value1 = Double.parseDouble(sicEntity.getStdMaxVal())
					- Double.parseDouble(sicEntity.getStdMinVal());
			count++;
		}
		double value2 = 0;
		// 百分比
		if (!sicEntity.getStdMinPercent().equals("")
				&& !sicEntity.getStdMaxPercent().equals("")) {
			value2 = (Double.parseDouble(sicEntity.getStdMaxPercent()) - Double
					.parseDouble(sicEntity.getStdMinPercent()))
					* Double.parseDouble(height);
			count++;
		}
		if (count >= 2) {
			if (sicEntity.getStdMinFormulaV().equals("1")) {
				// 取小值
				if (value1 > value2) {
					value1 = value2;
				}
				// 如果差值小于15
				if (value1 <= 15) {
					return false;
				}
			} else {
				// 取大值
				if (value1 < value2) {
					value1 = value2;
				}
				// 如果差值小于15
				if (value1 <= 15) {
					return false;
				}
			}
		} else {
			// 取大值
			if (value1 < value2) {
				value1 = value2;
			}
			if (value1 <= 15) {
				return false;
			}
		}
		return true;
	}
}
