package com.steering.mes.mcp.heatTreatment;

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.mcp.entity.QcmZgJugdeApplyEntity;

public class FrmHttHandleJuge extends DBCommon {

    /***
     * 查询申判数据
     * @param list
     * @param startTim
     * @param endTim
     * @param valueFlag
     * @return
     */
    public CoreReturnObject doQueryApplyData(ArrayList<String> list,
            String startTim, String endTim, String valueFlag,String proCess) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmHttHandleJuge.doQueryApplyData");
        sql+=" AND T.PROCESS_CODE = '"+proCess+"'";
        if (!list.get(0).equals("")) {
            sql += " AND T.JUDGE_STOVE_NO = '" + list.get(0) + "'";
        }
        if (!startTim.equals("")) {
            sql += " AND T.APPLY_TIME >= TO_DATE('"
                    + startTim
                    + "','YYYY-MM-DD HH24:MI:SS') AND T.APPLY_TIME <= TO_DATE('"
                    + endTim + "','YYYY-MM-DD HH24:MI:SS') ";
        }
        if (!valueFlag.equals("")) {
            sql += " AND T.VALIDFLAG = '" + valueFlag + "'";
        }
        return this.getData(sql);
    }

    /***
     * 进行手工判定
     * @param parm
     * @return
     */
    public CoreReturnObject doHandleApply(ArrayList<String> parm,String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String yearMoth = df.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        String valueFlag = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateApplyResultSql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttHandleJuge.doHandleApply");// 手工判定
            PreparedStatement updateApplyResultPsta = connection
                    .prepareStatement(updateApplyResultSql);
            for (String str : parm) {
                QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool
                        .getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttHandleJuge.SelectApplyData");// 查询否已判或者申判信息是否还在
                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
                psta = connection.prepareStatement(sql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    valueFlag = rs.getString("VALIDFLAG");
                }
                rs.close();
                psta.close();
                if (!valueFlag.equals("00")) {
                    cro.setV_errMsg("该炉信息已不是申判初始状态/或者已撤销申请判定，不能进行手工判定!");
                    return cro;
                }
                updateApplyResultPsta.setString(1, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateApplyResultPsta.setString(2, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateApplyResultPsta.setString(3, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateApplyResultPsta.setString(4, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateApplyResultPsta.setString(5, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateApplyResultPsta.setString(6, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                updateApplyResultPsta.setString(7, "40740701");
                if (qcmZgJugdeApplyTity.getIsendgx().equals("0")) {
                    updateApplyResultPsta.setString(8, "80150102");//半成品已判
                }
                else {
                    updateApplyResultPsta.setString(8, "80150202");// 成品已判
                }
                updateApplyResultPsta.setString(9, "40740701");
                updateApplyResultPsta.setString(10, "合同内合格");
                updateApplyResultPsta.setString(11, name);
                updateApplyResultPsta.setString(12, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateApplyResultPsta.setString(13, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateApplyResultPsta.setString(14, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateApplyResultPsta.setString(15, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateApplyResultPsta.setString(16, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateApplyResultPsta.setString(17, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                updateApplyResultPsta.addBatch();
            }
            updateApplyResultPsta.executeBatch();
            cro.setV_errMsg("判定成功!");
            connection.commit();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /***
     * 撤销手工判定
     * @param parm
     * @return
     */
    public CoreReturnObject doCancelHandleApply(ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String yearMoth = df.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        String valueFlag = "";
        int acount = 0;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateCancelApplyResultSql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttHandleJuge.doCancelHandleApply");// 撤销手工判定
            PreparedStatement updateCancelApplyResultPsta = connection
                    .prepareStatement(updateCancelApplyResultSql);
            for (String str : parm) {
                QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool
                        .getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttHandleJuge.SelectApplyData");// 查询否已判或者申判信息是否还在
                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
                psta = connection.prepareStatement(sql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    valueFlag = rs.getString("VALIDFLAG");
                }
                rs.close();
                psta.close();
                if (!valueFlag.equals("20")) {
                    cro.setV_errMsg("该炉信息不是已判定状态，不能进行判定撤销!");
                    return cro;
                }

//                sql = CoreXmlSqlParsersFactory
//                        .getSql("FrmHttHandleJuge.SelectDBKStovePlan");// 查询该炉信息是否已生成镦拔扩炉计划
//                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
//                psta = connection.prepareStatement(sql);
//                rs = psta.executeQuery();
//                if (rs.next()) {
//                    acount = Integer.parseInt(rs.getString("ACOUNT"));
//                }
//                rs.close();
//                psta.close();
//                if (acount > 0) {
//                    cro.setV_errMsg("该炉信息已生成镦拔扩炉计划，不能进行判定撤销!");
//                    return cro;
//                }

                updateCancelApplyResultPsta.setString(1, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateCancelApplyResultPsta.setString(2, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateCancelApplyResultPsta.setString(3, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateCancelApplyResultPsta.setString(4, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateCancelApplyResultPsta.setString(5, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateCancelApplyResultPsta.setString(6, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                if (qcmZgJugdeApplyTity.getIsendgx().equals("0")) {
                    updateCancelApplyResultPsta.setString(7, "80150105");//半成品已判
                }
                else {
                    updateCancelApplyResultPsta.setString(7, "80150201");// 成品已判
                }
                updateCancelApplyResultPsta.setString(8, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateCancelApplyResultPsta.setString(9, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateCancelApplyResultPsta.setString(10, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateCancelApplyResultPsta.setString(11, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateCancelApplyResultPsta.setString(12, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateCancelApplyResultPsta.setString(13, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                updateCancelApplyResultPsta.addBatch();
            }
            updateCancelApplyResultPsta.executeBatch();
            cro.setV_errMsg("撤销判定成功!");
            connection.commit();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /***
     * 对判定信息进行确认
     * @param parm
     * @return
     */
    public CoreReturnObject doComfirm(ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String yearMoth = df.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        String valueFlag = "";
        String valueFlag1 = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateComfirmApplyResultSql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttHandleJuge.doComfirm");// 确认判定信息
            PreparedStatement updateComfirmApplyResultPsta = connection
                    .prepareStatement(updateComfirmApplyResultSql);
            for (String str : parm) {
                QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool
                        .getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttHandleJuge.SelectApplyData");// 查询否已判或者申判信息是否还在
                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
                psta = connection.prepareStatement(sql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    valueFlag1 = rs.getString("VALIDFLAG");
                }
                rs.close();
                psta.close();
                if (!valueFlag1.equals("20")) {
                    cro.setV_errMsg("该炉信息未进行判定， 不能进行缴库确认!");
                    return cro;
                }

                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttHandleJuge.SelectComfirmApplyData");// 查询否已判或者申判信息是否还在
                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
                psta = connection.prepareStatement(sql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    valueFlag = rs.getString("INSTOCK_FLAG");
                }
                rs.close();
                psta.close();
                if (valueFlag.equals("1")) {
                    cro.setV_errMsg("该炉信息已进行了缴库确认，不能进行重复缴库确认!");
                    return cro;
                }
                updateComfirmApplyResultPsta.setString(1, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateComfirmApplyResultPsta.setString(2, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateComfirmApplyResultPsta.setString(3, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateComfirmApplyResultPsta.setString(4, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateComfirmApplyResultPsta.setString(5, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateComfirmApplyResultPsta.setString(6, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                updateComfirmApplyResultPsta.setString(7, qcmZgJugdeApplyTity
                        .getJudgeStoveNo());
                updateComfirmApplyResultPsta.setString(8, qcmZgJugdeApplyTity
                        .getBatchNo());
                updateComfirmApplyResultPsta.setString(9, qcmZgJugdeApplyTity
                        .getBatchGroudNo());
                updateComfirmApplyResultPsta.setString(10, qcmZgJugdeApplyTity
                        .getQualityGroup());
                updateComfirmApplyResultPsta.setString(11, qcmZgJugdeApplyTity
                        .getMatGowhereCode());
                updateComfirmApplyResultPsta.setString(12, qcmZgJugdeApplyTity
                        .getJugdeApplyCode());
                updateComfirmApplyResultPsta.addBatch();
            }
            updateComfirmApplyResultPsta.executeBatch();
            cro.setV_errMsg("确认成功!");
            connection.commit();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /***
     * 对判定信息取消确认
     * @param parm
     * @return
     */
    public CoreReturnObject doCancelComfirm(ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String yearMoth = df.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        String valueFlag = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateCancelComfirmApplyResultSql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttHandleJuge.doCancelComfirm");// 取消判定信息的确认
            PreparedStatement updateCancelComfirmApplyResultPsta = connection
                    .prepareStatement(updateCancelComfirmApplyResultSql);
            for (String str : parm) {
                QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool
                        .getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttHandleJuge.SelectComfirmApplyData");// 查询否已判或者申判信息是否还在
                sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
                psta = connection.prepareStatement(sql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    valueFlag = rs.getString("INSTOCK_FLAG");
                }
                rs.close();
                psta.close();
                if (!valueFlag.equals("1")) {
                    cro.setV_errMsg("该炉信息还未进行缴库确认，不能进行缴库确认撤销!");
                    return cro;
                }
                updateCancelComfirmApplyResultPsta.setString(1,
                        qcmZgJugdeApplyTity.getJudgeStoveNo());
                updateCancelComfirmApplyResultPsta.setString(2,
                        qcmZgJugdeApplyTity.getBatchNo());
                updateCancelComfirmApplyResultPsta.setString(3,
                        qcmZgJugdeApplyTity.getBatchGroudNo());
                updateCancelComfirmApplyResultPsta.setString(4,
                        qcmZgJugdeApplyTity.getQualityGroup());
                updateCancelComfirmApplyResultPsta.setString(5,
                        qcmZgJugdeApplyTity.getMatGowhereCode());
                updateCancelComfirmApplyResultPsta.setString(6,
                        qcmZgJugdeApplyTity.getJugdeApplyCode());
                updateCancelComfirmApplyResultPsta.setString(7,
                        qcmZgJugdeApplyTity.getJudgeStoveNo());
                updateCancelComfirmApplyResultPsta.setString(8,
                        qcmZgJugdeApplyTity.getBatchNo());
                updateCancelComfirmApplyResultPsta.setString(9,
                        qcmZgJugdeApplyTity.getBatchGroudNo());
                updateCancelComfirmApplyResultPsta.setString(10,
                        qcmZgJugdeApplyTity.getQualityGroup());
                updateCancelComfirmApplyResultPsta.setString(11,
                        qcmZgJugdeApplyTity.getMatGowhereCode());
                updateCancelComfirmApplyResultPsta.setString(12,
                        qcmZgJugdeApplyTity.getJugdeApplyCode());
                updateCancelComfirmApplyResultPsta.addBatch();
            }
            updateCancelComfirmApplyResultPsta.executeBatch();
            cro.setV_errMsg("撤销成功!");
            connection.commit();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }
}
