package com.steering.mes.mcp.coup;

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.Calendar;
import java.util.Date;
import java.util.Locale;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.PlnZyJgEntity;

/**
 * 委托管理
 * @author tjg 2016-7-12
 */
public class FrmOutManagement extends DBCommon {

    /**
     *查询委托单信息
     * @param planNo
     * @return
     */
    public CoreReturnObject geQueryEntrusted(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmOutManagement.geQueryEntrusted");
        return this.getData(sql, list.toArray());
    }

    /**
     *查询工单离线信息
     * @param planNo
     * @return
     */
    public CoreReturnObject geQueryPlnJgBill(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmOutManagement.geQueryPlnJgBill");
        return this.getData(sql, list.toArray());
    }

    /***
     * 生成委托单
     * @param plineCode
     * @param zygNo
     * @param connection
     * @return
     */
    public String doAddResultNo(String sysDate, Connection connection) {
        String resultNo = "";
        PreparedStatement psta = null;
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmOutManagement.QueryMaxEnt");// 查询最大委托单号
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
        try {
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, matter1.format(dt));
            psta.setObject(2, sysDate);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                resultNo = rs.getString("ENTRUSTED_NO");
            }
            rs.close();
            psta.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return resultNo;
    }

    /**
     * 新增委托单
     * @param r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addEntrustedResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        String systime = dateFormat.format(now);
        String sysDate=matter.format(now);
//        String sql = "";
//        PreparedStatement psta = null;
//        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.AddPlnBillResult");// 新增委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            
            String entNo = doAddResultNo(sysDate, con);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                String cutNum=list.get(2);//切断
                String cjgNum=list.get(3);//粗加工
                String impNum=list.get(4);//压印
                String latNum=list.get(5);//车丝
                String thrNum=list.get(6);//螺纹检验
                String detNum=list.get(7);//探伤
                String blaNum=list.get(8);//喷砂
                String pltNum=list.get(9);//镀铜
                String phoNum=list.get(10);//磷化
                String copNum=list.get(11);//喷标
                String plnNum=list.get(12);//离线支
                String bc = list.get(14);
                String bz = list.get(15);
                String user = list.get(13);
                // 获取计划实体了
                PlnZyJgEntity pln = PlanService.getSendPlanCInfo(con, plnNo);
                if (pln == null) {
                    cor.setV_errMsg("无工单信息");
                    return cor;
                }
//                if (pln.getZyPlanStatus().equals("0")) {
//                    cor.setV_errMsg("炉次计划未下发！");
//                    return cor;
//                }
//                if (pln.getZyPlanStatus().equals("3")) {
//                    cor.setV_errMsg("炉次计划已生产完成！");
//                    return cor;
//                }
//                if (pln.getZyPlanStatus().equals("5")) {
//                    cor.setV_errMsg("炉次计划已取消！");
//                    return cor;
//                }
//                // 查询计划重量
//                double weghit = 0;
//                sql = CoreXmlSqlParsersFactory
//                        .getSql("FrmOutManagement.QueryMatWeghit");
//                psta = con.prepareStatement(sql);
//                psta.setObject(1, judNo);
//                psta.setObject(2, batch);
//                psta.setObject(3, count + 1);
//                rs = psta.executeQuery();
//                if (rs.next()) {
//                    weghit = Double.parseDouble(rs.getString("ACT_WEIGHT"));
//                }
//                rs.close();
//                psta.close();
                // 新增锯切实绩 
                insertResultPsta.setObject(1, entNo);
                insertResultPsta.setObject(2, pln.getPlineCode());
                insertResultPsta.setObject(3, plnNo);
                insertResultPsta.setObject(4, woId);
                insertResultPsta.setObject(5, pln.getJudgeStoveNo());
                insertResultPsta.setObject(6, pln.getBatchNo());
                insertResultPsta.setObject(7, plnNum);
                insertResultPsta.setObject(8, cutNum);
                insertResultPsta.setObject(9, impNum);
                insertResultPsta.setObject(10, cjgNum);
                insertResultPsta.setObject(11, latNum);
                insertResultPsta.setObject(12, phoNum);
                insertResultPsta.setObject(13, blaNum);
                insertResultPsta.setObject(14, thrNum);
                insertResultPsta.setObject(15, pltNum);
                insertResultPsta.setObject(16, copNum);
                insertResultPsta.setObject(17, detNum);
                insertResultPsta.setObject(18, "1");
                insertResultPsta.setObject(19, "");
                insertResultPsta.setObject(20, pln.getCoatType());
                insertResultPsta.setObject(21, "1");
                insertResultPsta.setObject(22, systime);
                insertResultPsta.setObject(23, user);
                insertResultPsta.setObject(24, bc);
                insertResultPsta.setObject(25, bz);
                insertResultPsta.setObject(26, systime);
                insertResultPsta.addBatch();
            }
            insertResultPsta.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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject deleteEntrustedResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.DeletePlnBillResult");// 删除委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                String entr = list.get(2);
               
                // 查询委托单状态
                String entrStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                psta.setObject(3, entr);
                rs = psta.executeQuery();
                if (rs.next()) {
                    entrStrat = rs.getString("ENTRUSTED_LOG");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!entrStrat.equals("1")) {
                    cor.setV_errMsg("该委托单不是编辑状态,不允许删除！");
                    return cor;
                }
                insertResultPsta.setObject(1, plnNo);
                insertResultPsta.setObject(2, woId);
                insertResultPsta.setObject(3, entr);
                insertResultPsta.addBatch();
                insertResultPsta.executeBatch();

//                // 根据炉次计划号查询委托单表是否还有数据
//                int count = 0;
//                sql = CoreXmlSqlParsersFactory
//                        .getSql("FrmOutManagement.QueryPlnBillCount");
//                psta = con.prepareStatement(sql);
//                psta.setObject(1, plnNo);
//                rs = psta.executeQuery();
//                if (rs.next()) {
//                    count = Integer.parseInt(rs.getString("COUNT"));
//                }
//                rs.close();
//                psta.close();
//
//                if (count == 0) {
//                }
            }
            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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject commEntrustedResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.UpdatePlnBillStrat");// 委托申请委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                String entr = list.get(2);
                // 查询委托单状态
                String entrStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                psta.setObject(3, entr);
                rs = psta.executeQuery();
                if (rs.next()) {
                    entrStrat = rs.getString("ENTRUSTED_LOG");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!entrStrat.equals("1")) {
                    cor.setV_errMsg("该委托单不是编辑状态,不允许委托申请！");
                    return cor;
                }
                if (entrStrat.equals("2")) {
                    cor.setV_errMsg("该委托单已委托申请！");
                    return cor;
                }
                insertResultPsta.setObject(1, "2");
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, woId);
                insertResultPsta.setObject(4, entr);
                insertResultPsta.addBatch();
            }
            insertResultPsta.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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject kownCommEntrustedResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.UpdatePlnBillStrat");// 下发委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                String entr = list.get(2);
                // 查询委托单状态
                String entrStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                psta.setObject(3, entr);
                rs = psta.executeQuery();
                if (rs.next()) {
                    entrStrat = rs.getString("ENTRUSTED_LOG");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!entrStrat.equals("2")) {
                    cor.setV_errMsg("该委托单不是委托申请状态,不允许撤销！");
                    return cor;
                }
                insertResultPsta.setObject(1, "1");
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, woId);
                insertResultPsta.setObject(4, entr);
                insertResultPsta.addBatch();
            }
            insertResultPsta.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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject closePlnBillResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.UpdateClosePlnBillStrat");// 下发委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            String updatepln = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.updatePln");// 修改计划表状态
            PreparedStatement updateplnPsta = con.prepareStatement(updatepln);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                // 查询委托单状态
                String zyBillStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                rs = psta.executeQuery();
                if (rs.next()) {
                    zyBillStrat = rs.getString("ZY_BILL_STATUS");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!zyBillStrat.equals("3")) {
                    cor.setV_errMsg("该委托单不是生产完成状态,不允许关闭！");
                    return cor;
                }
                insertResultPsta.setObject(1, "4");
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, woId);
                insertResultPsta.addBatch();
                insertResultPsta.executeBatch();
                // 根据炉次计划号查询委托单表是否还有数据
                int count = 0;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillCount1");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                rs = psta.executeQuery();
                if (rs.next()) {
                    count = Integer.parseInt(rs.getString("COUNT"));
                }
                rs.close();
                psta.close();

                if (count == 0) {
                    updateplnPsta.setObject(1, "3");
                    updateplnPsta.setObject(2, plnNo);
                    updateplnPsta.addBatch();
                    updateplnPsta.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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject offPlnBillResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.UpdateOffPlnBillStrat");// 离线委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                // 查询委托单状态
                String zyBillStrat = "";
                String ifWorkOff = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                rs = psta.executeQuery();
                if (rs.next()) {
                    zyBillStrat = rs.getString("ZY_BILL_STATUS");
                    ifWorkOff = rs.getString("IF_ALL_OUT_WORK");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!zyBillStrat.equals("0")) {
                    cor.setV_errMsg("该委托单不是编辑状态,不允许离线！");
                    return cor;
                }
                if (ifWorkOff.equals("1")) {
                    cor.setV_errMsg("该委托单已离线！");
                    return cor;
                }
                insertResultPsta.setObject(1, "1");
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, woId);
                insertResultPsta.addBatch();
            }
            insertResultPsta.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 r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject downOffPlnBillResult(ArrayList<Object> pram) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmOutManagement.UpdateOffPlnBillStrat");// 离线委托单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String plnNo = list.get(0);
                String woId = list.get(1);
                // 查询委托单状态
                String zyBillStrat = "";
                String ifWorkOff = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmOutManagement.QueryPlnBillStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                rs = psta.executeQuery();
                if (rs.next()) {
                    zyBillStrat = rs.getString("ZY_BILL_STATUS");
                    ifWorkOff = rs.getString("IF_ALL_OUT_WORK");
                }
                else {
                    cor.setV_errMsg("该委托单已删除,请刷新！");
                    return cor;
                }
                rs.close();
                psta.close();

                if (!zyBillStrat.equals("0")) {
                    cor.setV_errMsg("该委托单不是编辑状态,不允许撤销离线！");
                    return cor;
                }
                if (ifWorkOff.equals("0")) {
                    cor.setV_errMsg("该委托单已撤销离线！");
                    return cor;
                }
                insertResultPsta.setObject(1, "0");
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, woId);
                insertResultPsta.addBatch();
            }
            insertResultPsta.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;
    }
}
