package com.steering.qcm.server;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.common.SqlBuilder;
import com.steering.qcm.entity.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

public class ZcColligateJudge extends DBCommon {

    /**
     * 转压库成品判定
     *
     * @return
     */
    public CoreReturnObject judge() {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        SqlBuilder b;
        try {
            connection.setAutoCommit(false);
            b = new SqlBuilder("QcmZgJugdeApplyDAL.Query");
            List<QcmZgJugdeApplyEntity> zgJudgeApplys = EntityTool.getEntitysBySqlNoClsCon(
                    b.toString(), connection, QcmZgJugdeApplyEntity.class);

            for (QcmZgJugdeApplyEntity zgJudgeApply : zgJudgeApplys) {
                // 成品判定
                judge(connection, zgJudgeApply.getJugdeApplyCode(), zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(), zgJudgeApply.getProcessCode(), "SYSTEM", "", "", "", "", "3");
            }
            core.setV_errMsg("操作成功！");
            core.setResult(1);
            connection.commit();

        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }

    public static void judge(Connection con, String judgeApplyCode, String judgeStoveNo,
                             String batchNo, String processCode, String user, String order,
                             String group, String judgeFileMemo, String judgeMemo,
                             String zbsFlag) throws Exception {
        Statement st = con.createStatement();
        Date now = new Date();
        SqlBuilder b;

        b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
        b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo});
        QcmZgJugdeApplyEntity zgJudgeApply = EntityTool.getEntityBySqlNoClsCon(
                b.toString(), con, QcmZgJugdeApplyEntity.class);

        String oldJudgeTolResult = zgJudgeApply.getJudgeTolresultCode();

        HashMap<String, QcmZgJugdeApplyEntity> applyMap = getAllJudgeApply(
                zgJudgeApply.getJugdeApplyCode(), zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(),
                processCode, zgJudgeApply.getUpperHeatplanId(), null, null, con);
        String zgLoadStoveNo = "";
        if (applyMap.containsKey("D")) {
            zgLoadStoveNo = applyMap.get("D").getLoadJudgeStoveNo();
        }
        //获取判定结果
        String judgeResult = getJudgeResult(applyMap, zgLoadStoveNo, con);
        if (judgeResult.equals("") && !zgJudgeApply.getIsAllScrap().equals("4")
                && zgJudgeApply.getTarget().equals("A")) {
            throw new Exception("工序判定结果不完整，不能判定");
        }

        if (zgJudgeApply.getCic().equals("")) {
            ComBaseProductEntity product = EntityTool.getEntityBySqlNoClsCon(
                    b.toString(), con, ComBaseProductEntity.class);
            String producType = product.getProducType();
            if (!producType.equals("C") && producType.equals("D") && producType.equals("E")) {
                throw new Exception("送判单异常，找不到成分索引码");
            }
        }

        //-------------------验证锁定
        checkJudgeLock(b, zgJudgeApply, applyMap, con);

        //************************成品判定*********************
        //直缝焊套管 新系统出加工信息质保书， 性能结果在外购的质保书中
        QcmZcJudgeEntity zcJudge = new QcmZcJudgeEntity();
        if (zgJudgeApply.getProduccode().equals("S000103") || zgJudgeApply.getProduccode().equals("S000125")) {
            zgJudgeApply.setJudgeTolresultCode("40740701");
            zcJudge.setJudgeResult("40740701");
        } else {
            zcJudge.setJudgeResult(judgeResult);
            if (zgJudgeApply.getJudgeTolresultCode().equals("40740709")
                    && zcJudge.getJudgeResult().equals("")) {
                zgJudgeApply.setJudgeTolresultCode("40740709");
                zcJudge.setJudgeResult("40740709");
            } else {
                zgJudgeApply.setJudgeTolresultCode(zcJudge.getJudgeResult());
            }
        }

        if (!zcJudge.getErrorMsg().equals("")) {
            throw new Exception(zcJudge.getErrorMsg());
        }

        zcJudge.setMemo(judgeMemo);
        zcJudge.setCreateName(user);
        zcJudge.setStoveNo(zgJudgeApply.getStoveNo());
        zcJudge.setJudgeStoveNo(judgeStoveNo);
        zcJudge.setBatchNo(batchNo);
        zcJudge.setProcessCdoe(processCode);
        zcJudge.setPlineCode(zgJudgeApply.getPlineCode());
        zcJudge.setDeliveryStateCode(zgJudgeApply.getDeliveryStateCode());
        zcJudge.setDeliveryStateName(zgJudgeApply.getDeliveryStateName());
        zcJudge.setJugdeApplyCode(judgeApplyCode);
        zcJudge.setActCount(zgJudgeApply.getActCount());
        zcJudge.setActWeight(zgJudgeApply.getActWeight());
        zcJudge.setActTheoryWeight(zgJudgeApply.getActTheoryWeight());
        zcJudge.setOrdLnDlyPk(zgJudgeApply.getOrdLnDlyPk());
        //新增前作废有效的综合判定记录
        b = new SqlBuilder("JdgQcmZcJudge.updateValid");
        b.Fill(new String[]{judgeStoveNo, batchNo, judgeApplyCode, "0"});
        st.executeUpdate(b.toString());
        //新增判定记录
        saveJudge(zcJudge, "judge", b, con, st);

        //更新成品判定条件
        String zcJudgeCondition = "1";
        for (String key : applyMap.keySet()) {
            if (applyMap.get(key).getJudgeResultCode().equals("40740709")) {
                zcJudgeCondition = "0";
                break;
            }
        }

        //回写申请判定表
        b = new SqlBuilder("QcmZgJugdeApplyDAL.setJudgeResultZc");
        b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, processCode,
                zcJudge.getJudgeResult(), user, order, group,
                judgeMemo, zcJudge.getJudgeId(), judgeFileMemo, zcJudgeCondition});
        st.executeUpdate(b.toString());
        if (!oldJudgeTolResult.equals("40740709")) {
            b = new SqlBuilder("QcmZgJugdeApplyDAL.setJudgeTimeZc");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, processCode, "1"});
            st.executeUpdate(b.toString());
        }

        //修改结转年月
        //对于A字头结转年月在成品判定写  缺项合格的不修改结转年月
        if (zgJudgeApply.getTarget().equals("A") && !oldJudgeTolResult.equals("40740709")) {
            b = new SqlBuilder("QcmZgJugdeApplyDAL.setBalYearMonth");
            b.Fill(new String[]{zgJudgeApply.getJugdeApplyCode(), judgeStoveNo, batchNo,
                    processCode, GetBalYear(now)});
            st.executeUpdate(b.toString());
        }

        if (!zgJudgeApply.getIsAllScrap().equals("4")) {
            //回写材料质量状态表
            b = new SqlBuilder("JdgMatBcQcmTrack.setJudgeTolResult");
            b.Fill(new String[]{judgeStoveNo, batchNo, processCode, zgJudgeApply.getJugdeApplyCode(),
                    zcJudge.getJudgeId(),
                    zgJudgeApply.getSteelStandrad(), zcJudge.getJudgeResult(), user, judgeMemo});
            st.executeUpdate(b.toString());

            //*********************更新材料表
            b = new SqlBuilder("JdgMatBcM.updateTolJudgeResult");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, zcJudge.getJudgeResult(),
                    user, zcJudge.getJudgeId()});
            st.executeUpdate(b.toString());
            if (!oldJudgeTolResult.equals("40740709")) {
                b = new SqlBuilder("JdgMatBcM.updateTolJudgeTime");
                b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, "1"});
                st.executeUpdate(b.toString());
            }
            //更新材料历史表
            b = new SqlBuilder("JdgMatBcBackup.updateTolJudgeResult");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, zcJudge.getJudgeResult(),
                    user, zcJudge.getJudgeId()});
            st.executeUpdate(b.toString());
            if (!oldJudgeTolResult.equals("40740709")) {
                b = new SqlBuilder("JdgMatBcBackup.updateTolJudgeTime");
                b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, "1"});
                st.executeUpdate(b.toString());
            }

            //更新利库改判表
            if (zgJudgeApply.getJudgeTolresultCode().equals("40740701")) {
                b = new SqlBuilder("JdgQcmLkJudgeChange.setAgreeFlag");
                b.Fill(new String[]{zgJudgeApply.getJugdeApplyCode() + zgJudgeApply.getJugdeApplySequeno(), "3"});
                st.executeUpdate(b.toString());
            }
        }

        //修改成品材料表
        b = new SqlBuilder("JdgMatZcM.setJudgeResult");
        b.Fill(new String[]{judgeStoveNo, batchNo, zgJudgeApply.getBatchGroudNo(), zgJudgeApply.getOrdLnDlyPk(), zcJudge.getJudgeResult()});
        st.executeUpdate(b.toString());
        if (zgJudgeApply.getIsAllScrap().equals("4") && (zcJudge.getJudgeResult().equals("40740701")
                || zcJudge.getJudgeResult().equals("40740709"))) {
            b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo});
            ArrayList<QcmZgJugdeApplyEntity> applys = EntityTool.getEntitysBySqlNoClsCon(
                    b.toString(), con, QcmZgJugdeApplyEntity.class);
            for (QcmZgJugdeApplyEntity apply : applys) {
                b = new SqlBuilder("JdgMatZcM.setLkLock");
                b.Fill(new String[]{judgeStoveNo, batchNo, apply.getBatchGroudNo(), apply.getOrdLnDlyPk(), "0", "0"});
                st.executeUpdate(b.toString());
            }
        }

        // 修改半成品表的所属权 判定不合格所属权不变。
        if ((zcJudge.getJudgeResult().equals("40740701") || zcJudge.getJudgeResult().equals("40740702")
                || zcJudge.getJudgeResult().equals("40740709")) && !oldJudgeTolResult.equals("40740709")) {
            if (zgJudgeApply.getBelongCodeJugde().equals("")) {
                throw new Exception("判定所属权为空，不能判定！");
            }
            b = new SqlBuilder("JdgMatBcM.updateBelongZc");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, zgJudgeApply.getBelongCodeJugde(), zgJudgeApply.getBelongNameJugde()});
            st.executeUpdate(b.toString());
            b = new SqlBuilder("JdgMatBcBackup.updateBelongZc");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo, zgJudgeApply.getBelongCodeJugde(), zgJudgeApply.getBelongNameJugde()});
            st.executeUpdate(b.toString());
        }

        //转存质保书
        CreateZbsInfo(judgeApplyCode, judgeStoveNo, batchNo, zbsFlag, oldJudgeTolResult,
                false, "", zgJudgeApply.getOrdLnDlyPk(), con, st);

    }

    /**
     * 创建质保书
     *
     * @param applyCode
     * @param judgeStoveNo
     * @param batchNo
     * @param zbsFlag
     * @param oldJudgeTolResult
     * @param spotInfo
     * @param groupNo
     * @param zcOrdLnDlyPk
     * @param con
     * @param st
     * @throws Exception
     */
    private static void CreateZbsInfo(String applyCode, String judgeStoveNo, String batchNo, String zbsFlag,
                                      String oldJudgeTolResult, boolean spotInfo, String groupNo,
                                      String zcOrdLnDlyPk, Connection con, Statement st) throws Exception {
        //*********（1）基础数据准备
        //是否强制老系统
        String zbsFlagNew = zbsFlag;
        //本工序送判单
        SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
        b.Fill(new String[]{applyCode, judgeStoveNo, batchNo});
        QcmZgJugdeApplyEntity curApply = EntityTool.getEntityBySqlNoClsCon(
                b.toString(), con, QcmZgJugdeApplyEntity.class);
        b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryGroupInfoByBatchNo");
        b.Fill(new String[]{applyCode, judgeStoveNo, batchNo});
        QcmZgJugdeApplyEntity zgJudgeApplyGroup = EntityTool.getEntityBySqlNoClsCon(
                b.toString(), con, QcmZgJugdeApplyEntity.class);
        curApply.setActCount(zgJudgeApplyGroup.getActCount());
        curApply.setActWeight(zgJudgeApplyGroup.getActWeight());
        curApply.setActTheoryWeight(zgJudgeApplyGroup.getActTheoryWeight());
        //成品综合判定表
        b = new SqlBuilder("JdgQcmZcJudge.getByBatchNo");
        b.Fill(new String[]{judgeStoveNo, batchNo, curApply.getProcessCode(), applyCode});
        QcmZcJudgeEntity zcJudge = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, QcmZcJudgeEntity.class);
        if (zcJudge == null) {
            throw new Exception("成品综合未判定，不能刷新！");
        }

        if (curApply.getOrdLnDlyPk() == null || "".equals(curApply.getOrdLnDlyPk())) {
            curApply.setZbsFlag("3");
            updateZbsFlagNoPk(curApply, curApply.getBatchGroudNo(), b, st);
            return;
        }
    }

    private static void updateZbsFlagNoPk(QcmZgJugdeApplyEntity curApply, String groupNo,
                                          SqlBuilder b, Statement st) throws SQLException {
        if (curApply.getProcessCode().equals("D") || curApply.getProcessCode().equals("E")) {
            if (!curApply.getIsAllScrap().equals("4")) {
                b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
                b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
                b = new SqlBuilder("JdgMatBcBackup.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
            } else {
                b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
                b = new SqlBuilder("JdgMatZcM.setZbsFlagNoPk");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
            }
        } else {
            if (!curApply.getIsAllScrap().equals("4")) {
                b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
                b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
            } else {
                b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
                b = new SqlBuilder("JdgMatZcM.setZbsFlagNoPk");
                b.Fill(new String[]{curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, curApply.getZbsFlag()});
                st.executeUpdate(b.toString());
            }
        }
    }

    /**
     * 查询所有送判信息
     *
     * @param judgeStoveNo
     * @param processCode
     * @param con
     * @return
     */
    public static HashMap<String, QcmZgJugdeApplyEntity> getAllJudgeApply(String judgeApplyCode, String judgeStoveNo,
                                                                          String batchNo, String processCode, String heatPlanNo, HashMap<String, QcmZgJugdeApplyEntity> map,
                                                                          JudgeCraftParam zzbCp, Connection con) {
        if (map == null) {
            map = new HashMap<String, QcmZgJugdeApplyEntity>();
        }
        SqlBuilder b = null;
        if (judgeApplyCode.equals("")) {
            b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo2");
            b.Fill(new String[]{judgeStoveNo, batchNo, processCode, heatPlanNo});
        } else {
            b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
            b.Fill(new String[]{judgeApplyCode, judgeStoveNo, batchNo});
        }

        QcmZgJugdeApplyEntity apply = EntityTool.getEntityBySqlNoClsCon(
                b.toString(), con, QcmZgJugdeApplyEntity.class);
        if (apply != null) {
            map.put(apply.getProcessCode(), apply);
        }
        if (apply != null) {
            if ("6".equals(apply.getIsAllScrap()) && !"".equals(apply.getLoadBatchNo())) {
                return getAllJudgeApply("", apply.getLoadJudgeStoveNo(), apply.getLoadBatchNo(), "H", getUpperPlanId(apply.getHeatPlanNo(), processCode, b, con),
                        null, zzbCp, con);
            }
        }
        if (processCode.toCharArray()[0] <= 'D') {
            return map;
        } else {
            String loadJudgeStoveNo = "";
            String loadBatchNo = "";
            String upHeatPlanNo = "";
            if (apply == null) {
                loadJudgeStoveNo = judgeStoveNo;
                loadBatchNo = batchNo;
                upHeatPlanNo = heatPlanNo;
            } else {
                loadJudgeStoveNo = apply.getLoadJudgeStoveNo();
                loadBatchNo = apply.getLoadBatchNo();
                upHeatPlanNo = getUpperPlanId(apply.getHeatPlanNo(), processCode, b, con);
            }
            char cProcessCode = (char) (processCode.toCharArray()[0] - 1);
            return getAllJudgeApply("", loadJudgeStoveNo, loadBatchNo, String.valueOf(cProcessCode),
                    upHeatPlanNo, map, zzbCp, con);
        }
    }

    private static String getUpperPlanId(String headPlanNo, String processCode, SqlBuilder b, Connection con) {
        if (processCode.equals("D")) {
            b = new SqlBuilder("JdgPlnZyZgM.queryUpperByPk");
            b.Fill(new String[]{headPlanNo});
        } else if (processCode.equals("E")) {
            b = new SqlBuilder("JdgPlnZyDbkM.queryUpperByPk");
            b.Fill(new String[]{headPlanNo});
        } else if (processCode.equals("F")) {
            b = new SqlBuilder("JdgPlnZyRclM.queryUpperByPk");
            b.Fill(new String[]{headPlanNo});
        } else if (processCode.equals("G")) {
            b = new SqlBuilder("JdgPlnZyJgxM.queryUpperByPk");
            b.Fill(new String[]{headPlanNo});
        } else if (processCode.equals("H")) {
            b = new SqlBuilder("JdgPlnZyJgM.queryUpperByPk");
            b.Fill(new String[]{headPlanNo});
        } else {
            return headPlanNo;
        }

        PlnZyZgMEntity upper = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, PlnZyZgMEntity.class);
        if (upper == null) return "";
        if (processCode.equals(upper.getUpperProcessCode())) {
            return getUpperPlanId(upper.getUpperHeatplanId(), processCode, b, con);
        } else {
            return upper.getUpperHeatplanId();
        }
    }

    public static String getJudgeResult(HashMap<String, QcmZgJugdeApplyEntity> applyMap, String lgJudgeStoveNo,
                                        Connection con) throws Exception {
        String judgeResult = "40740701";
        if (applyMap.size() == 0) throw new Exception("该炉号判定记录异常！");
        for (String key : applyMap.keySet()) {
            QcmZgJugdeApplyEntity apply = applyMap.get(key);
            if (apply.getJudgeResultCode().equals("")) {
                judgeResult = apply.getJudgeResultCode();
            } else if (apply.getJudgeResultCode().equals("40740709") && !judgeResult.equals("")) {
                judgeResult = apply.getJudgeResultCode();
            }
        }

        for (String key : applyMap.keySet()) {
            QcmZgJugdeApplyEntity apply = applyMap.get(key);
            if (apply.getProcessCode().equals("D")) {
                SqlBuilder b = new SqlBuilder("QcmLgJudgeDAL.queryByJudgeStoveNo");
                b.Fill(new String[]{lgJudgeStoveNo, "B"});
                QcmLgJudgeEntity lgJudge = EntityTool.getEntityBySqlNoClsCon(
                        b.toString(), con, QcmLgJudgeEntity.class);
                if (lgJudge == null) {
                    b = new SqlBuilder("JdgQcmGpJudgeChange.getJudgeStoveNo");
                    b.Fill(new String[]{lgJudgeStoveNo, "B"});
                    QcmGpJudgeChangeEntity judgeChange = EntityTool.getEntityBySqlNoClsCon(
                            b.toString(), con, QcmGpJudgeChangeEntity.class);
                    if (judgeChange == null) {
                        //throw new Exception("找不到该判定炉号对应的炼钢判定信息！");
                        break;
                    }
                    b = new SqlBuilder("QcmLgJudgeDAL.queryByJudgeStoveNo");
                    b.Fill(new String[]{judgeChange.getJudgeStoveNo(), "B"});
                    lgJudge = EntityTool.getEntityBySqlNoClsCon(
                            b.toString(), con, QcmLgJudgeEntity.class);
                }

                if (lgJudge == null) break;
                if (lgJudge.getJudgeresult().equals("")) {
                    judgeResult = lgJudge.getJudgeresult();
                } else if (lgJudge.getJudgeresult().equals("40740704") && !judgeResult.equals("")) {
                    judgeResult = lgJudge.getJudgeresult();
                }
                break;
            }
        }
        return judgeResult;
    }

    private static void checkJudgeLock(SqlBuilder b, QcmZgJugdeApplyEntity zgJudgeApply,
                                       HashMap<String, QcmZgJugdeApplyEntity> applyMap, Connection con) throws Exception {
        for (String key : applyMap.keySet()) {
            QcmZgJugdeApplyEntity apply = applyMap.get(key);
            b = new SqlBuilder("JdgQcmJudgeLock.getLockInfo");
            String processCode = apply.getProcessCode();
            if (apply.getTarget().equals("A")) {
                processCode = "ZC";
            }
            b.Fill(new String[]{apply.getJudgeStoveNo(), apply.getBatchNo(), processCode,
                    "ZC"});
            QcmJudgeLockEntity lockEntity = EntityTool.getEntityBySqlNoClsCon(
                    b.toString(), con, QcmJudgeLockEntity.class);
            if (lockEntity != null) {
                throw new Exception("炉号：" + zgJudgeApply.getJudgeStoveNo() + " 批号：" + zgJudgeApply.getBatchNo()
                        + "已被锁定，暂时无法操作！\r\n锁定人：" + lockEntity.getLockName() + " 锁定原因：" + lockEntity.getLockReason());
            }
        }
    }

    public static void saveJudge(QcmZcJudgeEntity zcJudge, String type, SqlBuilder b, Connection con, Statement st)
            throws SQLException {
        if (type.equals("judge")) {
            b = new SqlBuilder("JdgQcmZcJudge.GetMaxPk1");
        } else {
            b = new SqlBuilder("JdgQcmZcJudge.GetMaxPk0");
        }
        GenericEntity judgeId = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
        zcJudge.setJudgeId(judgeId.getMaxValue());
        String insertZcJudge = CoreXmlSqlParsersFactory.getSql("JdgQcmZcJudge.Insert");
        insertZcJudge = EntityTool.fillSqlByEntity(insertZcJudge, zcJudge);
        st.executeUpdate(insertZcJudge);
    }

    public static String GetBalYear(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyyMM", Locale.CHINA);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
     // 天淮结转年月 22日21小时
        cal.add(Calendar.HOUR, 3);
        cal.add(Calendar.DATE, -22);
        cal.add(Calendar.MONTH, 1);
        return dateFormat.format(cal.getTime());
    }

}
