package com.steering.mes.mcp.Mch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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;


/***
 * 委外审批管理
 * @author TJG 2016/09/27
 */
public class FrmMchSourcApp extends DBCommon {
    /**
     * 委外实绩查询
     * @param list
     * @param startTim
     * @param endTim
     * @return
     */
    public CoreReturnObject doQuery(ArrayList<String> list) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory.getSql("FrmMchSourcApp.doQueryOutSourc");
        if (!list.get(0).equals("")) {
            sql += "  AND T.SUBCONTRACT_TIME BETWEEN TO_DATE('" + list.get(0)
                    + "','YYYY-MM-DD HH24:MI:SS') " + " AND TO_DATE('"
                    + list.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
        }
        if (!list.get(2).equals("")) {
            sql += " AND T.JUDGE_STOVE_NO || '&' LIKE '%' || '" + list.get(2)
                    + "' || '%'";
        }
        if (!list.get(3).equals("")) {
            sql += " AND T.ZY_BATCH_ID || '&' LIKE '%' || '" + list.get(3)
                    + "' || '%'";
        }
        if (!list.get(4).equals("")) {
            sql += " AND T.SUBCONTRACT_LOG ='" + list.get(4) + "'";
        }
        return this.getData(sql);
    }

    /**
     * 查询产线
     * @param list
     * @return
     */
    public CoreReturnObject doQueryPlineCode(String plineCode) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchSourcApp.doQueryPlineCode");
        sql += "  ORDER BY T.PLINE_NAME ASC";
        return this.getData(sql);
    }

    /**
     *委外审批通过
     * @param result
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doAppThrough(ArrayList<Object> parm) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String updateSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmMchSourcApp.UpdateOutSourc");// 修改审批状态
            PreparedStatement pstaUpdateSourc = con
                    .prepareStatement(updateSourc);

            for (int i = 0; i < parm.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) parm.get(i);
                String spNot = list.get(0);// 审批说明
                String plineCode = list.get(1);
                String plineName = list.get(2);
                String subNo = list.get(3);// 委托单号
                String subNoSeq = list.get(4);// 委托单序号
                String name = list.get(5);
                String groupStart = "";// 状态
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmMchSourcApp.QuerySubConStart");// 查询委托状态
                psta = con.prepareStatement(sql);
                psta.setString(1, subNo);
                psta.setString(2, subNoSeq);
                rs = psta.executeQuery();
                if (rs.next()) {
                    groupStart = rs.getString("SUBCONTRACT_LOG");
                }
                rs.close();
                psta.close();
                if (groupStart.equals("")) {
                    cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
                    return cor;
                }
                else if (!groupStart.equals("00")) {
                    cor.setV_errMsg("选择委外单不是委外申请状态,不允许此操作!");
                    return cor;
                }
                // 修改审批状态
                pstaUpdateSourc.setObject(1, name);
                pstaUpdateSourc.setObject(2, systime);
                pstaUpdateSourc.setObject(3, "10");
                pstaUpdateSourc.setObject(4, plineCode);
                pstaUpdateSourc.setObject(5, plineName);
                pstaUpdateSourc.setObject(6, spNot);
                pstaUpdateSourc.setObject(7, subNo);
                pstaUpdateSourc.setObject(8, subNoSeq);
                pstaUpdateSourc.addBatch();

            }
            pstaUpdateSourc.executeBatch();

            cor.setV_errMsg("审批通过成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     *委外审批不通过
     * @param result
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doAppNotThrough(ArrayList<Object> parm) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String updateSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmMchSourcApp.UpdateOutSourc");// 修改审批状态
            PreparedStatement pstaUpdateSourc = con
                    .prepareStatement(updateSourc);
            String updateOff = CoreXmlSqlParsersFactory
                    .getSql("FrmMchSourcApp.UpdateOff");// 释放离线表委外支吨
            PreparedStatement pstaUpdatOff = con.prepareStatement(updateOff);
            for (int i = 0; i < parm.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) parm.get(i);
                String spNot = list.get(0);// 审批说明
                String plineCode = list.get(1);
                String plineName = list.get(2);
                String subNo = list.get(3);// 委托单号
                String subNoSeq = list.get(4);// 委托单序号
                String name = list.get(5);
                String resultNo = list.get(8);
                String groupStart = "";// 状态
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmMchSourcApp.QuerySubConStart");// 查询委托状态
                psta = con.prepareStatement(sql);
                psta.setString(1, subNo);
                psta.setString(2, subNoSeq);
                rs = psta.executeQuery();
                if (rs.next()) {
                    groupStart = rs.getString("SUBCONTRACT_LOG");
                }
                rs.close();
                psta.close();
                if (groupStart.equals("")) {
                    cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
                    return cor;
                }
                else if (!groupStart.equals("00")) {
                    cor.setV_errMsg("选择委外单不是委外申请状态,不允许此操作!");
                    return cor;
                }
                // 修改审批状态
                pstaUpdateSourc.setObject(1, name);
                pstaUpdateSourc.setObject(2, systime);
                pstaUpdateSourc.setObject(3, "20");
                pstaUpdateSourc.setObject(4, plineCode);
                pstaUpdateSourc.setObject(5, plineName);
                pstaUpdateSourc.setObject(6, spNot);
                pstaUpdateSourc.setObject(7, subNo);
                pstaUpdateSourc.setObject(8, subNoSeq);
                pstaUpdateSourc.addBatch();
                // 释放离线表委外支吨
                pstaUpdatOff.setObject(1, "0");
                pstaUpdatOff.setObject(2, "0");
                pstaUpdatOff.setObject(3, systime);
                pstaUpdatOff.setObject(4, name);
                pstaUpdatOff.setObject(5, resultNo);
                pstaUpdatOff.addBatch();
            }
            pstaUpdateSourc.executeBatch();
            pstaUpdatOff.executeBatch();
            cor.setV_errMsg("审批不通过成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     *委外回报
     * @param result
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doOutsourcReturn(ArrayList<Object> parm) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
//        Date now = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat(
//                "yyyy-MM-dd HH:mm:ss");
//        String systime = dateFormat.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String updateSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmMchSourcApp.UpdateOutSourcNum");// 修改委外回报量
            PreparedStatement pstaUpdateSourc = con
                    .prepareStatement(updateSourc);
            String updateOff = CoreXmlSqlParsersFactory
                    .getSql("FrmMchSourcApp.UpdateOffSourc");// 修改离线表委外回报
            PreparedStatement pstaUpdatOff = con.prepareStatement(updateOff);
            for (int i = 0; i < parm.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) parm.get(i);
                String hgCounNum = list.get(0);//委外回报合格支
                String hgCounWt = list.get(1);//委外回报合格吨
                String fpCounNum = list.get(2);//委外回报废品支
                String fpCounWt = list.get(3);//委外回报废品吨
                String subNo = list.get(4);// 委托单号
                String subNoSeq = list.get(5);// 委托单序号
//                String name = list.get(7);
                String resultNo = list.get(9);
                String groupStart = "";// 状态
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmMchSourcApp.QuerySubConStart");// 查询委托状态
                psta = con.prepareStatement(sql);
                psta.setString(1, subNo);
                psta.setString(2, subNoSeq);
                rs = psta.executeQuery();
                if (rs.next()) {
                    groupStart = rs.getString("SUBCONTRACT_LOG");
                }
                rs.close();
                psta.close();
                if (groupStart.equals("")) {
                    cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
                    return cor;
                }
                else if (!groupStart.equals("10")) {
                    cor.setV_errMsg("选择委外单不是审批通过状态,不允许此操作!");
                    return cor;
                }
                // 修改委外回报量
                pstaUpdateSourc.setObject(1, hgCounNum);
                pstaUpdateSourc.setObject(2, hgCounWt);
                pstaUpdateSourc.setObject(3, fpCounNum);
                pstaUpdateSourc.setObject(4, fpCounWt);
                pstaUpdateSourc.setObject(5, subNo);
                pstaUpdateSourc.setObject(6, subNoSeq);
                pstaUpdateSourc.addBatch();
                // 修改离线表委外回报
                pstaUpdatOff.setObject(1, hgCounNum);
                pstaUpdatOff.setObject(2, hgCounWt);
                pstaUpdatOff.setObject(3, fpCounNum);
                pstaUpdatOff.setObject(4, fpCounWt);
                pstaUpdatOff.setObject(5, resultNo);
                pstaUpdatOff.addBatch();
            }
            pstaUpdateSourc.executeBatch();
            pstaUpdatOff.executeBatch();
            cor.setV_errMsg("委外回报成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }
}
