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

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

import com.steering.pss.sale.order.model.ComMscEntity;
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.imp.MscChemDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdCicEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscChemImp;

public class IMscChem implements MscChemImp {

	/**
	 * 验证成分标准
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param design
	 * @param flag
	 *            (0:交付标准;1:特殊要求;2:内控标准)
	 * @return
	 * @throws SQLException
	 */
	public String checkChem(Connection con, String msc, DesignEntity design,
			String flag) throws SQLException {
		MscChemDaoImp chemDaoImp = FactoryDao.getChemDaoImp();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		String message = "";
		String designKey = design.getDesignKey();
		String modelCode = design.getModelCode();
		String dimater = design.getDimater();
		String height = design.getHeight();
		String specStyle = design.getSpecStyle();
		String specCode = design.getSpecCode();
		String specName = design.getSpecName();
		String useCode = design.getUserCode();
		String useDesc = design.getUserDesc();
		List<String> cics = null;
		ComMscEntity comMscEntity = mscDaoImp.getMsc(con, msc);
		String mscStatus = comMscEntity.getValidflag();

		// 如果冶金规范码已经生效，则查询已经审批通过的成分标准索引号
		if (mscStatus.equals("C")) {
			cics = chemDaoImp.getExamineCic(con, msc, modelCode);
		} else {
			// 如果没有生效，查询有效成分索引号
			cics = chemDaoImp.getValidCic(con, msc, modelCode);
		}

		// 如果找不到索引号
		if (cics == null || cics.size() == 0) {
			return message;
		}

		for (String cic : cics) {
			List<SlmOrdDesignStdCicEntity> chems = chemDaoImp.getChemByCic(con,
					cic);
			if (chems == null) {
				message = "成分索引号:" + cic + "不存在成分标准";
				continue;
			}
			List<String> chemsSize = IMscChem.getChemSize(chems);
			List<SlmOrdDesignStdCicEntity> chemAs = 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));
				chemAs = IMscChem.fiterChemByA(chems, dimater, height, dh, hd,
						in);
				// 管坯
			} else if (specStyle.equals("B")) {
				chemAs = IMscChem.fiterChemByB(chems, dimater);
			} else {
				// 钢锭 其它
				chemAs = IMscChem.fiterChemByCAndD(chems, specCode);
			}
			// 验证成分标准
			message = IMscChem.checkChem(chemAs, specStyle, dimater, height,
					specName);
			if (message.length() > 0) {
				message = "成分索引号:" + cic + ":" + message;
				continue;
			}
			// 过滤没有任何限制条件的元素记录，并且这个元素又存在满足约束的记录
			chemAs = IMscChem.fiterChemN(chemAs, specStyle);
			List<String> chemsSizeA = IMscChem.getChemSize(chemAs);
			String chemCodeStr = "";
			for (String chemCode : chemsSize) {
				if (!chemsSizeA.contains(chemCode)) {
					chemCodeStr += "," + chemCode;
				}
			}
//			if (chemCodeStr.length() > 0) {
//				message = "成分索引号:" + cic + "成分标准维护化学元素:" + chemsSize.size()
//						+ "项," + "但满足约束:" + chemsSizeA.size() + "项"
//						+ chemCodeStr + "不满足外径(" + dimater + ")/壁厚(" + height
//						+ ")或者规格:+(" + specName + ")约束";
//				continue;
//			}
			for (SlmOrdDesignStdCicEntity chem : chemAs) {
				chem.setDesginKey(designKey);
				// 是否特殊要求(0否;1是,2内控)
				chem.setSpeclFl(flag);
				if (flag.equals("1")) {
					chem.setUseCode(useCode);
					chem.setUseDesc(useDesc);
				}
				chemDaoImp.addChem(con, chem);
			}
			if (flag.equals("0")) {
				chemDaoImp.updateDesignStd(con, cic, designKey);
			}
			message = "";
			break;

		}
		return message;
	}

	// 钢锭与特殊规格过滤接箍规格，管坯过滤外径，钢管过滤外径，壁厚，同时也要考虑替代元素

	/**
	 * 查询维护成分元素项
	 * 
	 * @param chems
	 * @return
	 */
	public static List<String> getChemSize(List<SlmOrdDesignStdCicEntity> chems) {
		List<String> list = new ArrayList<String>();
		if (chems == null || chems.size() == 0) {
			return null;
		}
		for (SlmOrdDesignStdCicEntity chem : chems) {
			if (!list.contains(chem.getChemCode())) {
				list.add(chem.getChemCode());
			}
		}
		return list;
	}

	/**
	 * 过滤钢管规格
	 * 
	 * @param chems
	 *            成分集合
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param DHProportion
	 *            径壁比
	 * @param HDProportion
	 *            壁径比
	 * @param innerProportion
	 *            内径
	 * @return
	 */
	public static List<SlmOrdDesignStdCicEntity> fiterChemByA(
			List<SlmOrdDesignStdCicEntity> chems, String dimater,
			String height, String DHProportion, String HDProportion,
			String innerProportion) {
		if (chems == null || chems.size() == 0) {
			return null;
		}
		List<SlmOrdDesignStdCicEntity> list = new ArrayList<SlmOrdDesignStdCicEntity>();

		// 过滤外径壁厚
		for (int i = 0; i < chems.size(); i++) {
			SlmOrdDesignStdCicEntity chem = chems.get(i);
			if (chem.getSizeCode().equals("") && chem.getSizeCode2().equals("")) {
				list.add(chem);
				continue;
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (chem.getSizeCode2().equals("400301")) {
				if ((!QualityDesignTool.checkStr(dimater,
						chem.getSizeMinSign2(), chem.getSizeMin2()))
						|| (!QualityDesignTool.checkStr(dimater,
								chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
					continue;
				}
			}
			if (chem.getSizeCode2().equals("400302")) {
				if ((!QualityDesignTool.checkStr(height,
						chem.getSizeMinSign2(), chem.getSizeMin2()))
						|| (!QualityDesignTool.checkStr(height,
								chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
					continue;
				}
			}
			if (chem.getSizeCode2().equals("400303")) {
				if ((!QualityDesignTool.checkStr(DHProportion,
						chem.getSizeMinSign2(), chem.getSizeMin2()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
					continue;
				}
			}
			if (chem.getSizeCode2().equals("400304")) {
				if ((!QualityDesignTool.checkStr(HDProportion,
						chem.getSizeMinSign2(), chem.getSizeMin2()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
					continue;
				}
			}
			if (chem.getSizeCode2().equals("400305")) {
				if ((!QualityDesignTool.checkStr(innerProportion,
						chem.getSizeMinSign2(), chem.getSizeMin2()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
					continue;
				}
			}

			if (chem.getSizeCode().equals("400301")) {
				if ((!QualityDesignTool.checkStr(dimater,
						chem.getSizeMinSign(), chem.getSizeMin()))
						|| (!QualityDesignTool.checkStr(dimater,
								chem.getSizeMaxSign(), chem.getSizeMax()))) {
					continue;
				}
			}
			if (chem.getSizeCode().equals("400302")) {
				if ((!QualityDesignTool.checkStr(height, chem.getSizeMinSign(),
						chem.getSizeMin()))
						|| (!QualityDesignTool.checkStr(height,
								chem.getSizeMaxSign(), chem.getSizeMax()))) {
					continue;
				}
			}
			if (chem.getSizeCode().equals("400303")) {
				if ((!QualityDesignTool.checkStr(DHProportion,
						chem.getSizeMinSign(), chem.getSizeMin()))
						|| (!QualityDesignTool.checkStr(DHProportion,
								chem.getSizeMaxSign(), chem.getSizeMax()))) {
					continue;
				}
			}
			if (chem.getSizeCode().equals("400304")) {
				if ((!QualityDesignTool.checkStr(HDProportion,
						chem.getSizeMinSign(), chem.getSizeMin()))
						|| (!QualityDesignTool.checkStr(HDProportion,
								chem.getSizeMaxSign(), chem.getSizeMax()))) {
					continue;
				}
			}
			if (chem.getSizeCode().equals("400305")) {
				if ((!QualityDesignTool.checkStr(innerProportion,
						chem.getSizeMinSign(), chem.getSizeMin()))
						|| (!QualityDesignTool.checkStr(innerProportion,
								chem.getSizeMaxSign(), chem.getSizeMax()))) {
					continue;
				}
			}
			list.add(chem);
		}
		return list;
	}

	/**
	 * 过滤钢锭或者其它规格
	 * 
	 * @param chems
	 * @param specCode
	 * @return
	 */
	public static List<SlmOrdDesignStdCicEntity> fiterChemByCAndD(
			List<SlmOrdDesignStdCicEntity> chems, String specCode) {
		List<SlmOrdDesignStdCicEntity> list = new ArrayList<SlmOrdDesignStdCicEntity>();
		for (SlmOrdDesignStdCicEntity chem : chems) {
			if (chem.getSpecJg().equals("")
					|| chem.getSpecJg().equals(specCode)) {
				list.add(chem);
			}
		}
		return list;
	}

	/**
	 * 过滤管坯规格
	 * 
	 * @param chems
	 * @param dimater
	 * @return
	 */
	public static List<SlmOrdDesignStdCicEntity> fiterChemByB(
			List<SlmOrdDesignStdCicEntity> chems, String dimater) {
		List<SlmOrdDesignStdCicEntity> list = new ArrayList<SlmOrdDesignStdCicEntity>();
		for (SlmOrdDesignStdCicEntity chem : chems) {
			if (chem.getSizeCode().equals("") && chem.getSizeCode2().equals("")) {
				list.add(chem);
				continue;
			}
			if (!chem.getSizeCode().equals("400301")
					|| !chem.getSizeCode2().equals("400301")) {
				continue;
			} else {
				if (chem.getSizeCode2().equals("400301")) {
					if ((!QualityDesignTool.checkStr(dimater,
							chem.getSizeMinSign2(), chem.getSizeMin2()))
							|| (!QualityDesignTool.checkStr(dimater,
									chem.getSizeMaxSign2(), chem.getSizeMax2()))) {
						continue;
					}
				}
				if (chem.getSizeCode().equals("400301")) {
					if ((!QualityDesignTool.checkStr(dimater,
							chem.getSizeMinSign(), chem.getSizeMin()))
							|| (!QualityDesignTool.checkStr(dimater,
									chem.getSizeMaxSign(), chem.getSizeMax()))) {
						continue;
					}
				}
				list.add(chem);
			}
		}
		return list;
	}

	/**
	 * 验证过滤后的成分数据
	 * 
	 * @param chems
	 * @return
	 */
	public static String checkChem(List<SlmOrdDesignStdCicEntity> chems,
			String specStyle, String dimater, String height, String specName) {
		if (chems == null || chems.size() == 0) {
			if (specStyle.equals("A")) {
				return "<钢管产品>不存在满足合同外径(" + dimater + "),壁厚(" + height
						+ ")的成分标准";
			} else if (specStyle.equals("B")) {
				return "<管坯产品>不存在满足合同外径(" + dimater + ")的成分标准";
			} else {
				return "<钢锭/其它产品>不存在满足接箍规格(" + specName + ")的成分标准";
			}
		}
		// 有替代/限制元素
		List<SlmOrdDesignStdCicEntity> listRep = new ArrayList<SlmOrdDesignStdCicEntity>();
		List<String> strRep = new ArrayList<String>();
		// 没有任何限制规格/限制元素/替代元素
		List<SlmOrdDesignStdCicEntity> listNo = new ArrayList<SlmOrdDesignStdCicEntity>();
		List<String> strNo = new ArrayList<String>();
		// 只有限制规格
		List<SlmOrdDesignStdCicEntity> listSize = new ArrayList<SlmOrdDesignStdCicEntity>();
		List<String> strSize = new ArrayList<String>();
		for (SlmOrdDesignStdCicEntity chem : chems) {
			// 如果成分标准包含替代元素,限制元素
			if (!chem.getRepCode().equals("") || !chem.getEleCode().equals("")) {
				listRep.add(chem);
				strRep.add(chem.getChemName());
			} else {
				// 有限制规格
				if (specStyle.equals("A") || specStyle.equals("B")) {
					// 钢管/管坯
					if (!chem.getSizeCode().equals("")
							|| !chem.getSizeCode2().equals("")) {
						listSize.add(chem);
						strSize.add(chem.getChemName());
					} else {
						listNo.add(chem);
						strNo.add(chem.getChemName());
					}
				} else {
					// 钢锭/其它
					if (!chem.getSpecJg().equals("")) {
						listSize.add(chem);
						strSize.add(chem.getChemName());
					} else {
						listNo.add(chem);
						strNo.add(chem.getChemName());
					}
				}
			}

		}
		// 判断成分标准是否符合要求
		// 1.没有任何限制元素的集合，有相同元素，返回提示
		String message = "";
		message = IMscChem.getCommon(strNo);
		if (message.length() > 0) {
			return "存在两条相同成分元素:" + message + "并且没有任何限制条件的成分标准!";
		}
		message = IMscChem.getCommon(strSize);
		if (message.length() > 0) {
			if (specStyle.equals("A")) {
				return "<钢管产品>满足合同外径(" + dimater + "),壁厚(" + height
						+ ")并且没有替代/限制元素的成分标准,存在相同的元素:" + message;
			} else if (specStyle.equals("B")) {
				return "<管坯产品>满足合同外径(" + dimater + ")并且没有替代/限制元素的成分标准,存在相同的元素:"
						+ message;
			} else {
				return "<钢锭/其它产品>满足接箍规格(" + specName
						+ "并且没有替代/限制元素的 成分标准,存在相同的元素:" + message;
			}
		}
		if (strNo != null && strNo.size() > 0) {
			for (int i = 0; i < strRep.size(); i++) {
				if (strNo.contains(strRep.get(i))
						|| strSize.contains(strRep.get(i))) {
					return "化学元素:" + strRep.get(i) + "有替代/限制元素约束,所有满足外径("
							+ dimater + "),壁厚" + height + ")的此类化学元素必须有替代/限制元素!";
				}
			}
		}
		return "";

	}

	/**
	 * 取出List相同的元素
	 * 
	 * @param list
	 * @return
	 */
	public static String getCommon(List<String> list) {
		if (list == null || list.size() == 0) {
			return "";
		}
		for (int i = 0; i < list.size(); i++) {
			for (int j = i + 1; j < list.size(); j++) {
				if (list.get(i).equals(list.get(j))) {
					return list.get(j);
				}
			}
		}
		return "";
	}

	/**
	 * 过滤成分标准
	 * 
	 * @param chems
	 *            成分标准集合
	 * @return
	 */
	public static List<SlmOrdDesignStdCicEntity> fiterChemN(
			List<SlmOrdDesignStdCicEntity> chems, String specStyle) {
		List<SlmOrdDesignStdCicEntity> list = new ArrayList<SlmOrdDesignStdCicEntity>();
		for (SlmOrdDesignStdCicEntity chem : chems) {
			if (!chem.getSizeCode().equals("")
					|| !chem.getSizeCode2().equals("")
					|| !chem.getSpecJg().equals("")) {
				for (int i = 0; i < list.size(); i++) {
					SlmOrdDesignStdCicEntity chemi = list.get(i);
					if (chemi.getRepCode().equals("")
							&& chemi.getEleCode().equals("")
							&& chemi.getChemCode().equals(chem.getChemCode())) {
						if (specStyle.equals("A") || specStyle.equals("B")) {
							if (chemi.getSizeCode().equals("")
									&& chemi.getSizeCode2().equals("")) {
								list.remove(i);
							}
						} else {
							if (chemi.getSpecJg().equals("")) {
								list.remove(i);
							}
						}

					}
				}
			}
			list.add(chem);
		}
		return list;
	}
}
