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

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.idao.IDeviationChemDao;
import com.steering.pss.sale.order.qualityDesign.model.ComBaseGradeEntity;
import com.steering.pss.sale.order.qualityDesign.model.ComMscStdChemBiasEntity;
import com.steering.pss.sale.order.qualityDesign.model.ComMscStdCicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdBiasCEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdCicEntity;

public class IDeviationChem {
	public String errMessage = "";

	public boolean checkDeviationChem(Connection con, String designKey,
			String cic, String gradeCode, String gradeName, String steelcode,
			String steelName, String flag,
			List<SlmOrdDesignStdCicEntity> chems) throws SQLException {
		IDeviationChemDao deviationChemDao = new IDeviationChemDao();
		ComMscStdCicEntity cicEntity = deviationChemDao.getChemMain(con, cic);
		// 正常情况不会为null
		if (cicEntity == null) {
			errMessage = "数据异常!";
			return true;
		}
		// 成分偏差标准无要求
		if (cicEntity.getStdCode().equals("")) {
			errMessage = "成分偏差标准无要求!";
			return true;
		}
		ComBaseGradeEntity gradeEntity = deviationChemDao.getGradeType(con,
				gradeCode);
		// 钢种分类
		String gradeType = gradeEntity.getAssortmentCode();
		// 偏差标准
		String stdCode = cicEntity.getStdCode();
		// 通过钢种与成分偏差标准查询成分索引号
		ComMscStdChemBiasEntity biasEntityG = deviationChemDao.getChemBais(con,
				stdCode, gradeType);
		if (biasEntityG != null) {
			deviationChemDao.updateCraftStd(con, designKey,
					biasEntityG.getCic(), flag);
			errMessage = "成分偏差标准验证通过!";
			IDeviationChem.fiterChems(con, biasEntityG.getCic(), chems,
					designKey, flag);
			return true;
		} else {
			// 通过钢级与成分偏差标准查询成分索引号
			ComMscStdChemBiasEntity biasEntityS = deviationChemDao.getChemSteel(
					con, stdCode, steelcode);
			if (biasEntityS != null) {
				deviationChemDao.updateCraftStd(con, designKey,
						biasEntityS.getCic(), flag);
				errMessage = "成分偏差标准验证通过!";
				IDeviationChem.fiterChems(con, biasEntityS.getCic(), chems,
						designKey, flag);
				return true;
			} else {
				errMessage = "成分标准" + cic + ":(钢种 " + gradeName + "与成分偏差标准"
						+ cicEntity.getStdName() + ")以及钢级 " + steelName
						+ "与成分偏差标准" + cicEntity.getStdName() + "都未找到成分偏差标准索引号!";
				return false;
			}
		}
	}

	public static void fiterChems(Connection con, String biasCic,
			List<SlmOrdDesignStdCicEntity> chems, String designKey,
			String flag) throws SQLException {
		IDeviationChemDao deviationChemDao = new IDeviationChemDao();
		List<SlmOrdDesignStdBiasCEntity> biasCEntityList = deviationChemDao
				.getChemBiasC(con, biasCic);
		// List<CraftOrdDesignStdBiasCEntity> fiterList = new
		// ArrayList<CraftOrdDesignStdBiasCEntity>();
		if (biasCEntityList != null && biasCEntityList.size() > 0) {
			for (SlmOrdDesignStdCicEntity chem : chems) {
				String chemCode = chem.getChemCode();
				String stdMax = chem.getStdmax();
				if (stdMax.equals("")) {
					// 如果上限值为空，则不取偏差值
					continue;
				} else {
					if (QualityDesignTool.isNumeric(stdMax)) {
						for (SlmOrdDesignStdBiasCEntity biasCEntity : biasCEntityList) {
							if (biasCEntity.getChemCode().equals(chemCode)
									&& QualityDesignTool.checkStr(stdMax,
											biasCEntity.getMinSign(),
											biasCEntity.getMinVal())
									&& QualityDesignTool.checkStr(stdMax,
											biasCEntity.getMaxSign(),
											biasCEntity.getMaxVal())) {
								biasCEntity.setDesginKey(designKey);
								if (flag.equals("N")) {
									biasCEntity.setSpeclFl("1");
								} else {
									biasCEntity.setSpeclFl("0");
								}
								deviationChemDao.addChemBiasC(con, biasCEntity);
							}
						}
					} else {
						// 如果是公式
						for (SlmOrdDesignStdBiasCEntity biasCEntity : biasCEntityList) {
							if (biasCEntity.getChemCode().equals(chemCode)) {
								biasCEntity.setDesginKey(designKey);
								if (flag.equals("N")) {
									biasCEntity.setSpeclFl("1");
								} else {
									biasCEntity.setSpeclFl("0");
								}
								deviationChemDao.addChemBiasC(con, biasCEntity);
							}
						}
					}
				}
			}
		}
	}
}
