package com.steering.pss.plnsaleord.steelMarkingPlan;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA06.CoreReturnSortObject;
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.processOrder.base.BaseHelper;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.ImpFrmdispatchEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforOrdEntity;
import com.steering.pss.plnsaleord.stove.ComPlanFurnace;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 朱少波 on 2017/8/9.
 */
public class FrmSteelManagement extends DBCommon {


    //region 查询浇次表

    /**
     * 通过浇次号查询浇次信息
     *
     * @param castNo 浇次号
     * @param status 状态
     * @param line   铸造产线
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforcastEntity(
            String castNo,
            ArrayList status,
            ArrayList line
    ) throws Exception {
        return SelectPlnSteelforcastEntity(castNo, null, status, line, new ArrayList(), null, null, null, null);
    }

    public CoreReturnObject SelectPlnSteelforcastEntity(
            ArrayList CastNos
    ) throws Exception {
        return SelectPlnSteelforcastEntity("", CastNos, null, null, null, null, null, null, null);
    }

    public CoreReturnObject SelectPlnSteelforcastEntity(
            String castNo
    ) throws Exception {
        return SelectPlnSteelforcastEntity(castNo, null, null);
    }

    public CoreReturnObject SelectPlnSteelforcastEntity(
            Date Start,
            Date End,
            ArrayList line,
            ArrayList Efline,
            ArrayList status
    ) throws Exception {
        return SelectPlnSteelforcastEntity("", null, status, line, new ArrayList(), null, null, Start, End);
    }


    public CoreReturnObject SelectPlnSteelforcastEntity(
            ArrayList Line,
            Long MinSeq
    ) throws Exception {
        return SelectPlnSteelforcastEntity(Line, MinSeq, null);
    }

    public CoreReturnObject SelectPlnSteelforcastEntity(
            ArrayList Line,
            Long MinSeq,
            Long MaxSeq
    ) throws Exception {
        return SelectPlnSteelforcastEntity("", null, new ArrayList(), Line, new ArrayList(), MinSeq, MaxSeq, null, null);
    }

    public CoreReturnObject SelectPlnSteelforcastEntity(
            ArrayList Line,
            Long MinSeq,
            Long MaxSeq,
            ArrayList CastNos
    ) throws Exception {
        return SelectPlnSteelforcastEntity("", null, new ArrayList(), Line, CastNos, MinSeq, MaxSeq, null, null);
    }


    /**
     * 查询浇次信息
     *
     * @param castNo 浇次号
     * @param status 状态
     * @param line   铸造产线
     * @param Start  开始时间
     * @param End    结束时间
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforcastEntity(
            String castNo,
            ArrayList CastNos,
            ArrayList status,
            ArrayList line,
            ArrayList NocastNo,
            Long MinSeq,
            Long MaxSeq,
            Date Start,
            Date End
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if (Start != null) {
                Where = Where + " AND STARTTIME   >= TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') ";
                paras.add(sdf.format(Start));
            }

            if (End != null) {
                Where = Where + " AND STARTTIME  <=  TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') ";
                paras.add(sdf.format(End));
            }


            if (status != null && status.size() > 0) {
                Where = Where + " AND STATUS IN ";
                Where = getStatus(status, Where, paras);

            }

            if (MinSeq != null && MinSeq > 0) {
                Where = Where + " AND CAST_SEQ >=  ?  ";
                paras.add(MinSeq);
            }

            if (MaxSeq != null && MaxSeq > 0) {
                Where = Where + " AND CAST_SEQ <=  ?  ";
                paras.add(MaxSeq);
            }


            if (NocastNo != null && NocastNo.size() > 0) {
                Where = Where + " AND CAST_NO NOT IN ";
                Where = getStatus(NocastNo, Where, paras);
            }


            if (line != null && line.size() > 0) {
                Where = Where + " AND PLINE_CODE IN    ";
                Where = getStatus(line, Where, paras);
            }


            if (!"".equals(castNo)) {
                Where = Where + " AND CAST_NO =  ?  ";
                paras.add(castNo);
            }


            if (CastNos != null && CastNos.size() > 0) {
                Where = Where + " AND CAST_NO IN    ";
                Where = getStatus(CastNos, Where, paras);
            }


            String sql = MessageFormat.format(CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.SelectPlnSteelforcastEntity"), Where);

            Object[] ParaObject = new Object[paras.size()];
            for (int i = 0; i < ParaObject.length; i++) {
                ParaObject[i] = paras.get(i);
            }
            cro = getData(sql, ParaObject);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    /**
     * 查询浇次信息
     *
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforcastEntityByEfPline(
            String EfPLine,
            Long MinFurnacenoSeq,
            ArrayList Status
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {


            if (!"".equals(EfPLine)) {
                Where = Where + " AND B.EF_PLINE_CODE = ?";
                paras.add(EfPLine);
            }


            if (MinFurnacenoSeq != null && MinFurnacenoSeq > 0) {
                Where = Where + " AND B.FURNACENO_SEQ > ?";
                paras.add(MinFurnacenoSeq);
            }

            if (Status != null && Status.size() > 0) {
                Where = Where + " AND B.STATUS IN    ";
                Where = getStatus(Status, Where, paras);
            }
            String sql = MessageFormat.format(CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.SelectPlnSteelforcastEntity1"), Where);


            cro = getData(sql, paras.toArray());

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    //endregion

    //region 查询炉次表

    /**
     * 查询炉次表信息
     *
     * @param castno 浇次号
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno
    ) throws Exception {
        return SelectPlnSteelforfurnaceEntity(castno, null, "", "", new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList());
    }

    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String EafLine,
            Long MinFurnacenoSeq
    ) throws Exception {

        ArrayList EafLines = new ArrayList();
        EafLines.add(EafLine);

        return SelectPlnSteelforfurnaceEntity("", null, "", "", "", new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), EafLines, null, null, MinFurnacenoSeq, null, "", null, null, "");
    }


    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            ArrayList EafLines,
            String Gradecode,
            ArrayList notfurnaceno,
            ArrayList status,
            String DIAMETERfurnaceno
    ) throws Exception {
        return SelectPlnSteelforfurnaceEntity("", null, "", "", Gradecode, status, null, notfurnaceno, new ArrayList(), EafLines, null, null, null, null, "", null, null, DIAMETERfurnaceno);
    }


    /**
     * 查询炉次表信息
     *
     * @param castno    浇次号
     * @param furnaceno 虚拟炉次号
     * @param stove     熔炼炉号
     * @param status    状态
     * @param Line      铸造产线
     * @param EafLine   电炉产线
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno,
            String furnaceno,
            String stove,
            ArrayList status,
            ArrayList Line,
            ArrayList EafLine
    ) throws Exception {
        return SelectPlnSteelforfurnaceEntity(castno, null, furnaceno, stove, status, new ArrayList(), new ArrayList(), Line, EafLine);
    }

    /**
     * 查询炉次表信息
     *
     * @param Line    铸造产线
     * @param EafLine 电炉产线
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            Date Start,
            Date End,
            ArrayList Line,
            ArrayList EafLine,
            ArrayList status
    ) throws Exception {
        return SelectPlnSteelforfurnaceEntity("", null, "", "", "", status, new ArrayList(), new ArrayList(), new ArrayList(), EafLine, null, null, null, null, "", Start, End, "");

    }

    /**
     * 查询炉次表信息
     *
     * @param castno    浇次号
     * @param castSeq   浇次顺序号
     * @param furnaceno 虚拟炉次号
     * @param stove     熔炼炉号
     * @param status    状态
     * @param notStatus 非此状态
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno,
            Long castSeq,
            String furnaceno,
            String stove,
            ArrayList status,
            ArrayList notStatus,
            ArrayList notfurnaceno,
            ArrayList Line,
            ArrayList EafLine
    ) throws Exception {
        return SelectPlnSteelforfurnaceEntity(castno, castSeq, furnaceno, "", stove, status, notStatus, notfurnaceno, Line, EafLine, null, null, null, null, "", null, null, "");

    }


    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno,
            Long MinSeq,
            Long MaxSeq
    ) throws Exception {

        return SelectPlnSteelforfurnaceEntity(castno, null, "", "", "", new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), MinSeq, MaxSeq, null, null, "", null, null, "");
    }

    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String EfPline,
            Long MinFurnacenoSeq,
            Long MaxFurnacenoSeq,
            ArrayList notfurnaceno
    ) throws Exception {

        ArrayList EfPlines = new ArrayList();
        EfPlines.add(EfPline);
        return SelectPlnSteelforfurnaceEntity("", null, "", "", "", null, null, notfurnaceno, null, EfPlines, null, null, MinFurnacenoSeq, MaxFurnacenoSeq, "", null, null, "");
    }


    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            ArrayList notfurnacenoList
    ) throws Exception {

        return SelectPlnSteelforfurnaceEntity("", null, "", "", "", null, null, null, notfurnacenoList,
                null, null, null, null, null, null, "", null, null, ""
        );
    }

    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno,
            Long castSeq,
            String furnaceno,
            String stove,
            String Gradecode,
            ArrayList status,
            ArrayList notStatus,
            ArrayList notfurnaceno,
            ArrayList Line,
            ArrayList EafLine,
            Long MinSeq,
            Long MaxSeq,
            Long MinFurnacenoSeq,
            Long MaxFurnacenoSeq,
            String Lfurnaceno,
            Date Start,
            Date End,
            String DIAMETERfurnaceno
    ) throws Exception {

        return SelectPlnSteelforfurnaceEntity(castno, castSeq, furnaceno, stove, Gradecode, status, notStatus, notfurnaceno, null,
                Line, EafLine, MinSeq, MaxSeq, MinFurnacenoSeq, MaxFurnacenoSeq, Lfurnaceno, Start, End, DIAMETERfurnaceno
        );
    }


    /**
     * 查询炉次表信息
     *
     * @param castno    浇次号
     * @param castSeq   浇次顺序号
     * @param furnaceno 虚拟炉次号
     * @param stove     熔炼炉号
     * @param status    状态
     * @param notStatus 非此状态
     * @param Start     开始时间
     * @param End       结束时间
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforfurnaceEntity(
            String castno,
            Long castSeq,
            String furnaceno,
            String stove,
            String Gradecode,
            ArrayList status,
            ArrayList notStatus,
            ArrayList notfurnaceno,
            ArrayList furnacenoList,
            ArrayList Line,
            ArrayList EafLine,
            Long MinSeq,
            Long MaxSeq,
            Long MinFurnacenoSeq,
            Long MaxFurnacenoSeq,
            String Lfurnaceno,
            Date Start,
            Date End,
            String DIAMETERfurnaceno
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if (Start != null) {
                Where = Where + " AND A.PLN_STARTTIME  >=  TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') ";
                paras.add(sdf.format(Start));
            }

            if (End != null) {
                Where = Where + " AND A.PLN_STARTTIME  <= TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss')";
                paras.add(sdf.format(End));
            }

            if (status != null && status.size() > 0) {
                Where = Where + " AND A.STATUS IN ";
                Where = getStatus(status, Where, paras);
            }

            if (notStatus != null && notStatus.size() > 0) {
                Where = Where + " AND A.STATUS NOT IN  ";
                Where = getStatus(notStatus, Where, paras);
            }


            if (MinSeq != null && MinSeq > 0) {
                Where = Where + " AND A.CAST_STOVE_SEQ >=  ?  ";
                paras.add(MinSeq);
            }

            if (MaxSeq != null && MaxSeq > 0) {
                Where = Where + " AND A.CAST_STOVE_SEQ <=  ?  ";
                paras.add(MaxSeq);
            }

            if (!"".equals(Lfurnaceno)) {
                Where = Where + "  A.FURNACENO_SEQ >(SELECT FURNACENO_SEQFROM PLN_STEELFORFURNACE WHERE FURNACENO = ?)\n" +
                        "       AND A.EF_PLINE_CODE =(SELECT EF_PLINE_CODEFROM PLN_STEELFORFURNACEWHERE FURNACENO = ?) ";
                paras.add(Lfurnaceno);
                paras.add(Lfurnaceno);
            }

            if (!"".equals(Gradecode)) {
                Where = Where + " AND A.GRADECODE = ? ";
                paras.add(Gradecode);
            }


            if (( null != castSeq ) && ( castSeq > 0 )) {
                Where = Where + " AND A.CAST_SEQ = ? ";
                paras.add(castSeq);
            }

            if (!"".equals(castno)) {
                Where = Where + " AND A.CAST_NO = ? ";
                paras.add(castno);
            }


            if (!"".equals(stove)) {
                Where = Where + " AND A.STOVE_NO = ? ";
                paras.add(stove);
            }


            if (notfurnaceno != null && notfurnaceno.size() > 0) {
                Where = Where + " AND A.FURNACENO NOT IN  ";
                Where = getStatus(notfurnaceno, Where, paras);
            }


            if (!"".equals(DIAMETERfurnaceno)) {
                Where = Where + " AND EXISTS (SELECT 1 FROM PLN_STEELFOR_ORD WHERE FURNACENO= A.FURNACENO AND DIAMETER= (SELECT DIAMETER FROM PLN_STEELFOR_ORD WHERE FURNACENO=? AND ROWNUM =1 ) )\n";
                paras.add(DIAMETERfurnaceno);
            }


            if (Line != null && Line.size() > 0) {
                Where = Where + " AND A.PLINE_CODE IN ";
                Where = getStatus(Line, Where, paras);
            }

            if (EafLine != null && EafLine.size() > 0) {


                Where = Where + " AND A.EF_PLINE_CODE IN ";
                Where = getStatus(EafLine, Where, paras);
            }

            if (( null != MinFurnacenoSeq ) && ( MinFurnacenoSeq > 0 )) {
                Where = Where + " AND A.FURNACENO_SEQ > ? ";
                paras.add(MinFurnacenoSeq);
            }


            if (( null != MaxFurnacenoSeq ) && ( MaxFurnacenoSeq > 0 )) {
                Where = Where + " AND A.FURNACENO_SEQ < ? ";
                paras.add(MaxFurnacenoSeq);
            }


            if (!"".equals(furnaceno)) {
                Where = Where + " AND A.FURNACENO =  ?  ";
                paras.add(furnaceno);
            }

            if (furnacenoList != null && furnacenoList.size() > 0) {
                Where = Where + " AND A.FURNACENO  IN  ";
                Where = getStatus(furnacenoList, Where, paras);
            }


            String sql = MessageFormat.format(CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.SelectPlnSteelforfurnaceEntity"), Where);

            Object[] ParaObject = new Object[paras.size()];
            for (int i = 0; i < ParaObject.length; i++) {
                ParaObject[i] = paras.get(i);
            }
            cro = getData(sql, ParaObject);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }
    //endregion

    //region 查询订单表
    public CoreReturnObject SelectPlnSteelforOrdEntityByfurnaceno(
            String furnaceno
    ) throws Exception {
        return SelectPlnSteelforOrdEntity("", null, null, null, new ArrayList(), furnaceno, null, "", new ArrayList(), new ArrayList(), new ArrayList(), null, null);
    }

    public CoreReturnObject SelectPlnSteelforOrdEntityByfurnaceno(
            String furnaceno,
            Long SEQUENCENO
    ) throws Exception {
        return SelectPlnSteelforOrdEntity("", null, null, null, new ArrayList(), furnaceno, SEQUENCENO, "", new ArrayList(), new ArrayList(), new ArrayList(), null, null);
    }


    public CoreReturnObject SelectPlnSteelforOrdEntity(
            Date Start,
            Date End,
            ArrayList Line,
            ArrayList EafLine,
            ArrayList status
    ) throws Exception {
        return SelectPlnSteelforOrdEntity("", null, null, null, new ArrayList(), "", null, "", status, Line, new ArrayList(), Start, End);
    }

    /**
     * 查询订单表
     *
     * @param castno 浇次号
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforOrdEntity(
            String castno
    ) throws Exception {
        return SelectPlnSteelforOrdEntity(castno, null, null);
    }


    /**
     * 查询订单表
     *
     * @param castno       浇次号
     * @param castSeq      浇次顺序号
     * @param castStoveSeq 浇次内顺序号
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforOrdEntity(
            String castno,
            Long castSeq,
            Long castStoveSeq
    ) throws Exception {
        return SelectPlnSteelforOrdEntity(castno, castSeq, castStoveSeq, null, new ArrayList(), "", null, "", new ArrayList(), new ArrayList(), new ArrayList(), null, null);
    }


    public CoreReturnObject SelectPlnSteelforOrdEntity(
            String furnaceno,
            Long Len
    ) throws Exception {
        return SelectPlnSteelforOrdEntity("", null, null, null, new ArrayList(), furnaceno, null, "", new ArrayList(), new ArrayList(), new ArrayList(), null, null, null, "", "", Len);
    }

    public CoreReturnObject SelectPlnSteelforOrdEntity(
            ArrayList notfurnaceno,
            ArrayList Line,
            ArrayList Status,
            String Diameter,
            String GradeCode
    ) throws Exception {
        return SelectPlnSteelforOrdEntity("", null, null, null, notfurnaceno, "", null, "", Status, Line, new ArrayList(), null, null, null, Diameter, GradeCode, null);
    }

    public CoreReturnObject SelectPlnSteelforOrdEntity(
            String castno,
            Long castSeq,
            Long castStoveSeq,
            Long MinCCMSeq,
            ArrayList notfurnaceno,
            String furnaceno,
            Long SEQUENCENO,
            String stove,
            ArrayList status,
            ArrayList Line,
            ArrayList EafLine,
            Date Start,
            Date End
    ) throws Exception {
        return SelectPlnSteelforOrdEntity(castno, castSeq, castStoveSeq, MinCCMSeq, notfurnaceno, furnaceno, SEQUENCENO, stove, status,
                Line, EafLine, Start, End, null, "", "", null);
    }

    /**
     * 查询订单表
     *
     * @param castno       浇次号
     * @param castSeq      浇次顺序号
     * @param castStoveSeq 浇次内顺序号
     * @param furnaceno    虚拟炉次号
     * @param stove        熔炼炉号
     * @param status       状态
     * @param Start        开始时间
     * @param End          结束时间
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject SelectPlnSteelforOrdEntity(
            String castno,
            Long castSeq,
            Long castStoveSeq,
            Long MinCCMSeq,
            ArrayList notfurnaceno,
            String furnaceno,
            Long SEQUENCENO,
            String stove,
            ArrayList status,
            ArrayList Line,
            ArrayList EafLine,
            Date Start,
            Date End,
            ArrayList FurnaceStatus,
            String Diameter,
            String GradeCode,
            Long SingleLen
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if (Start != null) {
                Where = Where + " AND T3.PLN_STARTTIME  >= TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') ";
                paras.add(sdf.format(Start));
            }

            if (End != null) {
                Where = Where + " AND T3.PLN_STARTTIME  <= TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') ";
                paras.add(sdf.format(End));
            }

            if (status != null && status.size() > 0) {
                Where = Where + " AND T.STATUS IN ";
                Where = getStatus(status, Where, paras);

            }

            if (FurnaceStatus != null && FurnaceStatus.size() > 0) {
                Where = Where + " AND EXISTS (SELECT 1 FROM PLN_STEELFORFURNACE WHERE STATUS IN  ";
                Where = getStatus(FurnaceStatus, Where, paras);
                Where = Where + ")";
            }


            if (SingleLen != null && SingleLen > 0) {
                Where = Where + " AND T.LEN_GP_SINGLE = ? ";
                paras.add(SingleLen);
            }

            if (!"".equals(Diameter)) {
                Where = Where + " AND T.DIAMETER = ? ";
                paras.add(Diameter);
            }

            if (!"".equals(GradeCode)) {
                Where = Where + " AND T.GRADECODE = ? ";
                paras.add(GradeCode);
            }


            if (null != MinCCMSeq && MinCCMSeq > 0) {
                Where = Where + " AND T.CCM_SEQ >= ? ";
                paras.add(MinCCMSeq);
            }

            if (notfurnaceno != null && notfurnaceno.size() > 0) {
                Where = Where + " AND T.FURNACENO NOT IN  ";
                Where = getStatus(notfurnaceno, Where, paras);
            }

            if (Line != null && Line.size() > 0) {
                Where = Where + " AND T.PLINE_CODE IN ";
                Where = getStatus(Line, Where, paras);

            }

            if (EafLine != null && EafLine.size() > 0) {
                Where = Where + " AND T.EF_PLINE_CODE IN ";
                Where = getStatus(EafLine, Where, paras);

            }


            if (null != castSeq && castSeq > 0) {
                Where = Where + " AND T.CAST_SEQ = ? ";
                paras.add(castSeq);
            }

            if (castStoveSeq != null && castStoveSeq > 0) {
                Where = Where + " AND T.CAST_STOVE_SEQ = ? ";
                paras.add(castStoveSeq);
            }


            if (!"".equals(stove)) {
                Where = Where + " AND T.STOVE_NO = ? ";
                paras.add(stove);
            }

            if (!"".equals(castno)) {
                Where = Where + " AND   T.FURNACENO IN (SELECT FURNACENO FROM PLN_STEELFORFURNACE WHERE CAST_NO=?)";
                paras.add(castno);
            }


            if (!"".equals(furnaceno)) {
                Where = Where + " AND T.FURNACENO =  ?  ";
                paras.add(furnaceno);
            }

            if (null != SEQUENCENO && SEQUENCENO >= 0) {
                Where = Where + " AND T.SEQUENCENO = ? ";
                paras.add(SEQUENCENO);
            }

            String sql = MessageFormat.format(CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.SelectPlnSteelforOrdEntity").replace("'", "''")
                    , Where);

            Object[] ParaObject = new Object[paras.size()];
            for (int i = 0; i < ParaObject.length; i++) {
                ParaObject[i] = paras.get(i);
            }
            cro = getData(sql, ParaObject);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }

    public CoreReturnObject SelectPlnSteelforOrdEntityPlus(
            String furnaceno
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {
            cro = getDataBySqlID("FrmSteelManagement.SelectPlnSteelforOrdEntityPlus", new Object[]{furnaceno});
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    public CoreReturnObject SelectPlnSteelforMergeEntity(
            ArrayList MergeNos
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        String Where = "";
        ArrayList paras = new ArrayList();
        try {


            if (MergeNos != null && MergeNos.size() > 0) {
                Where = Where + " AND MERGE_NO IN ";
                Where = getStatus(MergeNos, Where, paras);

            }


            String sql = MessageFormat.format(CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.SelectPlnSteelforMergeEntity").replace("'", "''")
                    , Where);


            cro = getData(sql, paras.toArray());
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    //endregion

    //region 界面功能

    /**
     * 关闭浇次表计划
     *
     * @param CastList 需要关闭的浇次号List
     * @param Operator 关闭人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject ClosePlnSteelforcast(
            ArrayList CastList,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {

            if (CastList == null || CastList.size() == 0) {
                cro.setV_errMsg("浇次号未知！");
                return cro;
            }


            for (Object Cast : CastList
                    ) {

                List<Map<String, Object>> PlnSteelforcast =
                        ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(Cast.toString(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (PlnSteelforcast == null || PlnSteelforcast.size() <= 0) {
                    cro.setV_errMsg("找不到该浇次号：" + Cast.toString() + " 数据！");
                    return cro;
                }


                if (!"30".equals(PlnSteelforcast.get(0).get("STATUS"))) {
                    cro.setV_errMsg("浇次号：" + Cast.toString() + " 不是已生产状态，无法关闭！");
                    return cro;
                }

                ArrayList statusList = new ArrayList();
                statusList.add("40");


                List PlnSteelforfurnace =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(Cast.toString(), null, "", "", new ArrayList(), statusList,
                                new ArrayList(), new ArrayList(), new ArrayList()
                        ).getResult() ).getResult();

                if (PlnSteelforfurnace != null && PlnSteelforfurnace.size() > 0) {
                    cro.setV_errMsg("浇次号：" + Cast.toString() + " 下的炉次计划未全部关闭，请先关闭炉次计划！");
                    return cro;
                }


                listsqlID.add("FrmSteelManagement.ClosePlnSteelforcastEntity");

                listpara.add(new Object[]{Operator, Cast});


            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    public CoreReturnObject CheckUpdate(
            String Cast,
            String Status
    ) throws Exception   {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {

            if ("".equals(Cast)) {
                cro.setV_errMsg("浇次号未知！");
                return cro;
            }
            cro = SelectPlnSteelforcastEntity(Cast, new ArrayList(), new ArrayList());
            cro.setResult("");

            if ("10".equals(Status)) {
                List<Map<String, Object>>  TempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.CheckUpdate2",
                        new Object[]{Cast, Cast}).getResult() ).getResult();

                if (TempList!=null && TempList.size()>0){
                    String Stoves = "";
                    for (Map<String, Object>Tem :TempList
                            ) {
                        Stoves = (!"".equals(Stoves)?",":"") + String.valueOf(Tem.get("FURNACENO"));
                    }
                    cro.setResult("虚拟炉号："+  Stoves+ "存在同定尺同去向,是否确认下发?");
                }

            }

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * 更新浇次表的状态 (下发/取消下发)
     *
     * @param Cast     需要更新的浇次号List
     * @param Status   需要更新的状态
     * @param Operator 更新人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public synchronized CoreReturnObject UpdatePlnSteelforcastStatus(
            String Cast,
            String Status,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {

            if ("".equals(Cast)) {
                cro.setV_errMsg("浇次号未知！");
                return cro;
            }

            List<String> ProPlanIds = new ArrayList<String>();
            List<String> GxPlanNos = new ArrayList<String>();

            List<String> Furnacenos = new ArrayList<String>();

            List<Map<String, Object>> PlnSteelforcast =
                    ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(Cast, new ArrayList(), new ArrayList()).getResult() ).getResult();


            if (PlnSteelforcast == null || PlnSteelforcast.size() <= 0) {
                cro.setV_errMsg("找不到该浇次号：" + Cast + " 数据！");
                return cro;
            }


         /*   if ("00".equals(Status) && !"10".equals(PlnSteelforcast.get(0).get("STATUS"))) {
                cro.setV_errMsg("浇次号：" + Cast + " 不是下发状态，无法取消下发！");
                return cro;
            }*/

         /*   if ("00".equals(Status) && "1".equals(PlnSteelforcast.get(0).get("TO_EXCEL"))) {
                cro.setV_errMsg("浇次号：" + Cast + " 已经导入系统，无法取消下发！");
                return cro;
            }*/

            if ("10".equals(Status) && !"00".equals(PlnSteelforcast.get(0).get("STATUS"))) {
                cro.setV_errMsg("浇次号：" + Cast + " 不是编制状态，无法下发！");
                return cro;
            }


            Long CastSeq = ( (BigDecimal) ( PlnSteelforcast.get(0).get("CAST_SEQ") ) ).longValue();
            String CastPlineCode = PlnSteelforcast.get(0).get("PLINE_CODE").toString();
            ArrayList status = new ArrayList();
            ArrayList Plines = new ArrayList();

            Plines.add(CastPlineCode);

            Map<String, String> NewStoveNo = new HashMap<String, String>();
            Map<String, String> NewJudgeStoveNo = new HashMap<String, String>();
            String LastProMoth = "";

            if ("10".equals(Status)) {
                status.add("00");
                PlnSteelforcast =
                        ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NeedUpdateCast", new Object[]{Cast, Cast}).getResult() ).getResult();

            }
            if ("00".equals(Status)) {
                List<Map<String, Object>>  TempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.CheckUpdate1",
                        new Object[]{Cast, Cast}).getResult() ).getResult();

                if (TempList!=null && TempList.size()>0){
                    String Stoves = "";
                    for (Map<String, Object>Tem :TempList
                            ) {
                        Stoves = (!"".equals(Stoves)?",":"") + String.valueOf(Tem.get("JUDGE_STOVE_NO"));
                    }
                    cro.setV_errMsg("无法撤销下发,判定炉号："+  Stoves+ "轧管炉计划已经下发，请联系轧管相关人员撤销炉计划");
                    return cro;
                }


                PlnSteelforcast =
                        ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NeedUpdateCast2", new Object[]{Cast, Cast}).getResult() ).getResult();

            }


            StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();
            for (Map<String, Object> CastEntity : PlnSteelforcast
                    ) {

                Cast = CastEntity.get("CAST_NO").toString();

                String CastStatus = CastEntity.get("STATUS").toString();

                if ("00".equals(Status) && "00".equals(CastStatus)) {
                    continue;
                }


                if ("10".equals(Status) && !"1".equals(CastEntity.get("IF_CONFIRM"))) {
                    cro.setV_errMsg("浇次号：" + Cast + " 没有确认，无法下发！");
                    return cro;
                }


         /*       if ("00".equals(Status) && !"10".equals(CastStatus)) {
                    cro.setV_errMsg("浇次号：" + Cast + " 不是下发状态，无法取消下发！");
                    return cro;
                }*/


                listsqlID.add("FrmSteelManagement.UpdatePlnSteelforcastEntityStatus");

                listpara.add(new Object[]{Status, Operator, Cast});

                ArrayList statusList = new ArrayList();
                statusList.add("00".equals(Status) ? "10" : "00");


                List<Map<String, Object>> PlnSteelforfurnace =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(Cast, null, "", "", null, new ArrayList(),
                                new ArrayList(), new ArrayList(), new ArrayList()
                        ).getResult() ).getResult();

                if (PlnSteelforfurnace != null && PlnSteelforfurnace.size() > 0) {
                    for (Map<String, Object> data : PlnSteelforfurnace
                            ) {

                        String FurnacenoStatus = String.valueOf(data.get("STATUS"));
                        String Furnace = data.get("FURNACENO").toString();
                        String EfPlineCode = data.get("EF_PLINE_CODE").toString();
                        Date StartDate = (Date) data.get("PLN_STARTTIME");


                        if ("00".equals(Status) && !"10".equals(FurnacenoStatus)) {
                           cro.setV_errMsg("炉次号：" + Furnace + " 不是下发状态，无法取消下发！");
                        return cro;                 
                    }
                        if ("10".equals(Status) && !"1".equals(data.get("IF_CONFIRM"))) {
                            cro.setV_errMsg("炉次号：" + Furnace + " 未工艺确认，无法下发！");
                            return cro;
                        }
                        //String PlineCode = data.get("PLINE_CODE").toString();
                        String StoveNo = "";
                        /*if ("0".equals(data.get("IF_CONFIRM").toString())) {
                            cro.setV_errMsg("炉次号：" + Furnace + " 的工艺文件未确认，无法下发！");
                            return cro;
                        }*/

                        if ("10".equals(Status)) {
                            Date RealDate = BaseHelper.dateAddSecond(StartDate, 3 * 60 * 60);

                            if (!NewStoveNo.containsKey(EfPlineCode)) {
                                NewStoveNo.put(EfPlineCode, StoveNoGenerate.StoveNoGenerateByPineCode(EfPlineCode,RealDate));
                            }
                            else if(!StoveNoGenerate.CompareStove(EfPlineCode,NewStoveNo.get(EfPlineCode),  RealDate)){
                                NewStoveNo.put(EfPlineCode, StoveNoGenerate.StoveNoGenerateByPineCode(EfPlineCode, RealDate));
                            }




                            StoveNo = NewStoveNo.get(EfPlineCode);


                            String TempStove = "0000000" + ( Integer.parseInt(StoveNo) + 1 );
                            NewStoveNo.put(EfPlineCode, TempStove.substring(TempStove.length() - StoveNo.length()));
                        }

                        listsqlID.add("FrmSteelManagement.UpdatePlnSteelforfurnaceEntityStatusByFurnaceNo");

                        listpara.add(new Object[]{Status, StoveNo, Operator, Furnace});


                        Furnacenos.add(Furnace);

                    /*    listsqlID.add("FrmSteelManagement.UpdatePlnPlnSteelforOrdEntityStatusByFurnaceNo");

                        listpara.add(new Object[]{Furnace});*/

                        List<Map<String, Object>> PlnSteelforOrdEntitys =
                                ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(Furnace).getResult() ).getResult();


                        Long CurrentLen = null;
                        String TempJudgeStove = "";

                        Long Gp = 1L;


                        Map<String, List<String>> check = new HashMap<String, List<String>>();




                        for (Map<String, Object> orderEnetity : PlnSteelforOrdEntitys
                                ) {
                            Long FurnaceSeq = ( (BigDecimal) orderEnetity.get("SEQUENCENO") ).longValue();
                            Long Len = ( (BigDecimal) orderEnetity.get("LEN_GP_SINGLE") ).longValue();
                            String SURPLUS_TYPE = orderEnetity.get("SURPLUS_TYPE") == null ? "" : orderEnetity.get("SURPLUS_TYPE").toString();
                            String MLen = String.valueOf(orderEnetity.get("LEN_GP_SINGLE"));
                            String GOWHERE_NAME = String.valueOf(orderEnetity.get("GOWHERE_NAME"));
                            String OrderNo = orderEnetity.get("ORDER_NO") == null ? "" : orderEnetity.get("ORDER_NO").toString();
                            String MergeNo = orderEnetity.get("MERGE_NO") == null ? "" : orderEnetity.get("MERGE_NO").toString();
                            String ProPlanId =  orderEnetity.get("PRO_PLAN_ID") == null ? "" : orderEnetity.get("PRO_PLAN_ID").toString();
                            String GxPlanNo =  orderEnetity.get("GX_PLAN_NO") == null ? "" : orderEnetity.get("GX_PLAN_NO").toString();
                            String ProMoth = orderEnetity.get("PRO_MONTH") == null ? "" : orderEnetity.get("PRO_MONTH").toString();
                            String OrderStatus = String.valueOf(orderEnetity.get("STATUS"));


                            if ("00".equals(Status) && !"10".equals(OrderStatus)) {
                                cro.setV_errMsg("炉次号：" + Furnace + " 下的订单不是下发状态，无法取消下发！");
                                return cro;
                            }


                            if ("10".equals(Status)) {
                                if ("".equals(MergeNo)) {
                                    if (!"".equals(ProPlanId)) {
                                        ProPlanIds.add(ProPlanId);
                                        GxPlanNos.add(GxPlanNo);
                                    }
                                } else {
                                    ArrayList MergeNoList = new ArrayList();
                                    MergeNoList.add(MergeNo);
                                    List<Map<String, Object>> PlnSteelforMergeEntity =
                                            ( (CoreReturnSortObject) SelectPlnSteelforMergeEntity(MergeNoList).getResult() ).getResult();

                                    for (Map<String, Object> MergeDetail : PlnSteelforMergeEntity
                                            ) {
                                        ProPlanId = MergeDetail.get("PRO_PLAN_ID") == null ? "" : MergeDetail.get("PRO_PLAN_ID").toString();
                                        GxPlanNo = orderEnetity.get("GX_PLAN_NO") == null ? "" : orderEnetity.get("GX_PLAN_NO").toString();
                                        if (!"".equals(ProPlanId)) {
                                            ProPlanIds.add(ProPlanId);
                                            GxPlanNos.add(GxPlanNo);
                                        }
                                    }

                                }
                            }

                         /*   if ("10".equals(Status) && !"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE) && check.containsKey(MLen) && check.get(MLen).contains(GOWHERE_NAME)) {
                                cro.setV_errMsg("虚拟炉次号：" + Furnace + " 下有同定尺同去去向的订单,无法下发！");
                                return cro;
                            }*/

                            List<String> listGoWhere = check.get(MLen);
                            if (listGoWhere == null) listGoWhere = new ArrayList<String>();
                            if (!"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) {
                                listGoWhere.add(GOWHERE_NAME);
                                check.put(MLen, listGoWhere);
                            }


                            String PrintStove = "";

                            if ("10".equals(Status)) {
                                if (!"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) {
                                    if (!"".equals(SURPLUS_TYPE) && "".equals(OrderNo)) {
                                        cro.setV_errMsg("虚拟炉次号：" + Furnace + " 下有余材没有备料号！");
                                        return cro;
                                    }


                                    if (CurrentLen == null) {
                                        TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(EfPlineCode, StoveNo, 1L,ProMoth);
                                        CurrentLen = Len;
                                    } else {//if (!CurrentLen.equals(Len)){

                                        CurrentLen = Len;
                                        Gp++;
                                        if (!NewJudgeStoveNo.containsKey(EfPlineCode) ||   !LastProMoth.substring(0,4).equals(ProMoth.substring(0,4))) {
                                            TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(EfPlineCode, StoveNo, Gp,ProMoth);
                                            NewJudgeStoveNo.put(EfPlineCode, TempJudgeStove);

                                            //浇次内跨年
                                        } else {
                                            TempJudgeStove =  String.format("%06d",(Integer.parseInt(NewJudgeStoveNo.get(EfPlineCode)) + 1));
                                            NewJudgeStoveNo.put(EfPlineCode, TempJudgeStove);

                                        }
                                        LastProMoth = ProMoth;
                                    }

                                    PrintStove =  StoveNoGenerate.GePrintStove(EfPlineCode, StoveNo, Gp,ProMoth);

                                } else {
                                    TempJudgeStove = "";
                                    PrintStove = "";
                                }


                            }

                            listsqlID.add("FrmSteelManagement.UpdatePlnPlnSteelforOrdEntityStatusAndJudge");
                            listpara.add(new Object[]{PrintStove, TempJudgeStove, "".equals(TempJudgeStove) ? "" : ( TempJudgeStove + "-01" ), Furnace, FurnaceSeq});



                        }


                    }
                }
            }

 /*           List<String> ListUseProId = new ArrayList<String>();

            for (int i = 0; i < ProPlanIds.size() ; i++) {
                 if(ListUseProId.contains(ProPlanIds.get(i)+"-" + GxPlanNos.get(i))) continue;

                ListUseProId.add(ProPlanIds.get(i)+"-" + GxPlanNos.get(i) );
                List<Map<String, Object>> PlnOrderCastEntitys = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderCastS1",
                        new Object[]{ProPlanIds.get(i),GxPlanNos.get(i) }).getResult() ).getResult();
                if (PlnOrderCastEntitys != null && PlnOrderCastEntitys.size() > 0) {
                    listsqlID.add("FrmSteelManagement.InsertOrderCastSByCopy");
                    listpara.add( new Object[]{ProPlanIds.get(i),GxPlanNos.get(i)});
                    listsqlID.add("FrmSteelManagement.UpdateOrderCastS2");
                    listpara.add( new Object[]{ProPlanIds.get(i),GxPlanNos.get(i)});
                }

            }*/
            cro = setDataById(listsqlID, listpara);
            cro.setResult(Furnacenos.toArray(new String[Furnacenos.size()]));
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }

    /**
     * 更新浇次顺序
     *
     * @param CastList 需要调整顺序的浇次号List
     * @param TCast    需要调整的浇次号list 中最小的seq
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject AdjustCastSeq(
            ArrayList CastList,
            String TCast
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();


        try {

            if (CastList == null || CastList.size() == 0) {
                cro.setV_errMsg("浇次号未知！");
                return cro;
            }

            List<Map<String, Object>> TCastEntity =
                    ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(TCast).getResult() ).getResult();

            if (TCastEntity == null || TCastEntity.size() <= 0) {
                cro.setV_errMsg("找不到目标 浇次！");
                return cro;
            }

            Long TargetCastSeq = ( (BigDecimal) TCastEntity.get(0).get("CAST_SEQ") ).longValue();
            String TargetPline = String.valueOf(TCastEntity.get(0).get("PLINE_CODE"));

            List<Map<String, Object>> CastListEntity =
                    ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(CastList).getResult() ).getResult();

            if (CastListEntity == null || CastListEntity.size() <= 0) {
                cro.setV_errMsg("找不到需要移动的浇次！");
                return cro;
            }


            Long MinSeq = TargetCastSeq;
            Long MaxSeq = TargetCastSeq;
            String MinCastNo = TCast;

            for (Map<String, Object> Entity : CastListEntity
                    ) {
                Long TempCastSeq = ( (BigDecimal) Entity.get("CAST_SEQ") ).longValue();
                String TempPline = String.valueOf(Entity.get("PLINE_CODE"));
                String TempCastNo = String.valueOf(Entity.get("CAST_NO"));
                if (!TempPline.equals(TargetPline)) {
                    cro.setV_errMsg("不同铸造产线无法调整顺序！");
                    return cro;
                }

                if (TempCastSeq < MinSeq) {
                    MinSeq = TempCastSeq;
                    MinCastNo = TempCastNo;
                }

                if (TempCastSeq > MaxSeq) MaxSeq = TempCastSeq;


            }

            List<Map<String, Object>> NeedAdjustCastList = new ArrayList<Map<String, Object>>();
            ArrayList Lines = new ArrayList();
            Lines.add(TargetPline);

            List<Map<String, Object>> TempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(Lines, MinSeq, MaxSeq - 1L, CastList).getResult() ).getResult();

            if (MaxSeq == TargetCastSeq) {
                if (TempList != null && TempList.size() >= 0)
                    NeedAdjustCastList.addAll(TempList);
            }

            NeedAdjustCastList.addAll(CastListEntity);
            if (MinSeq == TargetCastSeq) {
                if (TempList != null && TempList.size() >= 0)
                    NeedAdjustCastList.addAll(TempList);
            }

            TempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMinCCMSeq",
                    new Object[]{MinCastNo, TargetPline}).getResult() ).getResult();
            Long MinCcmSeq = ( (BigDecimal) TempList.get(0).get("CCM_SEQ") ).longValue();


            for (Map<String, Object> Entity : NeedAdjustCastList
                    ) {
                String CastNo = String.valueOf(Entity.get("CAST_NO"));
                Long CurrentCastSeq = MinSeq++;
                listsqlID.add("FrmSteelManagement.UpdateCastSeqByCastNo");
                listpara.add(new Object[]{CurrentCastSeq, CastNo});

                listsqlID.add("FrmSteelManagement.UpdateFurnaceByCast");
                listpara.add(new Object[]{CastNo});


                List<Map<String, Object>> OrderList = ( (CoreReturnSortObject) SelectPlnSteelforOrdEntity(CastNo).getResult() ).getResult();
                for (Map<String, Object> Order : OrderList
                        ) {
                    listsqlID.add("FrmSteelManagement.UpdateCCMSeq1");
                    listpara.add(new Object[]{MinCcmSeq++, CurrentCastSeq, Order.get("FURNACENO"), Order.get("SEQUENCENO")});

                }


            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    public CoreReturnObject AdjustCastFurnaceSeq(
            String CastNo,
            String TCastNo,
            String EfPLine,
            Boolean First
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);

        List<Map<String, Object>> FurnaceList =
                ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(CastNo).getResult() ).getResult();

        if (FurnaceList == null || FurnaceList.size() <= 0) {
            cro.setV_errMsg("无拖动数据！");
            return cro;
        }


        List<Map<String, Object>> TFurnaceList =
                ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TCastNo).getResult() ).getResult();

        if (TFurnaceList == null || TFurnaceList.size() <= 0) {
            cro.setV_errMsg("找不到目标 浇次！");
            return cro;
        }


        return AdjustFurnaceSeq(FurnaceList, TFurnaceList.get(0), 0);


/*        List<Map<String, Object>> FurnaceList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectPlnSteelforfurnaceByCasts",
             new Object[]{EfPLine,CastNo,TCastNo,CastNo,TCastNo}).getResult() ).getResult();

        int Index1=0;
        int Index2=0;


        if(FurnaceList==null || FurnaceList.size()<=0  )
        {
            cro.setV_errMsg("无拖动数据！");
            return cro;
        }

        ArrayList FurnaceNoList = new ArrayList();
        ArrayList F1 = new ArrayList()     ;
         ArrayList F2 = new ArrayList()     ;

        for (Map<String, Object> data :
        FurnaceList ) {
            String CurrentFurnaceNo = data.get("FURNACENO").toString();
            String CurrentCastNo = data.get("CAST_NO").toString();
            if(CurrentCastNo.equals(CastNo))
            {
                F1.add(Index1++,CurrentFurnaceNo );
            }
            else   if(CurrentCastNo.equals(TCastNo))
            {
                F1.add(CurrentFurnaceNo );
            }else{
                F2.add(CurrentFurnaceNo);
            }





        }

        if (First)
        {
                 FurnaceNoList.addAll(F1);
                        FurnaceNoList.addAll(F2);
        }
        else {
            FurnaceNoList.addAll(F2);
            FurnaceNoList.addAll(F1);
        }




        return AdjustCastFurnaceSeq(FurnaceNoList  ,EfPLine);*/


    }


    public synchronized CoreReturnObject AdjustFurnaceSeq(
            ArrayList<String> FurnaceList,
            String TargetFurnace
    ) throws Exception {
        return AdjustFurnaceSeq(FurnaceList, TargetFurnace, 0);

    }

    /**
     * @param FurnaceList
     * @param TargetFurnace
     * @param Type          0 -正常调整顺序  1.更改成目标浇次号   2.生成新的浇次号
     * @return
     * @throws Exception
     */
    public synchronized CoreReturnObject AdjustFurnaceSeq(
            ArrayList<String> FurnaceList,
            String TargetFurnace,
            Integer Type
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        List<Map<String, Object>> tempList;


        List<Map<String, Object>> AllFurnaceList = new ArrayList<Map<String, Object>>();
        for (String Furnace : FurnaceList
                ) {
            tempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", Furnace, "", null, null, null).getResult() ).getResult();

            if (tempList != null && tempList.size() > 0) {
                AllFurnaceList.addAll(tempList);
            }
        }


        if (AllFurnaceList.size() <= 0) {
            cro.setV_errMsg("无拖动数据！");
            return cro;
        }


        List<Map<String, Object>> TFurnaceList =
                ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", TargetFurnace, "", null, null, null).getResult() ).getResult();

        if (TFurnaceList == null || TFurnaceList.size() <= 0) {
            cro.setV_errMsg("找不到目标 浇次！");
            return cro;
        }


        return AdjustFurnaceSeq(AllFurnaceList, TFurnaceList.get(0), Type.intValue());

    }


    public synchronized CoreReturnObject AdjustFurnaceSeq(
            List<Map<String, Object>> FurnaceListEntity,
            Map<String, Object> TargetFurnaceEntity,
            int Type
    ) throws Exception {

        return AdjustFurnaceSeq(FurnaceListEntity, TargetFurnaceEntity, "", Type);
    }


    /**
     * // TODO 目前没完成换产线逻辑
     *
     * @param FurnaceListEntity   调整顺序 选中的炉次号
     * @param TargetFurnaceEntity 选中的炉次号 调整至哪个炉次号之上
     * @param Type                0 -正常调整顺序  1.更改成目标浇次号（浇次中断）   2.浇次中断  3.调至最后 , 4,补炉 , 5重炼, 6,浇次中断（不新加炉），7重炼分切
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public synchronized CoreReturnObject AdjustFurnaceSeq(
            List<Map<String, Object>> FurnaceListEntity,
            Map<String, Object> TargetFurnaceEntity,
            String Operator,
            int Type
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        List<Map<String, Object>> tempList;

        StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();
        String sql = "";
        try {

            List<String> AddFurnaceno = new ArrayList<String>();
            List<String> DeleteFurnaceno = new ArrayList<String>();

            List<Map<String, Object>> NeedAdjustFurnaceList = new ArrayList<Map<String, Object>>();
            List<String> ListFurnace = new ArrayList<String>();
            Map<String, Double> FurnaceUseTimes = new HashMap<String, Double>();
            Boolean NeedChangePline = false;
            Integer CCMSeqAdjustCount = 0;
            Map<String, Object> AdjustFurnacenoEnity = null;
            Map<String, Long> CastStoveSeq = new HashMap<String, Long>();

            //region 查询目标炉号的数据

            String TargetEfPline = String.valueOf(TargetFurnaceEntity.get("EF_PLINE_CODE"));
            String TargetPline = String.valueOf(TargetFurnaceEntity.get("PLINE_CODE"));
            String TargetEfPlineName = String.valueOf(TargetFurnaceEntity.get("EF_PLINE_NAME"));
            String TargetCastNo = String.valueOf(TargetFurnaceEntity.get("CAST_NO"));
            Long TargetCastSeq = ( (BigDecimal) ( TargetFurnaceEntity.get("CAST_SEQ") ) ).longValue();
            Long TargetCastStoveSeq = ( (BigDecimal) TargetFurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
            Long TargetFurnaceSeq = ( (BigDecimal) TargetFurnaceEntity.get("FURNACENO_SEQ") ).longValue();
            String TargetGRADECODE = String.valueOf(TargetFurnaceEntity.get("GRADECODE"));
            String TargetFurnaceNo = String.valueOf(TargetFurnaceEntity.get("FURNACENO"));
            String TargetProMoth =String.valueOf(TargetFurnaceEntity.get("PRO_MONTH"));
            String TargetStove = "";

            if (TargetFurnaceEntity.get("STOVE_NO") != null) {
                TargetStove = TargetFurnaceEntity.get("STOVE_NO").toString();
            }

            //endregion


            Long MaxFurnaceSeq = null;
            Map<String, Long> MinFurnaceSeq = new HashMap<String, Long>();
            ArrayList<String> FurnaceList = new ArrayList<String>();


            String NewCastNo = "";
            String NewFurnaceNo = "";
            String OldFurnaceNo = "";
            Long OldSEQUENCENO = null;
            ;
            String OldCast = "";
            List<Long> OldCastStoveSeq = new ArrayList<Long>();

            //region 浇次中断  调整顺序前处理

            if (Type == 2) {

                //生成新的浇次号
                String Pre = TargetPline.equals("C004") ? "2" : TargetPline.equals("C007") ? "3" : "1";
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewCastNo",
                        new Object[]{Pre, Pre, Pre}).getResult() ).getResult();
                NewCastNo = tempList.get(0).get("CAST_NO").toString();

                listsqlID.add("FrmSteelManagement.InsertCastByCopyCast2");
                listpara.add(new Object[]{NewCastNo, FurnaceListEntity.size(), FurnaceListEntity.get(0).get("CAST_NO").toString()});

                //生成新的炉次号  补首炉加量  和断浇那炉的订单材
                OldFurnaceNo = TargetFurnaceNo;

                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewFurnace",
                        new Object[]{NewCastNo, NewCastNo}).getResult() ).getResult();
                NewFurnaceNo = tempList.get(0).get("FURNACENO").toString();


                listsqlID.add("FrmSteelManagement.InsertFurnaceByCopyFurnace");
                listpara.add(new Object[]{NewFurnaceNo, 1, "50", Operator, OldFurnaceNo});

                TargetFurnaceEntity.put("CAST_NO", NewCastNo);
                TargetFurnaceEntity.put("STOVE_NO", "");
                TargetFurnaceEntity.put("FURNACENO", NewFurnaceNo);
                TargetFurnaceEntity.put("FURNACENO_SEQ", FurnaceListEntity.get(0).get("FURNACENO_SEQ"));
                FurnaceListEntity.add(0, TargetFurnaceEntity);


            }


            if (Type == 6) {

                //生成新的浇次号
                String Pre = TargetPline.equals("C004") ? "2" : TargetPline.equals("C007") ? "3" : "1";
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewCastNo",
                        new Object[]{Pre, Pre, Pre}).getResult() ).getResult();
                NewCastNo = tempList.get(0).get("CAST_NO").toString();

                listsqlID.add("FrmSteelManagement.InsertCastByCopyCast2");
                listpara.add(new Object[]{NewCastNo, FurnaceListEntity.size(), FurnaceListEntity.get(0).get("CAST_NO").toString()});

            }
            //endregion

            //region 补炉 前处理
            if (Type == 4) {

                //生成新的炉次号 并生成一条余材订单计划
                OldFurnaceNo = FurnaceListEntity.get(0).get("FURNACENO").toString();

                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewFurnace",
                        new Object[]{TargetCastNo, TargetCastNo}).getResult() ).getResult();
                NewFurnaceNo = tempList.get(0).get("FURNACENO").toString();


                listsqlID.add("FrmSteelManagement.InsertFurnaceByCopyFurnace");
                listpara.add(new Object[]{NewFurnaceNo, TargetCastStoveSeq + 1, "20", Operator, OldFurnaceNo});

                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxSequenceNo",
                        new Object[]{OldFurnaceNo}).getResult() ).getResult();
                if (tempList.get(0).get("SEQUENCENO") == null) {
                    cro.setV_errMsg("请选择含有订单材的炉计划进行补炉！");
                    return cro;
                }
                OldSEQUENCENO = ( (BigDecimal) tempList.get(0).get("SEQUENCENO") ).longValue();


                listsqlID.add("FrmSteelManagement.InsertOrderByCopyFurnace");
                listpara.add(new Object[]{NewFurnaceNo, 1, 1, Operator, GetBackUpOrder(TargetEfPline), OldFurnaceNo, OldSEQUENCENO});
            }
            //endregion


            //region 重炼  调整顺序前处理
            if (Type == 5) {

                //生成新的浇次号
                String Pre = TargetPline.equals("C004") ? "2" : TargetPline.equals("C007") ? "3" : "1";
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewCastNo",
                        new Object[]{Pre, Pre, Pre}).getResult() ).getResult();
                NewCastNo = tempList.get(0).get("CAST_NO").toString();

                listsqlID.add("FrmSteelManagement.InsertCastByCopyCast2");
                listpara.add(new Object[]{NewCastNo, FurnaceListEntity.size(), FurnaceListEntity.get(0).get("CAST_NO").toString()});


                //生成新的炉次号 并生成一条余材订单计划
                OldFurnaceNo = FurnaceListEntity.get(0).get("FURNACENO").toString();

                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewFurnace",
                        new Object[]{TargetCastNo, TargetCastNo}).getResult() ).getResult();
                NewFurnaceNo = tempList.get(0).get("FURNACENO").toString();


                listsqlID.add("FrmSteelManagement.InsertFurnaceByCopyFurnace");
                listpara.add(new Object[]{NewFurnaceNo, TargetCastStoveSeq, "05", Operator, OldFurnaceNo});


                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(OldFurnaceNo).getResult() ).getResult();

                if (tempList == null && tempList.size() <= 0) {
                    cro.setV_errMsg("找不到所选中的炉计划订单数据！");
                    return cro;
                }

                int Count = 0;
                Map<String, Object> Data = null;

                for (Map<String, Object> Temp : tempList
                        ) {
                    String SURPLUS_TYPE = Temp.get("SURPLUS_TYPE") == null ? "" : Temp.get("SURPLUS_TYPE").toString();
                    String PRO_PLAN_ID = Temp.get("PRO_PLAN_ID") == null ? "" : Temp.get("PRO_PLAN_ID").toString();
                    String Status = Temp.get("STATUS").toString();


                    if ("2".equals(SURPLUS_TYPE) || "3".equals(SURPLUS_TYPE)) continue;

           /*         if (Status.equals("10") && Status.equals("00")){
                        cro.setV_errMsg("选中炉计划中的订单已经生产,无法重炼！");
                        return cro;
                    }*/

                    Count++;

                    if (!"".equals(PRO_PLAN_ID) && Data == null) {
                        Data = Temp;
                    }
                }

                if (Data == null) {
                    cro.setV_errMsg("选中炉计划中无订单材,无需重炼！");
                    return cro;
                }

                if (Count < tempList.size()) {
                    listsqlID.add("FrmSteelManagement.UpdateLastOrFirstFurnace");
                    listpara.add(new Object[]{
                            NewFurnaceNo,
                            OldFurnaceNo
                    });
                }

                if (Count > 1) {

                    listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
                    listpara.add(new Object[]{
                            1 - Count,
                            ( (BigDecimal) Data.get("CCM_SEQ") ).longValue() + 1,
                            TargetPline});
                    CCMSeqAdjustCount = 1 - Count;
                }

                String TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(TargetEfPline, TargetStove, 1L,TargetProMoth);

                listsqlID.add("FrmSteelManagement.InsertResmeltOrder1");
                listpara.add(new Object[]{
                        NewFurnaceNo,
                        1,
                        TargetStove,
                        TempJudgeStove,
                        TempJudgeStove + "-01",
                        StoveNoGenerate.GePrintStove(TargetEfPline, TargetStove, 1L,TargetProMoth),
                        GetBackUpOrder(TargetEfPline),
                        Data.get("FURNACENO"), Data.get("SEQUENCENO")
                });
            }

            if (Type == 7) {

                //生成新的浇次号
                String Pre = TargetPline.equals("C004") ? "2" : TargetPline.equals("C007") ? "3" : "1";
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewCastNo",
                        new Object[]{Pre, Pre, Pre}).getResult() ).getResult();
                NewCastNo = tempList.get(0).get("CAST_NO").toString();

                listsqlID.add("FrmSteelManagement.InsertCastByCopyCast2");
                listpara.add(new Object[]{NewCastNo, FurnaceListEntity.size(), FurnaceListEntity.get(0).get("CAST_NO").toString()});


                //生成新的炉次号 并生成一条余材订单计划
                OldFurnaceNo = TargetFurnaceEntity.get("FURNACENO").toString();

                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewFurnace",
                        new Object[]{NewCastNo, NewCastNo}).getResult() ).getResult();
                NewFurnaceNo = tempList.get(0).get("FURNACENO").toString();

                AddFurnaceno.add(NewFurnaceNo);
                AddFurnaceno.add(OldFurnaceNo);
                DeleteFurnaceno.add(OldFurnaceNo);

                listsqlID.add("FrmSteelManagement.InsertFurnaceByCopyFurnace");
                listpara.add(new Object[]{NewFurnaceNo, 1, "05", Operator, OldFurnaceNo});


                tempList = (List<Map<String, Object>>) TargetFurnaceEntity.get("OrderData");


                List<Map<String, Object>> tempList2 = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxCCMSeq1",
                        new Object[]{TargetPline}).getResult() ).getResult();
                Long LastCCMSeq = ( (BigDecimal) tempList2.get(0).get("CCM_SEQ") ).longValue();


                int Count = 1;
                for (Map<String, Object> Temp : tempList
                        ) {
                    String TempFurnaceno = String.valueOf(Temp.get("FURNACENO"));
                    String TempSequenceno = String.valueOf(Temp.get("SEQUENCENO"));


                    listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder");
                    listpara.add(new Object[]{
                            NewFurnaceNo, Count, TempFurnaceno, TempSequenceno
                    });

                    Temp.put("CAST_NO", NewCastNo);
                    Temp.put("FURNACENO", NewFurnaceNo);
                    Temp.put("SEQUENCENO", new BigDecimal(Count));
                    Temp.put("CCM_SEQ", new BigDecimal(LastCCMSeq));

                    listsqlID.add("FrmSteelManagement.UpdateResmeltOrder");
                    listpara.add(new Object[]{
                            GetBackUpOrder(TargetEfPline),
                            TempFurnaceno, TempSequenceno
                    });

                    Count++;
                }

                TargetFurnaceEntity.put("OrderData", tempList);
                TargetFurnaceEntity.put("CAST_NO", NewCastNo);
                TargetFurnaceEntity.put("FURNACENO", NewFurnaceNo);
                TargetFurnaceEntity.put("CAST_STOVE_SEQ", new BigDecimal(1));
                TargetFurnaceEntity.put("STOVE_NO", "");
                TargetFurnaceEntity.put("STATUS", "10");
                TargetFurnaceEntity.put("HEAT_STATE", "00");
                TargetFurnaceEntity.put("STARTTIME", null);
                TargetFurnaceEntity.put("ENDTIME", null);
                TargetFurnaceEntity.put("ENDTIME", null);


                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxCastSeq",
                        new Object[]{TargetPline}).getResult() ).getResult();
                TargetFurnaceEntity.put("CAST_SEQ", tempList.get(0).get("CAST_SEQ"));
            }
            //endregion


            //region 循环选中的炉次计划   获取最小炉次号 或者最大炉次号   并修改相应功能需要对选中炉次计划改变的逻辑  如修改浇次号
            for (Map<String, Object> Entity : FurnaceListEntity
                    ) {
                String tempEfPline = Entity.get("EF_PLINE_CODE").toString();
                Long FurnaceNoSeq = ( (BigDecimal) Entity.get("FURNACENO_SEQ") ).longValue();
                String GRADECODE = Entity.get("GRADECODE").toString();
                String Status = Entity.get("STATUS").toString();
                String tempFurnaceNo = Entity.get("FURNACENO").toString();
                ListFurnace.add(tempFurnaceNo);

                if (Type == 1) {
                    OldCast = Entity.get("CAST_NO").toString();
                    OldCastStoveSeq.add(( (BigDecimal) Entity.get("CAST_STOVE_SEQ") ).longValue());

                    if (!GRADECODE.equals(TargetGRADECODE)) {
                        cro.setV_errMsg("钢种不同,无法加入该浇次！");
                        return cro;
                    }

                  /*  if("1".equals(TargetCastStoveSeq)){
                        cro.setV_errMsg("无法调至首炉之前！");
                        return cro;
                    }*/

                    Entity.put("CAST_NO", TargetCastNo);
                    Entity.put("CAST_SEQ", new BigDecimal(TargetCastSeq));
                    Entity.put("CAST_STOVE_SEQ", new BigDecimal(TargetCastStoveSeq));
                }

                if (Type == 2) {
                    OldCast = Entity.get("CAST_NO").toString();
                    OldCastStoveSeq.add(( (BigDecimal) Entity.get("CAST_STOVE_SEQ") ).longValue());

                    Entity.put("CAST_NO", NewCastNo);
                    Entity.put("CAST_SEQ", new BigDecimal(( TargetCastSeq + 1 ) + ""));
                    Entity.put("CAST_STOVE_SEQ", new BigDecimal(1));
                }
                if (Type == 6) {
                    OldCast = Entity.get("CAST_NO").toString();
                    OldCastStoveSeq.add(( (BigDecimal) Entity.get("CAST_STOVE_SEQ") ).longValue());

                    Entity.put("CAST_NO", NewCastNo);
                    Entity.put("CAST_STOVE_SEQ", new BigDecimal(1));
                }
                if (Type == 5) {
                    Entity.put("CAST_NO", NewCastNo);
                    Entity.put("CAST_STOVE_SEQ", new BigDecimal(1));
                    Entity.put("STOVE_NO", "");
                    Entity.put("STATUS", "10");
                    Entity.put("HEAT_STATE", "00");
                    Entity.put("STARTTIME", null);
                    Entity.put("ENDTIME", null);


                    tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxCastSeq",
                            new Object[]{TargetPline}).getResult() ).getResult();
                    Entity.put("CAST_SEQ", tempList.get(0).get("CAST_SEQ"));

                }


                if (Type == 4) {
                    Entity.put("FURNACENO", NewFurnaceNo);
                    Entity.put("CAST_STOVE_SEQ", new BigDecimal(TargetCastStoveSeq));
                    Entity.put("STOVE_NO", "");
                }

                FurnaceList.add(Entity.get("FURNACENO").toString());

                if (!MinFurnaceSeq.containsKey(tempEfPline) || MinFurnaceSeq.get(tempEfPline) > FurnaceNoSeq) {
                    MinFurnaceSeq.put(tempEfPline, FurnaceNoSeq);

                }

                if (TargetEfPline.equals(tempEfPline)) {
                    if (MaxFurnaceSeq == null || FurnaceNoSeq > MaxFurnaceSeq) MaxFurnaceSeq = FurnaceNoSeq;
                } else {
                    NeedChangePline = true;
                }
            }
            //endregion


            //region 确定最终的需要调整的炉计划的顺序


            //region 正常调整顺序   确定最终炉计划顺序
            if (Type == 0) {
                //选中炉次List 在目标炉次号上面则将所有选中炉次到目标炉次的炉计划拿出来 放在最前面  否则认为目标炉次的炉次顺序号为最小炉次号
                if (MinFurnaceSeq.containsKey(TargetEfPline) && MinFurnaceSeq.get(TargetEfPline) < TargetFurnaceSeq) {
                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, MinFurnaceSeq.get(TargetEfPline) - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                } else {
                    MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);
                }

                //加上选中的炉计划
                NeedAdjustFurnaceList.addAll(FurnaceListEntity);


                //如选中炉计划在目标炉计划之下   目标炉计划到选中炉计划之间的炉计划排在选中炉计划之后     如果涉及到换产线 则后面的炉计划全部需要调整顺序（相当于当前产线增加炉计划）
                if (NeedChangePline || MaxFurnaceSeq == null || MaxFurnaceSeq > TargetFurnaceSeq) {
                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1,
                                    ( MaxFurnaceSeq == null || NeedChangePline ) ? null : MaxFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                }
            }
            //endregion

            if (Type == 1) {
                //选中炉次List 在目标炉次号上面则将所有选中炉次到目标炉次的炉计划拿出来 放在最前面  否则认为目标炉次的炉次顺序号为最小炉次号
                if (MinFurnaceSeq.containsKey(TargetEfPline) && MinFurnaceSeq.get(TargetEfPline) < TargetFurnaceSeq) {

                    List<Map<String, Object>> MaxSeqList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMinFurnacenoSeqByCast",
                            new Object[]{TargetEfPline, OldCast}).getResult() ).getResult();

                    Long OldCastMin = ( (BigDecimal) MaxSeqList.get(0).get("FURNACENO_SEQ") ).longValue();

                    if (MinFurnaceSeq.get(TargetEfPline) > OldCastMin) MinFurnaceSeq.put(TargetEfPline, OldCastMin);

                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, OldCastMin - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                } else {
                    MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);
                }

                //加上选中的炉计划
                NeedAdjustFurnaceList.addAll(FurnaceListEntity);


                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

                for (int i = 0; i < NeedAdjustFurnaceList.size(); i++) {
                    if (NeedAdjustFurnaceList.get(i).get("CAST_NO").toString().equals(OldCast) && !FurnaceListEntity.contains(NeedAdjustFurnaceList.get(i))) {
                        AdjustFurnacenoEnity = NeedAdjustFurnaceList.get(i);
                        List<Map<String, Object>> PlnSteelforOrdEntitys =
                                ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(NeedAdjustFurnaceList.get(i).get("FURNACENO").toString()).getResult() ).getResult();
                        AdjustFurnacenoEnity.put("OrderData", PlnSteelforOrdEntitys);
                    }
                }


            }

            //region 浇次中断   炉计划最终顺序  --》 将选中炉计划 到 不是编制中的的炉计划之间的所有炉计划 --- 选中炉计划 -- 编制中炉计划
            if (Type == 2) {

                //找到不是编制中的状态的最大炉次
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxFurnace",
                        new Object[]{TargetEfPline}).getResult() ).getResult();
                TargetFurnaceSeq = ( (BigDecimal) tempList.get(0).get("FURNACENO_SEQ") ).longValue();

                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, MinFurnaceSeq.get(TargetEfPline) - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }


                NeedAdjustFurnaceList.addAll(FurnaceListEntity);


                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

            }

            if (Type == 6) {

                //找到不是编制中的状态的最大炉次
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxFurnace",
                        new Object[]{TargetEfPline}).getResult() ).getResult();
                TargetFurnaceSeq = ( (BigDecimal) tempList.get(0).get("FURNACENO_SEQ") ).longValue();

                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, MinFurnaceSeq.get(TargetEfPline) - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }


                NeedAdjustFurnaceList.addAll(FurnaceListEntity);


                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

            }
            //endregion


            //region 补炉   炉计划最终顺序 -->  （浇次只有一炉）：尾炉--新加炉 -- 后面所有炉计划   （浇次有多炉）--新加炉--尾炉--后面所有炉
            if (Type == 4) {
                //目标炉计划永远是 最后一炉     所以调整顺序的最小炉次顺序是目标炉次顺序号
                MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);

                // 目标炉计划 同时也是首炉  尾炉需要移至前面
                if (TargetCastStoveSeq == 1) {
                    NeedAdjustFurnaceList.add(TargetFurnaceEntity);
                }

                NeedAdjustFurnaceList.addAll(FurnaceListEntity);

                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline,
                                ( TargetCastStoveSeq == 1 ) ? TargetFurnaceSeq : ( TargetFurnaceSeq - 1 ),
                                null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

            }
            //endregion

            //region 重炼   炉计划最终顺序  --》 将选中炉计划 到 不是编制中的的炉计划之间的所有炉计划 --- 选中炉计划 -- 编制中炉计划
            if (Type == 5) {


                //找到不是编制中的状态的最大炉次
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxFurnace",
                        new Object[]{TargetEfPline}).getResult() ).getResult();
                TargetFurnaceSeq = ( (BigDecimal) tempList.get(0).get("FURNACENO_SEQ") ).longValue();

                FurnaceListEntity.get(0).put("PLN_STARTTIME", tempList.get(0).get("PLN_ENDTIME"));
                MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);

                NeedAdjustFurnaceList.addAll(FurnaceListEntity);

                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

            }

            if (Type == 7) {


                //找到不是编制中的状态的最大炉次
                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxFurnace",
                        new Object[]{TargetEfPline}).getResult() ).getResult();
                TargetFurnaceSeq = ( (BigDecimal) tempList.get(0).get("FURNACENO_SEQ") ).longValue();

                FurnaceListEntity.get(0).put("PLN_STARTTIME", tempList.get(0).get("PLN_ENDTIME"));
                MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);

                NeedAdjustFurnaceList.add(TargetFurnaceEntity);

                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                if (tempList != null && tempList.size() > 0) {
                    NeedAdjustFurnaceList.addAll(tempList);
                }

            }


            //endregion


            //region 注释老代码
/*                //选中炉次List 在目标炉次号 上面
                if (Type == 2 || Type == 3) {
                    //找不处于编制中的最大浇次顺序号

                    tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxFurnace",
                            new Object[]{TargetEfPline}).getResult() ).getResult();
                    TargetFurnaceSeq = ( (BigDecimal) tempList.get(0).get("FURNACENO_SEQ") ).longValue();

                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, MinFurnaceSeq.get(TargetEfPline) - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }

                } else if (Type != 4 && MinFurnaceSeq.containsKey(TargetEfPline) && MinFurnaceSeq.get(TargetEfPline) < TargetFurnaceSeq) {

                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, MinFurnaceSeq.get(TargetEfPline) - 1, TargetFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                } else {
                    MinFurnaceSeq.put(TargetEfPline, TargetFurnaceSeq);
                }

                if (Type == 4 && TargetCastStoveSeq == 1) {
                        NeedAdjustFurnaceList.add(TargetFurnaceEntity);
                }


                NeedAdjustFurnaceList.addAll(FurnaceListEntity);


                if (Type == 2 || Type == 3) {
                    //找不处于编制中的最大浇次顺序号
                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1, null, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }

                } else if (Type == 4 || NeedChangePline || MaxFurnaceSeq == null || MaxFurnaceSeq > TargetFurnaceSeq) {
                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline,
                                    ( Type == 4 && TargetCastStoveSeq == 1 ) ? TargetFurnaceSeq : ( TargetFurnaceSeq - 1 ),
                                    ( Type == 4 || NeedChangePline ) ? null : MaxFurnaceSeq, FurnaceList).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                } else if (Type == 1) {
                    tempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(OldCast, OldCastStoveSeq, null).getResult() ).getResult();

                    if (tempList != null && tempList.size() > 0) {
                        NeedAdjustFurnaceList.addAll(tempList);
                    }
                }*/
            //endregion


            //endregion


            //region 初始化参数   取数据里的各个最小参数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            SimpleDateFormat sdf1 = new SimpleDateFormat("yy");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            String Year = sdf1.format(new Date());

            Date PlanStartDate = null;

            Double FurnaceUseTime = 50d;

            // 冶炼炉号
            String PlanStoveNo = "";
            Integer StoveIndex = 0;

            //判定炉号
            String PlanJudgeStoveNo = null;
            //对应电炉产线的炉次的炉次顺序号
            Integer Index = 0;


            //每个铸造产线CastSeq
            Map<String, List<Long>> CastSeqs = new HashMap<String, List<Long>>();

            //每个铸造产线CastSeq
            Map<String, Integer> CastSeqsIndex = new HashMap<String, Integer>();

            //每个浇次当前的CastSeq   防止浇次中炉交叉顺序  找对应的CastSeq
            Map<String, Long> CastCastSeq = new HashMap<String, Long>();


            //每个浇次首炉的浇次内炉顺序号
            Map<String, Long> CastFirstFurnacno = new HashMap<String, Long>();

            //每个浇次尾炉的浇次内炉顺序号
            Map<String, Long> CastLastFurnacno = new HashMap<String, Long>();

            //连铸顺序号
            Map<String, Long> CcmSeq = new HashMap<String, Long>();


            List<String> StoveList = new ArrayList<String>();

            Map<String, Map<String, Object>> StoveInfo = new HashMap<String, Map<String, Object>>();


            String CurrentStatus = "99";


            //首尾炉需要补炉  新加一炉  将尾炉加量余材移至新的一炉内
            Map<String, Object> NeedChangeOrder = null;

            //需要调整顺序的炉计划中 包含首炉的浇次
            List<String> LastFurnaces = new ArrayList<String>();
            //需要调整顺序的炉计划中  包含尾炉的浇次
            List<String> FirstFurnaces = new ArrayList<String>();

            for (Map<String, Object> FurnaceEntity : NeedAdjustFurnaceList
                    ) {
                String tempEfPline = FurnaceEntity.get("EF_PLINE_CODE").toString();
                String tempPline = FurnaceEntity.get("PLINE_CODE").toString();
                String tempFurnaceNo = FurnaceEntity.get("FURNACENO").toString();
                String tempStove = FurnaceEntity.get("STOVE_NO") == null ? "" : FurnaceEntity.get("STOVE_NO").toString();
                Date tempPlanStartTime = (Date) FurnaceEntity.get("PLN_STARTTIME");
                String tempCast = FurnaceEntity.get("CAST_NO").toString();
                Long tempCastSeq = ( (BigDecimal) FurnaceEntity.get("CAST_SEQ") ).longValue();
                Long tempCastStoveSeq = ( (BigDecimal) FurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
                String CASTINGTYPE = String.valueOf(FurnaceEntity.get("CASTINGTYPE"));
                String status = FurnaceEntity.get("STATUS").toString();
                BigDecimal WEIGHT = (BigDecimal) FurnaceEntity.get("AIM_STEEL_QTY");

                if ("10".equals(status) && "00".equals(CurrentStatus)) {
                    cro.setV_errMsg("编制状态中的炉计划不能调整下发状态之前！");
                    return cro;
                }

                if (tempCast.equals(OldCast)) {
                    int count = 0;
                    for (Long RemoveSeq : OldCastStoveSeq
                            ) {
                        if (tempCastStoveSeq > RemoveSeq) count++;
                    }
                    tempCastStoveSeq = tempCastStoveSeq - count;
                }


                CurrentStatus = status;


                if (!tempCast.equals(NewCastNo) && ( !CastFirstFurnacno.containsKey(tempCast) || !CastLastFurnacno.containsKey(tempCast) )) {

                    String sql1 = CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.FindMaxMinCastStoveSeq").replace("'", "''");


                    tempList = ( (CoreReturnSortObject) getData(sql1,
                            new Object[]{tempCast}).getResult() ).getResult();

                    if (tempList == null || tempList.size() <= 0 || tempList.get(0).get("MAXC") == null || tempList.get(0).get("MINC") == null) {
                        cro.setV_errMsg("数据异常,找不到最大最小浇次内顺序！");
                        return cro;
                    }


                    CastFirstFurnacno.put(tempCast, ( (BigDecimal) tempList.get(0).get("MINC") ).longValue());
                    CastLastFurnacno.put(tempCast, ( (BigDecimal) tempList.get(0).get("MAXC") ).longValue());


                    if (tempCast.equals(OldCast)) {
                        CastLastFurnacno.put(tempCast, ( (BigDecimal) tempList.get(0).get("MAXC") ).longValue() - OldCastStoveSeq.size());
                    }
                }

/*
                if (LastFurnaces.contains(tempCast) && !"2".equals(CASTINGTYPE) && Type!=4  ){
                    cro.setV_errMsg("无法将炉次移至尾炉之后！");
                    return cro;
                }*/


                //只有首尾炉都在该调整顺序中  才调整CastSeq 否则保存原CastSeq不变
                if (tempCastStoveSeq == CastFirstFurnacno.get(tempCast)) {
                    FirstFurnaces.add(tempCast);


                 /*   if(CastStoveSeq.containsKey(tempCast) ) {
                        cro.setV_errMsg("无法将炉次移至首炉之前！");
                        return cro;
                    }*/

                    if (LastFurnaces.contains(tempCast)) {

                        List<Long> TempCastSeqList = CastSeqs.containsKey(tempPline) ? CastSeqs.get(tempPline) : new ArrayList<Long>();
                        if (!TempCastSeqList.contains(tempCastSeq)) TempCastSeqList.add(tempCastSeq);
                        CastSeqs.put(tempPline, TempCastSeqList);
                        CastSeqsIndex.put(tempPline, 0);

                    }
                }

                if (tempCastStoveSeq == CastLastFurnacno.get(tempCast)) {
                    LastFurnaces.add(tempCast);

                    if (FirstFurnaces.contains(tempCast)) {
                        List<Long> TempCastSeqList = CastSeqs.containsKey(tempPline) ? CastSeqs.get(tempPline) : new ArrayList<Long>();
                        if (!TempCastSeqList.contains(tempCastSeq)) TempCastSeqList.add(tempCastSeq);
                        CastSeqs.put(tempPline, TempCastSeqList);
                        CastSeqsIndex.put(tempPline, 0);
                    }
                }


                if (!"".equals(tempStove) && tempEfPline.equals(TargetEfPline) && !StoveList.contains(tempStove)) {
                    StoveList.add(tempStove);
                    StoveInfo.put(tempStove, FurnaceEntity);


                }


                if (PlanStartDate == null || PlanStartDate.getTime() > tempPlanStartTime.getTime())
                    PlanStartDate = tempPlanStartTime;

                List<Map<String, Object>> PlnSteelforOrdEntitys = new ArrayList<Map<String, Object>>();

                if (Type == 4 && tempFurnaceNo.equals(NewFurnaceNo)) {
                    PlnSteelforOrdEntitys =
                            ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(OldFurnaceNo, OldSEQUENCENO).getResult() ).getResult();

                    PlnSteelforOrdEntitys.get(0).put("FURNACENO", NewFurnaceNo);
                    PlnSteelforOrdEntitys.get(0).put("SEQUENCENO", new BigDecimal(1));
                    PlnSteelforOrdEntitys.get(0).put("SURPLUS_TYPE", 6);


                } else if (Type == 2 && tempFurnaceNo.equals(NewFurnaceNo)) {

                    //region 浇次中断  对订单新增 转移
                    List<Map<String, Object>> TempList =
                            ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(OldFurnaceNo).getResult() ).getResult();


                    //找到该浇次首炉加量订单   复制一条给新增加的炉次
                    List<Map<String, Object>> TempListFirstOrder =
                            ( (CoreReturnSortObject) SelectPlnSteelforOrdEntity(TargetCastNo, null, null, null, null, "", 0L, "", null, null, null, null, null).getResult() ).getResult();
                    if (TempListFirstOrder.size() != 1 || TempListFirstOrder.get(0).get("TOT_WEIGHT_M") == null) {
                        cro.setV_errMsg("中断浇次首炉加量数据异常！");
                        return cro;
                    }
                    BigDecimal FirstOrderWeight = (BigDecimal) TempListFirstOrder.get(0).get("TOT_WEIGHT_M");
                    BigDecimal FirstOrderNum = (BigDecimal) TempListFirstOrder.get(0).get("TOT_MUM_M");
                    listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder2");
                    listpara.add(new Object[]{0, "", "", "", "", FirstOrderNum, FirstOrderWeight, NewFurnaceNo, TempListFirstOrder.get(0).get("FURNACENO"), TempListFirstOrder.get(0).get("SEQUENCENO")});


                    TempListFirstOrder.get(0).put("FURNACENO", NewFurnaceNo);
                    TempListFirstOrder.get(0).put("SEQUENCENO", new BigDecimal(0));
                    PlnSteelforOrdEntitys.add(TempListFirstOrder.get(0));

                    //找到该浇次尾炉加量订单  查到尾炉的量
                    List<Map<String, Object>> TempListLastOrder =
                            ( (CoreReturnSortObject) SelectPlnSteelforOrdEntity(TargetCastNo, null, null, null, null, "", 99L, "", null, null, null, null, null).getResult() ).getResult();
                    if (TempListLastOrder.size() != 1 || TempListLastOrder.get(0).get("TOT_WEIGHT_M") == null) {
                        cro.setV_errMsg("中断浇次尾炉加量数据异常！");
                        return cro;
                    }
                    BigDecimal LastOrderWeight = (BigDecimal) TempListLastOrder.get(0).get("TOT_WEIGHT_M");
                    BigDecimal OrLastOrderWeight = (BigDecimal) TempListLastOrder.get(0).get("TOT_WEIGHT_M");
                    BigDecimal LastOrderNum = (BigDecimal) TempListLastOrder.get(0).get("TOT_MUM_M");

                    boolean StopInLastFurnace = false;


                    BigDecimal TotalUsedWeight = new BigDecimal(0);

                    int OrderIndex = 0;
                    Long LastCcmSeq = 99l;


                    Long LastOrderSeq = null;

                    String IsSurplus = "0";
                    BigDecimal OrderMWeight = new BigDecimal(0);


                    for (int i = TempList.size() - 1; i >= 0 && LastOrderWeight.compareTo(new BigDecimal(0)) > 0; i--) {
                        IsSurplus = String.valueOf(TempList.get(i).get("SURPLUSFLAG"));
                        String SurplusType = String.valueOf(TempList.get(i).get("SURPLUS_TYPE"));
                        BigDecimal OrderWeight = (BigDecimal) TempList.get(i).get("TOT_WEIGHT_M");
                        BigDecimal OrderNum = (BigDecimal) TempList.get(i).get("TOT_MUM_M");
                        OrderMWeight = (BigDecimal) TempList.get(i).get("WEIGHT_M");
                        Long tempCcmSeq = ( (BigDecimal) TempList.get(i).get("CCM_SEQ") ).longValue();

                        //中断在尾炉   选中的炉计划  要补上尾炉
                        if ("3".equals(SurplusType)) {
                            StopInLastFurnace = true;
                            continue;
                        }


                        //需要调整的订单量等于当前订单量
                        if (LastOrderWeight.compareTo(OrderWeight) == 0) {


                            LastOrderSeq = ( (BigDecimal) TempList.get(i).get("SEQUENCENO") ).longValue();
                            listsqlID.add("FrmSteelManagement.ChangeOrderFurnaceNo");
                            listpara.add(new Object[]{NewFurnaceNo, ++OrderIndex, OldFurnaceNo, TempList.get(i).get("SEQUENCENO")});

                            TempList.get(i).put("FURNACENO"  , NewFurnaceNo);
                            TempList.get(i).put("SEQUENCENO", new BigDecimal(OrderIndex));

                            PlnSteelforOrdEntitys.add(TempList.get(i));
                            LastCcmSeq = tempCcmSeq;
                            TotalUsedWeight = TotalUsedWeight.add(OrderWeight);


                            if (!CcmSeq.containsKey(tempPline) || CcmSeq.get(tempPline) > tempCcmSeq + 1)
                                CcmSeq.put(tempPline, tempCcmSeq + 1);


                        } else if (LastOrderWeight.compareTo(OrderWeight) > 0) {

                            listsqlID.add("FrmSteelManagement.ChangeOrderFurnaceNo");
                            listpara.add(new Object[]{NewFurnaceNo, ++OrderIndex, OldFurnaceNo, TempList.get(i).get("SEQUENCENO")});
                            TempList.get(i).put("FURNACENO", NewFurnaceNo);
                            TempList.get(i).put("SEQUENCENO", new BigDecimal(OrderIndex));
                            PlnSteelforOrdEntitys.add(TempList.get(i));

                            TotalUsedWeight = TotalUsedWeight.add(OrderWeight);

                            if (!CcmSeq.containsKey(tempPline) || CcmSeq.get(tempPline) > tempCcmSeq + 1)
                                CcmSeq.put(tempPline, tempCcmSeq + 1);

                            TotalUsedWeight = TotalUsedWeight.add(OrderWeight);
                        } else {


                            BigDecimal Num = LastOrderWeight.divide(OrderMWeight, 0, BigDecimal.ROUND_CEILING);
                            listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder2");
                            listpara.add(new Object[]{++OrderIndex, tempCcmSeq, "", "", "", Num, Num.multiply(OrderMWeight), NewFurnaceNo, OldFurnaceNo, TempList.get(i).get("SEQUENCENO")});


                            listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                            listpara.add(new Object[]{new BigDecimal(0).subtract(Num), new BigDecimal(0).subtract(Num), OldFurnaceNo, TempList.get(i).get("SEQUENCENO")});

                            LastOrderSeq = ( (BigDecimal) TempList.get(i).get("SEQUENCENO") ).longValue();

                            TempList.get(i).put("JUDGE_STOVE_NO", "");
                            TempList.get(i).put("FURNACENO", NewFurnaceNo);
                            TempList.get(i).put("SEQUENCENO", new BigDecimal(OrderIndex));
                            PlnSteelforOrdEntitys.add(TempList.get(i));

                            LastCcmSeq = tempCcmSeq + 1;

                            if (!CcmSeq.containsKey(tempPline) || CcmSeq.get(tempPline) > tempCcmSeq + 2)
                                CcmSeq.put(tempPline, tempCcmSeq + 2);


                            TotalUsedWeight = TotalUsedWeight.add(Num.multiply(OrderMWeight));


                        }

                        LastOrderWeight = LastOrderWeight.subtract(OrderWeight);

                    }

                    //中断在浇次最后一炉  给分出去的炉计划加尾炉加量 加在第一炉内 / 否则在中断的那炉补上尾炉加量
                    if (StopInLastFurnace) {


                        listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder2");
                        listpara.add(new Object[]{99, 0, "", "", "", LastOrderNum, LastOrderWeight, NewFurnaceNo, TempListLastOrder.get(0).get("FURNACENO"), TempListLastOrder.get(0).get("SEQUENCENO")});
                        TotalUsedWeight = TotalUsedWeight.add(LastOrderWeight);


                        TempListLastOrder.get(0).put("FURNACENO", NewFurnaceNo);
                        TempListLastOrder.get(0).put("SEQUENCENO", new BigDecimal(99));
                        PlnSteelforOrdEntitys.add(TempListLastOrder.get(0));

                    } else {
                        listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder2");
                        listpara.add(new Object[]{99, LastCcmSeq, "", "", "", LastOrderNum, OrLastOrderWeight, OldFurnaceNo, TempListLastOrder.get(0).get("FURNACENO"), TempListLastOrder.get(0).get("SEQUENCENO")});

                        TempListLastOrder.get(0).put("FURNACENO", OldFurnaceNo);
                        TempListLastOrder.get(0).put("SEQUENCENO", new BigDecimal(99));
                        NeedChangeOrder = TempListLastOrder.get(0);
                    }


                    if (TotalUsedWeight.compareTo(WEIGHT) < 0) {
                        //前面补的是余材  则直接加在余材上
                        if (IsSurplus.equals("1")) {
                            BigDecimal Num = WEIGHT.subtract(TotalUsedWeight).divide(OrderMWeight, 0, BigDecimal.ROUND_CEILING);

                            listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                            listpara.add(new Object[]{Num, Num, OldFurnaceNo, LastOrderSeq});

                        } else {

                            BigDecimal Num = WEIGHT.subtract(TotalUsedWeight).divide(OrderMWeight, 0, BigDecimal.ROUND_CEILING);

                            listsqlID.add("FrmSteelManagement.InsertNoneOrderPlan");
                            listpara.add(new Object[]{NewFurnaceNo, ++OrderIndex, 0, "", "", "", Num, Num, "7", OldFurnaceNo, LastOrderSeq});

                            List<Map<String, Object>> TempList1 = ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(OldFurnaceNo, LastOrderSeq).getResult() ).getResult();

                            TempList1.get(0).put("JUDGE_STOVE_NO", "");
                            TempList1.get(0).put("FURNACENO", NewFurnaceNo);
                            TempList1.get(0).put("SEQUENCENO", new BigDecimal(OrderIndex));
                            TempList1.get(0).put("SURPLUS_TYPE", "7");
                            PlnSteelforOrdEntitys.add(StopInLastFurnace ? PlnSteelforOrdEntitys.size() - 1 : PlnSteelforOrdEntitys.size(), TempList1.get(0));
                        }

                    }


                    //endregion
                } else if (FurnaceEntity.containsKey("OrderData")) {
                    PlnSteelforOrdEntitys = (List<Map<String, Object>>) FurnaceEntity.get("OrderData");
                } else {

                    PlnSteelforOrdEntitys =
                            ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(tempFurnaceNo).getResult() ).getResult();
                }

                List<Map<String, Object>> NeedAdd = new ArrayList<Map<String, Object>>();
                List<Map<String, Object>> NeedRemove = new ArrayList<Map<String, Object>>();
                int SeqIndex = PlnSteelforOrdEntitys.size() - 1;

                if (PlnSteelforOrdEntitys != null && PlnSteelforOrdEntitys.size() > 0) {
                    for (Map<String, Object> PlnSteelforOrdEntity : PlnSteelforOrdEntitys) {
                        //String tempJudgeStove = PlnSteelforOrdEntity.get("FURNACENO").toString();
                        Long tempCcmSeq = ( (BigDecimal) PlnSteelforOrdEntity.get("CCM_SEQ") ).longValue();
                        String IsSurplus = String.valueOf(PlnSteelforOrdEntity.get("SURPLUSFLAG"));
                        String SurplusType = String.valueOf(PlnSteelforOrdEntity.get("SURPLUS_TYPE"));

                        String Status = String.valueOf(PlnSteelforOrdEntity.get("STATUS"));



                   /*     if ("1".equals(IsSurplus) && "2".equals(SurplusType)) {
                            if(Type==2 && tempCast.equals(NewCastNo))
                            {
                                cro.setV_errMsg("中断浇次剩余炉不能包含首炉！");
                                return cro;
                            }
                        }*/


                        if ("1".equals(IsSurplus) && "3".equals(SurplusType)) {

                            //补炉时  首尾炉在一起
                            if (TargetCastStoveSeq == 1 && Type == 4) {
                                NeedChangeOrder = PlnSteelforOrdEntity;
                            }
                        }

                        if (( "3".equals(SurplusType) ) && Type == 1 && FurnaceListEntity.contains(FurnaceEntity)) {
                            if(AdjustFurnacenoEnity==null){  //全部中断



                            }else {

                                List<Map<String, Object>> TempOrderEntity = (List<Map<String, Object>>) AdjustFurnacenoEnity.get("OrderData");


                                BigDecimal LastOrderWeight = (BigDecimal) PlnSteelforOrdEntity.get("TOT_WEIGHT_M");

                                List<Map<String, Object>> FirstNeedRemove = new ArrayList<Map<String, Object>>();
                                List<Map<String, Object>> FirstNeedAdd = new ArrayList<Map<String, Object>>();

/*                            for (int i = TempOrderEntity.size() - 1; i >= 0 && LastOrderWeight.compareTo(new BigDecimal(0)) > 0; i--) {
                                IsSurplus = String.valueOf(TempOrderEntity.get(i).get("SURPLUSFLAG"));
                                String TempSurplusType = String.valueOf(TempOrderEntity.get(i).get("SURPLUS_TYPE"));
                                BigDecimal OrderWeight = (BigDecimal) TempOrderEntity.get(i).get("TOT_WEIGHT_M");
                                BigDecimal OrderNum = (BigDecimal) TempOrderEntity.get(i).get("TOT_MUM_M");
                                BigDecimal OrderMWeight = (BigDecimal) TempOrderEntity.get(i).get("WEIGHT_M");
                                String TempPRO_PLAN_ID = String.valueOf(TempOrderEntity.get(i).get("PRO_PLAN_ID"));
                                String TempGX_PLAN_NO = String.valueOf(TempOrderEntity.get(i).get("GX_PLAN_NO"));
                                String MERGE_NO = TempOrderEntity.get(i).get("MERGE_NO") == null ? "" : TempOrderEntity.get(i).get("MERGE_NO").toString();


                                if ("3".equals(TempSurplusType)) {
                                    continue;
                                }




                                    //需要调整的订单量等于当前订单量
                                    if (LastOrderWeight.compareTo(OrderWeight) >= 0) {
                           *//*         listsqlID.add("FrmSteelManagement.ChangeOrderFurnaceNo");
                                    listpara.add(new Object[]{TempOrderEntity.get(i).get("FURNACENO"), "2".equals(SurplusType) ? 0 : 99, PlnSteelforOrdEntity.get("FURNACENO"), PlnSteelforOrdEntity.get("SEQUENCENO")});
*//*

                                        List<Map<String, Object>> TempPlnSteelforOrdEntitys =
                                                ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderByProID",
                                                        new Object[]{TempPRO_PLAN_ID, TempGX_PLAN_NO, tempFurnaceNo}).getResult() ).getResult();

                                        if (TempPRO_PLAN_ID == null || "".equals(TempPRO_PLAN_ID) || TempPlnSteelforOrdEntitys == null || TempPlnSteelforOrdEntitys.size() <= 0) {

                                            listsqlID.add("FrmSteelManagement.ChangeOrderFurnaceNo");
                                            listpara.add(new Object[]{tempFurnaceNo, ++SeqIndex, TempOrderEntity.get(i).get("FURNACENO"), TempOrderEntity.get(i).get("SEQUENCENO")});

                                            TempOrderEntity.get(i).put("FURNACENO", PlnSteelforOrdEntity.get("FURNACENO"));
                                            TempOrderEntity.get(i).put("SEQUENCENO", new BigDecimal(SeqIndex));
                                            NeedAdd.add(TempOrderEntity.get(i));

                                        } else {

                                            listsqlID.add("FrmSteelManagement.UpdateOrderQty2");
                                            listpara.add(new Object[]{OrderNum, OrderWeight, TempPlnSteelforOrdEntitys.get(0).get("FURNACENO"), TempPlnSteelforOrdEntitys.get(0).get("SEQUENCENO")});

                                            listsqlID.add("FrmSteelManagement.DeleteOrder");
                                            listpara.add(new Object[]{TempOrderEntity.get(i).get("FURNACENO"), TempOrderEntity.get(i).get("SEQUENCENO")});


                                        }

                                        FirstNeedRemove.add(TempOrderEntity.get(i));

                                    } else {

                                        BigDecimal Num = LastOrderWeight.divide(OrderMWeight, 0, BigDecimal.ROUND_CEILING);


                                        List<Map<String, Object>> TempPlnSteelforOrdEntitys =
                                                ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderByProID",
                                                        new Object[]{TempPRO_PLAN_ID, TempGX_PLAN_NO, tempFurnaceNo}).getResult() ).getResult();

                                        if (TempPRO_PLAN_ID == null || "".equals(TempPRO_PLAN_ID) || TempPlnSteelforOrdEntitys == null || TempPlnSteelforOrdEntitys.size() <= 0) {

                                            listsqlID.add("FrmSteelManagement.InsertOrderByCopyOrder");
                                            listpara.add(new Object[]{++SeqIndex, tempCcmSeq, "", "", "", Num, Num.multiply(OrderMWeight), tempFurnaceNo, TempOrderEntity.get(i).get("FURNACENO"), TempOrderEntity.get(i).get("SEQUENCENO")});


                                            listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                                            listpara.add(new Object[]{new BigDecimal(0).subtract(Num), new BigDecimal(0).subtract(Num), TempOrderEntity.get(i).get("FURNACENO"), TempOrderEntity.get(i).get("SEQUENCENO")});

                                            List<Map<String, Object>> TempList = ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno
                                                    (TempOrderEntity.get(i).get("FURNACENO").toString(), ( (BigDecimal) TempOrderEntity.get(i).get("SEQUENCENO") ).longValue()).getResult() ).getResult();


                                            TempList.get(0).put("JUDGE_STOVE_NO", "");
                                            TempList.get(0).put("FURNACENO", tempFurnaceNo);
                                            TempList.get(0).put("SEQUENCENO", new BigDecimal(SeqIndex));
                                            NeedAdd.add(TempList.get(0));
                                        } else {

                                            listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                                            listpara.add(new Object[]{Num, Num, TempPlnSteelforOrdEntitys.get(0).get("FURNACENO"), TempPlnSteelforOrdEntitys.get(0).get("SEQUENCENO")});

                                            listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                                            listpara.add(new Object[]{new BigDecimal(0).subtract(Num), new BigDecimal(0).subtract(Num),
                                                    TempOrderEntity.get(i).get("FURNACENO"), TempOrderEntity.get(i).get("SEQUENCENO")});
                                        }


                                    }

                                    LastOrderWeight = LastOrderWeight.subtract(OrderWeight);

                            }*/


                                listsqlID.add("FrmSteelManagement.ChangeOrderFurnaceNo");
                                listpara.add(new Object[]{AdjustFurnacenoEnity.get("FURNACENO"), "2".equals(SurplusType) ? 0 : 99, PlnSteelforOrdEntity.get("FURNACENO"), PlnSteelforOrdEntity.get("SEQUENCENO")});


                                PlnSteelforOrdEntity.put("FURNACENO", AdjustFurnacenoEnity.get("FURNACENO"));
                                PlnSteelforOrdEntity.put("SEQUENCENO", new BigDecimal("2".equals(SurplusType) ? 0 : 99));
                                FirstNeedAdd.add(PlnSteelforOrdEntity);
                                NeedRemove.add(PlnSteelforOrdEntity);

                                NeedChangeOrder = PlnSteelforOrdEntity;
                                TempOrderEntity.removeAll(FirstNeedRemove);

                                TempOrderEntity.addAll(FirstNeedAdd);
                            }

                        }

                        if (( Type == 5 ) && tempFurnaceNo.equals(OldFurnaceNo)) {
                            if (!CcmSeq.containsKey(tempPline)) {
                                tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectMaxCCMSeq1",
                                        new Object[]{TargetPline}).getResult() ).getResult();
                                CcmSeq.put(tempPline, ( (BigDecimal) tempList.get(0).get("CCM_SEQ") ).longValue() + CCMSeqAdjustCount);
                            }
                        } else if (Type == 2 && tempPline.equals(TargetPline)) {

                        } else {

                            if (!CcmSeq.containsKey(tempPline) || CcmSeq.get(tempPline) > tempCcmSeq)
                                CcmSeq.put(tempPline, tempCcmSeq);
                        }
                    }
                }


                PlnSteelforOrdEntitys.addAll(NeedAdd);
                PlnSteelforOrdEntitys.removeAll(NeedRemove);

                if (NeedChangeOrder != null) {
                    PlnSteelforOrdEntitys.remove(NeedChangeOrder);
                }
                FurnaceEntity.put("OrderData", PlnSteelforOrdEntitys);

                if (!CastStoveSeq.containsKey(tempCast) || CastStoveSeq.get(tempCast) > tempCastStoveSeq)
                    CastStoveSeq.put(tempCast, tempCastStoveSeq);


            }


            Collections.sort(StoveList);

            for (List<Long> value : CastSeqs.values()) {
                Collections.sort(value);
            }

            //endregion


            if (!LastFurnaces.contains(NewCastNo))
                LastFurnaces.add(NewCastNo);
            if (!FirstFurnaces.contains(NewCastNo))
                FirstFurnaces.add(NewCastNo);


            Long NewCastSeq = null;
            String NewJudgeStove = null;
            String LastProMoth= "";

            for (Map<String, Object> FurnaceEntity : NeedAdjustFurnaceList
                    ) {


                //region 从炉计划得到后续需要的数据
                String furnaceNo = FurnaceEntity.get("FURNACENO").toString();

                String EfLline = String.valueOf(FurnaceEntity.get("EF_PLINE_CODE"));
                String Line = FurnaceEntity.get("PLINE_CODE").toString();
                Long CastStoveNo = ( (BigDecimal) FurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
                Long CastSeq = ( (BigDecimal) FurnaceEntity.get("CAST_SEQ") ).longValue();
                String FurnaceWlidIn = FurnaceEntity.get("WLID_IN").toString();
                String FurnaceWlidOut = FurnaceEntity.get("MATERIAL_NO").toString();
                String FurnaceStatus = FurnaceEntity.get("STATUS").toString();
                String CASTINGTYPE = String.valueOf(FurnaceEntity.get("CASTINGTYPE"));
                String Cast = FurnaceEntity.get("CAST_NO").toString();
                String BofType = FurnaceEntity.get("BOF_TYPE").toString();
                String Status = FurnaceEntity.get("STATUS").toString();
                String Stove = FurnaceEntity.get("STOVE_NO") == null ? "" : FurnaceEntity.get("STOVE_NO").toString();
                String PRO_MONTH = String.valueOf(FurnaceEntity.get("PRO_MONTH"));

                if (Type == 2 && ListFurnace.contains(furnaceNo)) BofType = "50";
                if (Type == 1 && ListFurnace.contains(furnaceNo)) BofType = "50";
                if (Type == 4 && NewFurnaceNo.equals(furnaceNo)) BofType = "20";
                if (Type == 5 && OldFurnaceNo.equals(furnaceNo)) BofType = "10";
                if (Type == 7 && NewFurnaceNo.equals(furnaceNo)) BofType = "10";

                //endregion

                String status = FurnaceEntity.get("STATUS").toString();


         /*       if(!"00".equals(status) && !"10".equals(status) ){
                    cro.setV_errMsg("存在已生产的炉次,无法做调整顺序动作！");
                    return cro;

                }*/

                //首尾炉都在需要调整的顺序中  则按顺序增加CastSeq 否则保持原有的CastSeq
                Long CurrentCastSeq = CastSeq;
                if (CastCastSeq.containsKey(Cast)) {
                    CurrentCastSeq = CastCastSeq.get(Cast);
                } else {
                    if (FirstFurnaces.contains(Cast) && LastFurnaces.contains(Cast)) {
                        if (CastSeqs.containsKey(Line)) {
                            Integer tempIndex = CastSeqsIndex.get(Line);
                            List<Long> tempListCastSeq = CastSeqs.get(Line);
                            if (tempIndex < tempListCastSeq.size()) {
                                CurrentCastSeq = tempListCastSeq.get(tempIndex++);
                                CastSeqsIndex.put(Line, tempIndex);
                            } else {
                                //TODO  by 产线找最大的
                                CurrentCastSeq = ++NewCastSeq;

                            }
                        }

                    }
                    CastCastSeq.put(Cast, CurrentCastSeq);
                    NewCastSeq = CurrentCastSeq;
                }

                Long CurrentCastStoveSeq = CastStoveSeq.get(Cast);
                CastStoveSeq.put(Cast, CurrentCastStoveSeq + 1);


                Long CurrentFurnaceSeq = MinFurnaceSeq.get(TargetEfPline);
                MinFurnaceSeq.put(TargetEfPline, CurrentFurnaceSeq + 1);


                //初始化当前炉计划需要写入得熔炼炉号
                String StoveNo = "";


                //region 当前操作是下发状态  则按顺序获取熔炼炉号
                if (!"00".equals(FurnaceStatus)) {
                    if (StoveList != null && StoveIndex < StoveList.size()) {
                        //当存在的熔炼炉号List 还有可用的熔炼炉号  获取 并Index自加一
                        StoveNo = StoveList.get(StoveIndex++);
                    } else {
                        //当存在的熔炼炉号List 没有可用的熔炼炉号  新生成新的熔炼炉号   增加了新炉号 最后几炉走该逻辑
                        if (PlanJudgeStoveNo == null) {
                            StoveNo = StoveNoGenerate.StoveNoGenerateByPineCode(TargetEfPline, BaseHelper.dateAddSecond(PlanStartDate, 3 * 60 * 60));
                            PlanStoveNo = StoveNo;
                            StoveInfo.put(PlanStoveNo, FurnaceEntity);
                        } else {
                            StoveNo = PlanStoveNo;
                            StoveInfo.put(PlanStoveNo, FurnaceEntity);
                            PlanStoveNo = String.format("%07d", (  Integer.parseInt(StoveNo) + 1));
                        }
                    }
                }

                //endregion


                //当前的单倍尺
                Long CurrentLen = null;



                //当前的判断炉号
                String TempJudgeStove = "";
                //分切数
                Long Gp = 1L;


                if (FurnaceEntity.containsKey("OrderData")) {

                    List<Map<String, Object>> PlnSteelforOrdEntitys = (List<Map<String, Object>>) FurnaceEntity.get("OrderData");

                    if (furnaceNo.equals(NewFurnaceNo) && Type == 4 && NeedChangeOrder != null) {
                        PlnSteelforOrdEntitys.add(NeedChangeOrder);
                    }

                    if (furnaceNo.equals(OldFurnaceNo) && Type == 2 && NeedChangePline != null) {
                        PlnSteelforOrdEntitys.add(NeedChangeOrder);
                    }


                    if (PlnSteelforOrdEntitys != null && PlnSteelforOrdEntitys.size() > 0) {
                        for (int j = 0; j < PlnSteelforOrdEntitys.size(); j++) {

                            String ProMoth = sdf2.format(BaseHelper.dateAddSecond(PlanStartDate, 3 * 60 * 60));

                            String OrderStatus = String.valueOf(PlnSteelforOrdEntitys.get(j).get("STATUS"));


                            if (!StoveNo.equals(Stove) && !OrderStatus.equals("10") && !OrderStatus.equals("00")) {
                                cro.setV_errMsg("判定炉号：" + String.valueOf(PlnSteelforOrdEntitys.get(j).get("JUDGE_STOVE_NO")) + "已经生产,无法调整顺序！");
                                return cro;
                            }


                            String SURPLUS_TYPE = PlnSteelforOrdEntitys.get(j).get("SURPLUS_TYPE") == null ? "" : PlnSteelforOrdEntitys.get(j).get("SURPLUS_TYPE").toString();

                            if (Type == 5 && ( "2".equals(SURPLUS_TYPE) || "3".equals(SURPLUS_TYPE) )) continue;

                            Long Len = ( (BigDecimal) PlnSteelforOrdEntitys.get(j).get("LEN_GP_SINGLE") ).longValue();

                            //region 获取当前判定炉号  --只有不是首炉加量 或者尾炉加量  才有判定炉号
                            if (!"".equals(StoveNo) && !"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) {
                                if (CurrentLen == null) {
                                    TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(EfLline, StoveNo, 1l,ProMoth);
                                    CurrentLen = Len;
                                } else {
                                    //多分切判定炉号  固定
                                    TempJudgeStove = PlnSteelforOrdEntitys.get(j).get("JUDGE_STOVE_NO").toString();
                                    if ("".equals(TempJudgeStove)) {
                                        if (NewJudgeStove == null  || !LastProMoth.substring(0,4).equals(ProMoth.substring(0,4))) {
                                            NewJudgeStove = StoveNoGenerate.GetNewJudgeStove(EfLline, StoveNo, 2l,ProMoth);
                                        }

                                        TempJudgeStove = NewJudgeStove;
                                        NewJudgeStove =  String.format("%06d", ( Long.parseLong(NewJudgeStove) + 1 ) );

                                    }
                                    LastProMoth = ProMoth;

                                }

                            } else {
                                TempJudgeStove = "";
                            }
                            //endregion

                            listsqlID.add("FrmSteelManagement.UpdateOrderCastSeq");
                            listpara.add(new Object[]{
                                    Cast,
                                    furnaceNo,
                                    CurrentCastSeq,
                                    CurrentCastStoveSeq,
                                    CurrentFurnaceSeq,
                                    CcmSeq.get(Line),
                                    StoveNo,
                                    StoveNoGenerate.GePrintStove(EfLline, StoveNo, Gp,ProMoth),
                                    "".equals(StoveNo) ? "" : TempJudgeStove,
                                    "".equals(StoveNo) ? "" : ( TempJudgeStove + "-01" ),
                                    SURPLUS_TYPE,
                                    ProMoth,
                                    "2".equals(SURPLUS_TYPE) ? "0" : "3".equals(SURPLUS_TYPE) ? "99" : Gp,
                                    PlnSteelforOrdEntitys.get(j).get("FURNACENO"),
                                    PlnSteelforOrdEntitys.get(j).get("SEQUENCENO")});

                            if (!"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) Gp++;

                            CcmSeq.put(Line, CcmSeq.get(Line) + 1);


                        }
                    }
                }


                if (FurnaceUseTimes.containsKey(FurnaceWlidIn + "||" + FurnaceWlidOut)) {
                    FurnaceUseTime = FurnaceUseTimes.get(FurnaceWlidIn + "||" + FurnaceWlidOut);
                } else {
                    FurnaceUseTime = seqCapacityFurn(
                            TargetEfPline, FurnaceWlidIn, FurnaceWlidOut);
                    FurnaceUseTimes.put(FurnaceWlidIn + "||" + FurnaceWlidOut, FurnaceUseTime);
                }


                Date FurnaceEnd = BaseHelper.dateAddSecond(PlanStartDate, (int) Math.ceil(FurnaceUseTime) * 60);


                listsqlID.add("FrmSteelManagement.UpdateFurnaceCastSeq");
                listpara.add(new Object[]{Cast, CurrentCastSeq, CurrentCastStoveSeq, CurrentFurnaceSeq, sdf.format(PlanStartDate), sdf.format(FurnaceEnd), StoveNo,
                         sdf2.format(BaseHelper.dateAddSecond(PlanStartDate, 3 * 60 * 60)),
                        BofType,
                        StoveNo.equals("") ? "00" : StoveInfo.get(StoveNo).get("STATUS"),
                        StoveNo.equals("") ? "00" : StoveInfo.get(StoveNo).get("HEAT_STATE"),
                        StoveNo.equals("") || StoveInfo.get(StoveNo).get("STARTTIME") == null ? "" : sdf.format((Date) StoveInfo.get(StoveNo).get("STARTTIME")),
                        StoveNo.equals("") || StoveInfo.get(StoveNo).get("ENDTIME") == null ? "" : sdf.format((Date) StoveInfo.get(StoveNo).get("ENDTIME")),
                        furnaceNo});


                PlanStartDate = FurnaceEnd;


            }


            for (String Cast : CastCastSeq.keySet()
                    ) {
                listsqlID.add("FrmSteelManagement.UpdateCastQty3");
                listpara.add(new Object[]{Cast});

                listsqlID.add("FrmSteelManagement.UpdateCastQty4");
                listpara.add(new Object[]{Cast});

                listsqlID.add("FrmSteelManagement.UpdateFurnaceQty4");
                listpara.add(new Object[]{Cast});
            }

            if(!CastCastSeq.containsKey(OldCast)&& !"".equals(OldCast)){
                listsqlID.add("FrmSteelManagement.UpdateCastQty3");
                listpara.add(new Object[]{OldCast});

                listsqlID.add("FrmSteelManagement.UpdateCastQty4");
                listpara.add(new Object[]{OldCast});

                listsqlID.add("FrmSteelManagement.UpdateFurnaceQty4");
                listpara.add(new Object[]{OldCast});

                listsqlID.add("FrmSteelManagement.CloseCast");
                listpara.add(new Object[]{OldCast});
            }


            cro = setDataById(listsqlID, listpara);

            if (AddFurnaceno.size() > 0 || DeleteFurnaceno.size() > 0) {
                String[] addFurnacenos = AddFurnaceno.toArray(new String[AddFurnaceno.size()]);
                String[] deleteFurnacenos = DeleteFurnaceno.toArray(new String[DeleteFurnaceno.size()]);
                MyThread NewThread = new MyThread(addFurnacenos, deleteFurnacenos, Operator);
                NewThread.start();
            }

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }

        return cro;


    }


    public CoreReturnObject AdjustCcmSeq(
            ArrayList<String> jsonlist1,
            String jsonlist2
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {


            List<PlnSteelforOrdEntity> ordEntityList = SteelHelper
                    .jsonsToEntityList(PlnSteelforOrdEntity.class, jsonlist1);


            PlnSteelforOrdEntity TargertordEntityList = SteelHelper
                    .jsonToEntity(PlnSteelforOrdEntity.class, jsonlist2);


            Long MinCcmSeq = null;
            Long MaxCcmSeq = null;

            Long StartCcmSeq = null;

            StringBuffer str = new StringBuffer();
            str.append(" SELECT * FROM PLN_STEELFOR_ORD WHERE 1=1 ");

            str.append(" AND (furnaceno,SEQUENCENO) NOT IN (");


            Map<String, Long> MinCutNum = new HashMap<String, Long>();
            Map<String,String> NeedChangeJudgeStove =  new HashMap<String, String>();


            for (PlnSteelforOrdEntity Order : ordEntityList
                    ) {
                Long CurrentCcmSeq = Long.parseLong(Order.getCcmSeq());


                if (!Order.getCastNo().equals(TargertordEntityList.getCastNo())) {
                    cro.setV_errMsg("无法跨浇次调整铸造订单顺序！");
                    return cro;
                }

                if (MinCcmSeq == null || CurrentCcmSeq < MinCcmSeq) MinCcmSeq = CurrentCcmSeq;
                if (MaxCcmSeq == null || CurrentCcmSeq > MaxCcmSeq) MaxCcmSeq = CurrentCcmSeq;
                str.append("('");
                str.append(Order.getFurnaceno());
                str.append("',");
                str.append(Order.getSequenceno());
                str.append("),");
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");

            str.append(" AND PLINE_CODE='");
            str.append(TargertordEntityList.getPlineCode());
            str.append("' AND CCM_SEQ>=");
            str.append("{0}");
            str.append("  AND CCM_SEQ<=");
            str.append("{1}");
            str.append(" AND CAST_NO='");
            str.append(TargertordEntityList.getCastNo());
            str.append("'");

            Connection CurrentConnection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            List<PlnSteelforOrdEntity> tempList1 = new ArrayList<PlnSteelforOrdEntity>();
            List<PlnSteelforOrdEntity> tempList2 = new ArrayList<PlnSteelforOrdEntity>();
            StartCcmSeq = MinCcmSeq;
            try {

                if (MinCcmSeq < Long.parseLong(TargertordEntityList.getCcmSeq())) {
                    tempList1 = EntityTool.getEntitysBySqlNoClsCon(
                            MessageFormat.format(str.toString().replace("'", "''"), String.valueOf(MinCcmSeq), String.valueOf(Long.parseLong(TargertordEntityList.getCcmSeq()) - 1)),
                            CurrentConnection, PlnSteelforOrdEntity.class);
                } else {
                    StartCcmSeq = Long.parseLong(TargertordEntityList.getCcmSeq());
                }


                if (MaxCcmSeq > Long.parseLong(TargertordEntityList.getCcmSeq())) {
                    tempList2 = EntityTool.getEntitysBySqlNoClsCon(
                            MessageFormat.format(str.toString().replace("'", "''"), String.valueOf(Long.parseLong(TargertordEntityList.getCcmSeq())), String.valueOf(MaxCcmSeq)),
                            CurrentConnection, PlnSteelforOrdEntity.class);

                }


            } catch (Exception e) {
                cro.setV_errMsg(e.getMessage());
                return cro;
            } finally {

                if (CurrentConnection != null) {
                    try {
                        CurrentConnection.close();
                    } catch (Exception ex) {
                    }
                }

            }


            if (tempList1 == null) tempList1 = new ArrayList<PlnSteelforOrdEntity>();
            if (tempList2 == null) tempList2 = new ArrayList<PlnSteelforOrdEntity>();


            List<PlnSteelforOrdEntity> NeedAdjust = new ArrayList<PlnSteelforOrdEntity>();
            NeedAdjust.addAll(tempList1);
            NeedAdjust.addAll(ordEntityList);
            NeedAdjust.addAll(tempList2);


            StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();

            for (PlnSteelforOrdEntity Order : NeedAdjust
                    ) {
                Long CutNum = Long.parseLong(Order.getCutNum());
                String Furnaceno = Order.getFurnaceno();

                if (MinCutNum.containsKey(Furnaceno)) {
                    if (MinCutNum.get(Furnaceno) > CutNum && CutNum > 0) {
                        MinCutNum.put(Furnaceno, CutNum);
                    }
                } else {
                    MinCutNum.put(Furnaceno, CutNum);
                }
            }

            for (PlnSteelforOrdEntity Order : NeedAdjust
                    ) {

                if ("首炉加量".equals(Order.getSurplusType())) Order.setSurplusType("2");
                if ("尾炉加量".equals(Order.getSurplusType())) Order.setSurplusType("3");

                if(!Order.getStatus().equals("00") && !Order.getStatus().equals("10") &&  !Order.getStatus().equals("编制中") && !Order.getStatus().equals("已下发")){
                    cro.setV_errMsg("生成切割数据的订单计划无法调整顺序！");
                    return cro;
                }

                String CutNum = "2".equals(Order.getSurplusType()) ? "0" : "3".equals(Order.getSurplusType()) ? "99" : String.valueOf(MinCutNum.get(Order.getFurnaceno()));
                String PrintStove =StoveNoGenerate.GePrintStove(Order.getEfPlineCode(),Order.getStoveNo(),Long.parseLong(CutNum),Order.getProMonth());
                    String JudgeStoveNo = Order.getJudgeStoveNo();
                String FirstJudgeStoveNo = StoveNoGenerate.GetNewJudgeStove(Order.getEfPlineCode(),Order.getStoveNo(),1l,Order.getProMonth());

                if("1".equals(CutNum) && !"1".equals(Order.getCutNum()) && !JudgeStoveNo.equals(FirstJudgeStoveNo)){
                    JudgeStoveNo = FirstJudgeStoveNo;
                    NeedChangeJudgeStove.put(Order.getStoveNo(),Order.getJudgeStoveNo());
                }

                if(!"1".equals(CutNum) && "1".equals(Order.getCutNum()) && JudgeStoveNo.equals(FirstJudgeStoveNo) && NeedChangeJudgeStove.containsKey(Order.getStoveNo())){
                    JudgeStoveNo = NeedChangeJudgeStove.get(Order.getStoveNo());
                }

                listsqlID.add("FrmSteelManagement.UpdateCCMSeq");
                listpara.add(new Object[]{StartCcmSeq++, CutNum, PrintStove,JudgeStoveNo,"".equals(JudgeStoveNo)?"":(JudgeStoveNo+"-01"),Order.getFurnaceno(), Order.getSequenceno()});

                if (!"2".equals(Order.getSurplusType()) && !"3".equals(Order.getSurplusType()))
                    MinCutNum.put(Order.getFurnaceno(), MinCutNum.get(Order.getFurnaceno()) + 1);
            }

             cro = setDataById(listsqlID, listpara);

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }

        return cro;

    }


    /**
     * 更新产线
     *
     * @param FurnaceList 需要调整顺序的炉次号List
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject AdjustEFLine(
            ArrayList FurnaceList,
            String TargetFurnace
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {

            if (FurnaceList == null || FurnaceList.size() <= 0) {
                cro.setV_errMsg("炉次号未知！");
                return cro;
            }

            List<Map<String, Object>> TargetFurnaceEntityList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", TargetFurnace, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

            if (TargetFurnaceEntityList == null || TargetFurnaceEntityList.size() <= 0) {
                cro.setV_errMsg("无法找到目标炉次号 " + TargetFurnace + "数据！");
                return cro;
            }

            Map<String, Object> TargetFurnaceEntity = TargetFurnaceEntityList.get(0);

            String TargetEfPline = String.valueOf(TargetFurnaceEntity.get("EF_PLINE_CODE"));
            String TargetEfPlineName = String.valueOf(TargetFurnaceEntity.get("EF_PLINE_NAME"));
            String TargetCastNo = String.valueOf(TargetFurnaceEntity.get("CAST_NO"));
            String TargetCastSeq = String.valueOf(TargetFurnaceEntity.get("CAST_SEQ"));

            Long TargetCastStoveSeq = ( (BigDecimal) TargetFurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
            Long TargetFurnaceSeq = ( (BigDecimal) TargetFurnaceEntity.get("FURNACENO_SEQ") ).longValue();


            List<Map<String, Object>> TargetHandleFurnaceEntityList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(TargetEfPline, TargetFurnaceSeq - 1).getResult() ).getResult();


            for (Map<String, Object> TargetHandleFurnaceEntity : TargetHandleFurnaceEntityList
                    ) {

                String HandleFurnace = String.valueOf(TargetHandleFurnaceEntity.get("FURNACENO"));
                Long HandleFurnaceSeq = ( (BigDecimal) TargetHandleFurnaceEntity.get("FURNACENO_SEQ") ).longValue();
                Long HandleCastStoveSeq = ( (BigDecimal) TargetHandleFurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
                String HandleCastNo = String.valueOf(TargetHandleFurnaceEntity.get("CAST_NO"));

                listsqlID.add("FrmSteelManagement.UpdateFurnaceCastStoveSeq");
                listpara.add(new Object[]{HandleFurnaceSeq + FurnaceList.size(), HandleFurnace});


                listsqlID.add("FrmSteelManagement.UpdateOrderByFurnaceNo");
                listpara.add(new Object[]{HandleFurnace});

            }


            for (int i = 0; i < FurnaceList.size(); i++) {

                String Furnace = String.valueOf(FurnaceList.get(i));
                List<Map<String, Object>> FurnaceEntityList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", Furnace, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (FurnaceEntityList == null || FurnaceEntityList.size() <= 0) {
                    cro.setV_errMsg("无法找到炉次号 " + Furnace + "数据！");
                    return cro;
                }

                Map<String, Object> FurnaceEntity = FurnaceEntityList.get(0);
                String EfPline = String.valueOf(FurnaceEntity.get("EF_PLINE_CODE"));
                String CastNo = String.valueOf(FurnaceEntity.get("CAST_NO"));
                Long CastStoveSeq = ( (BigDecimal) FurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
                Long FurnaceSeq = ( (BigDecimal) FurnaceEntity.get("FURNACENO_SEQ") ).longValue();

                if (TargetEfPline.equals("C001") && !EfPline.equals("C001")) {
                    cro.setV_errMsg("炉次号 " + Furnace + "不是一练电炉,无法调整至一练电炉产线！");
                    return cro;
                }

                if (!TargetEfPline.equals("C001") && EfPline.equals("C001")) {
                    cro.setV_errMsg("炉次号 " + Furnace + "是一练电炉,无法调整至其他电炉产线！");
                    return cro;
                }

                if (TargetEfPline.equals(EfPline)) {
                    cro.setV_errMsg("炉次号 " + Furnace + "与目标电炉产线是相同,无需调整产线！");
                    return cro;
                }


                listsqlID.add("FrmSteelManagement.UpdateFurnaceCastAndCastStoveSeq");
                listpara.add(new Object[]{TargetFurnaceSeq + i, TargetEfPline, TargetEfPlineName, Furnace});

                listsqlID.add("FrmSteelManagement.UpdateOrderByFurnaceNo");
                listpara.add(new Object[]{Furnace});


                List<Map<String, Object>> HandleFurnaceEntityList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity(EfPline, FurnaceSeq).getResult() ).getResult();

                for (Map<String, Object> HandleFurnaceEntity : HandleFurnaceEntityList
                        ) {
                    String HandleFurnace = String.valueOf(HandleFurnaceEntity.get("FURNACENO"));
                    Long HandleFurnaceSeq = ( (BigDecimal) HandleFurnaceEntity.get("FURNACENO_SEQ") ).longValue();
                    Long HandleCastStoveSeq = ( (BigDecimal) HandleFurnaceEntity.get("CAST_STOVE_SEQ") ).longValue();
                    String HandleCastNo = String.valueOf(HandleFurnaceEntity.get("FURNACENO"));

                    listsqlID.add("FrmSteelManagement.UpdateFurnaceCastStoveSeq");
                    listpara.add(new Object[]{HandleFurnaceSeq - 1, HandleFurnace});


                    listsqlID.add("FrmSteelManagement.UpdateOrderByFurnaceNo");
                    listpara.add(new Object[]{HandleFurnace});
                }

            }

            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * 更新炉次表状态
     *
     * @param furnacenoList 虚拟炉次号List
     * @param Status        状态
     * @param Operator      更新人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject UpdatePlnSteelforfurnaceStatus(
            ArrayList furnacenoList,
            String Status,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {

            if (furnacenoList == null || furnacenoList.size() == 0) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            for (Object furnaceno : furnacenoList
                    ) {

                List<Map<String, Object>> PlnSteelforfurnace =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno.toString(), "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (PlnSteelforfurnace == null || PlnSteelforfurnace.size() <= 0) {
                    cro.setV_errMsg("找不到该炉次号：" + furnaceno.toString() + " 数据！");
                    return cro;
                }


                if ("10".equals(Status) && !"00".equals(PlnSteelforfurnace.get(0).get("STATUS"))) {
                    cro.setV_errMsg("炉次号：" + furnaceno.toString() + " 不是编制状态，无法下发！");
                    return cro;
                }

                //取消下发
                if ("00".equals(Status) && !"10".equals(PlnSteelforfurnace.get(0).get("STATUS"))) {
                    cro.setV_errMsg("炉次号：" + furnaceno.toString() + " 不是下发状态，无法取消下发！");
                    return cro;
                }


                if ("0".equals(PlnSteelforfurnace.get(0).get("IF_CONFIRM").toString())) {
                    cro.setV_errMsg("炉次号：" + furnaceno + " 的工艺文件未确认，无法下发！");
                    return cro;
                }


                String CastNo = PlnSteelforfurnace.get(0).get("CAST_NO").toString();


                List<Map<String, Object>> PlnSteelforcast =
                        ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(CastNo, new ArrayList(), new ArrayList()).getResult() ).getResult();

/*
                String StoveNoPre = "";
                if ("10".equals(Status))
                    StoveNoPre = new StoveNoGenerate().GetStovePrefix(PlnSteelforfurnace.get(0).get("EF_PLINE_CODE").toString());


                listsqlID.add("FrmSteelManagement.UpdatePlnSteelforfurnaceEntityStatusByFurnaceNo");

                listpara.add(new Object[]{Status, StoveNoPre, StoveNoPre, StoveNoPre, StoveNoPre, StoveNoPre, Operator, furnaceno});

                listsqlID.add("FrmSteelManagement.UpdatePlnPlnSteelforOrdEntityStatusByFurnaceNo");

                listpara.add(new Object[]{furnaceno});*/


                if (PlnSteelforcast != null && PlnSteelforcast.size() > 0) {
                    //取消下发
                    if ("00".equals(Status)) {
                        //更加浇次号查询炉次表 所有表 除当前炉次号外 是否有处于非编制中的数据，如果有则不更新浇次表状态，否则更新浇次表为编制中
                        listsqlID.add("FrmSteelManagement.UpdatePlnSteelforcastEntityByFurnace");
                        listpara.add(new Object[]{Operator, CastNo});
                    }

                    //下发  如果浇次信息是编制状态,炉次表任意一炉下发，则更改浇次信息表为下发
                    if ("10".equals(Status) && "00".equals(PlnSteelforcast.get(0).get("STATUS"))) {
                        listsqlID.add("FrmSteelManagement.UpdatePlnSteelforcastEntityStatus");
                        listpara.add(new Object[]{Status, Operator, CastNo});
                    }


                }

            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    public CoreReturnObject MakePlnSteelforfurnace(
            ArrayList furnacenoList,
            String Status,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {

            if (furnacenoList == null || furnacenoList.size() == 0) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            for (Object furnaceno : furnacenoList
                    ) {

                List<Map<String, Object>> PlnSteelforfurnace =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno.toString(), "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (PlnSteelforfurnace == null || PlnSteelforfurnace.size() <= 0) {
                    cro.setV_errMsg("找不到该炉次号：" + furnaceno.toString() + " 数据！");
                    return cro;
                }


                if ("10".equals(Status) && !"00".equals(PlnSteelforfurnace.get(0).get("STATUS"))) {
                    cro.setV_errMsg("炉次号：" + furnaceno.toString() + " 不是编制状态，无法下发！");
                    return cro;
                }

                //取消下发
                if ("00".equals(Status) && !"10".equals(PlnSteelforfurnace.get(0).get("STATUS"))) {
                    cro.setV_errMsg("炉次号：" + furnaceno.toString() + " 不是下发状态，无法取消下发！");
                    return cro;
                }


                if ("0".equals(PlnSteelforfurnace.get(0).get("IF_CONFIRM").toString())) {
                    cro.setV_errMsg("炉次号：" + furnaceno + " 的工艺文件未确认，无法下发！");
                    return cro;
                }


                String CastNo = PlnSteelforfurnace.get(0).get("CAST_NO").toString();


                List<Map<String, Object>> PlnSteelforcast =
                        ( (CoreReturnSortObject) SelectPlnSteelforcastEntity(CastNo, new ArrayList(), new ArrayList()).getResult() ).getResult();

/*
                String StoveNoPre = "";
                if ("10".equals(Status))
                    StoveNoPre = new StoveNoGenerate().GetStovePrefix(PlnSteelforfurnace.get(0).get("EF_PLINE_CODE").toString());


                listsqlID.add("FrmSteelManagement.UpdatePlnSteelforfurnaceEntityStatusByFurnaceNo");

                listpara.add(new Object[]{Status, StoveNoPre, StoveNoPre, StoveNoPre, StoveNoPre, StoveNoPre, Operator, furnaceno});

                listsqlID.add("FrmSteelManagement.UpdatePlnPlnSteelforOrdEntityStatusByFurnaceNo");

                listpara.add(new Object[]{furnaceno});*/


                if (PlnSteelforcast != null && PlnSteelforcast.size() > 0) {
                    //取消下发
                    if ("00".equals(Status)) {
                        //更加浇次号查询炉次表 所有表 除当前炉次号外 是否有处于非编制中的数据，如果有则不更新浇次表状态，否则更新浇次表为编制中
                        listsqlID.add("FrmSteelManagement.UpdatePlnSteelforcastEntityByFurnace");
                        listpara.add(new Object[]{Operator, CastNo});
                    }

                    //下发  如果浇次信息是编制状态,炉次表任意一炉下发，则更改浇次信息表为下发
                    if ("10".equals(Status) && "00".equals(PlnSteelforcast.get(0).get("STATUS"))) {
                        listsqlID.add("FrmSteelManagement.UpdatePlnSteelforcastEntityStatus");
                        listpara.add(new Object[]{Status, Operator, CastNo});
                    }


                }

            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * @param furnacenoList 虚拟炉次号List
     * @param Operator      关闭人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    public CoreReturnObject ClosePlnSteelforfurnace(
            ArrayList furnacenoList,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {

            if (furnacenoList == null || furnacenoList.size() == 0) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            for (Object furnaceno : furnacenoList
                    ) {

                List<Map<String, Object>> PlnSteelforfurnace =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno.toString(), "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (PlnSteelforfurnace == null || PlnSteelforfurnace.size() <= 0) {
                    cro.setV_errMsg("找不到该炉次号：" + furnaceno.toString() + " 数据！");
                    return cro;
                }


                if (!"30".equals(PlnSteelforfurnace.get(0).get("STATUS"))) {
                    cro.setV_errMsg("炉次号：" + furnaceno.toString() + " 不是已生产状态，无法关闭炉计划！");
                    return cro;
                }

                listsqlID.add("FrmSteelManagement.ClosePlnSteelforfurnaceEntity");

                listpara.add(new Object[]{Operator, furnaceno});
            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * 重炼
     *
     * @param furnaceno 需要重炼的炉次号
     * @param Operator  操作人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public synchronized CoreReturnObject Resmelt(
            String furnaceno,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {


            if ("".equals(furnaceno)) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            List<Map<String, Object>> TempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

            if (TempList == null || TempList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + furnaceno + " 数据！");
                return cro;
            }

            return AdjustFurnaceSeq(TempList, TempList.get(0), Operator, 5);

          /*  Map<String, Object> CurrentPlnSteelforfurnace = TempList.get(0);

            TempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(furnaceno).getResult() ).getResult();


            if(TempList==null || TempList.size()<=0) {
                cro.setV_errMsg("找不到该炉次铸造订单数据！");
                return cro;
            }

            for (Map<String, Object> Temp:TempList
                 ) {
                String SURPLUS_TYPE = Temp.get("SURPLUS_TYPE") == null ? "" : Temp.get("SURPLUS_TYPE").toString();
                String PRO_PLAN_ID = Temp.get("PRO_PLAN_ID") == null ? "" : Temp.get("PRO_PLAN_ID").toString();


                if ("2".equals(SURPLUS_TYPE) || "3".equals(SURPLUS_TYPE))  continue;

                if ("".equals(PRO_PLAN_ID)) {
                    cro.setV_errMsg("找不到该炉次合同数据,无需重炼！");
                    return cro;
                }

            }


            if (!"30".equals(CurrentPlnSteelforfurnace.get("STATUS")) && !"20".equals(CurrentPlnSteelforfurnace.get("STATUS"))) {
                cro.setV_errMsg("炉次号：" + furnaceno + " 不是生产状态，无法重炼炉计划！");
                return cro;
            }


            if (AddNewCastByFurnace(furnaceno, Operator, cro, listsqlID, listpara, CurrentPlnSteelforfurnace,"10"))
                return cro;

            //质量封锁
            listsqlID.add("FrmSteelManagement.AbolishPlnSteelforfurnaceEntity");
            listpara.add(new Object[]{furnaceno});

            //清空原订单的合同相关信息
            listsqlID.add("FrmSteelManagement.UpdateResmeltOrder");
            listpara.add(new Object[]{furnaceno});


                cro = setDataById(listsqlID, listpara);*/
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * 重炼
     *
     * @param furnaceno 需要重炼的炉次号
     * @param Operator  操作人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public synchronized CoreReturnObject Resmelt(
            String furnaceno,
            ArrayList<Long> Sequecenos,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        try {


            if ("".equals(furnaceno)) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            List<Map<String, Object>> TempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

            if (TempList == null || TempList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + furnaceno + " 数据！");
                return cro;
            }

            List<Map<String, Object>> TempList2 =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();


            List<Map<String, Object>> OrderList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> OrderList2 = new ArrayList<Map<String, Object>>();

            List<Map<String, Object>> AllOrderList = ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(furnaceno).getResult() ).getResult();


            for (int i = 0; i < AllOrderList.size(); i++) {
                Long TempSeq = ( (BigDecimal) AllOrderList.get(i).get("SEQUENCENO") ).longValue();
                if (Sequecenos.contains(TempSeq)) OrderList2.add(AllOrderList.get(i));
                else OrderList.add(AllOrderList.get(i));


            }

/*            List<Map<String, Object>> Order =
                    ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(furnaceno,Sequecenos.get(i)).getResult() ).getResult();
            OrderList.addAll(OrderList);*/

            TempList.get(0).put("OrderData", OrderList);
            TempList2.get(0).put("OrderData", OrderList2);

            // return AdjustFurnaceSeq(TempList, TempList2.get(0), Operator, 7);
            return AdjustFurnaceSeq(TempList2, TempList2.get(0), Operator, 7);

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;


    }


    /**
     * 补炉
     *
     * @param furnaceno 需要补炉的炉次号
     * @param Operator  操作人
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject SupplementStove(
            String furnaceno,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {

            if ("".equals(furnaceno)) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }

            List<Map<String, Object>> NeedSupplementStove = ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno.toString(), "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();
            if (NeedSupplementStove == null || NeedSupplementStove.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + furnaceno.toString() + " 数据！");
                return cro;
            }
            String Cast = NeedSupplementStove.get(0).get("CAST_NO").toString();

            List<Map<String, Object>> LastFurnace = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.FindLastFurnace", new Object[]{Cast, Cast}).getResult() ).getResult();

            if (LastFurnace == null || LastFurnace.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + furnaceno.toString() + " 数据！");
                return cro;
            }

            return AdjustFurnaceSeq(NeedSupplementStove, LastFurnace.get(0), Operator, 4);


        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }
    
    @SuppressWarnings("unchecked")
    public CoreReturnObject doSave( ArrayList<String> jsonlist)throws Exception {
    	 CoreReturnObject cro = new CoreReturnObject();
         cro.setV_errCode(-1);
         cro.setResult(false);
    	 try {
    		 List<PlnSteelforOrdEntity> ordEntityList = SteelHelper
                     .jsonsToEntityList(PlnSteelforOrdEntity.class, jsonlist);
    		 
    		 for (PlnSteelforOrdEntity PlnSteelforOrdEntity : ordEntityList
                     ) {
    			 if(PlnSteelforOrdEntity.getFurnaceno().equals("") || PlnSteelforOrdEntity.getFurnaceno() == null)return null;
    			    String sql = " update PLN_STEELFOR_ORD\n" + 
    			    		"set \n" + 
    			    		"Diameter = "+PlnSteelforOrdEntity.getDiameter()+",\n" + 
    			    		"Tot_Weight_M = "+PlnSteelforOrdEntity.getTotWeightM()+",\n" + 
    			    		"Weight_M = "+PlnSteelforOrdEntity.getWeightM()+",\n" + 
    			    		"Weight_Gp_One = "+PlnSteelforOrdEntity.getWeightGpOne()+",\n" + 
    			    		"Material_Name = '"+PlnSteelforOrdEntity.getMaterialName()+"'\n" + 
    			    		"where \n" + 
    			    		"furnaceno = '"+PlnSteelforOrdEntity.getFurnaceno()+"'"+
    			    		"and SEQUENCENO = '"+PlnSteelforOrdEntity.getSequenceno()+"'";
    			    this.setData(sql);
    		 }
    	 }
    	 catch (Exception e) {
             logger.error(e);
             cro.setV_errCode(-1);
             cro.setV_errMsg(e.getMessage());
         }
    	 cro.setV_errCode(1);
         cro.setResult(false);
         return cro;
    }
    

    /**
     * 保存编制中的补炉铸造订单计划
     *
     * @param jsonlist1 需要保存的订单计划
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject SaveSupplementOrder(
            ArrayList<String> jsonlist1
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {

            List<PlnSteelforOrdEntity> ordEntityList = SteelHelper
                    .jsonsToEntityList(PlnSteelforOrdEntity.class, jsonlist1);


            for (PlnSteelforOrdEntity PlnSteelforOrdEntity : ordEntityList
                    ) {

                if(PlnSteelforOrdEntity.getTotMumM().equals("0")){
                    listsqlID.add("FrmSteelManagement.DeleteOrder");
                    listpara.add(new Object[]{
                            PlnSteelforOrdEntity.getFurnaceno(),
                            PlnSteelforOrdEntity.getSequenceno()
                    });


                    listsqlID.add("FrmSteelManagement.UpdateCutNum");
                    listpara.add(new Object[]{
                            PlnSteelforOrdEntity.getFurnaceno(),
                            PlnSteelforOrdEntity.getCutNum()
                    });

                    if(PlnSteelforOrdEntity.getCutNum().endsWith("1")){


                        listsqlID.add("FrmSteelManagement.UpdateFirstCutNumStove");
                        listpara.add(new Object[]{
                                PlnSteelforOrdEntity.getFlagStoveNo(),
                                PlnSteelforOrdEntity.getJudgeStoveNo(),
                                PlnSteelforOrdEntity.getJudgeStoveNoSeq(),
                                PlnSteelforOrdEntity.getFurnaceno()
                        });
                    }

                }else {

                    listsqlID.add("FrmSteelManagement.SaveSupplementOrder");
                    listpara.add(new Object[]{
                            PlnSteelforOrdEntity.getLenGpSingle(),
                            PlnSteelforOrdEntity.getCutNumGp(),
                            PlnSteelforOrdEntity.getLengthM(),
                            PlnSteelforOrdEntity.getWeightGpOne(),
                            PlnSteelforOrdEntity.getWeightM(),
                            PlnSteelforOrdEntity.getTotMumM(),
                            PlnSteelforOrdEntity.getTotWeightM(),
                            PlnSteelforOrdEntity.getGowhereCode(),
                            PlnSteelforOrdEntity.getGowhereCode(),
                            PlnSteelforOrdEntity.getGowhereCode(),
                            PlnSteelforOrdEntity.getFurnaceno(),
                            PlnSteelforOrdEntity.getSequenceno()
                    });
                }

                listsqlID.add("FrmSteelManagement.UpdateFurnaceNoQty");
                listpara.add(new Object[]{PlnSteelforOrdEntity.getFurnaceno()});

                listsqlID.add("FrmSteelManagement.UpdateCastQty");
                listpara.add(new Object[]{PlnSteelforOrdEntity.getCastNo()});


                //暂时只在编制中修改出钢量
                listsqlID.add("FrmSteelManagement.UpdateCastQty4");
                listpara.add(new Object[]{PlnSteelforOrdEntity.getCastNo()});

                listsqlID.add("FrmSteelManagement.UpdateFurnaceQty4");
                listpara.add(new Object[]{PlnSteelforOrdEntity.getCastNo()});

            }


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    /**
     * 补炼
     *
     * @param furnaceno 补炼新加的炉次计划下的铸造订单
     * @param Operator  操作员
     * @return 执行结果
     * @throws Exception 执行异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject SteelSupplement(
            String furnaceno,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();

        try {


            List<Map<String, Object>> OrderList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.FindLastOrder", new Object[]{furnaceno, furnaceno}).getResult() ).getResult();

            if (OrderList == null || OrderList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + furnaceno + " 订单数据！");
                return cro;
            }

            Map<String, Object> CurrentOrder = OrderList.get(0);
            Long CcmSeq = ( (BigDecimal) CurrentOrder.get("CCM_SEQ") ).longValue();
            String PlineCode = CurrentOrder.get("PLINE_CODE").toString();
            String EFPlineCode = CurrentOrder.get("EF_PLINE_CODE").toString();
            String Cast = CurrentOrder.get("CAST_NO").toString();
            String isSurplus = CurrentOrder.get("SURPLUSFLAG").toString();
            Long Sequenceno = ( (BigDecimal) CurrentOrder.get("SEQUENCENO") ).longValue();
            Long CutNum = CurrentOrder.get("CUT_NUM") == null ? 0l : ( (BigDecimal) CurrentOrder.get("CUT_NUM") ).longValue();
            String StoveNo =  CurrentOrder.get("STOVE_NO") == null ? "" : String.valueOf(CurrentOrder.get("STOVE_NO"));
            String ProMoth = String.valueOf(CurrentOrder.get("PRO_MONTH"));

/*            if (isSurplus.equals("1")) {
                cro.setV_errMsg("该炉次号：" + furnaceno + "存在余材,不需补余材订单！");
                return cro;
            }*/

            String JudgeStove = "";

            if(!"".equals(StoveNo)){
                JudgeStove= new StoveNoGenerate().GetNewJudgeStove(EFPlineCode, StoveNo, CutNum + 1,ProMoth);
            }

            //根据铸造产线 大于MinCCMSeq的铸造顺序号自加一
            listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
            listpara.add(new Object[]{1, CcmSeq + 1, PlineCode});

            listsqlID.add("FrmSteelManagement.InsertOrderByCopyFurnace2");
            listpara.add(new Object[]{furnaceno , CutNum + 1,

                    JudgeStove,
                    "".equals(StoveNo) ? "" : ( JudgeStove + "-01" ),
                    new StoveNoGenerate().GePrintStove(EFPlineCode,StoveNo,CutNum+1,ProMoth),
                    Operator, GetBackUpOrder(EFPlineCode), furnaceno, Sequenceno});

            listsqlID.add("FrmSteelManagement.UpdateFurnaceNoQty");
            listpara.add(new Object[]{furnaceno});

            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{Cast});

            listsqlID.add("FrmSteelManagement.UpdateCastQty4");
            listpara.add(new Object[]{Cast});

            listsqlID.add("FrmSteelManagement.UpdateFurnaceQty4");
            listpara.add(new Object[]{Cast});


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;
    }


    /**
     * 转交逻辑
     *
     * @param furnaceno         需要转交的炉次号
     * @param HandOverFurnaceno 被转交的炉次号
     * @param Operator          操作人
     * @return 操作结果
     * @throws Exception 异常
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject HandOver(
            String furnaceno,
            String HandOverFurnaceno,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();

        try {
            List<Map<String, Object>> furnacenoEntityList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", furnaceno, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

            List<Map<String, Object>> HandOverFurnacenoEntityList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", HandOverFurnaceno, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();


            if (furnacenoEntityList == null || furnacenoEntityList.size() <= 0) {
                cro.setV_errMsg("无法找到炉次号：" + furnaceno + " 的数据！");
                return cro;
            }
            if (HandOverFurnacenoEntityList == null || HandOverFurnacenoEntityList.size() <= 0) {
                cro.setV_errMsg("无法找到炉次号：" + HandOverFurnaceno + " 的数据！");
                return cro;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String Cast = String.valueOf(furnacenoEntityList.get(0).get("CAST_NO"));
            String HandOverCast = String.valueOf(HandOverFurnacenoEntityList.get(0).get("CAST_NO"));


            Long CastSeq = ( (BigDecimal) furnacenoEntityList.get(0).get("CAST_SEQ") ).longValue();
            Long HandOverCastSeq = ( (BigDecimal) HandOverFurnacenoEntityList.get(0).get("CAST_SEQ") ).longValue();

            Long FurnaceSeq = ( (BigDecimal) furnacenoEntityList.get(0).get("FURNACENO_SEQ") ).longValue();
            Long HandOverFurnaceSeq = ( (BigDecimal) HandOverFurnacenoEntityList.get(0).get("FURNACENO_SEQ") ).longValue();

            Long CastStoveSeq = ( (BigDecimal) furnacenoEntityList.get(0).get("CAST_STOVE_SEQ") ).longValue();
            Long HandOverCastStoveSeq = ( (BigDecimal) HandOverFurnacenoEntityList.get(0).get("CAST_STOVE_SEQ") ).longValue();

            String Status = String.valueOf(furnacenoEntityList.get(0).get("STATUS"));
            String HandOverStatus = String.valueOf(HandOverFurnacenoEntityList.get(0).get("STATUS"));

            String Stove = String.valueOf(furnacenoEntityList.get(0).get("STOVE_NO"));
            String HandOverStove = String.valueOf(HandOverFurnacenoEntityList.get(0).get("STOVE_NO"));
            Stove = Stove.equals("null") ? "" : Stove;


            String Gradecode = String.valueOf(furnacenoEntityList.get(0).get("GRADECODE"));
            String HandOverGradecode = String.valueOf(HandOverFurnacenoEntityList.get(0).get("GRADECODE"));

            String PlineCode = String.valueOf(furnacenoEntityList.get(0).get("PLINE_CODE"));
            String HandOverPlineCode = String.valueOf(HandOverFurnacenoEntityList.get(0).get("PLINE_CODE"));


            String EfPlineCode = String.valueOf(furnacenoEntityList.get(0).get("EF_PLINE_CODE"));
            String HandOverEfPlineCode = String.valueOf(HandOverFurnacenoEntityList.get(0).get("EF_PLINE_CODE"));


            String EfPlineName = String.valueOf(furnacenoEntityList.get(0).get("EF_PLINE_NAME"));
            String HandOverEfPlineName = String.valueOf(HandOverFurnacenoEntityList.get(0).get("EF_PLINE_NAME"));


            Date PlanStart = (Date) furnacenoEntityList.get(0).get("PLN_STARTTIME");
            Date HandOverPlanStart = (Date) HandOverFurnacenoEntityList.get(0).get("PLN_STARTTIME");

            Date PlanEnd = (Date) furnacenoEntityList.get(0).get("PLN_ENDTIME");
            Date HandOverPlanEnd = (Date) HandOverFurnacenoEntityList.get(0).get("PLN_ENDTIME");

            String HeatState = String.valueOf(furnacenoEntityList.get(0).get("HEAT_STATE"));
            String HandOverHeatState = String.valueOf(HandOverFurnacenoEntityList.get(0).get("HEAT_STATE"));

            String Route = String.valueOf(furnacenoEntityList.get(0).get("FACT_ROUTE"));
            String HandOverRoute = String.valueOf(HandOverFurnacenoEntityList.get(0).get("FACT_ROUTE"));

            Date Start = (Date) furnacenoEntityList.get(0).get("STARTTIME");
            Date HandOverStart = (Date) HandOverFurnacenoEntityList.get(0).get("STARTTIME");



            Date End = (Date) furnacenoEntityList.get(0).get("ENDTIME");
            Date HandOverEnd = (Date) HandOverFurnacenoEntityList.get(0).get("ENDTIME");

            String ProMonth = String.valueOf(furnacenoEntityList.get(0).get("PRO_MONTH"));
            String HandOverProMonth = String.valueOf(HandOverFurnacenoEntityList.get(0).get("PRO_MONTH"));

            //region 判断
   /*         if (!"20".equals(Status) &&  !"30".equals(Status) ) {
                cro.setV_errMsg("需转交炉次号：" + furnaceno + " 现在不处于生产中或者已生产,！");
                return cro;
            }

            if ( !"10".equals(HandOverStatus) ) {
                cro.setV_errMsg("无法转交给炉次号：" + HandOverFurnaceno + " ,该炉次不处于下发状态中！");
                return cro;
            }*/

            if (!HandOverGradecode.equals(Gradecode)) {
                cro.setV_errMsg("转交的炉次的钢种不一致无法转交");
                return cro;
            }
            //endregion

            //region 注释原代码
/*
            listsqlID.add("FrmSteelManagement.SteelSupplement");
            listpara.add(new Object[]{ Status,"00" , "",SupplementStove,Operator,SupplementFurnaceno});


            listsqlID.add("FrmSteelManagement.SteelSupplementOrder");
            listpara.add(new Object[]{"","","","",SupplementFurnaceno});

            listsqlID.add("FrmSteelManagement.SteelSupplement");
            listpara.add(new Object[]{ SupplementStatus,"30" ,SupplementStove,Stove,Operator,Furnaceno});
            listsqlID.add("FrmSteelManagement.SteelSupplementOrder");
            listpara.add(new Object[]{ SupplementStatus,JudgeStoveNo,JudgeStoveNoSeq,FlagStoveNo,Furnaceno});*/
            //endregion


            listsqlID.add("FrmSteelManagement.HandOverFurnace");
            listpara.add(new Object[]{Cast, CastSeq, FurnaceSeq, CastStoveSeq, EfPlineCode, EfPlineName, Status, "30", Stove, HandOverStove,
                    PlanStart == null ? "" : sdf.format(PlanStart), PlanEnd == null ? "" : sdf.format(PlanEnd), HeatState, Route,
                    Start == null ? "" : sdf.format(Start), End == null ? "" : sdf.format(End), ProMonth,
                    HandOverFurnaceno});

            listsqlID.add("FrmSteelManagement.HandOverFurnace");
            listpara.add(new Object[]{HandOverCast, HandOverCastSeq, HandOverFurnaceSeq, HandOverCastStoveSeq, HandOverEfPlineCode, HandOverEfPlineName
                    , HandOverStatus, "30", HandOverStove, Stove,
                    sdf.format(HandOverPlanStart), sdf.format(HandOverPlanEnd), HandOverHeatState, HandOverRoute,
                    HandOverStart == null ? "" : sdf.format(HandOverStart), HandOverEnd == null ? "" : sdf.format(HandOverEnd), HandOverProMonth,
                    furnaceno});


            List<Map<String, Object>> OrderList =
                    ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(furnaceno).getResult() ).getResult();


            if (OrderList == null || OrderList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次铸造订单数据！");
                return cro;
            }

            List<Map<String, Object>> HandOverOrderList =
                    ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(HandOverFurnaceno).getResult() ).getResult();


            if (HandOverOrderList == null || HandOverOrderList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次铸造订单数据！");
                return cro;
            }


            List<Map<String, Object>> NeedRemove = new ArrayList<Map<String, Object>>();
            boolean doChangeFirst = false;

            for (Map<String, Object> Temp : OrderList
                    ) {

                String SURPLUS_TYPE = Temp.get("SURPLUS_TYPE") == null ? "" : Temp.get("SURPLUS_TYPE").toString();
                if ("2".equals(SURPLUS_TYPE) || "3".equals(SURPLUS_TYPE)) {
                    NeedRemove.add(Temp);
                    doChangeFirst = true;

                    listsqlID.add("FrmSteelManagement.UpdateLastOrFirstFurnace");
                    listpara.add(new Object[]{
                            "9999",
                            HandOverFurnaceno
                    });

                    listsqlID.add("FrmSteelManagement.UpdateLastOrFirstFurnace");
                    listpara.add(new Object[]{
                            HandOverFurnaceno,
                            furnaceno
                    });
                }

            }

            OrderList.removeAll(NeedRemove);
            NeedRemove = new ArrayList<Map<String, Object>>();


            for (Map<String, Object> Temp : HandOverOrderList
                    ) {

                String SURPLUS_TYPE = Temp.get("SURPLUS_TYPE") == null ? "" : Temp.get("SURPLUS_TYPE").toString();
                if ("2".equals(SURPLUS_TYPE) || "3".equals(SURPLUS_TYPE)) {
                    NeedRemove.add(Temp);

                    if (doChangeFirst) {

                        listsqlID.add("FrmSteelManagement.UpdateLastOrFirstFurnace");
                        listpara.add(new Object[]{
                                furnaceno,
                                "9999"
                        });
                    } else {

                        listsqlID.add("FrmSteelManagement.UpdateLastOrFirstFurnace");
                        listpara.add(new Object[]{
                                furnaceno, HandOverFurnaceno

                        });
                    }
                }
            }
            HandOverOrderList.removeAll(NeedRemove);

            Long MinCCMSeq = ( (BigDecimal) OrderList.get(0).get("CCM_SEQ") ).longValue();
            Long HandMinCCMSeq = ( (BigDecimal) HandOverOrderList.get(0).get("CCM_SEQ") ).longValue();


            Boolean HandOverIsDown = HandMinCCMSeq > MinCCMSeq;

            int Count = HandOverIsDown ? ( HandOverOrderList.size() - OrderList.size() ) : ( OrderList.size() - HandOverOrderList.size() );

            if (Count != 0) {
                listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine2");
                listpara.add(new Object[]{
                        Count,
                        HandOverIsDown ? MinCCMSeq : HandMinCCMSeq,
                        HandOverIsDown ? HandMinCCMSeq : MinCCMSeq,
                        PlineCode
                });
            }


            if (HandOverIsDown) HandMinCCMSeq += Count;
            else MinCCMSeq += Count;


            //当前的单倍尺
            Long CurrentLen = null;

            //当前的判断炉号
            String TempJudgeStove = "";

            Integer Gp = 1;

            for (int j = 0; j < OrderList.size(); j++) {


                String SURPLUS_TYPE = OrderList.get(j).get("SURPLUS_TYPE") == null ? "" : OrderList.get(j).get("SURPLUS_TYPE").toString();

                Long Len = ( (BigDecimal) OrderList.get(j).get("LEN_GP_SINGLE") ).longValue();

                //region 获取当前判定炉号  --只有不是首炉加量 或者尾炉加量  才有判定炉号
                if (!"".equals(HandOverStove) && !"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) {
                    if (CurrentLen == null) {
                        TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(HandOverEfPlineCode, HandOverStove, 1l,HandOverProMonth);
                        CurrentLen = Len;
                    } else {
                        //  if (!CurrentLen.equals(Len)) {
                        //多分切判定炉号  固定
                        TempJudgeStove = OrderList.get(j).get("JUDGE_STOVE_NO").toString();
                        Gp++;
                    }

                }
                //endregion

                listsqlID.add("FrmSteelManagement.UpdateOrderByFurnaceNo2");
                listpara.add(new Object[]{
                        HandMinCCMSeq++,
                        "".equals(HandOverStove) ? "" : TempJudgeStove,
                        "".equals(HandOverStove) ? "" : ( TempJudgeStove + "-01" ),
                        StoveNoGenerate.GePrintStove(HandOverEfPlineCode, HandOverStove, Long.valueOf(Gp),HandOverProMonth),
                        OrderList.get(j).get("FURNACENO"),
                        OrderList.get(j).get("SEQUENCENO")
                });


            }

            //当前的单倍尺
            CurrentLen = null;

            //当前的判断炉号
            TempJudgeStove = "";

            Gp = 1;

            for (int j = 0; j < HandOverOrderList.size(); j++) {


                String SURPLUS_TYPE = HandOverOrderList.get(j).get("SURPLUS_TYPE") == null ? "" : HandOverOrderList.get(j).get("SURPLUS_TYPE").toString();


                Long Len = ( (BigDecimal) HandOverOrderList.get(j).get("LEN_GP_SINGLE") ).longValue();

                //region 获取当前判定炉号  --只有不是首炉加量 或者尾炉加量  才有判定炉号
                if (!"".equals(Stove) && !"2".equals(SURPLUS_TYPE) && !"3".equals(SURPLUS_TYPE)) {
                    if (CurrentLen == null) {
                        TempJudgeStove = StoveNoGenerate.GetNewJudgeStove(EfPlineCode, Stove, 1l,ProMonth);
                        CurrentLen = Len;
                    } else {
                        //if (!CurrentLen.equals(Len)) {
                        //多分切判定炉号  固定
                        TempJudgeStove = HandOverOrderList.get(j).get("JUDGE_STOVE_NO").toString();
                        Gp++;
                    }

                }
                //endregion

                listsqlID.add("FrmSteelManagement.UpdateOrderByFurnaceNo2");
                listpara.add(new Object[]{
                        MinCCMSeq++,
                        "".equals(Stove) ? "" : TempJudgeStove,
                        "".equals(Stove) ? "" : ( TempJudgeStove + "-01" ),
                        StoveNoGenerate.GePrintStove(EfPlineCode, Stove, Long.parseLong(Gp +""),ProMonth),
                        HandOverOrderList.get(j).get("FURNACENO"),
                        HandOverOrderList.get(j).get("SEQUENCENO")
                });


            }

            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{Cast});

            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{HandOverCast});

            cro = setDataById(listsqlID, listpara);


        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    @SuppressWarnings("unchecked")
    public CoreReturnObject ChangeOrderPlan(
            String Type,
            String Para1,
            String Para2,
            Integer Num,
            String FurnaceNo,
            String Operator,
            Boolean NewJudgeStove
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();

        try {

            ArrayList<String> Furnaceno = new ArrayList<String>();
            Furnaceno.add(FurnaceNo);

            if ("".equals(FurnaceNo)) {
                cro.setV_errMsg("虚拟炉次号未知！");
                return cro;
            }


            List<Map<String, Object>> TempList =
                    ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", FurnaceNo, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

            if (TempList == null || TempList.size() <= 0) {
                cro.setV_errMsg("找不到该炉次号：" + FurnaceNo + " 数据！");
                return cro;
            }


            String PlineCode = TempList.get(0).get("PLINE_CODE").toString();
            String EfPlineCode = TempList.get(0).get("EF_PLINE_CODE").toString();
            String TargetCast = TempList.get(0).get("CAST_NO").toString();
            Long CastStovSeq = ( (BigDecimal) TempList.get(0).get("CAST_STOVE_SEQ") ).longValue();
            String Stove = String.valueOf(TempList.get(0).get("STOVE_NO"));
            String ProMoth = String.valueOf(TempList.get(0).get("PRO_MONTH"));

            TempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectLastOrder",
                    new Object[]{FurnaceNo}).getResult() ).getResult();


            Long CCMSeq = 0l;
            String JudgeStoveNo = "";
            String PrintStoveNo = "";
            int Gp = 1;

            if (TempList == null || TempList.size() <= 0) {
                List<Map<String, Object>> MinCCMSeqList;
                if (CastStovSeq > 1) {
                    MinCCMSeqList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectPlnSteelforOrdEntityByCastAndCastStoveSeq",
                            new Object[]{CCMSeq + 1, TargetCast}).getResult() ).getResult();
                    CCMSeq = ( (BigDecimal) MinCCMSeqList.get(0).get("CCM_SEQ") ).longValue();
                }
                if (CCMSeq == 1 || CCMSeq == 0) {
                    MinCCMSeqList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectPlnSteelforOrdEntityByCast",
                            new Object[]{TargetCast, TargetCast}).getResult() ).getResult();
                    CCMSeq = ( (BigDecimal) MinCCMSeqList.get(0).get("CCM_SEQ") ).longValue();
                }
                CCMSeq = CCMSeq + 1;

                JudgeStoveNo = StoveNoGenerate.GetNewJudgeStove(EfPlineCode, Stove, 1L,ProMoth);
                PrintStoveNo = StoveNoGenerate.GePrintStove(EfPlineCode, Stove, Long.valueOf(Gp),ProMoth);
                Gp++;
                if ("".equals(JudgeStoveNo)) {
                    cro.setV_errMsg("找不到该炉次号的判定炉号！");
                    return cro;
                }
            } else {
                CCMSeq = ( (BigDecimal) TempList.get(0).get("CCM_SEQ") ).longValue() + 1;
                Gp = ( (BigDecimal) TempList.get(0).get("SEQUENCENO") ).intValue() + 1;
                //if (TempList.get(0).get("SURPLUS_TYPE") != null && "2".equals(TempList.get(0).get("SURPLUS_TYPE").toString())) {
                JudgeStoveNo = StoveNoGenerate.GetNewJudgeStove(EfPlineCode, Stove, 2L,ProMoth);

                PrintStoveNo = StoveNoGenerate.GePrintStove(EfPlineCode, Stove, Long.valueOf(Gp),ProMoth);
                 Gp++ ;
                //  }
            }


            if ("0".equals(Type)) {

                Furnaceno.add(Para1);

                //region  铸造计划的数据 插入炉计划
                List<Map<String, Object>> PlnSteelforOrdEntitys =
                        ( (CoreReturnSortObject) SelectPlnSteelforOrdEntity("", null, null, null, null, Para1, Long.parseLong(Para2), "", null, null, null, null, null).getResult() ).getResult();

                if (PlnSteelforOrdEntitys == null || PlnSteelforOrdEntitys.size() <= 0) {
                    cro.setV_errMsg("找不到需要转分切的订单计划的数据,数据已更新！");
                    return cro;
                }


                List<Map<String, Object>> PlnSteelforfurnaceEntity =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", Para1, "", new ArrayList(), new ArrayList(), new ArrayList()).getResult() ).getResult();

                if (PlnSteelforfurnaceEntity == null || PlnSteelforfurnaceEntity.size() <= 0) {
                    cro.setV_errMsg("找不到该炉次号：" + FurnaceNo + " 数据！");
                    return cro;
                }

                if (!"10".equals(PlnSteelforfurnaceEntity.get(0).get("Status"))) {
                    cro.setV_errMsg("改订单的炉次已经处于生产中,无法转分切！");
                    return cro;
                }


                Map<String, Object> OrderPlan = PlnSteelforOrdEntitys.get(0);
                // Long Gp =   ((BigDecimal)OrderPlan.get("CUT_NUM_GP")).longValue();
                Long HandOverCCMSeq = ( (BigDecimal) OrderPlan.get("CCM_SEQ") ).longValue();
                String HandOverPline = OrderPlan.get("PLINE_CODE").toString();
                String HandOverCast = OrderPlan.get("CAST_NO").toString();
                Long HandOverSEQUENCENO = ( (BigDecimal) OrderPlan.get("SEQUENCENO") ).longValue();


                Long Len = ( (BigDecimal) OrderPlan.get("LEN_GP_SINGLE") ).longValue();


                Integer totNum = ( (BigDecimal) OrderPlan.get("TOT_MUM_M") ).intValue();

                String PRO_PLAN_ID = String.valueOf(OrderPlan.get("PRO_PLAN_ID"));
                String GX_PLAN_NO = String.valueOf(OrderPlan.get("GX_PLAN_NO"));

                if (totNum < Num) {
                    cro.setV_errMsg("转分切的数目大于原订单计划的数量！");
                    return cro;
                } else if (totNum.equals(Num)) {

                    PlnSteelforOrdEntitys =
                            ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderByProID",
                                    new Object[]{PRO_PLAN_ID, GX_PLAN_NO, FurnaceNo}).getResult() ).getResult();

                    if (NewJudgeStove || PRO_PLAN_ID == null || "".equals(PRO_PLAN_ID) || PlnSteelforOrdEntitys == null || PlnSteelforOrdEntitys.size() <= 0) {

                        //根据铸造产线 大于MinCCMSeq的铸造顺序号自加一
                        listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
                        listpara.add(new Object[]{1, CCMSeq, PlineCode});

                        listsqlID.add("FrmSteelManagement.ChangeHandOverOrderPlan");
                        listpara.add(new Object[]{FurnaceNo, FurnaceNo, CCMSeq, JudgeStoveNo, "".equals(JudgeStoveNo) ? "" : ( JudgeStoveNo + "-01" ), PrintStoveNo, Para1, Long.parseLong(Para2)});

                        //如果铸造产线相同  前面数据在最后会CCMSeq会自加一   这的到的CCMSeq就不准确  需要也自加一
                        if (PlineCode.equals(HandOverPline) && HandOverCCMSeq > CCMSeq) {
                            HandOverCCMSeq++;
                        }

                    } else {

                        listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                        listpara.add(new Object[]{Num, Num, PlnSteelforOrdEntitys.get(0).get("FURNACENO"), PlnSteelforOrdEntitys.get(0).get("SEQUENCENO")});

                        listsqlID.add("FrmSteelManagement.DeleteOrder");
                        listpara.add(new Object[]{Para1, Long.parseLong(Para2)});

                        //TODO  第一切全转走   需要改第二分切的判定炉号？

                    }

                    listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
                    listpara.add(new Object[]{-1, HandOverCCMSeq, HandOverPline});
                 /*   listsqlID.add("FrmSteelManagement.UpdateSEQUENCENO");
                    listpara.add(new Object[]{-1  , HandOverSEQUENCENO ,Para1 });*/


                } else {

                    PlnSteelforOrdEntitys =
                            ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderByProID",
                                    new Object[]{PRO_PLAN_ID, GX_PLAN_NO, FurnaceNo}).getResult() ).getResult();

                    if (NewJudgeStove || PRO_PLAN_ID == null || "".equals(PRO_PLAN_ID) || PlnSteelforOrdEntitys == null || PlnSteelforOrdEntitys.size() <= 0) {

                        //根据铸造产线 大于MinCCMSeq的铸造顺序号自加一
                        listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
                        listpara.add(new Object[]{1, CCMSeq, PlineCode});

                        listsqlID.add("FrmSteelManagement.InsertIntoHandOverPlan");
                        listpara.add(new Object[]{FurnaceNo, FurnaceNo, CCMSeq, JudgeStoveNo, "".equals(JudgeStoveNo) ? "" : ( JudgeStoveNo + "-01" ), PrintStoveNo, Num, Num, FurnaceNo, Para1, Long.parseLong(Para2)});

                    } else {

                        listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                        listpara.add(new Object[]{Num, Num, PlnSteelforOrdEntitys.get(0).get("FURNACENO"), PlnSteelforOrdEntitys.get(0).get("SEQUENCENO")});

                    }


                    listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                    listpara.add(new Object[]{0 - Num, 0 - Num, Para1, Long.parseLong(Para2)});


                }


                listsqlID.add("FrmSteelManagement.UpdateFurnaceNoQty");
                listpara.add(new Object[]{Para1});


                listsqlID.add("FrmSteelManagement.UpdateCastQty3");
                listpara.add(new Object[]{HandOverCast});

               /* listsqlID.add("FrmSteelManagement.UpdateCastQty");
                listpara.add(new Object[]{TargetCast});

                PlineCode = OrderPlan.get("PLINE_CODE").toString();
                CCMSeq=   ((BigDecimal)TempList.get(0).get("CCM_SEQ")).longValue() +1;
                TargetCast  =OrderPlan.get("CAST_NO").toString();

                listsqlID.add("FrmSteelManagement.UpdateCastQty");
                listpara.add(new Object[]{TargetCast});*/


                //endregion


            } else if ("1".equals(Type)) {


                List<Map<String, Object>> PlnOrderCastEntitys = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderCastS",
                        new Object[]{Para1, Para2}).getResult() ).getResult();
                if (PlnOrderCastEntitys == null || PlnOrderCastEntitys.size() <= 0) {
                    cro.setV_errMsg("找不到需要转分切的订单的数据,数据已更新！");
                    return cro;
                }

                Map<String, Object> OrderCastPlan = PlnOrderCastEntitys.get(0);


                Long NumGp = ( (BigDecimal) OrderCastPlan.get("CUT_NUM_GP") ).longValue();
                Long SingleLen = ( (BigDecimal) OrderCastPlan.get("LEN_GP_SINGLE") ).longValue();
                // Long Gp =OrderCastPlan.get("FLAG_STOVE_NO")==null?0: Long.parseLong( OrderCastPlan.get("FLAG_STOVE_NO").toString().substring(0,1));


                Integer totNum = ( OrderCastPlan.get("PLAN_NUM_S") == null ? 0 : ( (BigDecimal) OrderCastPlan.get("PLAN_NUM_S") ).intValue() )
                        - ( OrderCastPlan.get("PLANED_NUM") == null ? 0 : ( (BigDecimal) OrderCastPlan.get("PLANED_NUM") ).intValue() );

                if (totNum < Num) {
                    cro.setV_errMsg("转分切的数目大于原订单的可组炉支数！");
                    return cro;
                } else {


                    Double dimater = Double.parseDouble(OrderCastPlan.get("DIAMETER_LZ").toString().replace("250", "251"));


                    double mSingleWeight = ( 7.8 * Math.PI / 4 * dimater * dimater ) / 1000 / 1000;
                    // 单倍坯重
                    double singleWeight = mSingleWeight
                            * SingleLen / 1000;

                    double MainWeight = singleWeight * NumGp;


                    List<Map<String, Object>> PlnSteelforOrdEntitys =
                            ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.SelectOrderByProID",
                                    new Object[]{Para1, Para2, FurnaceNo}).getResult() ).getResult();

                    if (NewJudgeStove || PlnSteelforOrdEntitys == null || PlnSteelforOrdEntitys.size() <= 0) {
                        //根据铸造产线 大于MinCCMSeq的铸造顺序号自加一
                        listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
                        listpara.add(new Object[]{1, CCMSeq, PlineCode});

                        listsqlID.add("FrmSteelManagement.InsertIntoHandOverPlanByOrderCast");
                        listpara.add(new Object[]{CCMSeq, JudgeStoveNo, "".equals(JudgeStoveNo) ? "" : ( JudgeStoveNo + "-01" ), PrintStoveNo, singleWeight, MainWeight, Num, Num * MainWeight, Operator, Operator, FurnaceNo, Para1, Para2});
                    } else {
                        listsqlID.add("FrmSteelManagement.UpdateOrderQty");
                        listpara.add(new Object[]{Num, Num, PlnSteelforOrdEntitys.get(0).get("FURNACENO"), PlnSteelforOrdEntitys.get(0).get("SEQUENCENO")});

                    }

                    listsqlID.add("FrmSteelManagement.UpdateOrderCastS");
                    listpara.add(new Object[]{Num, Num * MainWeight, Para1, Para2});


                }


            }


            listsqlID.add("FrmSteelManagement.UpdateFurnaceNoQty");
            listpara.add(new Object[]{FurnaceNo});


            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{TargetCast});


            cro = setDataById(listsqlID, listpara);

            String[] Furnacenos = Furnaceno.toArray(new String[Furnaceno.size()]);
            MyThread NewThread = new MyThread(Furnacenos, Furnacenos, Operator);
            NewThread.start();

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }


    public CoreReturnObject ChangeOrderPlan(
            String OrFurnaceno,
            ArrayList<Map<String, Object>> OrOrder,
            String TargetFurnaceno,
            ArrayList<Map<String, Object>> TargetOrder,
            String Operator
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        StoveNoGenerate StoveNoGenerate = new StoveNoGenerate();

        try {

            Map<String, String> EfPlineNewJudgeStove = new HashMap<String, String>();
            Map<String, Map<Long, Integer>> EfPlineAdjustCcmSeq = new HashMap<String, Map<Long, Integer>>();


            if (ChangeOrderDate(OrFurnaceno, OrOrder, Operator, cro, listsqlID, listpara, StoveNoGenerate, EfPlineNewJudgeStove, EfPlineAdjustCcmSeq))
                return cro;

            if (ChangeOrderDate(TargetFurnaceno, TargetOrder, Operator, cro, listsqlID, listpara, StoveNoGenerate, EfPlineNewJudgeStove, EfPlineAdjustCcmSeq))
                return cro;

           cro = setDataById(listsqlID, listpara);

    /*        String[] Furnacenos = new String[]{OrFurnaceno, TargetFurnaceno};
            MyThread NewThread = new MyThread(Furnacenos, Furnacenos, Operator);
            NewThread.start();*/

        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;

    }

    private boolean ChangeOrderDate(String OrFurnaceno, ArrayList<Map<String, Object>> OrOrder, String Operator, CoreReturnObject cro, ArrayList<String> listsqlID, ArrayList<Object[]> listpara, StoveNoGenerate stoveNoGenerate, Map<String, String> efPlineNewJudgeStove, Map<String, Map<Long, Integer>> efPlineAdjustCcmSeq) throws Exception {
        List<Map<String, Object>> PlnSteelforfurnaceEntity = ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", OrFurnaceno, "", null, null, null).getResult() ).getResult();

        if (PlnSteelforfurnaceEntity == null || PlnSteelforfurnaceEntity.size() <= 0) {
            cro.setV_errMsg("数据发生改变!请重新调整");
            return true;
        }

        String CastNo = String.valueOf(PlnSteelforfurnaceEntity.get(0).get("CAST_NO"));

        String EfPline = String.valueOf(PlnSteelforfurnaceEntity.get(0).get("EF_PLINE_CODE"));
        String Pline = String.valueOf(PlnSteelforfurnaceEntity.get(0).get("PLINE_CODE"));
        String StoveNo = PlnSteelforfurnaceEntity.get(0).get("STOVE_NO") == null ? "" : String.valueOf(PlnSteelforfurnaceEntity.get(0).get("STOVE_NO"));
        String ProMoth =  String.valueOf(PlnSteelforfurnaceEntity.get(0).get("PRO_MONTH"));
        String CCM_STATE =  String.valueOf(PlnSteelforfurnaceEntity.get(0).get("CCM_STATE"));



        if(!CCM_STATE.equals("00")){
            cro.setV_errMsg("炉号"+StoveNo + "已经切割结炉,请先撤销切割结炉");
            return true;
        }

        List<Map<String, Object>> PlnSteelforOrdEntitys =
                ( (CoreReturnSortObject) SelectPlnSteelforOrdEntityByfurnaceno(OrFurnaceno).getResult() ).getResult();

        Long MaxCcmSeq = null;
        Long MinCcmSeq = null;
        List<Long> Seqs = new ArrayList<Long>();
        List<Long> UsedSeqs = new ArrayList<Long>();
        Long MaxSeq = 1l;
        int Count = 0;

        for (Map<String, Object> Order : PlnSteelforOrdEntitys
                ) {
            String Type = String.valueOf(Order.get("SURPLUS_TYPE"));
            Long CcmSeq = ( (BigDecimal) Order.get("CCM_SEQ") ).longValue();
            String Status = String.valueOf(Order.get("STATUS"));



            if(!"10".equals(Status)){
                cro.setV_errMsg("已经生产切割数据,请先删除切割数据再转分切");
                return true;
            }

            //调整后的CcmSeq
            if (efPlineAdjustCcmSeq.containsKey(Pline)) {
                for (Map.Entry<Long, Integer> entry : efPlineAdjustCcmSeq.get(Pline).entrySet()) {
                    if (CcmSeq >= entry.getKey()) {
                        CcmSeq = CcmSeq + entry.getValue();
                    }
                }
            }

            Long Seq = ( (BigDecimal) Order.get("SEQUENCENO") ).longValue();


            if (!"2".equals(Type) && !"3".equals(Type)) {
                Seqs.add(Seq);
                Count++;
                if (Seq > MaxSeq) MaxSeq = Seq;
                if (MinCcmSeq == null) {
                    MinCcmSeq = CcmSeq;
                    MaxCcmSeq = CcmSeq;
                } else {
                    if (MinCcmSeq > CcmSeq) MinCcmSeq = CcmSeq;
                    if (MaxCcmSeq < CcmSeq) MaxCcmSeq = CcmSeq;

                }

            }
            /*   else if(MinCcmSeq==null){
                   MinCcmSeq = CcmSeq +1;
                   MaxCcmSeq = CcmSeq ;
               }*/

        }


        if (MinCcmSeq == null) {
            cro.setV_errMsg("无法转入空炉！");
            return true;
        }

        if (OrOrder == null || OrOrder.size() <= 0) {
            cro.setV_errMsg("无法生成空炉！");
            return true;
        }


        if (OrOrder.size() != Count && MaxCcmSeq != null) {
            listsqlID.add("FrmSteelManagement.UpdateCCMSeqByLine");
            listpara.add(new Object[]{OrOrder.size() - Count, MaxCcmSeq + 1, Pline});

            if (!efPlineAdjustCcmSeq.containsKey(EfPline)) {
                efPlineAdjustCcmSeq.put(EfPline, new HashMap<Long, Integer>());
            }

            Map<Long, Integer> temp = efPlineAdjustCcmSeq.get(Pline);
            if (temp == null) temp = new HashMap<Long, Integer>();
            temp.put(MaxCcmSeq + 1, OrOrder.size() - Count);
            efPlineAdjustCcmSeq.put(Pline, temp);
        }


        int OrderCount = 0;
        String FirstJudgeStove = "";

        for (Map<String, Object> Order : OrOrder
                ) {


            Integer OrderTotalNum  = Integer.parseInt( Order.get("totMumM") == null ? "" : String.valueOf(Order.get("totMumM")));
            Integer MegerNum = 0;


            OrderCount++;
            Long Seq = (Long) Order.get("sequenceno");
            String temoFurnaceno = String.valueOf(Order.get("furnaceno"));
            List<Map<String, Object>> MergeListForSave = Order.get("mergeListForSave") == null ? null : (List<Map<String, Object>>) Order.get("mergeListForSave");
            List<Map<String, Object>> MergeListAll = Order.get("mergeListAll") == null ? null : (List<Map<String, Object>>) Order.get("mergeListAll");
            String MergeNo = MergeListForSave == null || Order.get("mergeNo") == null ? "" : Order.get("mergeNo").toString();
            String GpType =  String.valueOf(Order.get("gptype"));
            Long CutNumGp = Order.get("cutNumGp") == null ? 0 :  (Long) Order.get("cutNumGp");

            if (MergeListForSave != null && MergeListForSave.size() > 0) {

                MergeNo = OrFurnaceno + String.format("%02d", Seq);
                listsqlID.add("FrmSteelManagement.deleteMegerNo");
                listpara.add(new Object[]{MergeNo});

                int MergeSeq = 1;
                for (Map<String, Object> MergeEntity : MergeListForSave
                        ) {


                    listsqlID.add("FrmSteelManagement.addMegerNo");
                    listpara.add(new Object[]{
                            MergeNo,
                            MergeSeq++,
                            MergeEntity.get("proPlanId") == null ? "" : String.valueOf(MergeEntity.get("proPlanId")),
                            MergeEntity.get("gxPlanNo") == null ? "" : String.valueOf(MergeEntity.get("gxPlanNo")),
                            MergeEntity.get("lenGpSingle") == null ? "" : String.valueOf(MergeEntity.get("lenGpSingle")),
                            MergeEntity.get("cutNumGp") == null ? "" : String.valueOf(MergeEntity.get("cutNumGp")),
                            MergeEntity.get("weightGpOne") == null ? "" : String.valueOf(MergeEntity.get("weightGpOne")),
                            MergeEntity.get("totMumM") == null ? "" : String.valueOf(MergeEntity.get("totMumM")),
                            MergeEntity.get("totWeightM") == null ? "" : String.valueOf(MergeEntity.get("totWeightM")),
                            MergeEntity.get("cic") == null ? "" : String.valueOf(MergeEntity.get("cic")),
                            MergeEntity.get("pic") == null ? "" : String.valueOf(MergeEntity.get("pic")),
                            MergeEntity.get("sic") == null ? "" : String.valueOf(MergeEntity.get("sic")),
                            MergeEntity.get("dic") == null ? "" : String.valueOf(MergeEntity.get("dic")),
                            Operator,
                            MergeEntity.get("orderNo") == null ? "" : String.valueOf(MergeEntity.get("orderNo")),
                            MergeEntity.get("finalUserDesc") == null ? "" : String.valueOf(MergeEntity.get("finalUserDesc")),
                            MergeEntity.get("steelcode") == null ? "" : String.valueOf(MergeEntity.get("steelcode")),
                            MergeEntity.get("steelname") == null ? "" : String.valueOf(MergeEntity.get("steelname")),
                            MergeEntity.get("produccode") == null ? "" : String.valueOf(MergeEntity.get("produccode")),
                            MergeEntity.get("producname") == null ? "" : String.valueOf(MergeEntity.get("producname")),
                            MergeEntity.get("ifMonitorLg") == null ? "" : String.valueOf(MergeEntity.get("ifMonitorLg")),
                            MergeEntity.get("prdcrNo") == null ? "" : String.valueOf(MergeEntity.get("prdcrNo")),
                            OrFurnaceno,
                            MergeEntity.get("pdeptName") == null ? "" : String.valueOf(MergeEntity.get("pdeptName")),
                            MergeEntity.get("prdcrMan") == null ? "" : String.valueOf(MergeEntity.get("prdcrMan")),
                            MergeEntity.get("stateLk") == null ? "" : String.valueOf(MergeEntity.get("stateLk"))
                    });

                    MegerNum +=  Integer.parseInt(MergeEntity.get("totMumM") == null ? "0" : String.valueOf(MergeEntity.get("totMumM")));
                    if(!(Order.get("proPlanId") == null ? "" : String.valueOf(Order.get("proPlanId"))).contains(
                            (MergeEntity.get("proPlanId") == null ? "" : String.valueOf(MergeEntity.get("proPlanId")))))
                    {
                        cro.setV_errMsg("数据异常！");
                        return true;
                    }

                }
            }

            if(MegerNum!=0 && !MegerNum.equals(OrderTotalNum))
            {
                cro.setV_errMsg("数据异常！");
                return true;
            }


            String JudgeStoveNo = Order.get("judgeStoveNo") == null ? "" : String.valueOf(Order.get("judgeStoveNo"));
            if (OrderCount == 1) {
                JudgeStoveNo = stoveNoGenerate.GetNewJudgeStove(EfPline, StoveNo, 1l,ProMoth);
                FirstJudgeStove = JudgeStoveNo;
            }else
            {
                if(JudgeStoveNo.equals(FirstJudgeStove)){
                    JudgeStoveNo = stoveNoGenerate.GetNewJudgeStove(EfPline, StoveNo, new BigDecimal(OrderCount).longValue(),ProMoth);

                }
            }

           // Long MaxSeq = null;
            boolean isExist = temoFurnaceno.equals(OrFurnaceno) && Seqs.contains(Seq) && !UsedSeqs.contains(Seq);

            if (isExist) {

                listsqlID.add("FrmSteelManagement.UpdateChangeOrderPlan");
                listpara.add(new Object[]{
                        MinCcmSeq++,
                        Order.get("proPlanId") == null ? "" : String.valueOf(Order.get("proPlanId")),
                        Order.get("gxPlanNo") == null ? "" : String.valueOf(Order.get("gxPlanNo")),
                        JudgeStoveNo,
                        JudgeStoveNo + "-01",
                        stoveNoGenerate.GePrintStove(EfPline, StoveNo, Long.valueOf(OrderCount),ProMoth),
                        Order.get("totMumM") == null ? "" : String.valueOf(Order.get("totMumM")),
                        Order.get("totWeightM") == null ? "" : String.valueOf(Order.get("totWeightM")),
                        Order.get("cic") == null ? "" : String.valueOf(Order.get("cic")),
                        Order.get("pic") == null ? "" : String.valueOf(Order.get("pic")),
                        Order.get("sic") == null ? "" : String.valueOf(Order.get("sic")),
                        Order.get("dic") == null ? "" : String.valueOf(Order.get("dic")),
                        Order.get("orderNo") == null ? "" : String.valueOf(Order.get("orderNo")),
                        Order.get("finalUserDesc") == null ? "" : String.valueOf(Order.get("finalUserDesc")),
                        Order.get("prdcrNo") == null ? "" : String.valueOf(Order.get("prdcrNo")),
                        MergeNo,
                        Order.get("pdeptName") == null ? "" : String.valueOf(Order.get("pdeptName")),
                        Order.get("prdcrMan") == null ? "" : String.valueOf(Order.get("prdcrMan")),
                        OrderCount,
                        Order.get("stateLk") == null ? "" : String.valueOf(Order.get("stateLk")),
                        OrFurnaceno,
                        Seq
                });
            } else {
                if (OrderCount == 1) {
                    JudgeStoveNo = stoveNoGenerate.GetNewJudgeStove(EfPline, StoveNo, 1l,ProMoth);
                } else if (!efPlineNewJudgeStove.containsKey(EfPline)) {
                    JudgeStoveNo = stoveNoGenerate.GetNewJudgeStove(EfPline, StoveNo, new BigDecimal(OrderCount).longValue(),ProMoth);
                    efPlineNewJudgeStove.put(EfPline, JudgeStoveNo);
                } else {
                    JudgeStoveNo =  String.format("%06d", ( Integer.parseInt(efPlineNewJudgeStove.get(EfPline)) + 1 )) + "";
                    efPlineNewJudgeStove.put(EfPline, JudgeStoveNo);
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                MaxSeq++;

                Seq = MaxSeq;

                listsqlID.add("FrmSteelManagement.InsertOrder");
                listpara.add(new Object[]{
                        Seq,
                        Order.get("proPlanId") == null ? "" : String.valueOf(Order.get("proPlanId")),
                        Order.get("gxPlanNo") == null ? "" : String.valueOf(Order.get("gxPlanNo")),
                        MinCcmSeq++,
                        JudgeStoveNo,
                        JudgeStoveNo + "-01",
                        stoveNoGenerate.GePrintStove(EfPline, StoveNo, Long.valueOf(OrderCount),ProMoth),
                        Order.get("gptype") == null ? "" : String.valueOf(Order.get("gptype")),
                        Order.get("castingtype") == null ? "" : String.valueOf(Order.get("castingtype")),
                        Order.get("flowno") == null ? "" : String.valueOf(Order.get("flowno")),
                        Order.get("wlidIn") == null ? "" : String.valueOf(Order.get("wlidIn")),
                        Order.get("wldescIn") == null ? "" : String.valueOf(Order.get("wldescIn")),
                        Order.get("materialNo") == null ? "" : String.valueOf(Order.get("materialNo")),
                        Order.get("materialName") == null ? "" : String.valueOf(Order.get("materialName")),
                        Order.get("gradecode") == null ? "" : String.valueOf(Order.get("gradecode")),
                        Order.get("gradename") == null ? "" : String.valueOf(Order.get("gradename")),
                        Order.get("diameter") == null ? "" : String.valueOf(Order.get("diameter")),
                        Order.get("lenGpSingle") == null ? "" : String.valueOf(Order.get("lenGpSingle")),
                        Order.get("cutNumGp") == null ? "" : String.valueOf(Order.get("cutNumGp")),
                        Order.get("lengthM") == null ? "" : String.valueOf(Order.get("lengthM")),
                        Order.get("weightGpOne") == null ? "" : String.valueOf(Order.get("weightGpOne")),
                        Order.get("weightM") == null ? "" : String.valueOf(Order.get("weightM")),
                        Order.get("totMumM") == null ? "" : String.valueOf(Order.get("totMumM")),
                        Order.get("totWeightM") == null ? "" : String.valueOf(Order.get("totWeightM")),
                        Order.get("actWeightM") == null ? "" : String.valueOf(Order.get("actWeightM")),
                        Order.get("actMumM") == null ? "" : String.valueOf(Order.get("actMumM")),
                        Order.get("surplusflag") == null ? "" : String.valueOf(Order.get("surplusflag")),
                        Order.get("excessWt") == null ? "" : String.valueOf(Order.get("excessWt")),
                        Order.get("excessNum") == null ? "" : String.valueOf(Order.get("excessNum")),
                        Order.get("surplusType") == null ? "" : String.valueOf(Order.get("surplusType")),
                        Order.get("ifSurplusAlloted") == null ? "" : String.valueOf(Order.get("ifSurplusAlloted")),
                        Order.get("cic") == null ? "" : String.valueOf(Order.get("cic")),
                        Order.get("pic") == null ? "" : String.valueOf(Order.get("pic")),
                        Order.get("sic") == null ? "" : String.valueOf(Order.get("sic")),
                        Order.get("dic") == null ? "" : String.valueOf(Order.get("dic")),
                        Order.get("chemJudgeId") == null ? "" : String.valueOf(Order.get("chemJudgeId")),
                        Order.get("phyJudgeId") == null ? "" : String.valueOf(Order.get("phyJudgeId")),
                        Order.get("colligateJudgeId") == null ? "" : String.valueOf(Order.get("colligateJudgeId")),
                        Order.get("memo") == null ? "" : String.valueOf(Order.get("memo")),
                        Order.get("makeman") == null ? "" : String.valueOf(Order.get("makeman")),
                        Order.get("maketime") == null ? "" : sdf.format(Order.get("maketime")),
                        Order.get("downman") == null ? "" : String.valueOf(Order.get("downman")),
                        Order.get("downtime") == null ? "" : sdf.format(Order.get("downtime")),
                        Order.get("orderNo") == null ? "" : String.valueOf(Order.get("orderNo")),
                        Order.get("finalUserDesc") == null ? "" : String.valueOf(Order.get("finalUserDesc")),
                        Order.get("steelcode") == null ? "" : String.valueOf(Order.get("steelcode")),
                        Order.get("steelname") == null ? "" : String.valueOf(Order.get("steelname")),
                        Order.get("produccode") == null ? "" : String.valueOf(Order.get("produccode")),
                        Order.get("producname") == null ? "" : String.valueOf(Order.get("producname")),
                        Order.get("gowhereCode") == null ? "" : String.valueOf(Order.get("gowhereCode")),
                        Order.get("gowhereName") == null ? "" : String.valueOf(Order.get("gowhereName")),
                        Order.get("stateLk") == null ? "" : String.valueOf(Order.get("stateLk")),
                        Order.get("ifMonitorLg") == null ? "" : String.valueOf(Order.get("ifMonitorLg")),
                        Order.get("prdcrNo") == null ? "" : String.valueOf(Order.get("prdcrNo")),
                        MergeNo,
                        Order.get("pdeptName") == null ? "" : String.valueOf(Order.get("pdeptName")),
                        Order.get("prdcrMan") == null ? "" : String.valueOf(Order.get("prdcrMan")),
                        OrderCount,
                        OrFurnaceno

                });
            }

            for (Map<String, Object> MergeEntity : MergeListAll
                    ) {

                   String TempFurnaceno=     MergeEntity.get("furnaceno") == null ? "" : String.valueOf(MergeEntity.get("furnaceno"));
                    Long TempSeq  = MergeEntity.get("sequenceno") == null ? 0l : (Long)(MergeEntity.get("sequenceno"));
                   String TempProPlanId = MergeEntity.get("proPlanId") == null ? "" : String.valueOf(MergeEntity.get("proPlanId"));
                   String TempGxPlanNo =     MergeEntity.get("gxPlanNo") == null ? "" : String.valueOf(MergeEntity.get("gxPlanNo"));
                   Long TempNum =  MergeEntity.get("totMumM") == null ? 0 : (Long)MergeEntity.get("totMumM");
                   Double TempWeight =  MergeEntity.get("totWeightM") == null ? 0d :  (Double)MergeEntity.get("totWeightM");

                 if(!isExist || !(TempFurnaceno.equals(OrFurnaceno)&& TempSeq==Seq)){
                     Integer UsedNum = 0;
                     Double UsedWeight = 0d;

                     if("1".equals(GpType)){
                         List<Map<String, Object>> PlnZyZgM =
                                 ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.selectPlnZyZgM",new Object[]{TempFurnaceno,TempSeq,TempProPlanId,TempGxPlanNo}).getResult() ).getResult();

                         List<Map<String, Object>> PlnZyZgMOr =
                                 ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.selectPlnZyZgM",new Object[]{OrFurnaceno,Seq,TempProPlanId,TempGxPlanNo}).getResult() ).getResult();


                         for (int i = 0; i <PlnZyZgM.size() && UsedNum< TempNum; i++) {

                             String HeatPlanNo = String.valueOf( PlnZyZgM.get(i).get("HEAT_PLAN_NO"));
                             Integer ZgNum =  PlnZyZgM.get(i).get("PLN_INSINGLE_NUM") == null ? 0 :  new BigDecimal(String.valueOf( PlnZyZgM.get(i).get("PLN_INSINGLE_NUM"))).intValue();
                             Double ZgWeight =  PlnZyZgM.get(i).get("PLN_INSINGLE_NUM") == null ? 0d :  new BigDecimal(String.valueOf( PlnZyZgM.get(i).get("PLN_INSINGLE_NUM"))).doubleValue();
                             String Status = String.valueOf( PlnZyZgM.get(0).get("PLANSTATUS"));

                             if(ZgNum> TempNum* CutNumGp-UsedNum){

                                 if(PlnZyZgMOr.size()>0) {
                                     String TempHeatPlanNo = String.valueOf( PlnZyZgMOr.get(0).get("HEAT_PLAN_NO"));
                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyZgM2");
                                     listpara.add(new Object[]{ UsedNum-TempNum * CutNumGp , UsedWeight-TempWeight * CutNumGp ,
                                             UsedNum-TempNum * CutNumGp , UsedWeight-TempWeight * CutNumGp , UsedWeight-TempWeight * CutNumGp ,
                                             TempHeatPlanNo});
                                 }
                                 else{
                                     listsqlID.add("FrmSteelManagement.InsertPlnZyZgM");
                                     listpara.add(new Object[]{OrFurnaceno, Seq, TempNum * CutNumGp - UsedNum, TempWeight * CutNumGp - UsedWeight,
                                             TempNum * CutNumGp - UsedNum, TempWeight * CutNumGp - UsedWeight, TempWeight * CutNumGp - UsedWeight,
                                             HeatPlanNo});
                                 }

                                 listsqlID.add("FrmSteelManagement.UpdatePlnZyZgM2");
                                 listpara.add(new Object[]{TempNum* CutNumGp-UsedNum, TempWeight*CutNumGp -UsedWeight,
                                         TempNum* CutNumGp-UsedNum, TempWeight*CutNumGp -UsedWeight, TempWeight*CutNumGp -UsedWeight,
                                         HeatPlanNo});
                             }
                             else{

                                 if(PlnZyZgMOr.size()>0) {

                                     String TempHeatPlanNo = String.valueOf( PlnZyZgMOr.get(0).get("HEAT_PLAN_NO"));

                                     if(!"00".equals(Status)){
                                         cro.setV_errMsg("轧管炉计划已下发,无法合并！");
                                         return true;
                                     }

                                     listsqlID.add("FrmSteelManagement.DeletePlnZyZgM");
                                     listpara.add(new Object[]{ HeatPlanNo});

                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyZgM2");
                                     listpara.add(new Object[]{  0-ZgNum, 0d-ZgWeight,
                                             0-ZgNum, 0d-ZgWeight,0d-ZgWeight,
                                             TempHeatPlanNo});
                                 } else {
                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyZgM");
                                     listpara.add(new Object[]{OrFurnaceno, Seq, HeatPlanNo});
                                 }

                             }
                             UsedNum = UsedNum+ ZgNum;
                             UsedWeight = UsedWeight+ ZgWeight;
                         }






                     }else if("2".equals(GpType)){

                         List<Map<String, Object>> PlnZyDzM =
                                 ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.selectPlnZyDZM",new Object[]{TempFurnaceno,TempSeq,TempProPlanId,TempGxPlanNo}).getResult() ).getResult();
                         List<Map<String, Object>> PlnZyDzMOr =
                                 ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.selectPlnZyZgM",new Object[]{OrFurnaceno,Seq,TempProPlanId,TempGxPlanNo}).getResult() ).getResult();




                         for (int i = 0; i <PlnZyDzM.size() && UsedNum< TempNum; i++) {

                             String HeatPlanNo = String.valueOf( PlnZyDzM.get(i).get("HEAT_PLAN_NO"));
                             Integer DzNum =  PlnZyDzM.get(i).get("PLN_INDOUBLE_NUM") == null ? 0 :  new BigDecimal(String.valueOf( PlnZyDzM.get(i).get("PLN_INDOUBLE_NUM"))).intValue();
                             Double DzWeight =  PlnZyDzM.get(i).get("PLN_INDOUBLE_WT") == null ? 0d :  new BigDecimal(String.valueOf( PlnZyDzM.get(i).get("PLN_INDOUBLE_WT"))).doubleValue();
                             String Status = String.valueOf( PlnZyDzM.get(0).get("PLANSTATUS"));


                             if(DzNum>TempNum-UsedNum){

                                 if(PlnZyDzMOr.size()>0) {
                                     String TempHeatPlanNo = String.valueOf( PlnZyDzMOr.get(0).get("HEAT_PLAN_NO"));

                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyDzM2");
                                     listpara.add(new Object[]{
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             TempHeatPlanNo});
                                 }
                                 else {

                                     listsqlID.add("FrmSteelManagement.InsertPlnZyDzM");
                                     listpara.add(new Object[]{OrFurnaceno, Seq,
                                             TempNum - UsedNum, TempWeight - UsedWeight,
                                             TempNum - UsedNum, TempWeight - UsedWeight,
                                             TempNum - UsedNum, TempWeight - UsedWeight,
                                             TempNum - UsedNum, TempWeight - UsedWeight,
                                             HeatPlanNo});
                                 }

                                 listsqlID.add("FrmSteelManagement.UpdatePlnZyDzM2");
                                 listpara.add(new Object[]{
                                         TempNum-UsedNum , TempWeight-UsedWeight,
                                         TempNum-UsedNum , TempWeight-UsedWeight,
                                         TempNum-UsedNum , TempWeight-UsedWeight,
                                         TempNum-UsedNum , TempWeight-UsedWeight,
                                         HeatPlanNo});
                             }
                             else{
                                 if(PlnZyDzMOr.size()>0) {
                                     String TempHeatPlanNo = String.valueOf( PlnZyDzMOr.get(0).get("HEAT_PLAN_NO"));
                                     if(!"00".equals(Status)){
                                         cro.setV_errMsg("锻造炉计划已下发,无法合并！");
                                         return true;
                                     }

                                     listsqlID.add("FrmSteelManagement.DeletePlnZyDzM");
                                     listpara.add(new Object[]{ HeatPlanNo});

                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyDzM2");
                                     listpara.add(new Object[]{
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             UsedNum-TempNum , UsedWeight-TempWeight,
                                             TempHeatPlanNo});
                                 }
                                 else {
                                     listsqlID.add("FrmSteelManagement.UpdatePlnZyDzM");
                                     listpara.add(new Object[]{OrFurnaceno, Seq, HeatPlanNo});
                                 }

                             }
                             UsedNum = UsedNum+ DzNum;
                             UsedWeight = UsedWeight+ DzWeight;
                         }

                     }
                 }

            }



            UsedSeqs.add(Seq);

        }

        for (Long Seq : Seqs
                ) {
            if (!UsedSeqs.contains(Seq)) {
                listsqlID.add("FrmSteelManagement.DeleteOrder");
                listpara.add(new Object[]{OrFurnaceno, Seq});
            }
        }


        listsqlID.add("FrmSteelManagement.UpdateFurnaceNoQty");
        listpara.add(new Object[]{OrFurnaceno});


        listsqlID.add("FrmSteelManagement.UpdateCastQty3");
        listpara.add(new Object[]{CastNo});
        return false;
    }


    public CoreReturnObject NewCastNo(
            ArrayList<String> furnacenoList
    ) throws Exception {
        CoreReturnObject cro = new CoreReturnObject();
        cro.setV_errCode(-1);
        cro.setResult(false);
        ArrayList<String> listsqlID = new ArrayList<String>();
        ArrayList<Object[]> listpara = new ArrayList<Object[]>();
        List<Map<String, Object>> tempList;
        try {

            String OldCast = "";
            String NewCast = "";


            List<Map<String, Object>> AllFurnaceList = new ArrayList<Map<String, Object>>();
            for (String Furnace : furnacenoList
                    ) {

                tempList =

                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", Furnace, "", null, null, null).getResult() ).getResult();

                if (tempList == null || tempList.size() <= 0) {
                    cro.setV_errMsg("找不到该炉次号：" + Furnace + " 数据！");
                    return cro;
                }


                if ("".equals(NewCast)) {
                    String PlineCode = String.valueOf(tempList.get(0).get("PLINE_CODE"));
                    OldCast = String.valueOf(tempList.get(0).get("CAST_NO"));

                    String Pre = PlineCode.equals("C004") ? "2" : PlineCode.equals("C007") ? "3" : "1";
                    tempList = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.NewCastNo",
                            new Object[]{Pre, Pre, Pre}).getResult() ).getResult();
                    NewCast = tempList.get(0).get("CAST_NO").toString();

                    listsqlID.add("FrmSteelManagement.InsertCastByCopyCast2");
                    listpara.add(new Object[]{NewCast, furnacenoList.size(), OldCast});

                }


                tempList =
                        ( (CoreReturnSortObject) SelectPlnSteelforfurnaceEntity("", Furnace, "", null, null, null).getResult() ).getResult();


            }


            //生成新的浇次号


            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{NewCast});

            listsqlID.add("FrmSteelManagement.UpdateCastQty3");
            listpara.add(new Object[]{OldCast});


            cro = setDataById(listsqlID, listpara);
        } catch (Exception e) {
            logger.error(e);
            cro.setV_errCode(-1);
            cro.setV_errMsg(e.getMessage());
        }
        return cro;
    }


    //endregion

    //region 公用方法


    /**
     * 获取米单重公式
     *
     * @return 米单重公式
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject GetWeightPerMeterFormula() {
        CoreReturnObject cro = new CoreReturnObject();
        List<Map<String, Object>> WeightPerMeterFormula = ( (CoreReturnSortObject) getDataBySqlID("FrmSteelManagement.GetWeightPerMeterFormula").getResult() ).getResult();

        cro.setResult(WeightPerMeterFormula == null || WeightPerMeterFormula.size() <= 0 ? "" : WeightPerMeterFormula.get(0).get("BASENAME").toString());
        return cro;
    }


    /**
     * 查询电炉产线能力
     *
     * @param plineCode
     * @param wldescIn
     * @param materialNo
     * @throws SQLException
     */
    public double seqCapacityFurn(String plineCode,
                                  String wldescIn, String materialNo) throws SQLException {
        double capacity = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelMakingPlanChange.seqCapacityFurn");
        if (!wldescIn.equals("")) {
            sql += " AND T.IN_MATERIAL_NO='" + wldescIn + "'";
        }

        List<Map<String, Object>> CapacityFurn = ( (CoreReturnSortObject) getData(sql, new Object[]{plineCode, materialNo}).getResult() ).getResult();


        if (CapacityFurn != null && CapacityFurn.size() > 0) {

            capacity = CapacityFurn.get(0).get("HR_CAPCTY") == null ? 0 : ( (BigDecimal) CapacityFurn.get(0).get("HR_CAPCTY") ).doubleValue();
        }


        return capacity;
    }


    public String GetBackUpOrder(String plineCode) throws SQLException {
        String orderNo = "";
        SimpleDateFormat dFormat = new SimpleDateFormat("yyMM");
        SimpleDateFormat dFormat1 = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date Now = new Date();
        String orderSeq = "";
        try {
            String yearM = dFormat.format(Now);
            String proMatch = dFormat1.format(Now);
            if (plineCode.equals("C001")) {// 一炼
                orderSeq = "ZZBY-AY" + yearM + "/";
            } else if (plineCode.equals("C003")) {// 二炼
                orderSeq = "ZZBY-AE" + yearM + "/";
            } else if (plineCode.equals("C006")) {// 三炼
                orderSeq = "ZZBY-AS" + yearM + "/";
            }
            // 查询最大余材合同号


            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelMakingPlanChange.queryPlineOrderBl");
            sql+=" and T.ORDER_NO LIKE '%"+orderSeq+"%'";

            List<Map<String, Object>> BackUpOrder = ( (CoreReturnSortObject) getData(sql,
                    new Object[]{orderSeq, plineCode}).getResult() ).getResult();


            if (BackUpOrder != null && BackUpOrder.size() > 0) {

                orderNo = BackUpOrder.get(0).get("ORDER_NO") == null ? "" : String.valueOf(BackUpOrder.get(0).get("ORDER_NO"));
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return orderNo;
    }


    /**
     * 获取List 的In sql语句
     *
     * @param status
     * @param where
     * @param paras
     * @return
     */
    private String getStatus(ArrayList status, String where, ArrayList paras) {
        where = where + " (";
        for (int i = 0; i < status.size(); i++) {
            if (i != 0) where = where + ",";
            where = where + " ?";
            paras.add(status.get(i));
        }
        where = where + ")";
        return where;
    }
    //endregion


    public class MyThread extends Thread {
        private String[] AddFurnacenos;
        private String[] DeleteFurnacenos;
        private String Name;

        public MyThread(String[] addFurnacenos, String[] deleteFurnacenos, String name) {
            AddFurnacenos = addFurnacenos;
            DeleteFurnacenos = deleteFurnacenos;
            Name = name;
        }

        @Override
        public void run() {

            ComPlanFurnace ComPlanFurnace = new ComPlanFurnace();
            try {
                if (DeleteFurnacenos != null && DeleteFurnacenos.length > 0)
                    ComPlanFurnace.deletePlnSteelOrd(DeleteFurnacenos, Name);
                if (AddFurnacenos != null && AddFurnacenos.length > 0)
                    ComPlanFurnace.queryPlnSteelOrd(AddFurnacenos, Name);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    /***
     * 标识监制
     * @param plnSteelEntity
     * @return
     */
    public CoreReturnObject tabNotice(String plnSteelEntity,String userName){
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        String sql = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        String isNoticeFlag = "";
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        try {
        PlnSteelforOrdEntity plnEntity = EntityTool.getEntityByJSONStr(plnSteelEntity,PlnSteelforOrdEntity.class);
        sql = CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.selectNoticeFlag");
        sql = EntityTool.fillSqlByEntity(sql,plnEntity);
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        if(rs.next())
        {
            isNoticeFlag = rs.getString("IS_NOTICE_EP");
        }
        rs.close();
        ps.close();
        if(isNoticeFlag == null || isNoticeFlag.equals("0"))
        {
            plnEntity.setIsNoticeEp("1");
            plnEntity.setIsNoticeUser(userName);
            plnEntity.setIsNoticeTime(systime);
        }else if(isNoticeFlag.equals("1"))
        {
            plnEntity.setIsNoticeEp("0");
            plnEntity.setIsNoticeUser("");
            plnEntity.setIsNoticeTime("");
        }
        ArrayList<String> sqllist = new ArrayList<String>();
        sql = CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.noticeFlag");
        sql = EntityTool.fillSqlByEntity(sql,plnEntity);
        sqllist.add(sql);
        core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
        if(core.getV_errMsg().equals(""))
        {
            core.setV_errMsg("标识监制成功!");
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        if (con != null) {
            try {
                core.setV_errMsg(e.getMessage());
                con.rollback(); // 回滚事务
            } catch (Exception e2) {
            }
        }
    } finally {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }
        } catch (Exception e2) {
        }
    }
		return core;
    }

    /**
     * 操作记录
     * @param json
     * @return
     */
    public CoreReturnObject doAddLgLog(String json){
        CoreReturnObject core = new CoreReturnObject();
        try{
            ImpFrmdispatchEntity entity = EntityTool.getEntityByJSONStr(json,ImpFrmdispatchEntity.class);
            ArrayList<String> sqllist = new ArrayList<String>();
            String sql = CoreXmlSqlParsersFactory.getSql("FrmSteelManagement.doAddLgLog");
            sql = EntityTool.fillSqlByEntity(sql,entity);
            sqllist.add(sql);
            core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
            if(core.getV_errMsg().equals(""))
            {
                core.setV_errMsg("操作日志记录成功!");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return core;
    }

}
