package com.steering.pss.plan.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.plan.base.CorePlnComm;

/**
 * 合同交货标准公差
 * @author xiaohai
 */
public class CoreOrderStdSic extends DBCommon {

    /**
     * 获取公差
     * @return
     */
    public CoreReturnObject getStdTolerance(String desginKey, String biasCode,
            String speclFl, Double outdiameter, Double wallthick)
            throws ScriptException {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            ArrayList<Double> result = new ArrayList<Double>();
            Double wallLower = null;
            Double wallUpper = null;

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderStdSic.getStdTolerance");
            psta = conn.prepareStatement(sql);
            psta.setString(1, biasCode);
            psta.setString(2, speclFl);
            psta.setString(3, 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 = Double.valueOf(stdMinVal);
                }

                if (rs.getString("STD_MAX_VAL") != null) {
                    String stdMaxVal = "";
                    stdMaxVal = rs.getString("STD_MAX_VAL");// 标准正值(mm)
                    maxVal = Double.valueOf(stdMaxVal);
                }

                if (rs.getString("STD_MIN_PERCENT") != null) {
                    String stdMinPercent = "";
                    stdMinPercent = rs.getString("STD_MIN_PERCENT"); // 标准负值(%)
                    if (!stdMinPercent.trim().equals("")) {
                        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 = Double.valueOf(stdMaxPercent) * wallthick
                                * 0.01;
                    }
                }

                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 = (Double) CorePlnComm
                                .countFormula(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 = (Double) CorePlnComm
                                .countFormula(stdMaxFormula);
                    }
                }

                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 (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;
            }

            result.add(wallUpper);
            result.add(-wallLower);
            cro.setResult(result);
        }
        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 cro;
    }
}