package com.steering.pss.plnsaleord.order.baseMethod;

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

import javax.script.ScriptException;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.CorePlnComm;
import com.steering.pss.plnsaleord.order.model.CoreTolerOfNkParam;
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.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,
            String desginKey, String mscPline, String porcode) {
        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);
            // Double wallLowerYJ = null;
            // Double wallUpperYJ = null;
            // 冶金规范 内控标准
            // MscSpecDaoImp mscspec = FactoryDao.getSpecDaoImp();
            // List<DesignStdSicEntity> specsYJ = mscspec.getSpecBySics(conn,
            // sic);
            // if (specsYJ == null) {
            // return thickTolerance;
            // }
            // for (int i = specsYJ.size() - 1; i >= 0; i--) {
            // if (!specsYJ.get(i).getBiasCode().equals(bias_code)) {
            // specsYJ.remove(i);
            // }
            // }
            // MscSpecImp mscSpecImpYJ = FactoryServer.getMscSpecImp();
            // List<DesignStdSicEntity> list_impYJ = mscSpecImpYJ.fiterSpec(
            // specsYJ, 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_impYJ != null && list_impYJ.size() > 0) {
            // String stdMinValYJ = list_impYJ.get(0).getStdMinVal(); //
            // 标准负值(mm)
            // String stdMaxValYJ = list_impYJ.get(0).getStdMaxVal(); //
            // 标准正值(mm)
            // String stdMinPercentYJ = list_impYJ.get(0).getStdMinPercent(); //
            // 标准负值(%)
            // String stdMaxPercentYJ = list_impYJ.get(0).getStdMaxPercent(); //
            // 标准正值(%)
            // String stdMinFormulaYJ = list_impYJ.get(0).getStdMinFormula(); //
            // 标准公式负值(mm)
            // String stdMaxFormulaYJ = list_impYJ.get(0).getStdMaxFormula(); //
            // 标准公式正值(mm)
            // String stdMinFormulaVYJ =
            // list_impYJ.get(0).getStdMinFormulaV();// 取小值值
            // String stdMaxFormulaVYJ =
            // list_impYJ.get(0).getStdMaxFormulaV();// 取最大值
            //
            // Double minValYJ = null;
            // Double maxValYJ = null;
            // Double minPercentYJ = null;
            // Double maxPercentYJ = null;
            // Double minFormulaYJ = null;
            // Double maxFormulaYJ = null;
            //
            // if (!stdMinValYJ.equals("")) {
            // // if(Double.valueOf(stdMinVal) > 0){
            //
            // // }
            //
            // minValYJ = Double.valueOf(stdMinValYJ);
            // }
            // if (!stdMaxValYJ.equals("")) {
            // maxValYJ = Double.valueOf(stdMaxValYJ);
            // if (maxValYJ >= wallthick) {
            // maxValYJ = 0.25 * wallthick + minValYJ;
            // }
            // }
            //
            // if (!stdMinPercentYJ.equals("")) {
            // // minPercent = Math.abs(Double.valueOf(stdMinPercent) *
            // // wallthick
            // // * 0.01);
            // minPercentYJ = Double.valueOf(stdMinPercentYJ) * wallthick
            // * 0.01;
            // }
            // if (!stdMaxPercentYJ.equals("")) {
            // maxPercentYJ = Double.valueOf(stdMaxPercentYJ) * wallthick
            // * 0.01;
            // if (maxPercentYJ != null && maxPercentYJ >= wallthick) {
            // maxPercentYJ = 0.25 * wallthick + minPercentYJ;
            // }
            // }
            //
            // if (!stdMinFormulaYJ.equals("") && !stdMinFormulaYJ.equals("0"))
            // {
            // stdMinFormulaYJ = stdMinFormulaYJ.replaceAll("t", String
            // .valueOf(wallthick)); // 壁厚带入计算公式
            // stdMinFormulaYJ = stdMinFormulaYJ.replaceAll("D", String
            // .valueOf(outdiameter)); // 外径带入计算公式
            // // minFormula = (Double) CorePlnComm
            // // .countFormula(stdMinFormula);
            // minFormulaYJ = (Double) CorePlnComm
            // .getResult(stdMinFormulaYJ);
            //
            // }
            // if (!stdMaxFormulaYJ.equals("") && !stdMaxFormulaYJ.equals("0"))
            // {
            // stdMaxFormulaYJ = stdMaxFormulaYJ.replaceAll("t", String
            // .valueOf(wallthick)); // 壁厚带入计算公式
            // stdMaxFormulaYJ = stdMaxFormulaYJ.replaceAll("D", String
            // .valueOf(outdiameter)); // 外径带入计算公式
            //
            // // maxFormula = (Double) CorePlnComm
            // // .countFormula(stdMaxFormula);
            // maxFormulaYJ = (Double) CorePlnComm
            // .getResult(stdMaxFormulaYJ);
            // if (maxFormulaYJ != null && maxFormulaYJ >= wallthick) {
            // maxFormulaYJ = 0.25 * wallthick + minFormulaYJ;
            // }
            // }
            // if (stdMinFormulaVYJ != null && stdMinFormulaVYJ.equals("1")) {
            // // 取下限值
            // if (minPercentYJ != null && maxPercentYJ != null) {
            // wallLowerYJ = minPercentYJ;
            // wallUpperYJ = maxPercentYJ;
            // }
            // else if (minValYJ != null && maxValYJ != null) {
            // wallLowerYJ = minValYJ;
            // wallUpperYJ = maxValYJ;
            // }
            // else if (minFormulaYJ != null && maxFormulaYJ != null) {
            // wallLowerYJ = minFormulaYJ;
            // wallUpperYJ = maxFormulaYJ;
            // }
            //
            // if (minValYJ != null && maxValYJ != null) {
            // wallUpperYJ = Math.min(wallUpperYJ, maxValYJ);
            // wallLowerYJ = Math.max(wallLowerYJ, minValYJ);
            // }
            // if (minFormulaYJ != null && maxFormulaYJ != null) {
            // wallUpperYJ = Math.min(wallUpperYJ, maxFormulaYJ);
            // wallLowerYJ = Math.max(wallLowerYJ, minFormulaYJ);
            // }
            // }
            // else if (stdMaxFormulaVYJ != null
            // && stdMaxFormulaVYJ.equals("1")) {// 取上限值
            // if (minPercentYJ != null && maxPercentYJ != null) {
            // wallLowerYJ = minPercentYJ;
            // wallUpperYJ = maxPercentYJ;
            // }
            // else if (minValYJ != null && maxValYJ != null) {
            // wallLowerYJ = minValYJ;
            // wallUpperYJ = maxValYJ;
            // }
            // else if (minFormulaYJ != null && maxFormulaYJ != null) {
            // wallLowerYJ = minFormulaYJ;
            // wallUpperYJ = maxFormulaYJ;
            // }
            //
            // if (minValYJ != null && maxValYJ != null) {
            // wallUpperYJ = Math.max(wallUpperYJ, maxValYJ);
            // wallLowerYJ = Math.min(wallLowerYJ, minValYJ);
            // }
            // if (minFormulaYJ != null && maxFormulaYJ != null) {
            // wallUpperYJ = Math.max(wallUpperYJ, maxFormulaYJ);
            // wallLowerYJ = Math.min(wallLowerYJ, minFormulaYJ);
            // }
            // }
            // else {
            // if (minPercentYJ != null && maxPercentYJ != null) {
            // wallLowerYJ = minPercentYJ;
            // wallUpperYJ = maxPercentYJ;
            // }
            // else if (minValYJ != null && maxValYJ != null) {
            // wallLowerYJ = minValYJ;
            // wallUpperYJ = maxValYJ;
            // }
            // else if (minFormulaYJ != null && maxFormulaYJ != null) {
            // wallLowerYJ = minFormulaYJ;
            // wallUpperYJ = maxFormulaYJ;
            // }
            // }
            // }
            // if (wallUpper == null) {
            // wallUpperYJ = 0.0;
            // }
            // if (wallLower == null) {
            // wallLowerYJ = 0.0;
            // }
            String wholeBack = "";// 制程编号
            String wholeBackDesc = "";// 制程描述
            // 查询制程
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnProcessOrd.queryDesginMscpl");
            PreparedStatement psta = conn.prepareStatement(sql);
            psta.setString(1, desginKey);
            psta.setString(2, mscPline);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                wholeBack = rs.getString("WHOLE_BACKLOG");
                wholeBackDesc = rs.getString("WHOLE_BACKLOG_DESC");
            }
            rs.close();
            psta.close();
            // 工艺评审后内控标准
            List<DesignStdSicEntity> specs = new ArrayList<DesignStdSicEntity>();
            // 判断制程是否经过镦拔扩（经过取镦拔扩内控标准，不经过取轧管内控标准）
            if (!porcode.equals("D")) {// 如果不是轧管工序
                if (wholeBack != null && wholeBack != ""
                        && wholeBack.contains("E")) {
                    specs = getSpecBySicsDbk(conn, desginKey, mscPline);
                }
                else {
                    specs = getSpecBySics(conn, sic, desginKey, mscPline);// 
                }
            }
            else {
                specs = getSpecBySics(conn, sic, desginKey, mscPline);// 
            }
            if (specs == null) {
                return thickTolerance;
            }
            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("")) {
                    // if(Double.valueOf(stdMinVal) > 0){

                    // }

                    minVal = Double.valueOf(stdMinVal);
                }
                if (!stdMaxVal.equals("")) {
                    maxVal = Double.valueOf(stdMaxVal);
                    if (maxVal >= wallthick) {
                        maxVal = 0.25 * wallthick + minVal;
                    }
                }

                if (!stdMinPercent.equals("")) {
                    // minPercent = Math.abs(Double.valueOf(stdMinPercent) *
                    // wallthick
                    // * 0.01);
                    minPercent = Double.valueOf(stdMinPercent) * wallthick
                            * 0.01;
                }
                if (!stdMaxPercent.equals("")) {
                    maxPercent = Double.valueOf(stdMaxPercent) * wallthick
                            * 0.01;
                    if (maxPercent != null && maxPercent > wallthick) {
                        maxPercent = 0.25 * wallthick + minPercent;
                    }
                }

                if (!stdMinFormula.equals("") ) {//&& !stdMinFormula.equals("0")) {zhoujl 24-12-30出现上限是公式  下限是0
                    stdMinFormula = stdMinFormula.replaceAll("t", String
                            .valueOf(wallthick)); // 壁厚带入计算公式
                    stdMinFormula = stdMinFormula.replaceAll("D", String
                            .valueOf(outdiameter)); // 外径带入计算公式
                    minFormula = (Double) CorePlnComm.getResult(stdMinFormula);

                }
                if (!stdMaxFormula.equals("")) {// && !stdMaxFormula.equals("0")) {zhoujl 24-12-30出现上限是公式  下限是0
                    stdMaxFormula = stdMaxFormula.replaceAll("t", String
                            .valueOf(wallthick)); // 壁厚带入计算公式
                    stdMaxFormula = stdMaxFormula.replaceAll("D", String
                            .valueOf(outdiameter)); // 外径带入计算公式
                    maxFormula = (Double) CorePlnComm.getResult(stdMaxFormula);
                    if (maxFormula != null && maxFormula >= wallthick) {
                        maxFormula = 0.25 * wallthick + minFormula;
                    }
                }
                if (stdMinFormulaV != null && stdMinFormulaV.equals("1")) { // 取下限值
                    if (minPercent != null && maxPercent != null) {
                        wallLower = minPercent;
                        wallUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        wallLower = minVal;
                        wallUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        wallLower = minFormula;
                        wallUpper = maxFormula;
                    }

                    if (minVal != null && maxVal != null) {
                        wallUpper = Math.min(wallUpper, maxVal);
                        wallLower = Math.max(wallLower, minVal);
                    }
                    if (minFormula != null && maxFormula != null) {
                        wallUpper = Math.min(wallUpper, maxFormula);
                        wallLower = Math.max(wallLower, minFormula);
                    }
                }
                else if (stdMaxFormulaV != null && stdMaxFormulaV.equals("1")) {// 取上限值
                    if (minPercent != null && maxPercent != null) {
                        wallLower = minPercent;
                        wallUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        wallLower = minVal;
                        wallUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        wallLower = minFormula;
                        wallUpper = maxFormula;
                    }

                    if (minVal != null && maxVal != null) {
                        wallUpper = Math.max(wallUpper, maxVal);
                        wallLower = Math.min(wallLower, minVal);
                    }
                    if (minFormula != null && maxFormula != null) {
                        wallUpper = Math.max(wallUpper, maxFormula);
                        wallLower = Math.min(wallLower, minFormula);
                    }
                }
                else {
                    if (minPercent != null && maxPercent != null) {
                        wallLower = minPercent;
                        wallUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        wallLower = minVal;
                        wallUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        wallLower = minFormula;
                        wallUpper = maxFormula;
                    }
                }

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

            }

            if (wallUpper == null) {
                wallUpper = 0.0;
            }
            if (wallLower == null) {
                wallLower = 0.0;
            }
            // if (wallUpperYJ != wallUpper || wallLowerYJ != wallLower) {
            // // thickTolerance.
            // break;
            // // return thickTolerance;
            // }
            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;
    }

    /**
     * 查询内控标准，得到外径的公差
     * @param thickTolerance 外径公差实体类(内控标准)
     */
    public CoreTolerOfNkParam queryToleranceAim(
            CoreTolerOfNkParam thickTolerance, String desginKey,
            String mscPline, String porcode) {
        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 aimLower = null;
            Double aimUpper = null;

            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            String wholeBack = "";// 制程编号
            String wholeBackDesc = "";// 制程描述
            // 查询制程
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnProcessOrd.queryDesginMscpl");
            PreparedStatement psta = conn.prepareStatement(sql);
            psta.setString(1, desginKey);
            psta.setString(2, mscPline);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                wholeBack = rs.getString("WHOLE_BACKLOG");
                wholeBackDesc = rs.getString("WHOLE_BACKLOG_DESC");
            }
            rs.close();
            psta.close();
            // 工艺评审后内控标准
            List<DesignStdSicEntity> specs = new ArrayList<DesignStdSicEntity>();
            if (!porcode.equals("D")) {// 如果不是轧管工序
                // 判断制程是否经过镦拔扩（经过取镦拔扩内控标准，不经过取轧管内控标准）
                if (wholeBack != "" && wholeBack != null
                        && wholeBack.contains("E")) {
                    specs = getSpecBySicsDbk(conn, desginKey, mscPline);
                }
                else {
                    specs = getSpecBySics(conn, sic, desginKey, mscPline);// 
                }
            }
            else {
                specs = getSpecBySics(conn, sic, desginKey, mscPline);// 
            }

            if (specs == null) {
                return thickTolerance;
            }
            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(stdMinVal);
                }
                if (!stdMaxVal.equals("")) {
                    maxVal = Double.valueOf(stdMaxVal);
                }

                if (!stdMinPercent.equals("")) {
                    minPercent = Double.valueOf(stdMinPercent) * outdiameter
                            * 0.01;
                }
                if (!stdMaxPercent.equals("")) {
                    maxPercent = Double.valueOf(stdMaxPercent) * outdiameter
                            * 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.getResult(stdMinFormula);
                }
                if (!stdMaxFormula.equals("") && !stdMaxFormula.equals("0")) {
                    stdMaxFormula = stdMaxFormula.replaceAll("t", String
                            .valueOf(wallthick)); // 壁厚带入计算公式
                    stdMaxFormula = stdMaxFormula.replaceAll("D", String
                            .valueOf(outdiameter)); // 外径带入计算公式
                    maxFormula = (Double) CorePlnComm.getResult(stdMaxFormula);
                }

                if (stdMinFormulaV != null && stdMinFormulaV.equals("1")) { // 取下限值
                    if (minPercent != null && maxPercent != null) {
                        aimLower = minPercent;
                        aimUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        aimLower = minVal;
                        aimUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        aimLower = minFormula;
                        aimUpper = maxFormula;
                    }

                    if (minVal != null && maxVal != null) {
                        aimUpper = Math.min(aimUpper, maxVal);
                        aimLower = Math.max(aimLower, minVal);
                    }
                    if (minFormula != null && maxFormula != null) {
                        aimUpper = Math.min(aimUpper, maxFormula);
                        aimLower = Math.max(aimLower, minFormula);
                    }
                }
                else if (stdMaxFormulaV != null && stdMaxFormulaV.equals("1")) {// 取上限值
                    if (minPercent != null && maxPercent != null) {
                        aimLower = minPercent;
                        aimUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        aimLower = minVal;
                        aimUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        aimLower = minFormula;
                        aimUpper = maxFormula;
                    }

                    if (minVal != null && maxVal != null) {
                        aimUpper = Math.max(aimUpper, maxVal);
                        aimLower = Math.min(aimLower, minVal);
                    }
                    if (minFormula != null && maxFormula != null) {
                        aimUpper = Math.max(aimUpper, maxFormula);
                        aimLower = Math.min(aimLower, minFormula);
                    }
                }
                else {
                    if (minPercent != null && maxPercent != null) {
                        aimLower = minPercent;
                        aimUpper = maxPercent;
                    }
                    else if (minVal != null && maxVal != null) {
                        aimLower = minVal;
                        aimUpper = maxVal;
                    }
                    else if (minFormula != null && maxFormula != null) {
                        aimLower = minFormula;
                        aimUpper = maxFormula;
                    }
                }
                /*
                 * if (stdMinFormulaV != null && stdMinFormulaV.equals("1")) {
                 * // 取下限值 if (minPercent != null && maxPercent != null) {
                 * aimLower = minPercent; aimUpper = maxPercent; } if (minVal !=
                 * null && maxVal != null) { if (aimLower != null && aimUpper !=
                 * null) { if ((aimUpper + aimLower) > (maxVal + minVal)) {
                 * aimLower = minVal; aimUpper = maxVal; } } else { aimLower =
                 * minVal; aimUpper = maxVal; } } if (minFormula != null &&
                 * maxFormula != null) { if (aimLower != null && aimUpper !=
                 * null) { if ((aimUpper + aimLower) > (maxFormula +
                 * minFormula)) { aimLower = minFormula; aimUpper = maxFormula;
                 * } } else { aimLower = minFormula; aimUpper = maxFormula; } }
                 * } else if (stdMaxFormulaV != null &&
                 * stdMaxFormulaV.equals("1")) { // 取上限值 if (minPercent != null
                 * && maxPercent != null) { aimLower = minPercent; aimUpper =
                 * maxPercent; } if (minVal != null && maxVal != null) { if
                 * (aimLower != null && aimUpper != null) { if ((aimUpper +
                 * aimLower) < (maxVal + minVal)) { aimLower = minVal; aimUpper
                 * = maxVal; } } else { aimLower = minVal; aimUpper = maxVal; }
                 * } if (minFormula != null && maxFormula != null) { if
                 * (aimLower != null && aimUpper != null) { if ((aimUpper +
                 * aimLower) < (maxFormula + minFormula)) { aimLower =
                 * minFormula; aimUpper = maxFormula; } } else { aimLower =
                 * minFormula; aimUpper = maxFormula; } } } else { if
                 * (minPercent != null && maxPercent != null) { aimLower =
                 * minPercent; aimUpper = maxPercent; } else if (minVal != null
                 * && maxVal != null) { aimLower = minVal; aimUpper = maxVal; }
                 * else if (minFormula != null && maxFormula != null) { aimLower
                 * = minFormula; aimUpper = maxFormula; } }
                 */
            }

            if (aimUpper == null) {
                aimUpper = 0.0;
            }
            if (aimLower == null) {
                aimLower = 0.0;
            }

            thickTolerance.setAimLower(aimLower);
            thickTolerance.setAimUpper(aimUpper);
        }
        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;
    }

    /**
     * 根据公差索引号查询内控公差标准(轧管)
     * @param con 连接字符串
     * @param sic 公差索引号
     * @return List<DesignStdSicEntity>
     * @throws SQLException
     */
    public List<DesignStdSicEntity> getSpecBySics(Connection con, String sic,
            String desginKey, String mscPline) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderStdSic.getSpecBySic");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, desginKey);
        ps.setObject(4, mscPline);
        ResultSet rs = ps.executeQuery();
        List<DesignStdSicEntity> list = EntityTool.getEntitysByRs(rs,
                DesignStdSicEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (DesignStdSicEntity spec : list) {
                QualityDesignTool.nullConverNullString(spec);
            }
            return list;
        }
        else {
            return null;
        }
    }

    /**
     * 根据公差索引号查询内控公差标准(镦拔扩)
     * @param con 连接字符串
     * @param sic 公差索引号
     * @return List<DesignStdSicEntity>
     * @throws SQLException
     */
    public List<DesignStdSicEntity> getSpecBySicsDbk(Connection con,
            String desginKey, String mscPline) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderStdSic.getSpecBySicsDbk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, desginKey);
        ps.setObject(4, mscPline);
        ResultSet rs = ps.executeQuery();
        List<DesignStdSicEntity> list = EntityTool.getEntitysByRs(rs,
                DesignStdSicEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (DesignStdSicEntity spec : list) {
                QualityDesignTool.nullConverNullString(spec);
            }
            return list;
        }
        else {
            return null;
        }
    }
}