package com.steering.pss.plan.order;

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

import javax.script.ScriptException;

import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.pss.plan.base.CorePlnComm;
import com.steering.pss.sale.order.model.DesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscSpecDaoImp;
import com.steering.pss.sale.order.qualityDesign.server.factory.FactoryServer;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscSpecImp;

/**
 * 查询内控标准公差
 */
public class CoreTolerOfNk extends DBCommon {

	/*
	 * 查询内控标准，得到壁厚的公差
	 * 
	 * @param thickTolerance 壁厚公差实体类(内控标准)
	 */
	public CoreTolerOfNkParam queryTolerance(CoreTolerOfNkParam thickTolerance)
			throws ScriptException {
		Connection conn = null;
		try {
			double outdiameter = thickTolerance.getOutdiameter();
			double wallthick = thickTolerance.getWallthick();
			String sic = thickTolerance.getSic();
			String bias_code = thickTolerance.getBias_code();
			double lengthmax = thickTolerance.getLengthmax();
			double lengthmin = thickTolerance.getLengthmin();
			Double wallLower = null;
			Double wallUpper = null;

			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			MscSpecDaoImp mscspec = FactoryDao.getSpecDaoImp();
			List<DesignStdSicEntity> specs = mscspec.getSpecBySics(conn, sic);
			for (int i = specs.size() - 1; i >= 0; i--) {
				if (!specs.get(i).getBiasCode().equals(bias_code)) {
					specs.remove(i);
				}
			}
			MscSpecImp mscSpecImp = FactoryServer.getMscSpecImp();
			List<DesignStdSicEntity> list_imp = mscSpecImp.fiterSpec(specs,
					String.valueOf(outdiameter), String.valueOf(wallthick),
					String.valueOf(outdiameter / wallthick),
					String.valueOf(wallthick / outdiameter),
					String.valueOf(outdiameter - wallthick),
					String.valueOf(lengthmax), String.valueOf(lengthmin));
			if (list_imp != null && list_imp.size() > 0) {
				String stdMinVal = list_imp.get(0).getStdMinVal(); // 标准负值(mm)
				String stdMaxVal = list_imp.get(0).getStdMaxVal(); // 标准正值(mm)
				String stdMinPercent = list_imp.get(0).getStdMinPercent(); // 标准负值(%)
				String stdMaxPercent = list_imp.get(0).getStdMaxPercent(); // 标准正值(%)
				String stdMinFormula = list_imp.get(0).getStdMinFormula(); // 标准公式负值(mm)
				String stdMaxFormula = list_imp.get(0).getStdMaxFormula(); // 标准公式正值(mm)
				String stdMinFormulaV = list_imp.get(0).getStdMinFormulaV();// 取小值值
				String stdMaxFormulaV = list_imp.get(0).getStdMaxFormulaV();// 取最大值

				Double minVal = null;
				Double maxVal = null;
				Double minPercent = null;
				Double maxPercent = null;
				Double minFormula = null;
				Double maxFormula = null;

				if (!stdMinVal.equals("")) {
					minVal = Double.valueOf(stdMaxVal);
				}
				if (!stdMaxVal.equals("")) {
					maxVal = Double.valueOf(stdMaxVal);
				}

				if (!stdMinPercent.equals("")) {
					minPercent = Double.valueOf(stdMinPercent) * wallthick
							* 0.01;
				}
				if (!stdMaxPercent.equals("")) {
					maxPercent = Double.valueOf(stdMaxPercent) * wallthick
							* 0.01;
				}

				if (!stdMinFormula.equals("") && !stdMinFormula.equals("0")) {
					stdMinFormula = stdMinFormula.replaceAll("t",
							String.valueOf(wallthick)); // 壁厚带入计算公式
					stdMinFormula = stdMinFormula.replaceAll("D",
							String.valueOf(outdiameter)); // 外径带入计算公式
					minFormula = (Double) CorePlnComm
							.countFormula(stdMinFormula);
				}
				if (!stdMaxFormula.equals("") && !stdMaxFormula.equals("0")) {
					stdMaxFormula = stdMaxFormula.replaceAll("t",
							String.valueOf(wallthick)); // 壁厚带入计算公式
					stdMaxFormula = stdMaxFormula.replaceAll("D",
							String.valueOf(outdiameter)); // 外径带入计算公式
					maxFormula = (Double) CorePlnComm
							.countFormula(stdMaxFormula);
				}

				if (stdMinFormulaV.equals("1")) { // 取下限值
					if (minVal != null && maxVal != null) {
						wallLower = minVal;
						wallUpper = maxVal;
					}
					if (minPercent != null && maxPercent != null) {
						if (wallLower != null && wallUpper != null) {
							if ((wallLower + wallUpper) > (minPercent + maxPercent)) {
								wallLower = minPercent;
								wallUpper = maxPercent;
							}
						} else {
							wallLower = minPercent;
							wallUpper = maxPercent;
						}
					}
					if (minFormula != null && maxFormula != null) {
						if (wallLower != null && wallUpper != null) {
							if ((wallLower + wallUpper) > (minFormula + maxFormula)) {
								wallLower = minFormula;
								wallUpper = maxFormula;
							}
						} else {
							wallLower = minFormula;
							wallUpper = maxFormula;
						}
					}
				} else if (stdMaxFormulaV.equals("1")) { // 取上限值
					if (minVal != null && maxVal != null) {
						wallLower = minVal;
						wallUpper = maxVal;
					}
					if (minPercent != null && maxPercent != null) {
						if (wallLower != null && wallUpper != null) {
							if ((wallLower + wallUpper) < (minPercent + maxPercent)) {
								wallLower = minPercent;
								wallUpper = maxPercent;
							}
						} else {
							wallLower = minPercent;
							wallUpper = maxPercent;
						}
					}
					if (minFormula != null && maxFormula != null) {
						if (wallLower != null && wallUpper != null) {
							if ((wallLower + wallUpper) < (minFormula + maxFormula)) {
								wallLower = minFormula;
								wallUpper = maxFormula;
							}
						} else {
							wallLower = minFormula;
							wallUpper = maxFormula;
						}
					}
				} else {
					if (minVal != null && maxVal != null) {
						wallLower = minVal;
						wallUpper = maxVal;
					}
					if (minPercent != null && maxPercent != null) {
						wallLower = minPercent;
						wallUpper = maxPercent;
					}
					if (minFormula != null && maxFormula != null) {
						wallLower = minFormula;
						wallUpper = maxFormula;
					}
				}
			}

			if (wallUpper == null) {
				wallUpper = 0.0;
			}
			if (wallLower == null) {
				wallLower = 0.0;
			}

			thickTolerance.setWallLower(wallLower);
			thickTolerance.setWallUpper(wallUpper);
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return thickTolerance;
	}

}