package com.steering.pss.plnsaleord.GpOptimize;

import java.lang.reflect.InvocationTargetException;
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 CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBaseLzSpecCutEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBasePlineLgEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBasePlineLzEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnOrderCastSEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforOrdEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforfurnaceEntity;

/**
 * 查询钢种及钢种索引码
 * @author TJG
 * @version v1.1
 * @date 2017.07.11
 */
public class FrmGradeAnalysis extends DBCommon {
    /**
     * 查询钢种及钢种索引码（自炼）
     * @param weight
     * @return
     */
    public CoreReturnObject getQueryGradeZl(String[] groupCic,
            String outsBilletfl) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmChangeGrade.getQueryGrade");
        // 去除数组中的空格
        List<String> tmp = new ArrayList<String>();
        for (String str : groupCic) {
            if (str != null && str.length() != 0) {
                tmp.add(str.trim());
            }
        }
        groupCic = tmp.toArray(new String[0]);
        if (groupCic == null || groupCic.length == 0) {
            sql += " AND T.CIC =''";
        }
        sql += CoreConverter.toSqlConditionString("T.CIC", groupCic);
        sql += " AND T1.PURCHASE_STD='" + outsBilletfl + "'";
        cro = this.getData(sql);
        return cro;
    }

    /**
     * 查询出钢量上下限
     * @return
     */
    public CoreReturnObject getBasePlineLg(String plineCodeA) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.getBasePlineLg");
        return this.getData(sql, new Object[] { plineCodeA });
    }

    /**
     * 查询首炉切头
     * @return
     */
    public CoreReturnObject doPlnZgQueryFurnace(String diameterGp) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQueryFurnace");
        return this.getData(sql, new Object[] { diameterGp });
    }

    /**
     * 查询小时能力表中的浇次流数
     * @return
     */
    public CoreReturnObject doPlnZgQueryMaxJc(String plineCodeZz,
            String gradeCode, String wlidZz) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQueryMaxJc");
        return this.getData(sql,
                new Object[] { plineCodeZz, gradeCode, wlidZz });
    }

    /**
     * 查询
     * @return
     */
    public CoreReturnObject queryLZDataZg(String proPlanId, String gxPlanNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.queryLZDataZg");
        return this.getDao("MSSQL2Dao").ExcuteSortResultByQuery(sql,
                new Object[] { proPlanId, gxPlanNo });
    }

    /**
     * 查询断面，钢种，钢种索引码
     * @return
     */
    public CoreReturnObject getdoQueryGrade() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.getdoQueryGrade");
        return this.getData(sql);
    }

    /**
     * 查询成分元素
     * @return
     */
    public CoreReturnObject doPlnZgQuerySingle(String cicGp, String proPlanId,
            String gxPlanNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQuerySingle");
        return this.getData(sql, new Object[] { proPlanId, gxPlanNo, cicGp });
    }

    /**
     * 查询同钢种所有钢种索引码成分
     * @return
     */
    public CoreReturnObject doQueryGradecOll(String[] cicGp,String[] desginKey, String[] indexSeq) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doQueryGradecOll");
        sql += CoreConverter.toSqlConditionString("T.CIC", cicGp);
        sql += CoreConverter.toSqlConditionString("T.DESGIN_KEY", desginKey);
        sql += CoreConverter.toSqlConditionString("T.MSC_PLINE", indexSeq);
        sql+="  ORDER BY C.CHEM_SEQ ASC,T.INDEX_SEQ ASC";
        return this.getData(sql);
    }

    /**
     * 查询轧管订单
     * @return
     */
    public CoreReturnObject doPlnZgQuery(String diameterGp, String gradecode,
            String cicGp, String lenGpSingle, String flag, String plineCodeZz) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQuery");
        if (flag.equals("0")) {
            if (!lenGpSingle.equals("")) {
                sql += " AND T.LEN_GP_SINGLE =TO_NUMBER('" + lenGpSingle + "')";
            }
            sql += " AND T.GRADECODE='" + gradecode + "'  AND T.CIC_GP='"
                    + cicGp + "'";
        }
        else {
            sql += " AND INSTR(T2.GRADECODE_ALL,'" + gradecode
                    + "') > 0  AND INSTR(T.CIC_GROUP_GP,'" + cicGp + "') > 0";
        }
        if (!plineCodeZz.equals("")) {
            sql += "  AND T.PLINE_CODE_ZZ='" + plineCodeZz + "'";
        }
        return this.getData(sql, new Object[] { diameterGp });
    }

    /**
     * 查询轧管订单DesKEY
     * @return
     */
    public CoreReturnObject doPlnZgQueryDesKEY(String diameterGp,
            String gradecode, String cicGp, String plineCodeZz) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQueryDesKEY");
        sql += " AND T.GRADECODE='" + gradecode + "'  AND T.CIC_GP='" + cicGp
                + "'";
        if (!plineCodeZz.equals("")) {
            sql += "  AND T.PLINE_CODE_ZZ='" + plineCodeZz + "'";
        }
        return this.getData(sql, new Object[] { diameterGp });
    }

    /**
     * 查询所有为已计划确认未做管坯需求的订单
     * @return
     */
    public CoreReturnObject doPlnZgQueryHj() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmGradeAnalysis.doPlnZgQueryHj");
        return this.getData(sql);
    }

    /**
     * 修改铸造产线
     * @param list
     * @return
     */
    public CoreReturnObject doUpadatePlin(ArrayList<ArrayList<String>> jsonList) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            PreparedStatement ps = null;
            ResultSet rs = null;
            // 修改产线
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmGradeAnalysis.doUpadatePlin");
            PreparedStatement updatePart = conn.prepareStatement(sqlupdate);
            for (int i = 0; i < jsonList.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonList.get(i);
                String plineCode = list.get(0);
                String plineName = list.get(1);
                String proPlanId = list.get(2);
                String gxPlanNo = list.get(3);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CorePlnZgMStoGp.queryZgMSStatus");
                ps = conn.prepareStatement(sql);
                ps.setObject(1, proPlanId);
                ps.setObject(2, gxPlanNo);
                rs = ps.executeQuery();
                if (rs.next()) {
                    if (!rs.getString("EXE_STATUS").equals("0")) {
                        cro.setV_errMsg("轧管订单：" + proPlanId + '/' + gxPlanNo
                                + ",不是计划状态,不允许修改。");
                        return cro;
                    }
                }
                else {
                    cro.setV_errMsg("找不到轧管订单：" + proPlanId + '/' + gxPlanNo
                            + "。");
                    return cro;
                }
                rs.close();
                ps.close();
                updatePart.setObject(1, plineCode);
                updatePart.setObject(2, plineName);
                updatePart.setObject(3, proPlanId);
                updatePart.setObject(4, gxPlanNo);
                updatePart.addBatch();
            }
            updatePart.executeBatch();
            cro.setV_errMsg("修改成功!");
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询浇次，首炉加量，尾炉加量
     * @param jsonList
     * @return
     */
    public CoreReturnObject querySetFurn(ArrayList<String> jsonList) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);

        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            // for (int i = 0; i < jsonList.size(); i++) {
            // ArrayList<String> list = (ArrayList<String>) jsonList.get(i);
            // 铸造产线
//            String plineCode = jsonList.get(0);
            String plineName = jsonList.get(1);
            // 流量
            int flow = Integer.parseInt(jsonList.get(3));
            // 中包铸余
            double zbLeftWgt = Double.parseDouble(jsonList.get(2));
            // 最大炉数
            int maxCastNum = Integer.parseInt(jsonList.get(4));
            // 换包方式 1换中包 2不换中包
            String castMode = jsonList.get(5);
//            String gradeCode = jsonList.get(6);
            String gradeName = jsonList.get(7);
            String diameterGp = jsonList.get(8);// 待冶炼量
            double weight = Double.parseDouble(jsonList.get(9));
            // // 实时数据
            // List<PlnOrderCastSEntity> castList1 = new
            // ArrayList<PlnOrderCastSEntity>();
            /**
             * 2. 炉信息
             */
            List<PlnSteelforfurnaceEntity> furnaceList = new ArrayList<PlnSteelforfurnaceEntity>();// 炉次集合
            List<PlnSteelforOrdEntity> steelordList = new ArrayList<PlnSteelforOrdEntity>();// 炉次订单集合
            // 首炉加量
            double addWtBegin = 0;
            // 尾炉加量
            double addWtEnd = 0;
            // 开始钢种
            String beginGrade = "";
            // 结束钢种
            String endGrade = "";

            // 组浇重量
            double castWtg = 0;
            // 组浇支数
            int castNum = 0;
            // 用于预算
            double noPlanWgt = 0;// 非计划加量
            double ordWgt = 0;// 订单使用量
            // 炉数
            int furnaceCout = 1;
            // 2.生成炉
            for (int castSeq = 1; castSeq > 0; castSeq++) {
                ReturnEntity returnEntity = generationFurnace(con, flow,
                        castMode, zbLeftWgt, String.valueOf(castSeq),
                        maxCastNum, gradeName, diameterGp, weight, plineName);
                // 返回信息
                if (returnEntity.getMessage().length() > 0) {
                    core.setV_errMsg(returnEntity.getMessage());
                    return core;
                }
                furnaceList.add(returnEntity.getFurEntity());
                List<PlnSteelforOrdEntity> list1 = returnEntity
                        .getOrdEntityList();
                for (PlnSteelforOrdEntity entity : list1) {
                    steelordList.add(entity);
                }
                castWtg = returnEntity.getCaseWeght();
                furnaceCout = returnEntity.getLus();
                // 首炉
                if (castSeq == 1) {
                    addWtBegin = returnEntity.getBeginAddWgt();
                    beginGrade = returnEntity.getBeginGradename();
                }
                // 是否最后一个订单,如果到最后一个订单则不再循环
                if (returnEntity.isFlag()) {
                    addWtEnd = returnEntity.getEndAddWgt();
                    endGrade = returnEntity.getEndGradename();
                    break;
                }
                // 如果生成的炉号大于最大炉数
                if (castSeq >= maxCastNum) {
                    addWtEnd = returnEntity.getEndAddWgt();
                    endGrade = returnEntity.getEndGradename();
                    break;
                }

            }
//            String message = "预算结果      \r\n浇次重量:"
//                    + String.format("%.3f", castWtg)
//                    + "     浇次支数:"
//                    + castNum
//                    + "     \r\n开始钢种:"
//                    + beginGrade
//                    + "     结束钢种:"
//                    + endGrade
//                    + "      \r\n订单使用量:"
//                    + String.format("%.3f", ordWgt)
//                    + "     炉数:"
//                    + furnaceCout
//                    + "     \r\n生产加量:"
//                    + String
//                            .format("%.3f", (noPlanWgt - addWtBegin - addWtEnd))
//                    + "    首炉加量:" + String.format("%.3f", addWtBegin)
//                    + "     尾炉加量:" + String.format("%.3f", addWtEnd);
            String s = String.format("%.3f", castWtg) + "," + furnaceCout + ","
                    + String.format("%.3f", addWtBegin) + ","
                    + String.format("%.3f", addWtEnd);
            core.setV_errMsg(s);
            core.setResult(true);
            return core;
        }
        catch (Exception e) {
            try {
                e.printStackTrace();
                if (con != null)
                    con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if (con != null)
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
        }
        return core;
    }

    /**
     * 查询铸机基础表数据
     * @param con
     * @param plineCode
     * @return
     * @throws SQLException
     */
    PlnBasePlineLzEntity getPlineLzEntity(Connection con, String plineCode)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.getBasePlineLz");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ResultSet rs = ps.executeQuery();
        List<PlnBasePlineLzEntity> list = EntityTool.getEntitysByRs(rs,
                PlnBasePlineLzEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() != 1) {
            return null;
        }
        else {
            return list.get(0);
        }
    }

    /**
     * 生成炉
     * @param castList 订单集合
     * @param flow 　流数
     * @param castMode 　中包方式
     * @param zbLeftWgt 　中包铸余
     * @param castSeq 　浇次内顺序号
     * @param maxCastNum 　最大炉数
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws SQLException
     */
    ReturnEntity generationFurnace(Connection con, double flow,
            String castMode, double zbLeftWgt, String castSeq, int maxCastNum,
            String gradeName, String diameterGp, double weight, String plineName)
            throws IllegalAccessException, InvocationTargetException,
            SQLException {
        ReturnEntity returnEntity = new ReturnEntity();
        PlnOrderCastSEntity beginEntity = null;
        boolean lastFlag = false;
        // 是否有余材(0没有)
        String surplusFlag = "0";
        // 首炉加量(不是首炉，这个量为０，不影响后面计算)
        double beginAddWgt = 0;
        // 尾炉加量(首炉有开始钢种与首炉加量)
        double endAddWgt = 0;
        // 开始钢种
        String beignGradename = gradeName;
        String endGradename = "";
        int beginAddNum = 0;
        double fristStandSteel = 0;
        if (castSeq.equals("1")) {

            /**
             * 1 生成首炉加量的炉内计划
             */
            beginEntity = null;
            // 通过直径查询对应切头(待处理)
            double[] cutParams = getBaseLzSpecParams(con, diameterGp, castMode);
            if (cutParams == null) {
                returnEntity.setMessage("直径(" + diameterGp + ")没有维护连铸规格切头尾数据!");
                return returnEntity;
            }
            double cutLenBegin = cutParams[0];
            // 管坯直径
            double dimater = Double.parseDouble(diameterGp);
            // 计算米单重
            double mSingleWeight = (7.8 * Math.PI / 4 * dimater * dimater) / 1000 / 1000;
            // 首炉加量
            beginAddWgt = mSingleWeight * flow * cutLenBegin;
            surplusFlag = "1";
        }
        // 炉可组炉重量
        double sumPlanWgt = beginAddWgt;
        // 炉可组炉支数
        int sumPlanNum = beginAddNum;
        // 待冶炼量
        double planWgt = weight;
        // 已组炉量
        double planedWgt = 0;
        int furnNum = 1;// 炉数
        /**
         * 查询电炉的标准出钢量与出钢量下限
         */
        String plineNameA = "";
        String plineCodeA = "";
        if (plineName.equals("一炼铸机")) {
            plineCodeA = "C001";
            plineNameA = "一炼钢电炉";
        }
        else if (plineName.equals("二炼铸机")) {
            plineCodeA = "C003";
            plineNameA = "二炼钢电炉";
        }
        else {
            plineCodeA = "C006";
            plineNameA = "三炼钢电炉";
        }
        double[] steelArr = getPlineLgParams(con, plineCodeA);
        if (steelArr == null) {
            returnEntity.setMessage("电炉(" + plineNameA + ")没有排产基础数据!");
            return returnEntity;
        }
        double standSteel = steelArr[0];// 默认标准出钢量
        double minSteel = steelArr[1];// 默认出钢量下限

        if (standSteel <= 0 || minSteel <= 0) {
            returnEntity.setMessage("电炉(" + plineNameA + ")没有标准出钢量和出钢量下限不能为0!");
            return returnEntity;
        }
        if (fristStandSteel <= 0) {
            fristStandSteel = standSteel;
        }

        double[] cutParams = getBaseLzSpecParams(con, diameterGp, castMode);
        if (cutParams == null) {
            returnEntity.setMessage("直径(" + diameterGp + ")没有维护连铸规格切头尾数据!");
            return returnEntity;
        }
        /**
         * 查询对应直径的切尾
         */
        double cutLenEnd = cutParams[1];
        if (cutLenEnd <= 0) {
            String message = "[换中包]";
            if (castMode.equals("0")) {
                message = "[不换中包]";
            }
            returnEntity.setMessage("直径(" + diameterGp + ")连铸规格切头尾为0!"
                    + message);
            return returnEntity;
        }
        /**
         * 1.当前标准出钢量>可组炉总量
         */
        // if (standSteel > sumPlanWgt + planWgt - planedWgt) {
        /**
         * 1.2 已经到最后一个订单(即尾炉加量)并且 标准出钢量-排产总量不满足一炉
         */
        sumPlanWgt = sumPlanWgt + planWgt - planedWgt;
        lastFlag = true;
        // 计算尾炉加量
        double addEnd = CalculateEndAdd(cutLenEnd, Double
                .parseDouble(diameterGp), flow, zbLeftWgt);
        sumPlanWgt = sumPlanWgt + addEnd;
        // 存在以下几种情况　１.尾炉加量加前面的重量不够一炉(小于出钢量下限)／２.够一炉
        if (sumPlanWgt < minSteel) {
            // 如果小于出钢量下限，则按出钢量下限计算
            // 非计划加量
            double noPlanWgt = minSteel - sumPlanWgt;
            sumPlanWgt = sumPlanWgt + noPlanWgt;
        }
        else {
            // 如果总量小于标准出钢量，并且大于最小出钢量,则还需要生成非计划加量炉次订单
            if (sumPlanWgt <= standSteel) {
                double noPlanWgt = standSteel - sumPlanWgt;
                sumPlanWgt = sumPlanWgt + noPlanWgt;
            }
            // else {
            // // 则不需要生成非计划加量,并且前炉次订单要减量
            // double canWgt = standSteel - sumPlanWgt;
            // sumPlanWgt = sumPlanWgt + canWgt;
            // planWgt = canWgt;
            // }
        }
        surplusFlag = "1";
        // 返回值(结束钢种与尾炉加量,用于浇次使用)
        endAddWgt = addEnd;
        endGradename = gradeName;
        // 炉数
        // furnNum = 1;
        furnNum = (int) Math.ceil(sumPlanWgt / standSteel);
        // }
        // else {
        // /**
        // * 2.组炉量填充(.当前标准出钢量<可组炉总量,可组炉量有剩余)
        // */
        // // 最后一炉(//计算尾炉加量)如果是尾炉，则要加尾炉加量炉次订单
        // double addEnd = 0;
        // int addEndNum = 0;
        // if (Integer.parseInt(castSeq) == maxCastNum) {
        // addEnd = CalculateEndAdd(cutLenEnd, Double
        // .parseDouble(diameterGp), flow, zbLeftWgt);
        // }
        // // 生成炉次订单
        // double canWgt = standSteel - addEnd - sumPlanWgt;
        //
        // sumPlanWgt = sumPlanWgt + canWgt;
        // // 生成尾炉加量炉次订单
        // if (addEnd > 0) {
        // sumPlanWgt = sumPlanWgt + addEnd;
        // sumPlanNum = sumPlanNum + addEndNum;
        // surplusFlag = "1";
        // // 返回值(结束钢种与尾炉加量,用于浇次使用)
        // endAddWgt = addEnd;
        // endGradename = gradeName;
        // }
        // // 炉数
        // furnNum = (int) Math.ceil(sumPlanWgt / standSteel);
        // }
        returnEntity.setLus(furnNum);
        returnEntity.setCaseWeght(sumPlanWgt);
        returnEntity.setBeginAddWgt(beginAddWgt * furnNum);
        returnEntity.setEndAddWgt(endAddWgt * furnNum);
        returnEntity.setBeginGradename(beignGradename);
        returnEntity.setEndGradename(endGradename);
        returnEntity.setFlag(lastFlag);
        return returnEntity;
    }

    /**
     * 获取出钢量
     * @param con
     * @param plineCode
     * @return
     * @throws SQLException
     */
    static double[] getPlineLgParams(Connection con, String plineCode)
            throws SQLException {
        PlnBasePlineLgEntity entity = getPlineLgEntity(con, plineCode);
        if (entity == null) {
            return null;
        }
        double standSteel = 0;
        if (!entity.getStdOutWt().equals("")) {
            standSteel = Double.parseDouble(entity.getStdOutWt());
        }
        double minSteel = 0;
        if (!entity.getOutWtMin().equals("")) {
            minSteel = Double.parseDouble(entity.getOutWtMin());
        }
        double maxSteel = 0;
        if (!entity.getOutWtMax().equals("")) {
            maxSteel = Double.parseDouble(entity.getOutWtMax());
        }
        double[] paramArr = new double[] { standSteel, minSteel, maxSteel };
        return paramArr;
    }

    /**
     * 查询电炉基础表数据
     * @param con
     * @param plineCode 电炉
     * @return
     * @throws SQLException
     */
    static PlnBasePlineLgEntity getPlineLgEntity(Connection con,
            String plineCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.getBasePlineLg");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ResultSet rs = ps.executeQuery();
        List<PlnBasePlineLgEntity> list = EntityTool.getEntitysByRs(rs,
                PlnBasePlineLgEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() != 1) {
            return null;
        }
        else {
            return list.get(0);
        }
    }

    /**
     * 计算尾加量　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　
     * @param cutLenEnd 　切尾
     * @param dimater 　直径　
     * @param flow 　流数
     * @param zbLeftWgt 中包铸余
     * @return
     */
    static double CalculateEndAdd(double cutLenEnd, double dimater,
            double flow, double zbLeftWgt) {
        // 计算米单重
        double mSingleWeight = (7.8 * Math.PI / 4 * dimater * dimater) / 1000 / 1000;
        // 计算尾炉加量
        double endAddWgt = mSingleWeight * cutLenEnd * flow + zbLeftWgt;
        return endAddWgt;
    }

    /**
     * 查询连铸规格切头尾参数
     * @param con
     * @param dimater
     * @return 0 切头 1不换中包切尾长（m）2换中包切尾长（m）
     * @throws SQLException
     */
    static double[] getBaseLzSpecParams(Connection con, String dimater,
            String castMode) throws SQLException {
        PlnBaseLzSpecCutEntity entity = getBaseLzSpecCut(con, dimater);
        if (entity == null) {
            return null;
        }
        double cutLenBegin = 0;
        if (!entity.getCutLenBegin().equals("")) {
            cutLenBegin = Double.parseDouble(entity.getCutLenBegin());
        }
        double cutLenEndUnch = 0;
        if (!entity.getCutLenEndUnch().equals("")) {
            cutLenEndUnch = Double.parseDouble(entity.getCutLenEndUnch());
        }
        double cutLenEndCh = 0;
        if (!entity.getCutLenEndCh().equals("")) {
            cutLenEndCh = Double.parseDouble(entity.getCutLenEndCh());
        }
        double[] param = new double[2];
        param[0] = cutLenBegin;
        if (castMode.equals("1")) {
            param[1] = cutLenEndCh;
        }
        else {
            param[1] = cutLenEndUnch;
        }
        return param;
    }

    /**
     * 查询连铸规格切头尾参数表
     * @param con
     * @param dimater 直径
     * @return
     * @throws SQLException
     */
    static PlnBaseLzSpecCutEntity getBaseLzSpecCut(Connection con,
            String dimater) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.getBaseLzSpecCut");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, dimater);
        ResultSet rs = ps.executeQuery();
        List<PlnBaseLzSpecCutEntity> list = EntityTool.getEntitysByRs(rs,
                PlnBaseLzSpecCutEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {
            return null;
        }
        else {
            return list.get(0);
        }
    }

    /**
     * 内部类
     * @author Administrator
     */
    class ReturnEntity {
        private PlnSteelforfurnaceEntity furEntity;
        private List<PlnSteelforOrdEntity> ordEntityList = new ArrayList<PlnSteelforOrdEntity>();
        private double beginAddWgt = 0;
        private double endAddWgt = 0;
        private String beginGradename = "";
        private String endGradename = "";
        private double caseWeght = 0;
        private int lus = 0;
        boolean flag = false;

        /**
         * 浇次重量
         */
        public double getCaseWeght() {
            return caseWeght;
        }

        /**
         * 浇次重量
         */
        public void setCaseWeght(double caseWeght) {
            this.caseWeght = caseWeght;
        }

        /**
         * 炉数
         */
        public int getLus() {
            return lus;
        }

        /**
         * 炉数
         */
        public void setLus(int lus) {
            this.lus = lus;
        }

        /**
         * 首炉加量
         * @param beginAddWgt
         */
        public double getBeginAddWgt() {
            return beginAddWgt;
        }

        /**
         * 首炉加量
         * @param beginAddWgt
         */
        public void setBeginAddWgt(double beginAddWgt) {
            this.beginAddWgt = beginAddWgt;
        }

        /**
         * 尾炉加量
         * @param beginAddWgt
         */
        public double getEndAddWgt() {
            return endAddWgt;
        }

        /**
         * 尾炉加量
         * @param beginAddWgt
         */
        public void setEndAddWgt(double endAddWgt) {
            this.endAddWgt = endAddWgt;
        }

        /**
         * 开始钢种
         * @param beginGradename
         */
        public String getBeginGradename() {
            return beginGradename;
        }

        /**
         * 开始钢种
         * @param beginGradename
         */
        public void setBeginGradename(String beginGradename) {
            this.beginGradename = beginGradename;
        }

        /**
         * 结束钢种
         * @param beginGradename
         */
        public String getEndGradename() {
            return endGradename;
        }

        /**
         * 结束钢种
         * @param beginGradename
         */
        public void setEndGradename(String endGradename) {
            this.endGradename = endGradename;
        }

        String message = "";

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }

        public PlnSteelforfurnaceEntity getFurEntity() {
            return furEntity;
        }

        public void setFurEntity(PlnSteelforfurnaceEntity furEntity) {
            this.furEntity = furEntity;
        }

        public List<PlnSteelforOrdEntity> getOrdEntityList() {
            return ordEntityList;
        }

        public void setOrdEntityList(List<PlnSteelforOrdEntity> ordEntityList) {
            this.ordEntityList = ordEntityList;
        }
    }
}