package com.steering.pss.plnsaleord.steelMarkingPlan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.processOrder.base.BaseHelper;

public class FrmSteeSerialPublic extends DBCommon {

    /**
     * 删除炉次订单序号自增减连铸序号
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     */
    public static void seqIncreaseOrdDelete(Connection con, String plineCode,
            int proSeqB, int num) throws SQLException {
        String sql = " UPDATE PLN_STEELFOR_ORD T  SET T.CCM_SEQ = T.CCM_SEQ + ?  "
                + " WHERE T.PLINE_CODE = ? AND T.CCM_SEQ >=? AND T.CCM_PLN_TYPE='00'";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, num);
        ps.setObject(2, plineCode);
        ps.setObject(3, proSeqB);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 删除炉次订单序号自增减连铸序号,訂單序號
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     */
    public static void seqIncreaseOrdDeleteSeq(Connection con,
            String plineCode, int num, int sequenceNo, String furnaceno)
            throws SQLException {
        String sql = " UPDATE PLN_STEELFOR_ORD T  SET T.CUT_NUM = T.CUT_NUM +?  "
                + " WHERE T.PLINE_CODE = ? AND T.CUT_NUM >=? AND"
                + " (T.SURPLUS_TYPE NOT IN ('2','3') OR T.SURPLUS_TYPE IS NULL)  AND T.FURNACENO=? AND T.CCM_PLN_TYPE='00'";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, num);
        ps.setObject(2, plineCode);
        ps.setObject(3, sequenceNo);
        ps.setObject(4, furnaceno);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 炉次订单序号自增减
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     */
    public static void seqIncreaseOrd(Connection con, String plineCode,
            int proSeqB, int num, int proSeqE) throws SQLException {
        String sql = " UPDATE PLN_STEELFOR_ORD T  SET T.CCM_SEQ = T.CCM_SEQ + ?  "
                + " WHERE T.PLINE_CODE = ? ";
        if (proSeqE > 0) {
            sql += " AND T.CCM_SEQ >=?";
        }
        else {
            sql += " AND T.CCM_SEQ >=?";
        }
        sql+=" AND T.CCM_PLN_TYPE='00'";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, num);
        ps.setObject(2, plineCode);
        ps.setObject(3, proSeqB);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 删除时浇次,炉次,炉次订单浇次序号自增减
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     */
    public static void seqIncreaseCast(Connection con, String plineCode,
            int proSeqB, int num) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<String> castList = new ArrayList<String>();
        // 查询大于当前浇次顺序号的浇次号
        String sql = "SELECT T.CAST_NO  FROM PLN_STEELFORCAST T WHERE T.PLINE_CODE = ? AND T.CAST_SEQ >?";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, proSeqB);
        rs = ps.executeQuery();
        while (rs.next()) {
            castList.add(rs.getString("CAST_NO"));
        }
        rs.close();
        ps.close();
        // 修改浇次顺序号
        sql = " UPDATE PLN_STEELFORCAST T SET T.CAST_SEQ = T.CAST_SEQ + ?  "
                + " WHERE T.PLINE_CODE = ? AND T.CAST_SEQ >=?";
        ps = con.prepareStatement(sql);
        ps.setObject(1, num);
        ps.setObject(2, plineCode);
        ps.setObject(3, proSeqB);
        ps.executeUpdate();
        ps.close();
        for (int i = 0; i < castList.size(); i++) {
            // 修改炉次表浇次顺序号
            sql = " UPDATE PLN_STEELFORFURNACE T SET T.CAST_SEQ="
                    + "(SELECT CAST_SEQ FROM PLN_STEELFORCAST WHERE CAST_NO=?) "
                    + " WHERE T.CAST_NO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, castList.get(i));
            ps.setObject(2, castList.get(i));
            ps.executeUpdate();
            ps.close();
            // 修改炉次订单表浇次顺序号
            sql = " UPDATE PLN_STEELFOR_ORD T SET T.CAST_SEQ="
                    + "(SELECT CAST_SEQ FROM PLN_STEELFORCAST WHERE CAST_NO=? ) "
                    + " WHERE T.CAST_NO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, castList.get(i));
            ps.setObject(2, castList.get(i));
            ps.executeUpdate();
            ps.close();
        }
    }

    /**
     * 删除时炉次,炉次订单炉次序号自增减
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     * @throws ParseException
     * @throws NumberFormatException
     */
    public static void seqIncreaseFurn(Connection con, String plineCode,
            int proSeqB, int num, String stratTime, String capacity,
            String capacityZz, int castSeq, int castStoveSeq,
            String plineCodeA, String castNo, String flag) throws SQLException,
            NumberFormatException, ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<Object> furnList = new ArrayList<Object>();
        // 查询炉次
//        String sql = "SELECT T.FURNACENO,T.CAST_STOVE_SEQ FROM PLN_STEELFORFURNACE T "
//                + "WHERE T.EF_PLINE_CODE = ? AND T.FURNACENO_SEQ>=? ORDER BY T.FURNACENO ASC,T.FURNACENO_SEQ ASC";
        int minFurn=1;
        String minsql="SELECT MIN(T.FURNACENO_SEQ) FURNACENO_SEQ   FROM PLN_STEELFORFURNACE T WHERE T.EF_PLINE_CODE = ? AND T.STATUS = '00'";
        ps = con.prepareStatement(minsql);
        ps.setObject(1, plineCodeA);
        rs = ps.executeQuery();
        if (rs.next()) {
            minFurn=rs.getInt("FURNACENO_SEQ");
        }
        rs.close();
        ps.close();
        String sql = "SELECT T.FURNACENO,T.CAST_STOVE_SEQ FROM PLN_STEELFORFURNACE T "
            + "WHERE T.EF_PLINE_CODE = ? AND T.STATUS='00' ORDER BY T.FURNACENO ASC,T.FURNACENO_SEQ ASC";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCodeA);
//        ps.setObject(2, proSeqB);
        rs = ps.executeQuery();
        while (rs.next()) {
            // ArrayList<String> list = new ArrayList<String>();
            // list.add();
            // list.add(rs.getString("CAST_STOVE_SEQ"));
            furnList.add(rs.getString("FURNACENO"));
        }
        rs.close();
        ps.close();
        if (!flag.equals("1")) {
            // 修改炉次顺序号
            sql = "  UPDATE PLN_STEELFORFURNACE T SET T.CAST_STOVE_SEQ=T.CAST_STOVE_SEQ+?"
                    + " WHERE T.EF_PLINE_CODE=? AND T.FURNACENO_SEQ>=? AND T.CAST_NO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, castStoveSeq);
            ps.setObject(2, plineCodeA);
            ps.setObject(3, proSeqB);
            ps.setObject(4, castNo);
            ps.executeUpdate();
            ps.close();
        }
//        sql = "  UPDATE PLN_STEELFORFURNACE T SET T.FURNACENO_SEQ=T.FURNACENO_SEQ+?"
//            + " WHERE T.EF_PLINE_CODE=? AND T.FURNACENO_SEQ>=?";
//    ps = con.prepareStatement(sql);
//    ps.setObject(1, num);
//    ps.setObject(2, plineCodeA);
//    ps.setObject(3, proSeqB);
//    ps.executeUpdate();
//    ps.close();
        String statTime = stratTime;

        for (int i = 0; i < furnList.size(); i++) {
            int furnseq=minFurn+i;
            // 修改炉次电炉顺序号
            sql = "  UPDATE PLN_STEELFORFURNACE T SET T.FURNACENO_SEQ=? "
                    + " WHERE T.EF_PLINE_CODE=? AND T.FURNACENO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, furnseq);
            ps.setObject(2, plineCodeA);
            ps.setObject(3,furnList.get(i));
            ps.executeUpdate();
            ps.close();
            
            // 修改炉次订单表炉次顺序号
            sql = "  UPDATE PLN_STEELFOR_ORD T SET T.FURNACENO_SEQ="
                    + " (SELECT FURNACENO_SEQ FROM PLN_STEELFORFURNACE WHERE FURNACENO=?),"
                    + " T.CAST_STOVE_SEQ=(SELECT CAST_STOVE_SEQ FROM PLN_STEELFORFURNACE WHERE FURNACENO=?)"
                    + " WHERE T.FURNACENO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, furnList.get(i));
            ps.setObject(2, furnList.get(i));
            ps.setObject(3, furnList.get(i));
            ps.executeUpdate();
            ps.close();
        }
        String castNoSeq = "";
        String proMonth = "";
        // 修改查询浇次对应炉号
        sql = "SELECT T1.FURNACENO,T.CAST_NO FROM PLN_STEELFORCAST T,PLN_STEELFORFURNACE T1 "
                + "WHERE T.CAST_NO=T1.CAST_NO AND T1.EF_PLINE_CODE=? AND T1.FURNACENO_SEQ>=? ORDER BY T1.FURNACENO ASC ,T1.CAST_STOVE_SEQ ASC";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCodeA);
        ps.setObject(2, proSeqB);
        rs = ps.executeQuery();
        while (rs.next()) {
//            if (castNoSeq.equals("")
//                    || !castNoSeq.equals(rs.getString("CAST_NO"))) {
                Date date = df.parse(statTime);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.HOUR, 3);// 24小时制
                date = cal.getTime();
                proMonth = df.format(date).substring(0, 10).replace("-", "");
//                castNoSeq = rs.getString("CAST_NO");
//            }
            String plnEndtime = df.format(BaseHelper.dateAddSecond(df
                    .parse(statTime), (int) Math.ceil(Double
                    .parseDouble(capacity)) * 60));
            // 修改炉次计划开始结束时间
            sql = "  UPDATE PLN_STEELFORFURNACE SET PLN_STARTTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),"
                    + "PLN_ENDTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),PRO_MONTH=? WHERE FURNACENO=?";
            PreparedStatement ps2 = con.prepareStatement(sql);
            ps2.setObject(1, statTime);
            ps2.setObject(2, plnEndtime);
            ps2.setObject(3, proMonth);
            ps2.setObject(4, rs.getString("FURNACENO"));
            ps2.executeUpdate();
            ps2.close();

            // 修改炉次订单生产年月
            sql = "  UPDATE PLN_STEELFOR_ORD SET PRO_MONTH=? WHERE FURNACENO=?";
            ps2 = con.prepareStatement(sql);
            ps2.setObject(1, proMonth);
            ps2.setObject(2, rs.getString("FURNACENO"));
            ps2.executeUpdate();
            ps2.close();
            statTime = plnEndtime;
        }
        rs.close();
        ps.close();
        String castNoSeq1 = "";
        String proMonth1 = "";
        // 查询浇次对应的炉次信息最大时间
        sql = "SELECT * FROM(SELECT MAX(TO_CHAR(T1.PLN_ENDTIME,'YYYY-MM-DD HH24:MI:SS')) PLN_ENDTIME ,T.CAST_NO FROM PLN_STEELFORCAST T,"
                + "PLN_STEELFORFURNACE T1 WHERE T.CAST_NO=T1.CAST_NO AND T1.EF_PLINE_CODE=? AND T1.FURNACENO_SEQ>=? GROUP BY T.CAST_NO) ORDER BY CAST_NO";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCodeA);
        ps.setObject(2, proSeqB);
        rs = ps.executeQuery();
        while (rs.next()) {
            if (castNoSeq1.equals("")
                    || !castNoSeq1.equals(rs.getString("CAST_NO"))) {
                Date date = df.parse(statTime);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.HOUR, 3);// 24小时制
                date = cal.getTime();
                proMonth1 = df.format(date).substring(0, 10).replace("-", "");
                castNoSeq1 = rs.getString("CAST_NO");
            }
            String plnEndtimejc = df.format(BaseHelper.dateAddSecond(df
                    .parse(rs.getString("PLN_ENDTIME")), (int) Math.ceil(Double
                    .parseDouble(capacityZz)) * 60));
            // 修改浇次开始结束时间
            sql = " UPDATE PLN_STEELFORCAST SET STARTTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),"
                    + "ENDTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),PRO_MONTH=? WHERE CAST_NO=?";
            PreparedStatement ps1 = con.prepareStatement(sql);
            ps1.setObject(1, statTime);
            ps1.setObject(2, plnEndtimejc);
            ps1.setObject(3, proMonth1);
            ps1.setObject(4, rs.getString("CAST_NO"));
            ps1.executeUpdate();
            statTime = rs.getString("PLN_ENDTIME");
        }
        rs.close();
        ps.close();
    }

    /**
     *查询电炉产线能力
     * @param con
     * @param plineCode
     * @param wldescIn
     * @param materialNo
     * @throws SQLException
     */
    public static double seqCapacityFurn(Connection con, String plineCode,
            String wldescIn, String materialNo) throws SQLException {
        double capacity = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.seqCapacityFurn");
        if (!wldescIn.equals("")) {
            sql += " AND T.IN_MATERIAL_NO='" + wldescIn + "'";
        }
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, materialNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            capacity = rs.getDouble("HR_CAPCTY");
        }
        rs.close();
        ps.close();

        return capacity;
    }

    /**
     *查询铸造产线能力
     * @param con
     * @param plineCode
     * @param wldescIn
     * @param materialNo
     * @throws SQLException
     */
    public static double seqCapacityCast(Connection con, String plineCode,
            String wldescIn, String materialNo) throws SQLException {
        double capacity = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.seqCapacityCast");
        if (!wldescIn.equals("")) {
            sql += " AND T.IN_MATERIAL_NO='" + wldescIn + "'";
        }
        sql += " ORDER BY T.PLINE_LEVEL DESC";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, materialNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            capacity = rs.getDouble("HR_CAPCTY");
        }
        rs.close();
        ps.close();

        return capacity;
    }

    /**
     * 调整时间炉次,炉次订单炉次修改计划开始结束时间
     * @param con
     * @param plineCode
     * @param gxProSeqB
     * @param num
     * @throws SQLException
     * @throws ParseException
     * @throws NumberFormatException
     */
    public static void seqUpdateTime(Connection con, String plineCode,
            String stratTime,int furnacenoSeq)
            throws SQLException, NumberFormatException, ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PreparedStatement ps = null;
        ResultSet rs = null;
        String statTime = stratTime;
//        String castNoSeq = "";
        String proMonth = "";
        // 修改大于等于当前电炉顺序好炉数
        String sql = "SELECT T1.FURNACENO,T1.CAST_NO,T1.EF_PLINE_CODE,T1.WLID_IN,T1.MATERIAL_NO FROM PLN_STEELFORFURNACE T1 "
                + "WHERE  T1.EF_PLINE_CODE=? AND T1.FURNACENO_SEQ>=? ORDER BY T1.FURNACENO_SEQ ASC";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, furnacenoSeq);
        rs = ps.executeQuery();
        while (rs.next()) {
//            if (castNoSeq.equals("")
//                   || !castNoSeq.equals(rs.getString("FURNACENO"))) {
                Date date = df.parse(statTime);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.HOUR, 3);// 24小时制
                date = cal.getTime();
                proMonth = df.format(date).substring(0, 10).replace("-", "");
//                castNoSeq = rs.getString("CAST_NO");
//            }       
            // 查询电炉产线，钢种对应投入产出小时能力
            double capacity = FrmSteeSerialPublic.seqCapacityFurn(con,
                    rs.getString("EF_PLINE_CODE"), rs.getString("WLID_IN"), rs.getString("MATERIAL_NO"));
            // String proMonth = statTime.substring(0, 10).replace("-", "");
            String plnEndtime = df.format(BaseHelper.dateAddSecond(df
                    .parse(statTime), (int)capacity * 60));
            // 修改炉次计划开始结束时间
            String sqlupdate = "  UPDATE PLN_STEELFORFURNACE SET PLN_STARTTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),"
                    + "PLN_ENDTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),PRO_MONTH=? WHERE FURNACENO=?";// ,PRO_MONTH=?
            PreparedStatement ps2 = con.prepareStatement(sqlupdate);
            ps2.setObject(1, statTime);
            ps2.setObject(2, plnEndtime);
            ps2.setObject(3, proMonth);
            ps2.setObject(4, rs.getString("FURNACENO"));
            ps2.executeUpdate();
            ps2.close();

            // 修改炉次订单生产年月
            sqlupdate = "  UPDATE PLN_STEELFOR_ORD SET PRO_MONTH=? WHERE FURNACENO=?";
            ps2 = con.prepareStatement(sqlupdate);
            ps2.setObject(1, proMonth);
            ps2.setObject(2, rs.getString("FURNACENO"));
            ps2.executeUpdate();
            ps2.close();
            statTime = plnEndtime;
        }
        rs.close();
        ps.close();
        String castNoSeq1 = "";
        String proMonth1 = "";
        // 查询大于等于当前电炉顺序好炉数对应浇次
        sql = "SELECT DISTINCT CAST_NO  FROM (SELECT T1.CAST_NO  FROM PLN_STEELFORFURNACE T1 WHERE T1.EF_PLINE_CODE = ? "
                + "AND T1.FURNACENO_SEQ >= ? ORDER BY T1.FURNACENO_SEQ ASC)";
        ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, furnacenoSeq);
        rs = ps.executeQuery();
        while (rs.next()) {
            String stratTimejc = "";
            String endTimejc = "";
            String sqlTime = "SELECT MIN(TO_CHAR(T.PLN_STARTTIME,'YYYY-MM-DD HH24:MI:SS')) PLN_STARTTIME,"
                    + " MAX(TO_CHAR(T.PLN_ENDTIME,'YYYY-MM-DD HH24:MI:SS')) PLN_ENDTIME FROM PLN_STEELFORFURNACE  T WHERE T.CAST_NO=? ";
            PreparedStatement ps1 = con.prepareStatement(sqlTime);
            ps1.setObject(1, rs.getString("CAST_NO"));
            ResultSet rs1 = ps1.executeQuery();
            if (rs1.next()) {
                stratTimejc = rs1.getString("PLN_STARTTIME");
                endTimejc = rs1.getString("PLN_ENDTIME");
            }
            rs1.close();
            ps1.close();
            if (castNoSeq1.equals("")
                    || !castNoSeq1.equals(rs.getString("CAST_NO"))) {
                Date date = df.parse(stratTimejc);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.HOUR, 3);// 24小时制
                date = cal.getTime();
                proMonth1 = df.format(date).substring(0, 10).replace("-", "");
                castNoSeq1 = rs.getString("CAST_NO");
            }
            // String proMonth = stratTimejc.substring(0, 10).replace("-", "");
            // 修改浇次开始结束时间
            String sqlupdate = " UPDATE PLN_STEELFORCAST SET STARTTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),"
                    + "ENDTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),PRO_MONTH=? WHERE CAST_NO=?";
            ps1 = con.prepareStatement(sqlupdate);
            ps1.setObject(1, stratTimejc);
            ps1.setObject(2, endTimejc);
            ps1.setObject(3, proMonth1);
            ps1.setObject(4, rs.getString("CAST_NO"));
            ps1.executeUpdate();
        }
        rs.close();
        ps.close();
    }

}
