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 javax.script.ScriptException;

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.base.CorePlnComm;

/**
 * 合同交货标准公差
 * @author xiaohai
 */
public class CoreOrderStdSic extends DBCommon {
    /**
     * 获取交货标准公差
     * @param desginKey
     * @param biasCode
     * @param speclFl
     * @param outdiameter
     * @param wallthick
     * @return
     * @throws ScriptException
     */
    public CoreReturnObject getStdTolerance(String desginKey,
            Double outdiameter, Double wallthick) throws ScriptException {
        CoreReturnObject cro = new CoreReturnObject();

        ArrayList<Double> result = new ArrayList<Double>();
        Double[] rs = getStdTole(desginKey, outdiameter, wallthick);
        result.add(rs[0]);
        result.add(rs[1]);
        cro.setResult(result);
        return cro;
    }

    /**
     * 获取交货标准公差
     * @param desginKey 质量设计
     * @param outdiameter
     * @param wallthick
     * @return Double[0] 公差上限,Double[1] 公差下限
     * @throws ScriptException
     */
    public Double[] getStdTole(String desginKey, Double outdiameter,
            Double wallthick) {
        Double[] stdTolerance = null;
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            stdTolerance = getConnStdTole(conn, desginKey, outdiameter,
                    wallthick);
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        catch (ScriptException 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 stdTolerance;
    }

    /**
     * 获取交货标准公差
     * @param conn
     * @param desginKey
     * @param outdiameter
     * @param wallthick
     * @return Double[0] 公差上限,Double[1] 公差下限
     * @throws SQLException
     * @throws ScriptException
     */
    public static Double[] getConnStdTole(Connection conn, String desginKey,
            Double outdiameter, Double wallthick) throws SQLException,
            ScriptException {
        Double[] stdTolerance = new Double[2];
        PreparedStatement psta = null;

        Double wallLower = null;
        Double wallUpper = null;

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderStdSic.getStdTolerance");
        psta = conn.prepareStatement(sql);
        psta.setString(1, desginKey);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            Double minVal = null;
            Double maxVal = null;
            Double minPercent = null;
            Double maxPercent = null;
            Double minFormula = null;
            Double maxFormula = null;
            String stdMinFormulaV = "";
            String stdMaxFormulaV = "";
            if (rs.getString("STD_MIN_VAL") != null) {
                String stdMinVal = "";
                stdMinVal = rs.getString("STD_MIN_VAL");// 标准负值(mm)
                //minVal = Math.abs(Double.valueOf(stdMinVal));
                minVal = Double.valueOf(stdMinVal);
            }

            if (rs.getString("STD_MAX_VAL") != null) {
                String stdMaxVal = "";
                stdMaxVal = rs.getString("STD_MAX_VAL");// 标准正值(mm)
                //maxVal = Math.abs(Double.valueOf(stdMaxVal));
                maxVal = Double.valueOf(stdMaxVal);
                if (maxVal >= wallthick) {
                    //maxVal = 0.25 * wallthick - Math.abs(minVal);
                	maxVal = 0.25 * wallthick + minVal;
                }
            }

            if (rs.getString("STD_MIN_PERCENT") != null) {
                String stdMinPercent = "";
                stdMinPercent = rs.getString("STD_MIN_PERCENT"); // 标准负值(%)
                if (!stdMinPercent.trim().equals("")) {
                    //minPercent = Math.abs(Double.valueOf(stdMinPercent)* wallthick * 0.01);
                	minPercent = Double.valueOf(stdMinPercent)* wallthick * 0.01;
                }
            }

            if (rs.getString("STD_MAX_PERCENT") != null) {
                String stdMaxPercent = "";
                stdMaxPercent = rs.getString("STD_MAX_PERCENT"); // 标准正值(%)
                if (!stdMaxPercent.trim().equals("")) {
                    //maxPercent = Math.abs(Double.valueOf(stdMaxPercent) * wallthick * 0.01);
                	maxPercent = Double.valueOf(stdMaxPercent) * wallthick * 0.01;
                }
                if (maxPercent != null && maxPercent >= wallthick) {
                    //maxPercent = 0.25 * wallthick - Math.abs(minPercent);
                	maxPercent = 0.25 * wallthick + minPercent;
                }
            }

            if (rs.getString("STD_MIN_FORMULA") != null) {
                String stdMinFormula = "";
                stdMinFormula = rs.getString("STD_MIN_FORMULA"); // 标准公式负值(mm)
                if (!stdMinFormula.trim().equals("")
                        //&& !stdMinFormula.equals("0")
                        ) {
                    stdMinFormula = stdMinFormula.replaceAll("t",
                            String.valueOf(wallthick)); // 壁厚带入计算公式
                    stdMinFormula = stdMinFormula.replaceAll("D",
                            String.valueOf(outdiameter)); // 外径带入计算公式
                    //minFormula = Math.abs((Double) CorePlnComm.countFormula(stdMinFormula));
                    minFormula = (Double) CorePlnComm.getResult(stdMinFormula);
                }
            }

            if (rs.getString("STD_MAX_FORMULA") != null) {
                String stdMaxFormula = "";
                stdMaxFormula = rs.getString("STD_MAX_FORMULA"); // 标准公式正值(mm)
                if (!stdMaxFormula.trim().equals("")
                        //&& !stdMaxFormula.equals("0")
                		) {
                    stdMaxFormula = stdMaxFormula.replaceAll("t",
                            String.valueOf(wallthick)); // 壁厚带入计算公式
                    stdMaxFormula = stdMaxFormula.replaceAll("D",
                            String.valueOf(outdiameter)); // 外径带入计算公式
                    //maxFormula = Math.abs((Double) CorePlnComm.countFormula(stdMaxFormula));
                    maxFormula = (Double) CorePlnComm.getResult(stdMaxFormula);
                }
                if (maxFormula != null && maxFormula >= wallthick) {
                    //maxFormula = 0.25 * wallthick - Math.abs(minFormula);
                	maxFormula = 0.25 * wallthick + minFormula;
                }
            }

            if (rs.getString("STD_MIN_FORMULA_V") != null) {
                stdMinFormulaV = rs.getString("STD_MIN_FORMULA_V");// 取小值值
            }

            if (rs.getString("STD_MAX_FORMULA_V") != null) {
                stdMaxFormulaV = rs.getString("STD_MAX_FORMULA_V");// 取最大值
            }

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

        stdTolerance[0] = wallUpper;
        stdTolerance[1] = wallLower;
        return stdTolerance;
    }

    /**
     * 获取交货标准外径公差
     * @param desginKey
     * @param biasCode
     * @param speclFl
     * @param outdiameter
     * @param wallthick
     * @return
     * @throws ScriptException
     */
    public CoreReturnObject getStdOutDiameter(String desginKey,
            Double outdiameter, Double wallthick) throws ScriptException {
        CoreReturnObject cro = new CoreReturnObject();

        ArrayList<Double> result = new ArrayList<Double>();
        Double[] rs = getStdOutD(desginKey, outdiameter, wallthick);
        result.add(rs[0]);
        result.add(rs[1]);
        cro.setResult(result);
        return cro;
    }

    /**
     * 获取交货标准外径公差
     * @param desginKey 质量设计
     * @param outdiameter
     * @param wallthick
     * @return Double[0] 公差上限,Double[1] 公差下限
     * @throws ScriptException
     */
    public Double[] getStdOutD(String desginKey, Double outdiameter,
            Double wallthick) {
        Double[] stdTolerance = null;
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            stdTolerance = getConnStdOutD(conn, desginKey, outdiameter,
                    wallthick);
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        catch (ScriptException 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 stdTolerance;
    }
    
    /**
     * 获取交货标准外径公差
     * @param conn
     * @param desginKey
     * @param outdiameter
     * @param wallthick
     * @return Double[0] 公差上限,Double[1] 公差下限
     * @throws SQLException
     * @throws ScriptException
     */
    public static Double[] getConnStdOutD(Connection conn, String desginKey,
            Double outdiameter, Double wallthick) throws SQLException,
            ScriptException {
        Double[] stdTolerance = new Double[2];
        PreparedStatement psta = null;

        Double wallLower = null;
        Double wallUpper = null;

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderStdSic.getStdOutDiameter");
        psta = conn.prepareStatement(sql);
        psta.setString(1, desginKey);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            Double minVal = null;
            Double maxVal = null;
            Double minPercent = null;
            Double maxPercent = null;
            Double minFormula = null;
            Double maxFormula = null;
            String stdMinFormulaV = "";
            String stdMaxFormulaV = "";
            if (rs.getString("STD_MIN_VAL") != null) {
                String stdMinVal = "";
                stdMinVal = rs.getString("STD_MIN_VAL");// 标准负值(mm)
                //minVal = Math.abs(Double.valueOf(stdMinVal));
                minVal = Double.valueOf(stdMinVal);
            }

            if (rs.getString("STD_MAX_VAL") != null) {
                String stdMaxVal = "";
                stdMaxVal = rs.getString("STD_MAX_VAL");// 标准正值(mm)
                //maxVal = Math.abs(Double.valueOf(stdMaxVal));
                maxVal = Double.valueOf(stdMaxVal);
                /*if (maxVal >= outdiameter) {
                    //maxVal = 0.25 * outdiameter - Math.abs(minVal);
                	maxVal = 0.25 * outdiameter + minVal;
                }*/
            }

            if (rs.getString("STD_MIN_PERCENT") != null) {
                String stdMinPercent = "";
                stdMinPercent = rs.getString("STD_MIN_PERCENT"); // 标准负值(%)
                if (!stdMinPercent.trim().equals("")) {
                    //minPercent = Math.abs(Double.valueOf(stdMinPercent)* outdiameter * 0.01);
                	minPercent = Double.valueOf(stdMinPercent)* outdiameter * 0.01;
                }
            }

            if (rs.getString("STD_MAX_PERCENT") != null) {
                String stdMaxPercent = "";
                stdMaxPercent = rs.getString("STD_MAX_PERCENT"); // 标准正值(%)
                if (!stdMaxPercent.trim().equals("")) {
                    //maxPercent = Math.abs(Double.valueOf(stdMaxPercent) * outdiameter * 0.01);
                	maxPercent = Double.valueOf(stdMaxPercent) * outdiameter * 0.01;
                }
                /*if (maxPercent != null && maxPercent >= outdiameter) {
                    //maxPercent = 0.25 * outdiameter - Math.abs(minPercent);
                	maxPercent = 0.25 * outdiameter + minPercent;
                }*/
            }

            if (rs.getString("STD_MIN_FORMULA") != null) {
                String stdMinFormula = "";
                stdMinFormula = rs.getString("STD_MIN_FORMULA"); // 标准公式负值(mm)
                if (!stdMinFormula.trim().equals("")
                        && !stdMinFormula.equals("0")) {
                    stdMinFormula = stdMinFormula.replaceAll("t",
                            String.valueOf(wallthick)); // 壁厚带入计算公式
                    stdMinFormula = stdMinFormula.replaceAll("D",
                            String.valueOf(outdiameter)); // 外径带入计算公式
                    //minFormula = Math.abs((Double) CorePlnComm.countFormula(stdMinFormula));
                    minFormula = (Double) CorePlnComm.getResult(stdMinFormula);
                }
            }

            if (rs.getString("STD_MAX_FORMULA") != null) {
                String stdMaxFormula = "";
                stdMaxFormula = rs.getString("STD_MAX_FORMULA"); // 标准公式正值(mm)
                if (!stdMaxFormula.trim().equals("")
                        && !stdMaxFormula.equals("0")) {
                    stdMaxFormula = stdMaxFormula.replaceAll("t",
                            String.valueOf(wallthick)); // 壁厚带入计算公式
                    stdMaxFormula = stdMaxFormula.replaceAll("D",
                            String.valueOf(outdiameter)); // 外径带入计算公式
                    //maxFormula = Math.abs((Double) CorePlnComm.countFormula(stdMaxFormula));
                    maxFormula = (Double) CorePlnComm.getResult(stdMaxFormula);
                }
                /*if (maxFormula != null && maxFormula >= outdiameter) {
                    //maxFormula = 0.25 * outdiameter - Math.abs(minFormula);
                	maxFormula = 0.25 * outdiameter + minFormula;
                }*/
            }

            if (rs.getString("STD_MIN_FORMULA_V") != null) {
                stdMinFormulaV = rs.getString("STD_MIN_FORMULA_V");// 取小值值
            }

            if (rs.getString("STD_MAX_FORMULA_V") != null) {
                stdMaxFormulaV = rs.getString("STD_MAX_FORMULA_V");// 取最大值
            }

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

        stdTolerance[0] = wallUpper;
        stdTolerance[1] = wallLower;
        return stdTolerance;
    }

}