package com.steering.pss.plnsaleord.steelMarkingPlan;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnOrderCastSEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforOrdEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforcastEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforfurnaceEntity;
import org.apache.commons.lang.StringUtils;
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.*;

/**
 * 炉次合并,及调整
 * @author Administrator 2017/08/09 tjg
 */
public class FrmSteelMerge extends DBCommon {

    /**
     * 查询炼钢数据
     * @param plinecode
     * @return
     */
    public CoreReturnObject getBasePlineLg(String plinecode) {
        Object[] params = new Object[] { plinecode };
        return this.getDataBySqlID("FrmSteelMakingPlanChange.getBasePlineLg",
                params);
    }

    /**
     * 查询铸造数据
     * @param plinecode
     * @return
     */
    public CoreReturnObject getBasePlineLz(String plinecode) {
        Object[] params = new Object[] { plinecode };
        return this.getDataBySqlID("FrmSteelMakingPlanChange.getBasePlineLz",
                params);
    }

    /**
     * 查询钢种索引码及钢种索引码集合
     * @param
     * @return
     */
    public CoreReturnObject getDoGraderCIC(String furnNo1, String furnNo2) {
        Object[] params = new Object[] { furnNo1, furnNo1, furnNo1, furnNo2,
                furnNo2, furnNo2, furnNo1 + furnNo2, furnNo2 + furnNo1 };
        return this.getDataBySqlID("FrmSteelMerge.getDoGraderCIC", params);
    }

    /**
     * 查询是否存在CIC交集
     * @param
     * @return
     */
    public CoreReturnObject getQueryCicAll(String cic) {
        Object[] params = new Object[] { cic };
        return this.getDataBySqlID("FrmSteelMerge.getQueryCicAll", params);
    }

    /**
     * 根据铸造订单,排产序号,CIC,合并炉次是否有相同记录
     * @param con
     * @param
     * @return
     * @throws SQLException
     */
    public static List<PlnSteelforOrdEntity> getQueryFurnOrd(Connection con,
            String furnNo, String proPlanId, String gxPlanNo, String cic)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.QueryFurnOrd");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ps.setObject(2, proPlanId);
        ps.setObject(3, gxPlanNo);
        ps.setObject(4, cic);
        ResultSet rs = ps.executeQuery();
        List<PlnSteelforOrdEntity> list = EntityTool.getEntitysByRs(rs,
                PlnSteelforOrdEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {
            return null;
        }
        else {
            return list;
        }
    }

    /**
     * 查询最大炉内顺序号
     * @param con
     * @param
     * @return
     * @throws SQLException
     */
    public static int getMaxSequenceno(Connection con, String furnNo)
            throws SQLException {
        int sequenceno = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.QueryMaxSequenceno");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            sequenceno = rs.getInt("SEQUENCENO") + 1;
        }
        rs.close();
        ps.close();
        return sequenceno;
    }

    /**
     * 查询最大分切
     * @param con
     * @param
     * @return
     * @throws SQLException
     */
    public int getMaxCutNumNo(Connection con, String furnNo)
            throws SQLException {
        int cutNumNo = 0;
        String sql = "SELECT MAX(T.CUT_NUM) CUT_NUM FROM PLN_STEELFOR_ORD T "
                + " WHERE T.FURNACENO = ?  AND T.CCM_PLN_TYPE ='00' AND (T.SURPLUS_TYPE NOT IN ('2','3') OR T.SURPLUS_TYPE IS NULL)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cutNumNo = rs.getInt("CUT_NUM") + 1;
        }
        rs.close();
        ps.close();
        return cutNumNo;
    }

    /**
     * 查询连铸订单
     * @param con
     * @param
     * @param
     * @return PlnOrderCastSEntity
     * @throws SQLException
     */
    public static PlnSteelforOrdEntity getOrderOrdStee(Connection con,
            String furnaceNo, String sequenceNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.getOrderOrdStee");
        sql += " AND T.FURNACENO=? AND T.SEQUENCENO=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnaceNo);
        ps.setObject(2, sequenceNo);
        ResultSet rs = ps.executeQuery();
        List<PlnSteelforOrdEntity> list = EntityTool.getEntitysByRs(rs,
                PlnSteelforOrdEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 通过炉号，炉次顺序号查询炉次
     * @param con
     * @param
     * @return
     * @throws SQLException
     */
    static PlnSteelforfurnaceEntity getSteelFurnSeqHb(Connection con,
            String furnNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.getSteelFurnSeqHb");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ResultSet rs = ps.executeQuery();
        PlnSteelforfurnaceEntity entity = EntityTool.getEntityByRs(rs,
                PlnSteelforfurnaceEntity.class);
        rs.close();
        ps.close();
        return entity;
    }

    /**
     * 炉次合并
     * @param
     * @return
     */
    public CoreReturnObject doUpdateFurnOrd(ArrayList<String> jsonList,
            String furnNo1, String furnNo2, String totWeightM,
            String plineCode, String plineName, String makeman, String falg,
            String furnacenoSeq1, String furnacenoSeq2, String cicAll,
            String countHb) {
        CoreReturnObject cro = new CoreReturnObject();
        // 转json为实体类
        List<PlnSteelforOrdEntity> entityList = SteelHelper.jsonsToEntityList(
                PlnSteelforOrdEntity.class, jsonList);
        Date nowTime = new Date();
        SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            ResultSet rs = null;
            // 修改炉次重量
            String sqlupdatef = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.Updatefurnace");
            PreparedStatement psupdateF = conn.prepareStatement(sqlupdatef);
            // 删除炉次
            String sqldeletef = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.Deletefurnace");
            PreparedStatement psdeleteF = conn.prepareStatement(sqldeletef);
            // 修改炉次订单重量
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.UpdateFurnOrd");
            PreparedStatement psupdate = conn.prepareStatement(sqlupdate);

            /**
             * 查询电炉的标准出钢量与出钢量下限
             */
            // double[] steelArr =
            // FrmSteelMakingPlanChange.getPlineLgParams(conn,
            // plineCode);
            // if (steelArr == null) {
            // cro.setV_errMsg("电炉(" + plineName + ")没有排产基础数据!");
            // return cro;
            // }
            // double standSteel = steelArr[0];// 标准出钢量
            // double minSteel = steelArr[1];// 出钢量下限
            // double maxSteel = steelArr[2];// 出钢量上限
            // double wtSum = 0;
            double xzwt = 0;
            int xznum = 0;
            String castNo = "";
            int sequenceno = getMaxSequenceno(conn, furnNo2);// 查询最大炉次内顺序号
            int cutNumno = getMaxCutNumNo(conn, furnNo2);// 查询炉次内最大分切
            int ccmSeqNo = FrmSteelPlnZg.getMaxCcmSeqno(conn, plineCode,
                    furnNo2);
            int deleteSeq = 0;// 删除几次炉次订单
            String cutNum = "";// 获取选择炉次最小炉次分切
//            int aimSteefalg = 0;
            for (PlnSteelforOrdEntity entity : entityList) {// 选中记录
                if (!entity.getMergeNo().equals("")) {
                    cro.setV_errMsg("选中炉已同定尺同去向合并，不能炉次合并，请转分切!");
                    return cro;
                }
                if(entity.getIfConfirm().equals("是")){
                	  cro.setV_errMsg("该浇次已确认,不允许合并!");
                      return cro;
                }
                cutNum = entity.getCutNum();
                double wt = Double.parseDouble(entity.getEditWt());
                int num = Integer.parseInt(entity.getEditNum());
                double totWeightMs = Double.parseDouble(entity.getTotWeightM());
                int totNumMs = Integer.parseInt(entity.getTotMumM());
                castNo = entity.getCastNo();
                xzwt += wt;
                xznum += num;
                PlnSteelforOrdEntity entity1 = getOrderOrdStee(conn, entity
                        .getFurnaceno(), entity.getSequenceno());
                // wtSum += wt;
                // 选择炉次订单材剩余量
                int NumM1 = totNumMs - num;
                double wtM1 = totWeightMs - wt;
                if (NumM1 <= 0) {
                    wtM1 = 0;
                }

                // 根据铸造订单,排产序号,CIC,合并炉次是否有相同记录
                List<PlnSteelforOrdEntity> listOrd = getQueryFurnOrd(conn,
                        furnNo2, entity.getProPlanId(), entity.getGxPlanNo(),
                        entity.getCic());
                if (listOrd == null) {
                    int ccmSeqNo1 = ccmSeqNo;
                    if (ccmSeqNo <= 0) {
                        ccmSeqNo1 = Integer.parseInt(entity1.getCcmSeq());
                    }
                    String furnacenoSeq = furnacenoSeq2;
                    // 新增炉次订单
                    entity1.setFurnaceno(furnNo2);
                    entity1.setSequenceno(String.valueOf(sequenceno));
                    entity1.setCutNum(String.valueOf(cutNumno));
                    entity1.setMakeman(makeman);
                    // entity.setMaketime(dFormat.format(nowTime));
                    entity1.setCcmSeq(String.valueOf(ccmSeqNo1));
                    entity1.setTotMumM(String.valueOf(num));
                    entity1.setTotWeightM(String.valueOf(wt));
                    entity1.setFurnacenoSeq(furnacenoSeq);
                    entity1.setMaketime(dFormat.format(nowTime));
                    // if (!entity.getCcmSeq().equals(entity1.getCcmSeq())) {

                    if (ccmSeqNo > 0) {
                        // 更新連鑄訂單順序號
                        FrmSteeSerialPublic.seqIncreaseOrd(conn, plineCode,
                                ccmSeqNo, 1, -1);
                    }
                    //
                    FrmSteeSerialPublic.seqIncreaseOrdDeleteSeq(conn,
                            plineCode, 1, cutNumno, furnNo2);
                    // }
                    String sqladd = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelMerge.AddFurnOrd"),
                            entity1);
                    PreparedStatement psadd = conn.prepareStatement(sqladd);
                    psadd.executeUpdate();
                    psadd.close();
                    sequenceno++;
                    cutNumno++;
                    ccmSeqNo++;
                }
                else {
                    // 修改
                    for (PlnSteelforOrdEntity groupEntity : listOrd) {
                        if (!groupEntity.getStatus().equals("00")) {
                            cro.setV_errMsg("只有编制中状态的炉次订单才能合并!");
                            return cro;
                        }
                        // 修改
                        psupdate.setObject(1, wt);
                        psupdate.setObject(2, num);
                        psupdate.setObject(3, groupEntity.getFurnaceno());
                        psupdate.setObject(4, groupEntity.getSequenceno());
                        psupdate.addBatch();
                        psupdate.executeBatch();
                        break;
                    }
                }
                if (wtM1 <= 0) {
                    int ccmSeq = 0;
                    // 查询炉次下有多少炉次订单
                    String sql = "SELECT T.CCM_SEQ  FROM PLN_STEELFOR_ORD T WHERE T.FURNACENO = ? AND T.SEQUENCENO=?";
                    ps = conn.prepareStatement(sql);
                    ps.setObject(1, entity.getFurnaceno());
                    ps.setObject(2, entity.getSequenceno());
                    rs = ps.executeQuery();
                    if (rs.next()) {
                        ccmSeq = rs.getInt("CCM_SEQ");
                    }
                    rs.close();
                    ps.close();
                    // 删除炉次订单重量
                    String sqldelete = CoreXmlSqlParsersFactory
                            .getSql("FrmSteelMerge.DeleteFurnOrd");
                    PreparedStatement psdelete = conn
                            .prepareStatement(sqldelete);
                    psdelete.setObject(1, entity.getFurnaceno());
                    psdelete.setObject(2, entity.getSequenceno());
                    psdelete.executeUpdate();
                    psdelete.close();
                    FrmSteeSerialPublic.seqIncreaseOrdDelete(conn, plineCode,
                            ccmSeq, -1);

                    deleteSeq++;
                }
                else {
                    // 修改选中炉次订单
                    psupdate.setObject(1, "-" + wt);
                    psupdate.setObject(2, "-" + num);
                    psupdate.setObject(3, entity.getFurnaceno());
                    psupdate.setObject(4, entity.getSequenceno());
                    psupdate.addBatch();
                    psupdate.executeBatch();
                }
            }
            if (deleteSeq > 0) {// 更新炉次订单序号
                FrmSteeSerialPublic.seqIncreaseOrdDeleteSeq(conn, plineCode,
                        -deleteSeq, Integer.parseInt(cutNum), furnNo1);
            }
            double countTotWt = 0;
//            double countTotNum = 0;
            // 查询选择Gird炉次是否全部合并(订单材,头尾加量)
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.QueryFurnGirdOrd");
            sql += " AND (T.SURPLUSFLAG = '0' OR T.SURPLUS_TYPE IN ('2', '3')) ";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, furnNo1);
            rs = ps.executeQuery();
            if (rs.next()) {
                countTotWt = rs.getDouble("TOT_WEIGHT_M");
//                countTotNum = rs.getDouble("TOT_MUM_M");
            }
            rs.close();
            ps.close();

            if (countTotWt <= 0) {

                PlnSteelforfurnaceEntity entityFur = getSteelFurnSeqHb(conn,
                        furnNo1);
                // 删除炉次
                psdeleteF.setObject(1, furnNo1);
                psdeleteF.addBatch();
                psdeleteF.executeBatch();

                String materialNoZz = "";
                // 查询连铸产出物料
                String sqllz = "SELECT T.MATERIAL_NO,T.WLID_IN FROM PLN_STEELFOR_ORD T WHERE T.FURNACENO=?  AND T.SURPLUSFLAG='0' GROUP BY T.MATERIAL_NO,T.WLID_IN";
                ps = conn.prepareStatement(sqllz);
                ps.setObject(1, furnNo2);
                rs = ps.executeQuery();
                if (rs.next()) {
                    materialNoZz = rs.getString("MATERIAL_NO");
                }
                rs.close();
                ps.close();

                // 查询电炉产线，钢种对应投入产出小时能力
                double capacity = FrmSteeSerialPublic.seqCapacityFurn(conn,
                        entityFur.getEfPlineCode(), entityFur.getWlidIn(),
                        entityFur.getMaterialNo());
                // 查询铸造产线，钢种对应投入产出小时能力
                double capacityZz = FrmSteeSerialPublic.seqCapacityCast(conn,
                        entityFur.getPlineCode(), entityFur.getMaterialNo(),
                        materialNoZz);
                // 修改炉次顺序号
                FrmSteeSerialPublic.seqIncreaseFurn(conn, plineCode, Integer
                        .parseInt(entityFur.getFurnacenoSeq()), -1, entityFur
                        .getPlnStarttime(), String.valueOf(capacity), String
                        .valueOf(capacityZz), Integer.parseInt(entityFur
                        .getCastSeq()), -1, entityFur.getEfPlineCode(),
                        entityFur.getCastNo(), "0");

                int count = 0;
                int ccmSeq = 0;
                // 查询炉次下有多少炉次订单
                sql = "SELECT COUNT(*) COUNT,MAX(T.CCM_SEQ) CCM_SEQ  FROM PLN_STEELFOR_ORD T WHERE T.FURNACENO = ?";
                ps = conn.prepareStatement(sql);
                ps.setObject(1, furnNo1);
                rs = ps.executeQuery();
                if (rs.next()) {
                    count = rs.getInt("COUNT");
                    ccmSeq = rs.getInt("CCM_SEQ");
                }
                rs.close();
                ps.close();
                if (ccmSeq > 0) {

                    // 删除炉次订单
                    sql = "DELETE PLN_STEELFOR_ORD T WHERE T.FURNACENO=? ";
                    ps = conn.prepareStatement(sql);
                    ps.setObject(1, furnNo1);
                    ps.executeUpdate();
                    ps.close();
                    // 修改炉次订单顺序号
                    FrmSteeSerialPublic.seqIncreaseOrd(conn, entityFur
                            .getPlineCode(), ccmSeq, -count, 1);

                }
//                aimSteefalg = 1;
            }
            else {
                // 修改炉次
                psupdateF.setObject(1, "-" + xzwt);
                psupdateF.setObject(2, "-" + xznum);
                psupdateF.setObject(3, furnNo1);
                psupdateF.setObject(4, furnNo1);
                psupdateF.addBatch();
                psupdateF.executeBatch();
            }

            double countTotWt1 = 0;
//            int countTotNum1 = 0;
            String ccmSeq = "";
            String sequrNoYc = "";
            int deleteYc = 0;
            int countyc = 0;
            double weightM = 0;
            // 查询合并炉次订单余材信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.QueryFurnGirdOrd");
            sql += " AND T.SURPLUSFLAG='1' AND T.SURPLUS_TYPE='1' ";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, furnNo2);
            rs = ps.executeQuery();
            if (rs.next()) {
                countTotWt1 = rs.getDouble("TOT_WEIGHT_M");
//                countTotNum1 = rs.getInt("TOT_MUM_M");
                ccmSeq = rs.getString("CCM_SEQ");
                sequrNoYc = rs.getString("CUT_NUM");
                deleteYc = rs.getInt("COUNT");
                weightM = rs.getDouble("WEIGHT_M");
            }
            rs.close();
            ps.close();
            // 判断余材是否等于合并的订单材
            if (sequrNoYc != null) {
                if (countTotWt1 - xzwt <= 0.00002) {
                    // 删除余材
                    String sqldeleteYC = "DELETE PLN_STEELFOR_ORD T WHERE T.FURNACENO=? AND T.SURPLUSFLAG='1' AND T.SURPLUS_TYPE='1' ";
                    ps = conn.prepareStatement(sqldeleteYC);
                    ps.setObject(1, furnNo2);
                    ps.executeUpdate();
                    ps.close();
                    countyc++;
                    FrmSteeSerialPublic.seqIncreaseOrd(conn, plineCode, Integer
                            .parseInt(ccmSeq), -1, -1);
                }
                else {

                    int ycNum = (int) Math
                            .floor((countTotWt1 - xzwt) / weightM);
                    if (ycNum < 0) {
                        // 删除余材
                        String sqldeleteYC = "DELETE  PLN_STEELFOR_ORD T WHERE T.FURNACENO=? AND T.SURPLUSFLAG='1' AND T.SURPLUS_TYPE='1' ";
                        ps = conn.prepareStatement(sqldeleteYC);
                        ps.setObject(1, furnNo2);
                        ps.executeUpdate();
                        ps.close();
                        countyc++;
                        FrmSteeSerialPublic.seqIncreaseOrd(conn, plineCode,
                                Integer.parseInt(ccmSeq), -1, -1);
                    }
                    else {
                        // 修改較次量
                        String sqlUPDATEYC = "  UPDATE  PLN_STEELFOR_ORD T SET T.TOT_MUM_M=TO_NUMBER(?),"
                                + "T.TOT_WEIGHT_M=TO_NUMBER(?) WHERE T.FURNACENO=? AND T.SURPLUSFLAG='1' AND T.SURPLUS_TYPE='1'";
                        ps = conn.prepareStatement(sqlUPDATEYC);
                        ps.setObject(1, ycNum);
                        ps.setObject(2, ycNum * weightM);
                        ps.setObject(3, furnNo2);
                        ps.executeUpdate();
                        ps.close();
                    }
                }
            }
            if (countyc > 0) {
                if (deleteYc > 0) {
                    FrmSteeSerialPublic.seqIncreaseOrdDeleteSeq(conn,
                            plineCode, -deleteYc, Integer.parseInt(sequrNoYc),
                            furnNo2);
                }
            }

            // 如果只有两炉的情况并且只剩首尾量时合并成一炉
            if (countHb.equals("2")) {
                int count = 0;
                String surplusType = "";
                String sqltw = "SELECT T.SURPLUS_TYPE FROM PLN_STEELFOR_ORD T WHERE T.FURNACENO=?";
                ps = conn.prepareStatement(sqltw);
                ps.setObject(1, furnNo1);
                rs = ps.executeQuery();
                while (rs.next()) {
                    count++;
                    surplusType = rs.getString("SURPLUS_TYPE");
                }
                rs.close();
                ps.close();
                if (count == 1
                        && (surplusType.equals("2") || surplusType.equals("3"))) {
                    // 修改头尾连铸订单
                    String sqlup = "UPDATE PLN_STEELFOR_ORD T SET T.FURNACENO=? WHERE T.FURNACENO=? ";
                    ps = conn.prepareStatement(sqlup);
                    ps.setObject(1, furnNo2);
                    ps.setObject(2, furnNo1);
                    ps.executeUpdate();
                    ps.close();
                    // 删除炉次订单
                    sql = "DELETE PLN_STEELFORFURNACE T WHERE T.FURNACENO=? ";
                    ps = conn.prepareStatement(sql);
                    ps.setObject(1, furnNo1);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            // 修改合并炉量
            String sqlHbLc = "  UPDATE PLN_STEELFORFURNACE T SET T.QUANTITY=(SELECT SUM(TOT_MUM_M) FROM PLN_STEELFOR_ORD WHERE FURNACENO =?), "
                    + "T.WEIGHT=(SELECT SUM(TOT_WEIGHT_M) FROM PLN_STEELFOR_ORD WHERE FURNACENO =?),T.CIC=? WHERE T.FURNACENO=? ";
            ps = conn.prepareStatement(sqlHbLc);
            ps.setObject(1, furnNo2);
            ps.setObject(2, furnNo2);
            ps.setObject(3, cicAll);
            ps.setObject(4, furnNo2);
            ps.executeUpdate();
            ps.close();

            // 修改較次量
            String sqldelete = " UPDATE PLN_STEELFORCAST T    SET T.TOT_NUM  =  (SELECT SUM(QUANTITY) FROM PLN_STEELFORFURNACE WHERE CAST_NO = ?), "
                    + "      T.TOT_WT       =       (SELECT SUM(WEIGHT) FROM PLN_STEELFORFURNACE WHERE CAST_NO = ?), "
                    + "      T.FURNACENUMBER =       (SELECT COUNT(*) FROM PLN_STEELFORFURNACE WHERE CAST_NO = ?) WHERE T.CAST_NO = ? ";
            ps = conn.prepareStatement(sqldelete);
            ps.setObject(1, castNo);
            ps.setObject(2, castNo);
            ps.setObject(3, castNo);
            ps.setObject(4, castNo);
            ps.executeUpdate();
            ps.close();
            // if (aimSteefalg != 0) {
            // 更新目标钢水量
            updatePlanOrder(conn, castNo);
            // }
            if (falg.equals("0")) {
                cro.setV_errMsg("向下合并成功!");
            }
            else {
                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());
        }
        catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 修改目标值
     * @param
     * @return
     */
    public CoreReturnObject doUpdateCic(ArrayList<ArrayList<String>> jsonList,
            String cic1, String cic2, String name) {
        CoreReturnObject cro = new CoreReturnObject();
        // Date nowTime = new Date();
        // SimpleDateFormat dFormat = new
        // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            // PreparedStatement ps = null;
            // ResultSet rs = null;
            // 修改交集表
            String sqldeletef = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getUpdateSteeleCic");
            PreparedStatement psdeleteF = conn.prepareStatement(sqldeletef);
            for (int i = 0; i < jsonList.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonList.get(i);
                String chenName = list.get(0);
                String stdtarget = list.get(1);
                // 否则修改成分索引码交集表
                psdeleteF.setObject(1, stdtarget);
                psdeleteF.setObject(2, cic1);
                psdeleteF.setObject(3, cic2);
                psdeleteF.setObject(4, chenName);
                psdeleteF.addBatch();

            }
            psdeleteF.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 con
     * @param caseNo
     * @return
     * @throws SQLException
     */
    public static List<PlnSteelforfurnaceEntity> getCastFurnNoList(
            Connection con, String caseNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.getCastFurnNoList");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, caseNo);
        ResultSet rs = ps.executeQuery();
        List<PlnSteelforfurnaceEntity> list = EntityTool.getEntitysByRs(rs,
                PlnSteelforfurnaceEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {
            return null;
        }
        else {
            return list;
        }
    }

    /**
     * 铸造一键获取工艺
     * @param
     * @return
     */
    public CoreReturnObject doProcessAll(ArrayList<ArrayList<String>> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            // List<PlnSteelforfurnaceEntity> entitylc = new
            // ArrayList<PlnSteelforfurnaceEntity>();
            // if (jsonListjc != null && jsonListjc.size() > 0) {
            // // json转化为实体类
            // List<PlnSteelforcastEntity> entityjc = SteelHelper
            // .jsonsToEntityList(PlnSteelforcastEntity.class,
            // jsonListjc);
            // for (PlnSteelforcastEntity entjc : entityjc) {
            // // 根据浇次查询炉次
            // entitylc = getCastFurnNoList(conn, entjc.getCastNo());
            // if (entitylc == null || entitylc.size() == 0) {
            // cro.setV_errMsg("浇次为【" + entjc.getCastNo()
            // + "】的炉次信息有误!");
            // return cro;
            // }
            // for (PlnSteelforfurnaceEntity entlc : entitylc) {
            // PlnSteelforcastEntity entity = ControlCastPlan
            // .getCastByCastno(conn, entlc.getCastNo());
            // if (entity == null) {
            // cro.setV_errMsg("数据异常,请刷新数据!");
            // return cro;
            // }
            // if (Integer.parseInt(entity.getStatus()) >= 1) {
            // cro.setV_errMsg("浇次(" + entlc.getCastNo()
            // + ")不是编制中的状态,不能一键获取工艺!");
            // return cro;
            // }
            // // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
            // String craftNo = FrmSteelMakingPlanChange
            // .queryFurnNoCraftNo(conn, entlc.getCic(), entlc
            // .getEfPlineCode(), entlc.getGradecode());
            //
            // if (!craftNo.equals("")) {
            // String sqlUpdate = CoreXmlSqlParsersFactory
            // .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
            // ps = conn.prepareStatement(sqlUpdate);
            // ps.setObject(1, craftNo);
            // ps.setObject(2, entlc.getFurnaceno());
            // ps.executeUpdate();
            // ps.close();
            // }
            // }
            // }
            // }
            // else {
            // // json转化为实体类
            // entitylc = SteelHelper.jsonsToEntityList(
            // PlnSteelforfurnaceEntity.class, jsonListlc);
            // for (PlnSteelforfurnaceEntity entlc : entitylc) {
            // PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
            // .getFurnaceByFurnaceno(conn, entlc.getFurnaceno());
            // if (entityN == null) {
            // cro.setV_errMsg("数据异常,请重新刷新数据!");
            // return cro;
            // }
            // // 验证数据
            // if (!entityN.getStatus().equals("编制中")) {
            // cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
            // + ")不是编制中的状态,不能一键获取工艺!");
            // return cro;
            // }
            // // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
            // String craftNo = FrmSteelMakingPlanChange
            // .queryFurnNoCraftNo(conn, entlc.getCic(), entlc
            // .getEfPlineCode(), entlc.getGradecode());
            // if (!craftNo.equals("")) {
            // String sqlUpdate = CoreXmlSqlParsersFactory
            // .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
            // ps = conn.prepareStatement(sqlUpdate);
            // ps.setObject(1, craftNo);
            // ps.setObject(2, entlc.getFurnaceno());
            // ps.executeUpdate();
            // ps.close();
            // }
            // }
            // }

            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String proPlanId = list.get(0);
                String gxPlanNo = list.get(1);
                PlnOrderCastSEntity entity1 = ControlOrderPlan
                        .getOrderCastById(conn, proPlanId, gxPlanNo);
                if (entity1 == null) {
                    cro.setV_errMsg("数据异常,请刷新数据!");
                    return cro;
                }
                if (Integer.parseInt(entity1.getExeStatus()) > 1) {
                    cro
                            .setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                                    + entity1.getGxPlanNo()
                                    + "),不是计划和生产中的状态,不能一键获取工艺!");
                    return cro;
                }
                // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
                String craftNo = FrmSteelMakingPlanChange.queryFurnNoCraftNo(
                        conn, entity1.getCic(), entity1.getPlineCodeA(),
                        entity1.getGradecode(), entity1.getStationDesc());
                if (!craftNo.equals("")) {
                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("FrmSteelMerge.getUpdatePlnZzCraftNo");
                    ps = conn.prepareStatement(sqlUpdate);
                    ps.setObject(1, craftNo);
                    ps.setObject(2, proPlanId);
                    ps.setObject(3, gxPlanNo);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            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
     * @return
     */
    public CoreReturnObject doClearProcess(
            ArrayList<ArrayList<String>> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            // List<PlnSteelforfurnaceEntity> entitylc = new
            // ArrayList<PlnSteelforfurnaceEntity>();
            // if (jsonListjc != null && jsonListjc.size() > 0) {
            // // json转化为实体类
            // List<PlnSteelforcastEntity> entityjc = SteelHelper
            // .jsonsToEntityList(PlnSteelforcastEntity.class,
            // jsonListjc);
            // for (PlnSteelforcastEntity entjc : entityjc) {
            // // 根据浇次查询炉次
            // entitylc = getCastFurnNoList(conn, entjc.getCastNo());
            // if (entitylc == null || entitylc.size() == 0) {
            // cro.setV_errMsg("浇次为【" + entjc.getCastNo()
            // + "】的炉次信息有误!");
            // return cro;
            // }
            // for (PlnSteelforfurnaceEntity entlc : entitylc) {
            // PlnSteelforcastEntity entity = ControlCastPlan
            // .getCastByCastno(conn, entlc.getCastNo());
            // if (entity == null) {
            // cro.setV_errMsg("数据异常,请刷新数据!");
            // return cro;
            // }
            // if (Integer.parseInt(entity.getStatus()) >= 1) {
            // cro.setV_errMsg("浇次(" + entlc.getCastNo()
            // + ")不是编制中的状态,不能一键获取工艺!");
            // return cro;
            // }
            // // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
            // String craftNo = FrmSteelMakingPlanChange
            // .queryFurnNoCraftNo(conn, entlc.getCic(), entlc
            // .getEfPlineCode(), entlc.getGradecode());
            //
            // if (!craftNo.equals("")) {
            // String sqlUpdate = CoreXmlSqlParsersFactory
            // .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
            // ps = conn.prepareStatement(sqlUpdate);
            // ps.setObject(1, craftNo);
            // ps.setObject(2, entlc.getFurnaceno());
            // ps.executeUpdate();
            // ps.close();
            // }
            // }
            // }
            // }
            // else {
            // // json转化为实体类
            // entitylc = SteelHelper.jsonsToEntityList(
            // PlnSteelforfurnaceEntity.class, jsonListlc);
            // for (PlnSteelforfurnaceEntity entlc : entitylc) {
            // PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
            // .getFurnaceByFurnaceno(conn, entlc.getFurnaceno());
            // if (entityN == null) {
            // cro.setV_errMsg("数据异常,请重新刷新数据!");
            // return cro;
            // }
            // // 验证数据
            // if (!entityN.getStatus().equals("编制中")) {
            // cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
            // + ")不是编制中的状态,不能一键获取工艺!");
            // return cro;
            // }
            // // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
            // String craftNo = FrmSteelMakingPlanChange
            // .queryFurnNoCraftNo(conn, entlc.getCic(), entlc
            // .getEfPlineCode(), entlc.getGradecode());
            // if (!craftNo.equals("")) {
            // String sqlUpdate = CoreXmlSqlParsersFactory
            // .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
            // ps = conn.prepareStatement(sqlUpdate);
            // ps.setObject(1, craftNo);
            // ps.setObject(2, entlc.getFurnaceno());
            // ps.executeUpdate();
            // ps.close();
            // }
            // }
            // }

            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String proPlanId = list.get(0);
                String gxPlanNo = list.get(1);
                PlnOrderCastSEntity entity1 = ControlOrderPlan
                        .getOrderCastById(conn, proPlanId, gxPlanNo);
                if (entity1 == null) {
                    cro.setV_errMsg("数据异常,请刷新数据!");
                    return cro;
                }
                if (Integer.parseInt(entity1.getExeStatus()) > 1) {
                    cro
                            .setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                                    + entity1.getGxPlanNo()
                                    + "),不是计划和生产中的状态,不能一键取消工艺!");
                    return cro;
                }
                // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件

                String sqlUpdate = CoreXmlSqlParsersFactory
                        .getSql("FrmSteelMerge.getUpdatePlnZzCraftNo");
                ps = conn.prepareStatement(sqlUpdate);
                ps.setObject(1, "");
                ps.setObject(2, proPlanId);
                ps.setObject(3, gxPlanNo);
                ps.executeUpdate();
                ps.close();

            }
            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
     * @return
     */
    public CoreReturnObject doConfirm(ArrayList<ArrayList<String>> jsonListjc,
            String falg) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 修改确认标志
            // String sqlupdate = CoreXmlSqlParsersFactory
            // .getSql("FrmSteelMerge.getUpdateFurnConfirm");
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getUpdateFurnConfirmPlnORDER");
            PreparedStatement psUpdate = con.prepareStatement(sqlupdate);
            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String furnNo = list.get(0);
                String gxPlanNo = list.get(1);
                // PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                // .getFurnaceByFurnaceno(conn, furnNo,gxPlanNo);
                // if (entityN == null) {
                // cro.setV_errMsg("数据异常,请重新刷新数据!");
                // return cro;
                // }
                // // 验证数据
                // if (!entityN.getStatus().equals("编制中")) {
                // cro
                // .setV_errMsg("炉次(" + furnNo
                // + ")不是编制中的状态,不能确认工艺或取消确认工艺!");
                // return cro;
                // }
                // psUpdate.setObject(1, falg);
                // psUpdate.setObject(2, furnNo);
                // psUpdate.addBatch();
                PlnOrderCastSEntity entity1 = ControlOrderPlan
                        .getOrderCastById(con, furnNo, gxPlanNo);
                if (entity1 == null) {
                    cro.setV_errMsg("数据异常,请刷新数据!");
                    return cro;
                }
                if (falg.equals("1")) {
                    if (entity1.getIfConfirm() != null
                            && entity1.getIfConfirm().equals("1")) {
                        cro.setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                                + entity1.getGxPlanNo() + "),炼钢工艺已确认!");
                        return cro;
                    }
                }
                else {
                    if (entity1.getIfConfirm() == null
                            || entity1.getIfConfirm().equals("0")) {
                        cro.setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                                + entity1.getGxPlanNo() + "),炼钢工艺未确认!");
                        return cro;
                    }
                }
                // if (Integer.parseInt(entity1.getExeStatus()) > 1) {
                // cro.setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                // + entity1.getGxPlanNo() + "),不是计划和生产中的状态!");
                // return cro;
                // }
                psUpdate.setObject(1, falg);
                psUpdate.setObject(2, furnNo);
                psUpdate.setObject(3, gxPlanNo);
                psUpdate.addBatch();
            }
            psUpdate.executeBatch();
            if (falg.equals("1")) {
                cro.setV_errMsg("确认成功!");
            }
            else {
                cro.setV_errMsg("取消确认成功!");
            }
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                con.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 炉次一键获取工艺
     * @param
     * @return
     */
    public CoreReturnObject doProcessAllFur(ArrayList<String> jsonListlc,
            ArrayList<String> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            List<PlnSteelforfurnaceEntity> entitylc = new ArrayList<PlnSteelforfurnaceEntity>();
            if (jsonListjc != null && jsonListjc.size() > 0) {
                // json转化为实体类
                List<PlnSteelforcastEntity> entityjc = SteelHelper
                        .jsonsToEntityList(PlnSteelforcastEntity.class,
                                jsonListjc);
                for (PlnSteelforcastEntity entjc : entityjc) {
                    // 根据浇次查询炉次
                    entitylc = getCastFurnNoList(conn, entjc.getCastNo());
                    if (entitylc == null || entitylc.size() == 0) {
                        cro.setV_errMsg("浇次为【" + entjc.getCastNo()
                                + "】的炉次信息有误!");
                        return cro;
                    }

                    for (int i = 0; i < entitylc.size(); i++) {
                        PlnSteelforfurnaceEntity entlc = entitylc.get(i);

                        PlnSteelforcastEntity entity = ControlCastPlan
                                .getCastByCastno(conn, entlc.getCastNo());
                        if (entity == null) {
                            cro.setV_errMsg("数据异常,请刷新数据!");
                            return cro;
                        }
                        if (Integer.parseInt(entity.getStatus()) >= 1) {
                            cro.setV_errMsg("浇次(" + entlc.getCastNo()
                                    + ")不是编制中的状态,不能一键获取工艺!");
                            return cro;
                        }

                        String Cic = entlc.getCic();
                        if (i > 0) {
                            Cic = Cic + "," + entitylc.get(i - 1).getCic();
                        }

                        if (i < entitylc.size() - 1) {
                            Cic = Cic + "," + entitylc.get(i + 1).getCic();
                        }

                        String[] Cics = Cic.split(",");

                        // Arrays.sort(cicAll);
                        TreeSet<String> prodCodeSet = new TreeSet<String>();
                        prodCodeSet.addAll(Arrays.asList(Cics));
                        Cics = prodCodeSet.toArray(new String[] {});
                        Cic = StringUtils.join(Cics, ",");

                        // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
                        String craftNo = FrmSteelMakingPlanChange
                                .queryFurnNoCraftNo(conn, Cic, entlc
                                        .getEfPlineCode(),
                                        entlc.getGradecode(), entlc
                                                .getPlanRoute());

/*                        if (craftNo.equals("")) {
                            craftNo = FrmSteelMakingPlanChange
                                    .queryFurnNoCraftNo(conn, entlc.getCic(),
                                            entlc.getEfPlineCode(), entlc
                                                    .getGradecode(), entlc
                                                    .getPlanRoute());
                        }*/
                        //if (!craftNo.equals("")) {
                            String sqlUpdate = CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
                            ps = conn.prepareStatement(sqlUpdate);
                            ps.setObject(1, craftNo);
                            ps.setObject(2, entlc.getFurnaceno());
                            ps.executeUpdate();
                            ps.close();
                    //    }
                    }
                }
            }
            else {
                // json转化为实体类
                entitylc = SteelHelper.jsonsToEntityList(
                        PlnSteelforfurnaceEntity.class, jsonListlc);
                for (PlnSteelforfurnaceEntity entlc : entitylc) {
                    PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                            .getFurnaceByFurnaceno(conn, entlc.getFurnaceno());
                    if (entityN == null) {
                        cro.setV_errMsg("数据异常,请重新刷新数据!");
                        return cro;
                    }
                    // 验证数据
                    if (!entityN.getStatus().equals("编制中")) {
                        cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
                                + ")不是编制中的状态,不能一键获取工艺!");
                        return cro;
                    }
                    // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
                    String craftNo = FrmSteelMakingPlanChange
                            .queryFurnNoCraftNo(conn, entlc.getCic(), entlc
                                    .getEfPlineCode(), entlc.getGradecode(),
                                    entlc.getPlanRoute());
                    if (!craftNo.equals("")) {
                        String sqlUpdate = CoreXmlSqlParsersFactory
                                .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
                        ps = conn.prepareStatement(sqlUpdate);
                        ps.setObject(1, craftNo);
                        ps.setObject(2, entlc.getFurnaceno());
                        ps.executeUpdate();
                        ps.close();
                    }
                }
            }

            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
     * @return
     */
    public CoreReturnObject doClearProcessFur(ArrayList<String> jsonListlc,
            ArrayList<String> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            List<PlnSteelforfurnaceEntity> entitylc = new ArrayList<PlnSteelforfurnaceEntity>();
            if (jsonListjc != null && jsonListjc.size() > 0) {
                // json转化为实体类
                List<PlnSteelforcastEntity> entityjc = SteelHelper
                        .jsonsToEntityList(PlnSteelforcastEntity.class,
                                jsonListjc);
                for (PlnSteelforcastEntity entjc : entityjc) {
                    // 根据浇次查询炉次
                    entitylc = getCastFurnNoList(conn, entjc.getCastNo());
                    if (entitylc == null || entitylc.size() == 0) {
                        cro.setV_errMsg("浇次为【" + entjc.getCastNo()
                                + "】的炉次信息有误!");
                        return cro;
                    }
                    for (PlnSteelforfurnaceEntity entlc : entitylc) {
                        PlnSteelforcastEntity entity = ControlCastPlan
                                .getCastByCastno(conn, entlc.getCastNo());
                        if (entity == null) {
                            cro.setV_errMsg("数据异常,请刷新数据!");
                            return cro;
                        }
                        if(entlc.getIfConfirm().equals("1")){ 
                        	cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
                                + ")已工艺确认,不能取消工艺!");
                        return cro;}
                        if (Integer.parseInt(entity.getStatus()) >= 1) {
                            cro.setV_errMsg("浇次(" + entlc.getCastNo()
                                    + ")不是编制中的状态,不能取消工艺!");
                            return cro;
                        }
                        // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件

                        String sqlUpdate = CoreXmlSqlParsersFactory
                                .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
                        ps = conn.prepareStatement(sqlUpdate);
                        ps.setObject(1, "");
                        ps.setObject(2, entlc.getFurnaceno());
                        ps.executeUpdate();
                        ps.close();

                    }
                }
            }
            else {
                // json转化为实体类
                entitylc = SteelHelper.jsonsToEntityList(
                        PlnSteelforfurnaceEntity.class, jsonListlc);
                for (PlnSteelforfurnaceEntity entlc : entitylc) {
                    PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                            .getFurnaceByFurnaceno(conn, entlc.getFurnaceno());
                    if (entityN == null) {
                        cro.setV_errMsg("数据异常,请重新刷新数据!");
                        return cro;
                    }
                    if(entlc.getIfConfirm().equals("1")){ 
                    	cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
                            + ")已工艺确认,不能取消工艺!");
                    return cro;}
                    // 验证数据
                    if (!entityN.getStatus().equals("编制中")) {
                        cro.setV_errMsg("炉次(" + entlc.getFurnaceno()
                                + ")不是编制中的状态,不能取消工艺!");
                        return cro;
                    }
                    // 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件

                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("FrmSteelMerge.getUpdateFurnNoCraftNo");
                    ps = conn.prepareStatement(sqlUpdate);
                    ps.setObject(1, "");
                    ps.setObject(2, entlc.getFurnaceno());
                    ps.executeUpdate();
                    ps.close();

                }
            }

            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
     * @return
     */
    public CoreReturnObject doConfirmFur(
            ArrayList<ArrayList<String>> jsonListjc, String falg) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 修改确认标志
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getUpdateFurnConfirm");
            PreparedStatement psUpdate = con.prepareStatement(sqlupdate);
            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String furnNo = list.get(0);
                PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                        .getFurnaceByFurnaceno(con, furnNo);
                if (entityN == null) {
                    cro.setV_errMsg("数据异常,请重新刷新数据!");
                    return cro;
                }
                // 验证数据
                if (!entityN.getStatus().equals("编制中")) {
                    cro
                            .setV_errMsg("炉次(" + furnNo
                                    + ")不是编制中的状态,不能确认工艺或取消确认工艺!");
                    return cro;
                }
                psUpdate.setObject(1, falg);
                psUpdate.setObject(2, furnNo);
                psUpdate.addBatch();
            }
            psUpdate.executeBatch();
            if (falg.equals("1")) {
                cro.setV_errMsg("确认成功!");
            }
            else {
                cro.setV_errMsg("取消确认成功!");
            }
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                con.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 调整配料方案
     * @param
     * @return
     */
    public CoreReturnObject doUpdateScheme(
            ArrayList<ArrayList<String>> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            // 修改炉次配料方案
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.doUpdateScheme");
            PreparedStatement psUpdate = conn.prepareStatement(sqlupdate);
            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String batchingCode = list.get(0);
                String batchingName = list.get(1);
                String hotmetalNum = list.get(2);
                String ironNum = list.get(3);
                String steelscrapNum = list.get(4);
                String furnNo = list.get(5);
                PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                        .getFurnaceByFurnaceno(conn, furnNo);
                if (entityN == null) {
                    cro.setV_errMsg("数据异常,请重新刷新数据!");
                    return cro;
                }
                // 验证数据
                if (!entityN.getStatus().equals("编制中")) {
                    cro.setV_errMsg("炉次(" + furnNo + ")不是编制中的状态,不能调整配料方案!");
                    return cro;
                }
                psUpdate.setObject(1, batchingCode);
                psUpdate.setObject(2, batchingName);
                psUpdate.setObject(3, hotmetalNum);
                psUpdate.setObject(4, ironNum);
                psUpdate.setObject(5, steelscrapNum);
                psUpdate.setObject(6, furnNo);
                psUpdate.addBatch();

            }
            psUpdate.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
     * @return
     */
    public CoreReturnObject doUpdatePath(ArrayList<ArrayList<String>> jsonListjc) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            // 修改工艺路径
            String sqlupdate = "UPDATE  PLN_STEELFORFURNACE T SET T.PLAN_ROUTE=?, T.CRAFT_NO=?, T.IF_CONFIRM = '0' WHERE T.FURNACENO=?";
            PreparedStatement psUpdate = conn.prepareStatement(sqlupdate);
            for (int i = 0; i < jsonListjc.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonListjc.get(i);
                String path = list.get(0);
                String furnNo = list.get(1);
                PlnSteelforfurnaceEntity entityN = ControlFurNacePlan
                        .getFurnaceByFurnaceno(conn, furnNo);
                if (entityN == null) {
                    cro.setV_errMsg("数据异常,请重新刷新数据!");
                    return cro;
                }
                // 验证数据
                if (!entityN.getStatus().equals("编制中")) {
                    cro.setV_errMsg("炉次(" + furnNo + ")不是编制中的状态,不能修改工艺路径!");
                    return cro;
                }
                /*
                 * if (!entityN.getCraftNo().equals("")) { cro.setV_errMsg("炉次("
                 * + furnNo + ")炼钢工艺文件不为空,不能修改工艺路径!"); return cro; }
                 */

                String craftNo = FrmSteelMakingPlanChange.queryFurnNoCraftNo(
                        conn, entityN.getCic(), entityN.getEfPlineCode(),
                        entityN.getGradecode(), entityN.getPlanRoute());

                psUpdate.setObject(1, path);
                psUpdate.setObject(2, craftNo);
                psUpdate.setObject(3, furnNo);
                psUpdate.addBatch();

            }
            psUpdate.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
     * @return
     */
    public CoreReturnObject doUpdateLenght(ArrayList<String> jsonList) {
        CoreReturnObject core = new CoreReturnObject();
        // 转json为实体类
        List<PlnSteelforOrdEntity> entityList = SteelHelper.jsonsToEntityList(
                PlnSteelforOrdEntity.class, jsonList);
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 修改炉次订单单倍坯长
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.doUpdateLenght");
            PreparedStatement psUpdate = con.prepareStatement(sqlupdate);

            String castNo = "";
            for (PlnSteelforOrdEntity entity : entityList) {
                PlnSteelforOrdEntity entity1 = ControlFurNacePlan
                        .getSteelOrdById(con, entity.getFurnaceno(), entity
                                .getSequenceno());
                if (entity1 == null) {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    return core;
                }
                if (!entity1.getStatus().equals("编制中")) {
                    core.setV_errMsg("不是编制中状态的炉次订单,不能调整!");
                    return core;
                }
                if(entity.getIfConfirm().equals("是")){
                	 core.setV_errMsg("浇次已确认,不能调整!");
                     return core;
                }
                if (entity1.getSurplusflag().equals("余材")) {
                    if (!entity1.getSurplusType().equals("生产超量")
                            && !entity1.getSurplusType().equals("补炉")) {
                        core.setV_errMsg("不是生产超量或补炉的炉次订单,不能调整!");
                        return core;
                    }
                }
                else {
                    core.setV_errMsg("不是余材的炉次订单,不能调整!");
                    return core;
                }
                castNo = entity1.getCastNo();
                // double zgwtSingle = CoreOrdBase.GpweightOfmi(Double
                // .parseDouble(entity1.getDiameter())); // 坯米单重
                // // 单倍坯重
                // double singleWeight = zgwtSingle
                // * (Double.parseDouble(entity.getLenGpSingle()) / 1000);
                // double totWtM = Double.parseDouble(entity1.getTotWeightM());
                // double lengthLz = Double.parseDouble(entity.getLenGpSingle())
                // * Double.parseDouble(entity1.getCutNumGp());// 倍尺坯长
                // double wtCastOne = zgwtSingle * (lengthLz / 1000);
                // int totNum = (int) Math.ceil(totWtM / wtCastOne);
                psUpdate.setObject(1, entity.getLenGpSingle());
                psUpdate.setObject(2, entity.getLengthM());
                psUpdate.setObject(3, entity.getWeightGpOne());
                psUpdate.setObject(4, entity.getWeightM());
                psUpdate.setObject(5, entity.getTotMumM());
                psUpdate.setObject(6, entity.getTotWeightM());
                psUpdate.setObject(7, entity.getCutNumGp());
                psUpdate.setObject(8, entity.getFurnaceno());
                psUpdate.setObject(9, entity.getSequenceno());
                psUpdate.addBatch();
                psUpdate.executeBatch();
                // 修改炉次量
                String sqlHbLc = "  UPDATE PLN_STEELFORFURNACE T SET T.QUANTITY=(SELECT SUM(TOT_MUM_M) FROM PLN_STEELFOR_ORD WHERE FURNACENO =?), "
                        + "T.WEIGHT=(SELECT SUM(TOT_WEIGHT_M) FROM PLN_STEELFOR_ORD WHERE FURNACENO =?) WHERE T.FURNACENO=? ";
                ps = con.prepareStatement(sqlHbLc);
                ps.setObject(1, entity.getFurnaceno());
                ps.setObject(2, entity.getFurnaceno());
                ps.setObject(3, entity.getFurnaceno());
                ps.executeUpdate();
                ps.close();
            }

            // 修改較次量
            String sqldelete = " UPDATE PLN_STEELFORCAST T    SET T.TOT_NUM  =  (SELECT SUM(QUANTITY) FROM PLN_STEELFORFURNACE WHERE CAST_NO = ?), "
                    + "      T.TOT_WT       = (SELECT SUM(WEIGHT) FROM PLN_STEELFORFURNACE WHERE CAST_NO = ?) "
                    + "  WHERE T.CAST_NO = ? ";
            ps = con.prepareStatement(sqldelete);
            ps.setObject(1, castNo);
            ps.setObject(2, castNo);
            ps.setObject(3, castNo);
            ps.executeUpdate();
            ps.close();
            // 更新目标钢水量
            updatePlanOrder(con, castNo);
            core.setV_errMsg("调整余材成功!");
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                con.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 拆分余材量
     * @param
     * @return
     */
    public CoreReturnObject spitPlanOrder(ArrayList<String> jsonList,
            String spitWgt, String spitNum, String makeman) {
        CoreReturnObject core = new CoreReturnObject();
        // 转json为实体类
        List<PlnSteelforOrdEntity> entityList = SteelHelper.jsonsToEntityList(
                PlnSteelforOrdEntity.class, jsonList);
        core.setResult(false);
        Connection con = null;
        // PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            PreparedStatement ps = null;
            for (PlnSteelforOrdEntity entity : entityList) {
                String sql = CoreXmlSqlParsersFactory
                        .getSql("FrmSteelMerge.getOrderOrdStee");
                sql += "AND T.FURNACENO=? AND T.SEQUENCENO=?";
                ps = con.prepareStatement(sql);
                ps.setObject(1, entity.getFurnaceno());
                ps.setObject(2, entity.getSequenceno());
                ResultSet rs = ps.executeQuery();
                PlnSteelforOrdEntity entity1 = EntityTool.getEntityByRs(rs,
                        PlnSteelforOrdEntity.class);
                rs.close();
                ps.close();
                if (entity1 == null) {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    return core;
                }
                if (!entity1.getStatus().equals("00")) {
                    core.setV_errMsg("不是编制中状态的炉次订单,不能拆分余材!");
                    return core;
                }
                if(entity.getIfConfirm().equals("1")){
                	 core.setV_errMsg("浇次已确认,不能拆分余材!");
                     return core;
                }
                if (entity1.getSurplusflag().equals("1")) {
                    if (!entity1.getSurplusType().equals("1")
                            && !entity1.getSurplusType().equals("6")) {
                        core.setV_errMsg("不是生产超量或补炉的炉次订单,不能拆分!");
                        return core;
                    }
                }
                else {
                    core.setV_errMsg("不是余材的炉次订单,不能拆分!");
                    return core;
                }
                // 拆分量
                double totWeightM = Double.parseDouble(spitWgt);
                int totMumM = Integer.parseInt(spitNum);
                // 总量
                double oldwt = Double.parseDouble(entity1.getTotWeightM());
                int oldnum = Integer.parseInt(entity1.getTotMumM());
                // 剩余量
                double wt = oldwt - totWeightM;
                int num = oldnum - totMumM;

                int sequenceno = Integer.parseInt(entity.getSequenceno()) + 1;
                int cutNum = Integer.parseInt(entity.getCutNum()) + 1;
                int ccmSeq = Integer.parseInt(entity.getCcmSeq()) + 1;
                entity1.setMakeman(makeman);
                entity1.setSequenceno(String.valueOf(sequenceno));
                entity1.setCcmSeq(String.valueOf(ccmSeq));
                entity1.setTotMumM(String.valueOf(totMumM));
                entity1.setTotWeightM(String.valueOf(totWeightM));
                entity1.setCutNum(String.valueOf(cutNum));
                entity1.setOrderNo("");
                // 修改顺序号
                FrmSteeSerialPublic.seqIncreaseOrdDeleteSeq(con, entity
                        .getPlineCode(), 1, cutNum, entity.getFurnaceno());
                FrmSteeSerialPublic.seqIncreaseOrd(con, entity.getPlineCode(),
                        ccmSeq, 1, 1);

                // 新增炉次订单余材
                String sqladd = EntityTool
                        .fillSqlByEntity(CoreXmlSqlParsersFactory
                                .getSql("FrmSteelMerge.addFurnaceOrd"), entity1);
                ps = con.prepareStatement(sqladd);
                ps.executeUpdate();
                ps.close();

                // 修改余材量
                String squpdate = "UPDATE PLN_STEELFOR_ORD T SET T.TOT_MUM_M=?,T.TOT_WEIGHT_M=? WHERE T.FURNACENO=? AND T.SEQUENCENO=?";
                ps = con.prepareStatement(squpdate);
                ps.setObject(1, num);
                ps.setObject(2, wt);
                ps.setObject(3, entity.getFurnaceno());
                ps.setObject(4, entity.getSequenceno());
                ps.executeUpdate();
                ps.close();
            }

            core.setV_errMsg("拆分成功!");
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                con.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 调整计划时间
     * @param
     * @return
     */
    public CoreReturnObject doTimeAdjust(String furnace, String starttime,
            String efPlineCode) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            ResultSet rs = null;
//            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int furnacenoSeq = 0;
//            String plnStarttime = "";
//            String plnEndtime = "";
            // 查询浇次炉次信息
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getQueryCastFurnTime");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, furnace);
            ps.setObject(2, efPlineCode);
            rs = ps.executeQuery();
            if (rs.next()) {
                furnacenoSeq = rs.getInt("FURNACENO_SEQ");
//                plnStarttime = rs.getString("PLN_STARTTIME");
//                plnEndtime = rs.getString("PLN_ENDTIME");
            }
            else {
                cro.setV_errMsg("未找到炉次信息!");
                return cro;
            }
            rs.close();
            ps.close();
            FrmSteeSerialPublic.seqUpdateTime(conn, efPlineCode, starttime,
                    furnacenoSeq);

            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());
        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 修改工艺备注,计划备注
     * @param
     * @return
     */
    public CoreReturnObject doProMemo(ArrayList<String> jsonList) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            List<PlnOrderCastSEntity> list = PlnHelper.jsonsToEntityList(
                    PlnOrderCastSEntity.class, jsonList);
            PreparedStatement ps = null;
            // ResultSet rs = null;

            for (PlnOrderCastSEntity zgSEntity : list) {
                PlnOrderCastSEntity entity1 = ControlOrderPlan
                        .getOrderCastById(conn, zgSEntity.getProPlanId(),
                                zgSEntity.getGxPlanNo());
                if (entity1 == null) {
                    cro.setV_errMsg("数据异常,请刷新数据!");
                    return cro;
                }
                if (Integer.parseInt(entity1.getExeStatus()) > 1) {
                    cro.setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
                            + entity1.getGxPlanNo() + "),不是计划和生产中的状态!");
                    return cro;
                }
                // 修改工艺备注/修改计划备注
                String sqlupdate = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelMerge.UpdateDoProMemo"),
                        zgSEntity);
                ps = conn.prepareStatement(sqlupdate);
                ps.executeUpdate();
                ps.close();

            }
            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 con
     * @param
     * @return
     * @throws SQLException
     */
    public static String getMaxMegerNo(Connection con, String castNo)
            throws SQLException {
        String furnSeqno = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMerge.getMaxMegerNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, castNo);
        ps.setObject(2, castNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            furnSeqno = rs.getString("MERGE_NO");
        }
        rs.close();
        ps.close();
        return furnSeqno;
    }

    /**
     * 同定尺，去向炉次订单合并
     * @param
     * @return
     */
    public CoreReturnObject doMastMerge(String furnacenoMast,
            String sequencenoMast, ArrayList<ArrayList<String>> jsonList,
            String makeman) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            PreparedStatement ps = null;
            ResultSet rs = null;
            // 获取数据库当前时间
            String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getOrderOrdStee");
            sql += "AND T.FURNACENO=? AND T.SEQUENCENO=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, furnacenoMast);
            ps.setObject(2, sequencenoMast);
            rs = ps.executeQuery();
            PlnSteelforOrdEntity entity = EntityTool.getEntityByRs(rs,
                    PlnSteelforOrdEntity.class);
            rs.close();
            ps.close();
            if (entity == null) {
                core.setV_errMsg("选择主记录数据异常,请刷新数据!");
                return core;
            }
            // 总量
            double oldwt = 0;
            int oldnum = 0;
            String furnaceSeqNo = furnacenoMast + "/" + sequencenoMast;
            String megerNo = getMaxMegerNo(con, entity.getFurnaceno());// 合并编号
            int megetSeq = 1;

            // // 获取选择记录最小订单序号
            // int sequencenoMin = 0;
            // // 获取选择记录最大订单序号
            // int sequencenoMax = 0;
            String proPlanId = "";
            String gxPlanNo = "";
            String orderNo = "";
            String finalUserDesc = "";
            String ifMonitorLg = "";
            String prdcrNo = "";
            String cic = "";
            String pic = "";
            String sic = "";
            String dic = "";
            String pdeptName = "";
            String prdcrMan = "";
            int count = 0;
            for (int i = 0; i < jsonList.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) jsonList.get(i);
                String furnaceno = list.get(0);
                String sequenceno = list.get(1);
                count++;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmSteelMerge.getOrderOrdStee");
                sql += " AND T.FURNACENO=? AND T.SEQUENCENO=?";
                ps = con.prepareStatement(sql);
                ps.setObject(1, furnaceno);
                ps.setObject(2, sequenceno);
                rs = ps.executeQuery();
                PlnSteelforOrdEntity entity1 = EntityTool.getEntityByRs(rs,
                        PlnSteelforOrdEntity.class);
                rs.close();
                ps.close();
                if (entity1 == null) {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    return core;
                }
                if (!entity1.getSurplusflag().equals("0")) {
                    core.setV_errMsg("不是订单材的炉次订单,不能合并!");
                    return core;
                }
                if (!entity1.getStatus().equals("00")) {
                    core.setV_errMsg("不是编制中状态的炉次订单,不能合并!");
                    return core;
                }
                if(entity1.getIfConfirm().equals("1")){
                	 core.setV_errMsg("浇次已确认,不能合并!");
                     return core;
                }
                // if (i == 0) {
                // sequencenoMin = Integer.parseInt(entity1.getSequenceno());
                // }
                // sequencenoMax = Integer.parseInt(entity1.getSequenceno());
                if (proPlanId.equals("")) {
                    proPlanId = entity1.getProPlanId();
                    gxPlanNo = entity1.getGxPlanNo();
                }
                if (count > 1) {
                    if (!proPlanId.equals("")) {
                        proPlanId = proPlanId + "," + entity1.getProPlanId();
                        gxPlanNo = gxPlanNo + "," + entity1.getGxPlanNo();
                    }
                }
                String[] orderS = entity1.getOrderNo().split(",");
                for (int j = 0; j < orderS.length; j++) {
                    if (orderNo.equals("")) {
                        orderNo = orderS[j];
                    }
                    if (!orderNo.contains(orderS[j])) {
                        orderNo = orderNo + "," + orderS[j];
                    }
                }
                String[] finalUserDescS = entity1.getFinalUserDesc().split(",");
                for (int j = 0; j < finalUserDescS.length; j++) {
                    if (finalUserDesc.equals("")) {
                        finalUserDesc = finalUserDescS[j];
                    }
                    if (!finalUserDesc.contains(finalUserDescS[j])) {
                        finalUserDesc = finalUserDesc + "," + finalUserDescS[j];
                    }
                }
                if (ifMonitorLg.equals("")) {
                    ifMonitorLg = entity1.getIfMonitorLg();
                }
                if (!ifMonitorLg.contains(entity1.getIfMonitorLg())) {
                    ifMonitorLg = ifMonitorLg + "," + entity1.getIfMonitorLg();
                }
                if (prdcrNo.equals("")) {
                    prdcrNo = entity1.getPrdcrNo();
                }
                if (!prdcrNo.contains(entity1.getPrdcrNo())) {
                    prdcrNo = prdcrNo + "," + entity1.getPrdcrNo();
                }
                if (cic.equals("")) {
                    cic = entity1.getCic();
                }
                if (!cic.contains(entity1.getCic())) {
                    cic = cic + "," + entity1.getCic();
                }
                if (pic.equals("")) {
                    pic = entity1.getPic();
                }
                if (!pic.contains(entity1.getPic())) {
                    pic = pic + "," + entity1.getPic();
                }
                if (sic.equals("")) {
                    sic = entity1.getSic();
                }
                if (!sic.contains(entity1.getSic())) {
                    sic = sic + "," + entity1.getSic();
                }
                if (dic.equals("")) {
                    dic = entity1.getDic();
                }
                if (!dic.contains(entity1.getDic())) {
                    dic = dic + "," + entity1.getDic();
                }
                if (pdeptName.equals("")) {
                    pdeptName = entity1.getPdeptName();
                }
                if (!pdeptName.contains(entity1.getPdeptName())) {
                    pdeptName = pdeptName + "," + entity1.getPdeptName();
                }
                if (prdcrMan.equals("")) {
                    prdcrMan = entity1.getPrdcrMan();
                }
                if (!prdcrMan.contains(entity1.getPrdcrMan())) {
                    prdcrMan = dic + "," + entity1.getPrdcrMan();
                }
                // 总量
                oldwt += Double.parseDouble(entity1.getTotWeightM());
                oldnum += Integer.parseInt(entity1.getTotMumM());

                entity1.setMakeman(makeman);
                // entity1.setOrderNo("");
                // 新增炉次订单合并记录
                String sqladd = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelMerge.addMegerNo"), entity1);
                ps = con.prepareStatement(sqladd);
                ps.setObject(1, megerNo);
                ps.setObject(2, megetSeq);
                ps.setObject(3, dateTime);
                ps.executeUpdate();
                ps.close();
                megetSeq++;

                // 判定是否是主记录，不是主记录这删除数据
                if (!furnaceSeqNo.equals(entity1.getFurnaceno() + "/"
                        + entity1.getSequenceno())) {
                    // 删除炉次订单
                    String detelefurnOrd = "DELETE PLN_STEELFOR_ORD  WHERE FURNACENO = ? AND SEQUENCENO = ?";
                    ps = con.prepareStatement(detelefurnOrd);
                    ps.setObject(1, entity1.getFurnaceno());
                    ps.setObject(2, entity1.getSequenceno());
                    ps.executeUpdate();
                    ps.close();
                    // 修改连铸顺序号
                    FrmSteeSerialPublic.seqIncreaseOrd(con, entity
                            .getPlineCode(), Integer.parseInt(entity1
                            .getCcmSeq()), -1, 0);
                    // // 修改订单顺序号
                    // FrmSteeSerialPublic.seqIncreaseOrdDeleteSeq(con, entity
                    // .getPlineCode(), -1, Integer.parseInt(entity1
                    // .getSequenceno()), entity.getFurnaceno());
                }
            }

            // 修改主记录量
            String squpdate = "UPDATE PLN_STEELFOR_ORD T SET T.TOT_MUM_M=?,T.TOT_WEIGHT_M=?,T.MERGE_NO=?,"
                    + " T.PRO_PLAN_ID=?,T.CIC=?,T.PIC=?,T.SIC=?,T.DIC=?,T.ORDER_NO=?,T.FINAL_USER_DESC=?,"
                    + "T.IF_MONITOR_LG=?,T.PRDCR_NO=?,T.PDEPT_NAME=?,T.PRDCR_MAN=?,T.GX_PLAN_NO=? WHERE T.FURNACENO=? AND T.SEQUENCENO=?";
            ps = con.prepareStatement(squpdate);
            ps.setObject(1, oldnum);
            ps.setObject(2, oldwt);
            ps.setObject(3, megerNo);
            ps.setObject(4, proPlanId);
            ps.setObject(5, cic);
            ps.setObject(6, pic);
            ps.setObject(7, sic);
            ps.setObject(8, dic);
            ps.setObject(9, orderNo);
            ps.setObject(10, finalUserDesc);
            ps.setObject(11, ifMonitorLg);
            ps.setObject(12, prdcrNo);
            ps.setObject(13, pdeptName);
            ps.setObject(14, prdcrMan);
            ps.setObject(15, gxPlanNo);
            ps.setObject(16, entity.getFurnaceno());
            ps.setObject(17, entity.getSequenceno());
            ps.executeUpdate();
            ps.close();

            int sequeCounbt = 1;
            // 查询订单材条数
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMerge.getOrderOrdStee");
            sql += "AND T.FURNACENO=? AND (T.SURPLUS_TYPE NOT IN ('2','3') OR T.SURPLUS_TYPE IS NULL) ORDER BY T.CCM_SEQ ASC";
            ps = con.prepareStatement(sql);
            ps.setObject(1, furnacenoMast);
            rs = ps.executeQuery();
            while (rs.next()) {
                // 修改炉次订单顺序号
                String sqlupdateSeq = " UPDATE PLN_STEELFOR_ORD T  SET T.CUT_NUM = '"
                        + sequeCounbt
                        + "'"
                        + " WHERE (T.SURPLUS_TYPE NOT IN ('2','3') OR T.SURPLUS_TYPE IS NULL) AND T.FURNACENO='"
                        + furnacenoMast + "' AND T.SEQUENCENO=?";
                PreparedStatement ps1 = con.prepareStatement(sqlupdateSeq);
                ps1.setObject(1, rs.getString("SEQUENCENO"));
                ps1.executeUpdate();
                ps1.close();
                sequeCounbt++;
            }
            rs.close();
            ps.close();

            core.setV_errMsg("合并成功!");
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                if (con != null) {
                    con.rollback();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 一键获取余材合同
     * @param
     * @return
     * @throws ParseException
     */
    public CoreReturnObject doAddBlOrder(String plineCode)
            throws ParseException {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = null;
            ResultSet rs = null;
            String sql = "SELECT T.FURNACENO,T.SEQUENCENO,T.PRO_MONTH FROM PLN_STEELFOR_ORD T  WHERE T.EF_PLINE_CODE=? AND T.SURPLUSFLAG='1'  AND T.SURPLUS_TYPE IN ('1','6') AND T.STATUS IN ('00','10') AND T.ORDER_NO IS NULL ORDER BY T.CCM_SEQ ASC";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, plineCode);
            rs = ps.executeQuery();
            while (rs.next()) {
                // 查询浇炉次订单信息
                String order = FrmSteelMakingPlanChange.getBlOrder(conn,
                        plineCode, rs.getString("PRO_MONTH"));
                String sqlupdate = "UPDATE PLN_STEELFOR_ORD T SET T.ORDER_NO=? WHERE T.FURNACENO=? AND T.SEQUENCENO=? AND T.SURPLUSFLAG='1' AND T.SURPLUS_TYPE IN ('1','6') AND T.STATUS IN ('00','10') AND T.ORDER_NO IS NULL";
                PreparedStatement ps1 = conn.prepareStatement(sqlupdate);
                ps1.setObject(1, order);
                ps1.setObject(2, rs.getString("FURNACENO"));
                ps1.setObject(3, rs.getString("SEQUENCENO"));
                ps1.executeUpdate();
                ps1.close();
            }
            rs.close();
            ps.close();

            cro.setV_errMsg("获取成功!");
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                if (conn != null) {
                    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 con
     * @param caseNo
     * @throws SQLException
     */
    public static void updatePlanOrder(Connection con, String caseNo)
            throws SQLException {
        String sql = "";
        // 查询浇次对应炉次量
        sql = CoreXmlSqlParsersFactory.getSql("FrmSteelMerge.updateAimSteelWt");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, caseNo);
        ps.setObject(2, caseNo);
        ps.setObject(3, caseNo);
        ps.setObject(4, caseNo);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 修改炼钢去向
     * @param
     * @return
     */
    public CoreReturnObject updateGoWhereName(String furnaceno,
            String sequenceno, String gowhereName) {
        String sql = "UPDATE PLN_STEELFOR_ORD T SET T.GOWHERE_CODE = (SELECT T1.PLINE_CODE FROM COM_BASE_PLINE T1 WHERE T1.PLINE_NAME = ?),T.GOWHERE_NAME = ? WHERE T.FURNACENO = ? AND T.SEQUENCENO = ?";
        return this.setData(sql, new Object[] { gowhereName, gowhereName,furnaceno,
                sequenceno });
    }
}