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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.ordAmCal.PlnProducCal;
import com.steering.pss.plnsaleord.ordAmCal.model.InOrdParamEntity;
import com.steering.pss.plnsaleord.ordAmCal.model.OutOrdParamEntity;
import com.steering.pss.plnsaleord.order.model.CoreTolerOfNkParam;
import com.steering.pss.plnsaleord.order.model.PlnPorderBlReqEntity;
import com.steering.pss.plnsaleord.order.model.PlnSaleordProcEntity;

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

public class CoreGetOrdAmount extends DBCommon {
    /**
     * 获取产线名称
     * @param plineCode
     * @return
     */
    public CoreReturnObject getPlineNameByCode(String plineCode) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreGetOrdAmount.getPlineNameByCode",
                new Object[] { plineCode });
        return cro;
    }

    /**
     * 合同量转换
     * @param inEntity
     * @return
     */
    public CoreReturnObject getOrdCalResult(InOrdParamEntity inEntity) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        OutOrdParamEntity outEntity = PlnProducCal.calculate(conn, inEntity);
        cro.setResult(outEntity);
        return cro;
    }

    /**
     * 查询合同量转换参数
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws ScriptException
     */
    public CoreReturnObject getInOrdParam(String desginKey, String mscPline,
            String ordLnDlyPk, String plineCodeD, String bom, String codeJg,
            Double putRate) {
        CoreReturnObject cro = new CoreReturnObject();
        InOrdParamEntity inOrdParam = null;
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            inOrdParam = getInOrdEntity(conn, desginKey, mscPline, ordLnDlyPk,
                    plineCodeD, bom, codeJg, putRate);
            cro.setResult(inOrdParam);
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询合同量转换参数
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plnDivideId 排产流水号
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws ScriptException
     */
    public CoreReturnObject getInOrdPlnParam(String desginKey, String mscPline,
            String ordLnDlyPk, String plnDivideId, String plineCodeD,
            String bom, String codeJg, Double putRate) {
        CoreReturnObject cro = new CoreReturnObject();
        InOrdParamEntity inOrdParam = null;
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            inOrdParam = getInOrdPlnEntity(conn, desginKey, mscPline,
                    ordLnDlyPk, plnDivideId, plineCodeD, bom, codeJg, putRate);
            inOrdParam.setDesginKey(desginKey);
            inOrdParam.setIndexSeq(mscPline);
            cro.setResult(inOrdParam);
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询合同量转换参数
     * @param orderQty 订货量
     * @param orderUnit 订货单位
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws ScriptException
     */
    public CoreReturnObject getInOrdPlnQtyParam(Double orderQty,
            String orderUnit, String desginKey, String mscPline,
            String ordLnDlyPk, String plineCodeD, String bom, String codeJg,
            Double putRate) {
        CoreReturnObject cro = new CoreReturnObject();
        InOrdParamEntity inOrdParam = null;
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            inOrdParam = getInOrdPlnQtyEntity(conn, orderQty, orderUnit,
                    desginKey, mscPline, ordLnDlyPk, plineCodeD, bom, codeJg,
                    putRate);
            inOrdParam.setDesginKey(desginKey);
            inOrdParam.setIndexSeq(mscPline);
            cro.setResult(inOrdParam);
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询合同量转换参数
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws SQLException
     * @throws ScriptException
     */
    public static InOrdParamEntity getInOrdEntity(Connection conn,
            String desginKey, String mscPline, String ordLnDlyPk,
            String plineCodeD, String bom, String codeJg, double putRate)
            throws SQLException {
        InOrdParamEntity inOrdParam = new InOrdParamEntity();
        String dInMaterialNo = "";
        String dOutMaterialNo = "";
        String eInMaterialNo = "";
        String eOutMaterialNo = "";
        PreparedStatement ps = null;
        double cutMax = 0;// 最大分切数
        double tubeEndRatio = 0;// 管端系数
        String wholeBacklog = "";
        String wholeBacklogDesc = "";
        inOrdParam.setDesginKey(desginKey);
        inOrdParam.setIndexSeq(mscPline);
        inOrdParam.setPutRate(putRate);
        // 查询交货行信息
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getPlnSaleOrdInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            String lenUnit = "";// 长度单位
            if (rs.getString("LEN_UNIT") != null) {
                lenUnit = rs.getString("LEN_UNIT");
            }
            inOrdParam.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
            inOrdParam.setOrderQty(rs.getDouble("ORDER_QTY"));
            if (rs.getString("ORDER_UNIT") != null) {
                inOrdParam.setOrderUnit(rs.getString("ORDER_UNIT"));
            }
            inOrdParam.setTotlength(rs.getDouble("TOTLENGTH"));
            inOrdParam.setTotlengthM(rs.getDouble("TOTLENGTH_M"));
            if (rs.getString("TOTLENGTH_UNIT") != null) {
                inOrdParam.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
            }
            inOrdParam.setLengthMax(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMAX")));
            inOrdParam.setLengthMin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMIN")));
            inOrdParam.setOutdiameter(rs.getDouble("OUTDIAMETER"));
            inOrdParam.setWallthick(rs.getDouble("WALLTHICK"));
            if (rs.getString("MODEL_DESC") != null) {
                inOrdParam.setModelDesc(rs.getString("MODEL_DESC"));
            }
            if (rs.getString("ORDER_UNIT") != null
                    && (rs.getString("ORDER_UNIT").trim().equals("吨") || rs
                            .getString("ORDER_UNIT").trim().equals("英磅"))) {
                if (rs.getString("WEIGH_TYPE") != null
                        && rs.getString("WEIGH_TYPE").contains("理")) {
                    inOrdParam.setWeightType(0);
                }
                else {
                    inOrdParam.setWeightType(1);
                }
            }
            else {
                inOrdParam.setWeightType(-1);
            }
            inOrdParam.setDelvryRangeMax(rs.getDouble("DELVRY_RANGE_MAX"));
            inOrdParam.setDelvryRangeMin(rs.getDouble("DELVRY_RANGE_MIN"));
            if (rs.getString("DELVRY_RANGE_TPE") != null
                    && rs.getString("DELVRY_RANGE_TPE").trim().equals("绝对值")) {
                inOrdParam.setDelvryRangeTpe(2);
            }
            else {
                inOrdParam.setDelvryRangeTpe(1);
            }
            inOrdParam.setSigllen(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SIGL_LEN")));
            inOrdParam.setFootmin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SHORTEST")));
            if (rs.getString("SHRT_STOVE") != null
                    && !rs.getString("SHRT_STOVE").equals("")
                    && rs.getString("SHRT_STOVE").equals("/")) {
                inOrdParam.setShrtStove(true);
            }
            else {
                inOrdParam.setShrtStove(false);
            }
            inOrdParam.setInspectLen(rs.getDouble("INSPECTION_VALUE"));

            // 获取交货标准壁厚公差
            CoreOrderStdSic stdTolerance = new CoreOrderStdSic();
            Double[] stdTole = stdTolerance.getStdTole(desginKey, rs
                    .getDouble("OUTDIAMETER"), rs.getDouble("WALLTHICK"));
            if (stdTole != null) {
                inOrdParam.setStdWallthickMax(stdTole[0]);
                inOrdParam.setStdWallthickMin(stdTole[1]);
            }

            // 获取交货标准外径公差
            Double[] stdOutD = stdTolerance.getStdOutD(desginKey, rs
                    .getDouble("OUTDIAMETER"), rs.getDouble("WALLTHICK"));
            if (stdOutD != null) {
                inOrdParam.setOutdOrdMax(stdOutD[0]);
                inOrdParam.setOutdOrdMin(stdOutD[1]);
            }
            inOrdParam.setAvgLenMin(rs.getDouble("AVG_LEN_MIN"));
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询BOM轧管规格，坯料规格，轧管、镦拔扩投入产出物料编码
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getBomZgSpecInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, bom);
        rs = ps.executeQuery();
        if (rs.next()) {
            List<Double> dimianPl = new ArrayList<Double>();
            dimianPl.add(rs.getDouble("SPEC_BILT"));
            inOrdParam.setDiameterZg(rs.getDouble("DIMATER"));
            inOrdParam.setHeightZg(rs.getDouble("HEIGHT"));
            inOrdParam.setDiameterPl(dimianPl);
            if (rs.getString("D_IN_MATERIAL_NO") != null) {
                dInMaterialNo = rs.getString("D_IN_MATERIAL_NO");
            }
            if (rs.getString("D_OUT_MATERIAL_NO") != null) {
                dOutMaterialNo = rs.getString("D_OUT_MATERIAL_NO");
            }
            if (rs.getString("E_IN_MATERIAL_NO") != null) {
                eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
            }
            if (rs.getString("E_OUT_MATERIAL_NO") != null) {
                eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
            }
            wholeBacklog=rs.getString("WHOLE_BACKLOG");
            wholeBacklogDesc=rs.getString("WHOLE_BACKLOG_DESC");
            inOrdParam.setWholeBacklog(wholeBacklog);
            inOrdParam.setWholeBacklogDesc(wholeBacklogDesc);
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询接箍信息
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.getComMscJg");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, codeJg);
        rs = ps.executeQuery();
        if (rs.next()) {
            inOrdParam.setWeightJg(rs.getDouble("WEIGHT_JG"));
            inOrdParam.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
        }
        rs.close();
        ps.close();

        // /查询轧管内控壁厚公差标准索引号
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.queryMscPlPrc");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, "D");
        rs = ps.executeQuery();
        if (rs.next()) {
            String sicZg = "";
            if (rs.getString("SIC") != null) {
                sicZg = rs.getString("SIC");
                if (sicZg.contains(",")) {
                    String[] sicList = sicZg.split(",");
                    if (sicList.length > 0) {
                        sicZg = sicList[0];
                    }
                }
            }

            // 查询轧管工序切头尾
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialDPl");
            PreparedStatement psta = conn.prepareStatement(sql);
            psta.setObject(1, dInMaterialNo);
            psta.setObject(2, dOutMaterialNo);
            psta.setObject(3, plineCodeD);
            ResultSet result = psta.executeQuery();
            if (result.next()) {
                double cutHeadEnd = result.getDouble("CROP_END")
                        + result.getDouble("CROP_HEAD");
                tubeEndRatio = result.getDouble("TUBE_END_RATIO");
                cutMax = result.getDouble("DBL_LEN_MAX");
                // 切头尾默认1000mm
                if (cutHeadEnd == 0) {
                    cutHeadEnd = 1000;
                }
                inOrdParam.setCutHeadEnd(cutHeadEnd);
                inOrdParam.setTubeEndRatio(tubeEndRatio);
                inOrdParam.setCutMax(cutMax);
            }
            result.close();
            psta.close();
            // 查询轧管产线基础信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getZgBasePline");
            psta = conn.prepareStatement(sql);
            psta.setObject(1, plineCodeD);
            result = psta.executeQuery();
            if (result.next()) {
                inOrdParam.setLengthColdbed(result.getDouble("LENGTH_COLDBED"));
                inOrdParam
                        .setLengthMaxRing(result.getDouble("LENGTH_MAX_RING"));
                inOrdParam.setBurnoutRate(result.getDouble("BURNOUT_RATE"));
                inOrdParam.setPlineWallthick(result.getDouble("TOLERANCE_ZONE")
                        * inOrdParam.getWallthick() / 100);
                inOrdParam.setPlineLenMin(result.getDouble("LENGTH_MIN"));
                inOrdParam.setPlineLenMax(result.getDouble("LENGTH_MAX"));
                inOrdParam.setToleranceZone(result.getDouble("TOLERANCE_ZONE"));
                inOrdParam
                        .setLengthMinTube(result.getDouble("LENGTH_MIN_TUBE"));
                inOrdParam
                        .setLengthMaxTube(result.getDouble("LENGTH_MAX_TUBE"));
            }
            result.close();
            psta.close();

            // 查询镦拔扩工序加长
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialEPl");
            psta = conn.prepareStatement(sql);
            psta.setObject(1, eInMaterialNo);
            psta.setObject(2, eOutMaterialNo);
            psta.setObject(3, desginKey);
            psta.setObject(4, mscPline);
            result = psta.executeQuery();
            if (result.next()) {
                double cutHeadEnd = result.getDouble("CROP_END")
                        + result.getDouble("CROP_HEAD");
                double lenDc = result.getDouble("UPSETTING_LENGTH");
                double burnoutRate = result.getDouble("BURNOUT_RATE");
                String plineCode = result.getString("PLINE_CODE");
                // 默认值
                if (plineCode.trim().equals("C021")
                        || plineCode.trim().equals("C067")) {
                    if (lenDc == 0) {
                        lenDc = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                else {
                    if (cutHeadEnd == 0) {
                        cutHeadEnd = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                // 经过镦拔扩并且是镦粗的判断基础数据
//                if (wholeBacklog != null && wholeBacklog != ""
//                        && wholeBacklog.contains("E")) {
//                    if (wholeBacklogDesc.contains("镦粗")) {
//                        if (result.getString("UPSETTING_LENGTH") == null
//                                || result.getString("UPSETTING_LENGTH").equals(
//                                        "")) {
//                            inOrdParam.setErrCode(-1);
//                            inOrdParam.setErrMessage("镦粗加长为空,请维护基础数据！");
//                            return inOrdParam;
//                        }
//                        if (result.getString("CROP_HEAD") == null
//                                || result.getString("CROP_HEAD").equals("")) {
//                            inOrdParam.setErrCode(-1);
//                            inOrdParam.setErrMessage("镦拔扩切头为空,请维护基础数据！");
//                            return inOrdParam;
//                        }
//                        if (result.getString("CROP_END") == null
//                                || result.getString("CROP_END").equals("")) {
//                            inOrdParam.setErrCode(-1);
//                            inOrdParam.setErrMessage("镦拔扩切尾为空,请维护基础数据！");
//                            return inOrdParam;
//                        }
//                    }
//                }
                inOrdParam.setBurnoutDbk(burnoutRate);
                inOrdParam.setAddLengthDbk(cutHeadEnd);
                inOrdParam.setAddLengthDc(lenDc);
            }
            result.close();
            psta.close();
            inOrdParam.setOrdLnDlyPk(ordLnDlyPk);
            // 轧管内控壁厚公差
            CoreTolerOfNk tolerOfNk = new CoreTolerOfNk();
            CoreTolerOfNkParam nkParam = new CoreTolerOfNkParam();
            nkParam.setBias_code("404019");
            nkParam.setOutdiameter(inOrdParam.getDiameterZg());
            nkParam.setWallthick(inOrdParam.getHeightZg());
            nkParam.setSic(sicZg);
            nkParam.setLengthmax(inOrdParam.getLengthMax());
            nkParam.setLengthmin(inOrdParam.getLengthMin());
            nkParam = tolerOfNk.queryTolerance(nkParam, desginKey, mscPline,
                    "1");
            if (nkParam != null) {
                double wallUpper = nkParam.getWallUpper();
                double wallLower = nkParam.getWallLower();
                if (wallUpper == 0 && wallLower == 0) {
                    // wallUpper = inOrdParam.getStdWallthickMax();
                    // wallLower = inOrdParam.getStdWallthickMin();
                    wallUpper = 0;
                    wallLower = 0;
                }
                inOrdParam.setNkWallthickMax(wallUpper);
                inOrdParam.setNkWallthickMin(wallLower);
            }
            // 轧管内外径公差
            nkParam.setBias_code("404012");
            nkParam.setOutdiameter(inOrdParam.getDiameterZg());
            nkParam.setWallthick(inOrdParam.getHeightZg());
            nkParam.setSic(sicZg);
            nkParam.setLengthmax(inOrdParam.getLengthMax());
            nkParam.setLengthmin(inOrdParam.getLengthMin());
            nkParam = tolerOfNk.queryToleranceAim(nkParam, desginKey, mscPline,
                    "1");
            if (nkParam != null) {
                double aimUpper = nkParam.getAimUpper();
                double aimLower = nkParam.getAimLower();
                if (aimUpper == 0 && aimLower == 0) {
                    // aimUpper = inOrdParam.getOutdOrdMax();
                    // aimLower = inOrdParam.getOutdOrdMin();
                    aimUpper = 0;
                    aimLower = 0;
                }
                inOrdParam.setNkAimthickMax(aimUpper);
                inOrdParam.setNkAimthickMin(aimLower);
            }
        }
        rs.close();
        ps.close();
        conn.commit();
        return inOrdParam;
    }

    /**
     * 查询合同量转换参数
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plnDivideId 排产流水号
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws SQLException
     * @throws ScriptException
     */
    public static InOrdParamEntity getInOrdPlnEntity(Connection conn,
            String desginKey, String mscPline, String ordLnDlyPk,
            String plnDivideId, String plineCodeD, String bom, String codeJg,
            double putRate) throws SQLException {
        InOrdParamEntity inOrdParam = new InOrdParamEntity();
        PreparedStatement ps = null;
        String dInMaterialNo = "";
        String dOutMaterialNo = "";
        String eInMaterialNo = "";
        String eOutMaterialNo = "";
        double cutMax = 0;// 最大分切数
        double tubeEndRatio = 0;// 管端系数
        inOrdParam.setPutRate(putRate);
        // 查询交货行信息
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getPlnSaleOrdDivideInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, plnDivideId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            String lenUnit = "";// 长度单位
            if (rs.getString("LEN_UNIT") != null) {
                lenUnit = rs.getString("LEN_UNIT");
            }
            inOrdParam.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
            inOrdParam.setOrderQty(rs.getDouble("ORDER_QTY_D"));
            if (rs.getString("ORDER_UNIT") != null) {
                inOrdParam.setOrderUnit(rs.getString("ORDER_UNIT"));
            }
            inOrdParam.setTotlength(rs.getDouble("TOTLENGTH"));
            if (rs.getString("TOTLENGTH_UNIT") != null) {
                inOrdParam.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
            }
            inOrdParam.setLengthMax(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMAX")));
            inOrdParam.setLengthMin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMIN")));
            inOrdParam.setOutdiameter(rs.getDouble("OUTDIAMETER"));
            inOrdParam.setWallthick(rs.getDouble("WALLTHICK"));
            if (rs.getString("MODEL_DESC") != null) {
                inOrdParam.setModelDesc(rs.getString("MODEL_DESC"));
            }
            if (rs.getString("ORDER_UNIT") != null
                    && (rs.getString("ORDER_UNIT").trim().equals("吨") || rs
                            .getString("ORDER_UNIT").trim().equals("英磅"))) {
                if (rs.getString("WEIGH_TYPE") != null
                        && rs.getString("WEIGH_TYPE").contains("理")) {
                    inOrdParam.setWeightType(0);
                }
                else {
                    inOrdParam.setWeightType(1);
                }
            }
            else {
                inOrdParam.setWeightType(-1);
            }
            inOrdParam.setDelvryRangeMax(rs.getDouble("DELVRY_RANGE_MAX"));
            inOrdParam.setDelvryRangeMin(rs.getDouble("DELVRY_RANGE_MIN"));
            if (rs.getString("DELVRY_RANGE_TPE") != null
                    && rs.getString("DELVRY_RANGE_TPE").trim().equals("绝对值")) {
                inOrdParam.setDelvryRangeTpe(2);
            }
            else {
                inOrdParam.setDelvryRangeTpe(1);
            }
            inOrdParam.setSigllen(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SIGL_LEN")));
            inOrdParam.setFootmin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SHORTEST")));
            if (rs.getDouble("SHRT_LEN_RATO") > 0) {
                inOrdParam.setShrtStove(true);
            }
            else {
                inOrdParam.setShrtStove(false);
            }
            inOrdParam.setInspectLen(rs.getDouble("INSPECTION_VALUE"));

            // 获取交货标准壁厚公差
            CoreOrderStdSic stdTolerance = new CoreOrderStdSic();
            Double[] stdTole = stdTolerance.getStdTole(desginKey, rs
                    .getDouble("OUTDIAMETER"), rs.getDouble("WALLTHICK"));
            if (stdTole != null) {
                inOrdParam.setStdWallthickMax(stdTole[0]);
                inOrdParam.setStdWallthickMin(stdTole[1]);
            }
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询BOM轧管规格，坯料规格，轧管、镦拔扩投入产出物料编码
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getBomZgSpecInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, bom);
        rs = ps.executeQuery();
        if (rs.next()) {
            List<Double> dimianPl = new ArrayList<Double>();
            dimianPl.add(rs.getDouble("SPEC_BILT"));
            inOrdParam.setDiameterZg(rs.getDouble("DIMATER"));
            inOrdParam.setHeightZg(rs.getDouble("HEIGHT"));
            inOrdParam.setDiameterPl(dimianPl);
            if (rs.getString("D_IN_MATERIAL_NO") != null) {
                dInMaterialNo = rs.getString("D_IN_MATERIAL_NO");
            }
            if (rs.getString("D_OUT_MATERIAL_NO") != null) {
                dOutMaterialNo = rs.getString("D_OUT_MATERIAL_NO");
            }
            if (rs.getString("E_IN_MATERIAL_NO") != null) {
                eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
            }
            if (rs.getString("E_OUT_MATERIAL_NO") != null) {
                eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
            }

            // // H镦拔扩(D镦拔扩 - H镦拔扩)/H轧管(D轧管 - H轧管)
            // rateEtoD = (inOrdParam.getWallthick() * (inOrdParam
            // .getOutdiameter() - inOrdParam.getWallthick()))
            // / (rs.getDouble("HEIGHT") * (rs.getDouble("DIMATER") - rs
            // .getDouble("HEIGHT")));
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询接箍信息
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.getComMscJg");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, codeJg);
        rs = ps.executeQuery();
        if (rs.next()) {
            inOrdParam.setWeightJg(rs.getDouble("WEIGHT_JG"));
            inOrdParam.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
        }
        rs.close();
        ps.close();

        // /查询轧管内控壁厚公差标准索引号
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.queryMscPlPrc");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, "D");
        rs = ps.executeQuery();
        if (rs.next()) {
            String sicZg = "";
            if (rs.getString("SIC") != null) {
                sicZg = rs.getString("SIC");
                if (sicZg.contains(",")) {
                    String[] sicList = sicZg.split(",");
                    if (sicList.length > 0) {
                        sicZg = sicList[0];
                    }
                }
            }

            // 查询轧管工序切头尾
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialDPl");
            PreparedStatement psta = conn.prepareStatement(sql);
            psta.setObject(1, dInMaterialNo);
            psta.setObject(2, dOutMaterialNo);
            psta.setObject(3, plineCodeD);
            ResultSet result = psta.executeQuery();
            if (result.next()) {
                double cutHeadEnd = result.getDouble("CROP_END")
                        + result.getDouble("CROP_HEAD");
                tubeEndRatio = result.getDouble("TUBE_END_RATIO");
                cutMax = result.getDouble("DBL_LEN_MAX");
                // 切头尾默认1000mm
                if (cutHeadEnd == 0) {
                    cutHeadEnd = 1000;
                }
                inOrdParam.setCutHeadEnd(cutHeadEnd);
                inOrdParam.setTubeEndRatio(tubeEndRatio);
                inOrdParam.setCutMax(cutMax);
            }
            result.close();
            psta.close();
            // 查询轧管产线基础信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getZgBasePline");
            psta = conn.prepareStatement(sql);
            psta.setObject(1, plineCodeD);
            result = psta.executeQuery();
            if (result.next()) {
                inOrdParam.setLengthColdbed(result.getDouble("LENGTH_COLDBED"));
                inOrdParam
                        .setLengthMaxRing(result.getDouble("LENGTH_MAX_RING"));
                inOrdParam.setBurnoutRate(result.getDouble("BURNOUT_RATE"));
                inOrdParam.setPlineWallthick(result.getDouble("TOLERANCE_ZONE")
                        * inOrdParam.getWallthick() / 100);
                inOrdParam.setToleranceZone(result.getDouble("TOLERANCE_ZONE"));
                inOrdParam.setPlineLenMin(result.getDouble("LENGTH_MIN"));
                inOrdParam.setPlineLenMax(result.getDouble("LENGTH_MAX"));
            }
            result.close();
            psta.close();

            // 查询镦拔扩工序加长
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialEPl");
            psta = conn.prepareStatement(sql);
            psta.setObject(1, eInMaterialNo);
            psta.setObject(2, eOutMaterialNo);
            psta.setObject(3, desginKey);
            psta.setObject(4, mscPline);
            result = psta.executeQuery();
            if (result.next()) {
                double cutHeadEnd = result.getDouble("CROP_END")
                        + result.getDouble("CROP_HEAD");
                double lenDc = result.getDouble("UPSETTING_LENGTH");
                double burnoutRate = result.getDouble("BURNOUT_RATE");
                String plineCode = result.getString("PLINE_CODE");
                // 默认值
                if (plineCode.trim().equals("C021")
                        || plineCode.trim().equals("C067")) {
                    if (lenDc == 0) {
                        lenDc = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                else {
                    if (cutHeadEnd == 0) {
                        cutHeadEnd = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                inOrdParam.setBurnoutDbk(burnoutRate);
                inOrdParam.setAddLengthDbk(cutHeadEnd);
                inOrdParam.setAddLengthDc(lenDc);
            }
            result.close();
            psta.close();
            inOrdParam.setOrdLnDlyPk(ordLnDlyPk);
            // 轧管内控壁厚公差
            CoreTolerOfNk tolerOfNk = new CoreTolerOfNk();
            CoreTolerOfNkParam nkParam = new CoreTolerOfNkParam();
            nkParam.setBias_code("404019");
            nkParam.setOutdiameter(inOrdParam.getOutdiameter());
            nkParam.setWallthick(inOrdParam.getWallthick());
            nkParam.setSic(sicZg);
            nkParam.setLengthmax(inOrdParam.getLengthMax());
            nkParam.setLengthmin(inOrdParam.getLengthMin());
            nkParam = tolerOfNk.queryTolerance(nkParam, desginKey, mscPline,
                    "1");
            if (nkParam != null) {
                double wallUpper = nkParam.getWallUpper();
                double wallLower = nkParam.getWallLower();
                if (wallUpper == 0 && wallLower == 0) {
                    // wallUpper = inOrdParam.getStdWallthickMax();
                    // wallLower = inOrdParam.getStdWallthickMin();
                    wallUpper = 0;
                    wallLower = 0;
                }
                inOrdParam.setNkWallthickMax(wallUpper);
                inOrdParam.setNkWallthickMin(wallLower);
            }
        }
        rs.close();
        ps.close();
        conn.commit();
        return inOrdParam;
    }

    /**
     * 查询合同量转换参数
     * @param orderQty 订货量
     * @param orderUnit 订货单位
     * @param desginKey 质量设计
     * @param mscPline 制程
     * @param ordLnDlyPk 交货行pK
     * @param plineCodeD 轧管产线代码
     * @param bom 交货行选用BOM
     * @param codeJg 交货行接箍码
     * @param putRate 排产月投料率
     * @return
     * @throws SQLException
     * @throws ScriptException
     */
    public static InOrdParamEntity getInOrdPlnQtyEntity(Connection conn,
            double orderQty, String orderUnit, String desginKey,
            String mscPline, String ordLnDlyPk, String plineCodeD, String bom,
            String codeJg, double putRate) throws SQLException {
        InOrdParamEntity inOrdParam = new InOrdParamEntity();
        PreparedStatement ps = null;
        String dInMaterialNo = "";
        String dOutMaterialNo = "";
        String eInMaterialNo = "";
        String eOutMaterialNo = "";
        double cutMax = 0;// 最大分切数
        double tubeEndRatio = 0;// 管端系数
        inOrdParam.setPutRate(putRate);
        // 查询交货行信息
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getPlnSaleOrdInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            String lenUnit = "";// 长度单位
            if (rs.getString("LEN_UNIT") != null) {
                lenUnit = rs.getString("LEN_UNIT");
            }
            inOrdParam.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
            inOrdParam.setOrderQty(orderQty);
            inOrdParam.setOrderUnit(orderUnit);
            inOrdParam.setTotlength(rs.getDouble("TOTLENGTH") * orderQty
                    / rs.getDouble("ORDER_QTY"));
            if (rs.getString("TOTLENGTH_UNIT") != null) {
                inOrdParam.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
            }
            inOrdParam.setLengthMax(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMAX")));
            inOrdParam.setLengthMin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("LENGTHMIN")));
            inOrdParam.setOutdiameter(rs.getDouble("OUTDIAMETER"));
            inOrdParam.setWallthick(rs.getDouble("WALLTHICK"));
            if (rs.getString("MODEL_DESC") != null) {
                inOrdParam.setModelDesc(rs.getString("MODEL_DESC"));
            }
            if (orderUnit.trim().equals("吨") || orderUnit.trim().equals("英磅")) {
                if (rs.getString("WEIGH_TYPE") != null
                        && rs.getString("WEIGH_TYPE").contains("理")) {
                    inOrdParam.setWeightType(0);
                }
                else {
                    inOrdParam.setWeightType(1);
                }
            }
            else {
                inOrdParam.setWeightType(-1);
            }
            inOrdParam.setDelvryRangeMax(rs.getDouble("DELVRY_RANGE_MAX"));
            inOrdParam.setDelvryRangeMin(rs.getDouble("DELVRY_RANGE_MIN"));
            if (rs.getString("DELVRY_RANGE_TPE") != null
                    && rs.getString("DELVRY_RANGE_TPE").trim().equals("绝对值")) {
                inOrdParam.setDelvryRangeTpe(2);
            }
            else {
                inOrdParam.setDelvryRangeTpe(1);
            }
            inOrdParam.setSigllen(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SIGL_LEN")));
            inOrdParam.setFootmin(CoreOrdBase.lenConversion(lenUnit, rs
                    .getDouble("SHORTEST")));
            if (rs.getDouble("SHRT_LEN_RATO") > 0) {
                inOrdParam.setShrtStove(true);
            }
            else {
                inOrdParam.setShrtStove(false);
            }
            inOrdParam.setInspectLen(rs.getDouble("INSPECTION_VALUE"));

            // 获取交货标准壁厚公差
            CoreOrderStdSic stdTolerance = new CoreOrderStdSic();
            Double[] stdTole = stdTolerance.getStdTole(desginKey, rs
                    .getDouble("OUTDIAMETER"), rs.getDouble("WALLTHICK"));
            if (stdTole != null) {
                inOrdParam.setStdWallthickMax(stdTole[0]);
                inOrdParam.setStdWallthickMin(stdTole[1]);
            }
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询BOM轧管规格，坯料规格，轧管、镦拔扩投入产出物料编码
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getBomZgSpecInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, bom);
        rs = ps.executeQuery();
        if (rs.next()) {
            List<Double> dimianPl = new ArrayList<Double>();
            dimianPl.add(rs.getDouble("SPEC_BILT"));
            inOrdParam.setDiameterZg(rs.getDouble("DIMATER"));
            inOrdParam.setHeightZg(rs.getDouble("HEIGHT"));
            inOrdParam.setDiameterPl(dimianPl);
            if (rs.getString("D_IN_MATERIAL_NO") != null) {
                dInMaterialNo = rs.getString("D_IN_MATERIAL_NO");
            }
            if (rs.getString("D_OUT_MATERIAL_NO") != null) {
                dOutMaterialNo = rs.getString("D_OUT_MATERIAL_NO");
            }
            if (rs.getString("E_IN_MATERIAL_NO") != null) {
                eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
            }
            if (rs.getString("E_OUT_MATERIAL_NO") != null) {
                eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
            }
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        // 查询接箍信息
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.getComMscJg");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, codeJg);
        rs = ps.executeQuery();
        if (rs.next()) {
            inOrdParam.setWeightJg(rs.getDouble("WEIGHT_JG"));
            inOrdParam.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
        }
        rs.close();
        ps.close();

        // /查询轧管内控壁厚公差标准索引号
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.queryMscPlPrc");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, "D");
        rs = ps.executeQuery();
        // 2016-7-13 如果没有取到内控就取交互标准
        String sicZg = "";
        if (rs.next()) {
            if (rs.getString("SIC") != null) {
                sicZg = rs.getString("SIC");
                if (sicZg.contains(",")) {
                    String[] sicList = sicZg.split(",");
                    if (sicList.length > 0) {
                        sicZg = sicList[0];
                    }
                }
            }
        }
        rs.close();
        ps.close();
        if (sicZg.equals("")) {
            String sicsql = "select nvl((select t.sic   from CRAFT_ORD_DESIGN_STD_SIC t  where t.desgin_key = ?  and t.specl_fl = '1'  group by t.sic), (select t.sic   from CRAFT_ORD_DESIGN_STD_SIC t where t.desgin_key = ? and t.specl_fl = '0' group by t.sic)) sic from dual";
            ps = conn.prepareStatement(sicsql);
            ps.setObject(1, desginKey);
            ps.setObject(2, desginKey);
            rs = ps.executeQuery();
            if (rs.next()) {
                sicZg = rs.getString("sic");
            }
            rs.close();
            ps.close();
        }
        // 查询轧管工序切头尾
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getMaterialDPl");
        PreparedStatement psta = conn.prepareStatement(sql);
        psta.setObject(1, dInMaterialNo);
        psta.setObject(2, dOutMaterialNo);
        psta.setObject(3, plineCodeD);
        ResultSet result = psta.executeQuery();
        if (result.next()) {
            double cutHeadEnd = result.getDouble("CROP_END")
                    + result.getDouble("CROP_HEAD");
            tubeEndRatio = result.getDouble("TUBE_END_RATIO");
            cutMax = result.getDouble("DBL_LEN_MAX");
            // 切头尾默认1000mm
            if (cutHeadEnd == 0) {
                cutHeadEnd = 1000;
            }
            inOrdParam.setCutHeadEnd(cutHeadEnd);
            inOrdParam.setTubeEndRatio(tubeEndRatio);
            inOrdParam.setCutMax(cutMax);
        }
        result.close();
        psta.close();
        // 查询轧管产线基础信息
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getZgBasePline");
        psta = conn.prepareStatement(sql);
        psta.setObject(1, plineCodeD);
        result = psta.executeQuery();
        if (result.next()) {
            inOrdParam.setLengthColdbed(result.getDouble("LENGTH_COLDBED"));
            inOrdParam.setLengthMaxRing(result.getDouble("LENGTH_MAX_RING"));
            inOrdParam.setBurnoutRate(result.getDouble("BURNOUT_RATE"));
            inOrdParam.setPlineWallthick(result.getDouble("TOLERANCE_ZONE")
                    * inOrdParam.getWallthick() / 100);
            inOrdParam.setToleranceZone(result.getDouble("TOLERANCE_ZONE"));
            inOrdParam.setPlineLenMin(result.getDouble("LENGTH_MIN"));
            inOrdParam.setPlineLenMax(result.getDouble("LENGTH_MAX"));
        }
        result.close();
        psta.close();

        // 查询镦拔扩工序加长
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getMaterialEPl");
        psta = conn.prepareStatement(sql);
        psta.setObject(1, eInMaterialNo);
        psta.setObject(2, eOutMaterialNo);
        psta.setObject(3, desginKey);
        psta.setObject(4, mscPline);
        result = psta.executeQuery();
        if (result.next()) {
            double cutHeadEnd = result.getDouble("CROP_END")
                    + result.getDouble("CROP_HEAD");
            double lenDc = result.getDouble("UPSETTING_LENGTH");
            double burnoutRate = result.getDouble("BURNOUT_RATE");
            String plineCode = result.getString("PLINE_CODE");
            // 默认值
            if (plineCode.trim().equals("C021")
                    || plineCode.trim().equals("C067")) {
                if (lenDc == 0) {
                    lenDc = CoreOrdBase.addDbkDefault(plineCode);
                }
            }
            else {
                if (cutHeadEnd == 0) {
                    cutHeadEnd = CoreOrdBase.addDbkDefault(plineCode);
                }
            }
            inOrdParam.setBurnoutDbk(burnoutRate);
            inOrdParam.setAddLengthDbk(cutHeadEnd);
            inOrdParam.setAddLengthDc(lenDc);
        }
        result.close();
        psta.close();
        inOrdParam.setOrdLnDlyPk(ordLnDlyPk);
        // 轧管内控壁厚公差
        CoreTolerOfNk tolerOfNk = new CoreTolerOfNk();
        CoreTolerOfNkParam nkParam = new CoreTolerOfNkParam();
        nkParam.setBias_code("404019");
        nkParam.setOutdiameter(inOrdParam.getOutdiameter());
        nkParam.setWallthick(inOrdParam.getWallthick());
        nkParam.setSic(sicZg);
        nkParam.setLengthmax(inOrdParam.getLengthMax());
        nkParam.setLengthmin(inOrdParam.getLengthMin());
        nkParam = tolerOfNk.queryTolerance(nkParam, desginKey, mscPline, "1");
        if (nkParam != null) {
            double wallUpper = nkParam.getWallUpper();
            double wallLower = nkParam.getWallLower();
            if (wallUpper == 0 && wallLower == 0) {
                wallUpper = inOrdParam.getStdWallthickMax();
                wallLower = inOrdParam.getStdWallthickMin();
            }
            inOrdParam.setNkWallthickMax(wallUpper);
            inOrdParam.setNkWallthickMin(wallLower);
        }
        rs.close();
        ps.close();
        conn.commit();
        return inOrdParam;
    }

    /**
     * 获取备料计算参数
     * @param conn
     * @param blReqEntity
     * @param ordProcEntity
     * @return
     * @throws ScriptException
     * @throws SQLException
     */
    public static InOrdParamEntity getBlInOrdParam(Connection conn,
            String desginKey, PlnPorderBlReqEntity blReqEntity,
            PlnSaleordProcEntity ordProcEntity) throws SQLException,
            ScriptException {
        String sql = "";
        String dInMaterialNo = "";
        String dOutMaterialNo = "";
        String eInMaterialNo = "";
        String eOutMaterialNo = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        String codeBl = "";
        double cutMax = 0;// 最大分切数
        double tubeEndRatio = 0;// 管端系数
        InOrdParamEntity inOrdParam = new InOrdParamEntity();
        double wallthick = Double.parseDouble(blReqEntity.getWallthick());
        double outdiameter = Double.parseDouble(blReqEntity.getOutdiameter());

        if (blReqEntity.getCodeDjBl().trim().equals("")) {
            codeBl = blReqEntity.getCodeDjBl();
        }
        else {
            codeBl = blReqEntity.getCodeJgBl();
        }
        // h获取交货标准壁厚公差
        Double[] stdTole = CoreOrderStdSic.getConnStdTole(conn, desginKey,
                outdiameter, wallthick);
        if (stdTole != null) {
            inOrdParam.setStdWallthickMax(stdTole[0]);
            inOrdParam.setStdWallthickMin(stdTole[1]);
        }
        // 获取交货标准外径公差
        Double[] stdToleD = CoreOrderStdSic.getConnStdOutD(conn, desginKey,
                outdiameter, wallthick);
        if (stdToleD != null) {
            inOrdParam.setOutdOrdMax(stdToleD[0]);
            inOrdParam.setOutdOrdMin(stdToleD[1]);
        }
        // 查询BOM轧管规格，坯料规格，轧管、镦拔扩投入产出物料编码
        sql = CoreXmlSqlParsersFactory
                .getSql("CoreGetOrdAmount.getBomZgSpecInf");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordProcEntity.getBom());
        rs = ps.executeQuery();
        if (rs.next()) {
            List<Double> dimianPl = new ArrayList<Double>();
            dimianPl.add(rs.getDouble("SPEC_BILT"));
            inOrdParam.setDiameterZg(rs.getDouble("DIMATER"));
            inOrdParam.setHeightZg(rs.getDouble("HEIGHT"));
            inOrdParam.setDiameterPl(dimianPl);
            if (rs.getString("D_IN_MATERIAL_NO") != null) {
                dInMaterialNo = rs.getString("D_IN_MATERIAL_NO");
            }
            if (rs.getString("D_OUT_MATERIAL_NO") != null) {
                dOutMaterialNo = rs.getString("D_OUT_MATERIAL_NO");
            }
            if (rs.getString("E_IN_MATERIAL_NO") != null) {
                eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
            }
            if (rs.getString("E_OUT_MATERIAL_NO") != null) {
                eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
            }
        }
        else {
            return null;
        }
        rs.close();
        ps.close();

        inOrdParam.setOrderUnit("吨");
        inOrdParam.setOrderQty(Double.parseDouble(blReqEntity
                .getJgblReqWeight()));
        inOrdParam.setTotlength(0);
        inOrdParam.setLengthMax(Double.parseDouble(blReqEntity.getLengthmax()));
        inOrdParam.setLengthMin(Double.parseDouble(blReqEntity.getLengthmin()));
        inOrdParam.setOutdiameter(outdiameter);
        inOrdParam.setWallthick(wallthick);
        inOrdParam.setWeightType(1);

        // /查询轧管内控壁厚公差标准索引号
        sql = CoreXmlSqlParsersFactory.getSql("CoreGetOrdAmount.queryMscPlPrc");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, ordProcEntity.getIndexSeq());
        ps.setObject(3, "D");
        rs = ps.executeQuery();
        if (rs.next()) {
            PreparedStatement pst = null;
            ResultSet rst = null;
            String sicZg = "";
            if (rs.getString("SIC") != null) {
                sicZg = rs.getString("SIC");
                if (sicZg.contains(",")) {
                    String[] sicList = sicZg.split(",");
                    if (sicList.length > 0) {
                        sicZg = sicList[0];
                    }
                }
            }

            // 查询轧管工序切头尾
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialDPl");
            pst = conn.prepareStatement(sql);
            pst.setObject(1, dInMaterialNo);
            pst.setObject(2, dOutMaterialNo);
            pst.setObject(3, ordProcEntity.getGroupRoll());
            rst = pst.executeQuery();
            if (rst.next()) {
                double cutHeadEnd = rst.getDouble("CROP_END")
                        + rst.getDouble("CROP_HEAD");
                tubeEndRatio = rst.getDouble("TUBE_END_RATIO");
                cutMax = rst.getDouble("DBL_LEN_MAX");
                // 切头尾默认1000mm
                if (cutHeadEnd == 0) {
                    cutHeadEnd = 1000;
                }
                inOrdParam.setCutHeadEnd(cutHeadEnd);
                inOrdParam.setTubeEndRatio(tubeEndRatio);
                inOrdParam.setCutMax(cutMax);
            }
            rst.close();
            pst.close();
            // 查询轧管产线基础信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getZgBasePline");
            pst = conn.prepareStatement(sql);
            pst.setObject(1, ordProcEntity.getGroupRoll());
            rst = pst.executeQuery();
            if (rst.next()) {
                inOrdParam.setLengthColdbed(rst.getDouble("LENGTH_COLDBED"));
                inOrdParam.setLengthMaxRing(rst.getDouble("LENGTH_MAX_RING"));
                inOrdParam.setBurnoutRate(rst.getDouble("BURNOUT_RATE"));
                inOrdParam.setPlineWallthick(rst.getDouble("TOLERANCE_ZONE")
                        * inOrdParam.getWallthick() / 100);
                inOrdParam.setPlineLenMin(rst.getDouble("LENGTH_MIN"));
                inOrdParam.setPlineLenMax(rst.getDouble("LENGTH_MAX"));
                inOrdParam.setToleranceZone(rst.getDouble("TOLERANCE_ZONE"));
                inOrdParam.setLengthMinTube(rst.getDouble("LENGTH_MIN_TUBE"));
                inOrdParam.setLengthMaxTube(rst.getDouble("LENGTH_MAX_TUBE"));
            }
            rst.close();
            pst.close();

            // 查询扣型
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMscBlInfo");
            pst = conn.prepareStatement(sql);
            pst.setObject(1, codeBl);
            rst = pst.executeQuery();
            if (rst.next()) {
                if (rst.getString("MODEL_DESC") != null) {
                    inOrdParam.setModelDesc(rst.getString("MODEL_DESC"));
                }
            }
            rst.close();
            pst.close();

            // 查询镦拔扩工序加长
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreGetOrdAmount.getMaterialEPl");
            pst = conn.prepareStatement(sql);
            pst.setObject(1, eInMaterialNo);
            pst.setObject(2, eOutMaterialNo);
            pst.setObject(3, desginKey);
            pst.setObject(4, ordProcEntity.getIndexSeq());
            rst = pst.executeQuery();
            if (rst.next()) {
                double cutHeadEnd = rst.getDouble("CROP_END")
                        + rst.getDouble("CROP_HEAD");
                double lenDc = rst.getDouble("UPSETTING_LENGTH");
                double burnoutRate = rst.getDouble("BURNOUT_RATE");
                String plineCode = rst.getString("PLINE_CODE");
                // 默认值
                if (plineCode.trim().equals("C021")
                        || plineCode.trim().equals("C067")) {
                    if (lenDc == 0) {
                        lenDc = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                else {
                    if (cutHeadEnd == 0) {
                        cutHeadEnd = CoreOrdBase.addDbkDefault(plineCode);
                    }
                }
                inOrdParam.setBurnoutDbk(burnoutRate);
                inOrdParam.setAddLengthDbk(cutHeadEnd);
                inOrdParam.setAddLengthDc(lenDc);
            }
            rst.close();
            pst.close();
            inOrdParam.setOrdLnDlyPk(ordProcEntity.getOrdLnDlyPk());
            // 轧管内控壁厚公差
            CoreTolerOfNk tolerOfNk = new CoreTolerOfNk();
            CoreTolerOfNkParam nkParam = new CoreTolerOfNkParam();
            nkParam.setBias_code("404019");
            nkParam.setOutdiameter(inOrdParam.getOutdiameter());
            nkParam.setWallthick(inOrdParam.getWallthick());
            nkParam.setSic(sicZg);
            nkParam.setLengthmax(inOrdParam.getLengthMax());
            nkParam.setLengthmin(inOrdParam.getLengthMin());
            nkParam = tolerOfNk.queryTolerance(nkParam, desginKey,
                    ordProcEntity.getIndexSeq(), "1");
            if (nkParam != null) {
                double wallUpper = nkParam.getWallUpper();
                double wallLower = nkParam.getWallLower();
                if (wallUpper == 0 && wallLower == 0) {
                    // wallUpper = inOrdParam.getStdWallthickMax();
                    // wallLower = inOrdParam.getStdWallthickMin();
                    wallUpper = 0;
                    wallLower = 0;
                }
                inOrdParam.setNkWallthickMax(wallUpper);
                inOrdParam.setNkWallthickMin(wallLower);
            }
            // 轧管内控外径公差
            nkParam.setBias_code("404012");
            nkParam.setOutdiameter(inOrdParam.getOutdiameter());
            nkParam.setWallthick(inOrdParam.getWallthick());
            nkParam.setSic(sicZg);
            nkParam.setLengthmax(inOrdParam.getLengthMax());
            nkParam.setLengthmin(inOrdParam.getLengthMin());
            nkParam = tolerOfNk.queryToleranceAim(nkParam, desginKey,
                    ordProcEntity.getIndexSeq(), "1");
            if (nkParam != null) {
                double aimUpper = nkParam.getAimUpper();
                double aimLower = nkParam.getAimLower();
                if (aimUpper == 0 && aimLower == 0) {
                    // aimUpper = inOrdParam.getOutdOrdMax();
                    // aimLower = inOrdParam.getOutdOrdMin();
                    aimUpper = 0;
                    aimLower = 0;
                }
                inOrdParam.setNkAimthickMax(aimUpper);
                inOrdParam.setNkAimthickMin(aimLower);
            }
        }
        inOrdParam.setDesginKey(desginKey);
        inOrdParam.setIndexSeq(ordProcEntity.getIndexSeq());
        return inOrdParam;
    }

}