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-11
 */
public class FrmPlnZyJgWorkBill extends DBCommon {

    /**
     *查询炉计划
     * @param planNo
     * @return
     */
    public CoreReturnObject geQueryPlnJg(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPlnZyJgWorkBill.geQueryPlnJg");
        return this.getData(sql, list.toArray());
    }

    /**
     *查询工单信息
     * @param planNo
     * @return
     */
    public CoreReturnObject geQueryPlnJgBill(String plnNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPlnZyJgWorkBill.geQueryPlnJgBill");
        return this.getData(sql, new Object[] { plnNo });
    }

    /***
     * 生成工单号
     * @param plineCode
     * @param zygNo
     * @param connection
     * @return
     */
    public String doAddResultNo(String zygNo, Connection connection) {
        String resultNo = "";
        PreparedStatement psta = null;
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmPlnZyJgWorkBill.QueryMatBill");// 查询最大工单号
        try {
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, zygNo);
            psta.setObject(2, zygNo);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                resultNo = rs.getString("WO_ID");
            }
            rs.close();
            psta.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return resultNo;
    }

    /**
     * 新增工单
     * @param r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addPlnBillResult(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);
        String systime = dateFormat.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.AddPlnBillResult");// 新增工单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            String updatepln = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.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);
                int count = Integer.parseInt(list.get(1));// 可编辑支数
                String bc = list.get(2);
                String bz = list.get(3);
                String user = list.get(4);
                String judNo = list.get(5);
                String batch = list.get(6);
                // 获取计划实体了
                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("FrmPlnZyJgWorkBill.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();
                // 新增锯切实绩
                String woId = doAddResultNo(plnNo, con);
                insertResultPsta.setObject(1, woId);
                insertResultPsta.setObject(2, plnNo);
                insertResultPsta.setObject(3, judNo);
                insertResultPsta.setObject(4, batch);
                insertResultPsta.setObject(5, "");
                insertResultPsta.setObject(6, pln.getPlineCode());
                insertResultPsta.setObject(7, pln.getPlineName());
                insertResultPsta.setObject(8, pln.getIfNeedCjg());
                insertResultPsta.setObject(9, pln.getIfImpress());
                insertResultPsta.setObject(10, pln.getIfBlast());
                insertResultPsta.setObject(11, pln.getIfPlateCu());
                insertResultPsta.setObject(12, pln.getIfPhosphating());
                insertResultPsta.setObject(13, pln.getCoatType());
                insertResultPsta.setObject(14, list.get(7));// 计划个数
                insertResultPsta.setObject(15, list.get(8));// 测算个数
                insertResultPsta.setObject(16, list.get(9));// 需求个数
                insertResultPsta.setObject(17, pln.getCutMachGrpNo());
                insertResultPsta.setObject(18, pln.getCutMachGrpNm());
                insertResultPsta.setObject(19, pln.getDemandCopper());
                insertResultPsta.setObject(20, pln.getLatheGrpNo());
                insertResultPsta.setObject(21, pln.getLatheGrpNm());
                insertResultPsta.setObject(22, pln.getDemandDetection());
                insertResultPsta.setObject(23, pln.getDemandBlast());
                insertResultPsta.setObject(24, pln.getDemandBonderizing());
                insertResultPsta.setObject(25, "");
                insertResultPsta.setObject(26, user);
                insertResultPsta.setObject(27, systime);
                insertResultPsta.setObject(28, bc);
                insertResultPsta.setObject(29, bz);
                insertResultPsta.setObject(30, count);
                insertResultPsta.setObject(31, weghit);
                insertResultPsta.setObject(32, list.get(7));
                insertResultPsta.setObject(33, list.get(7));
                insertResultPsta.setObject(34, list.get(7));
                insertResultPsta.setObject(35, list.get(7));
                insertResultPsta.setObject(36, list.get(7));
                insertResultPsta.setObject(37, list.get(7));
                insertResultPsta.setObject(38, list.get(7));
                insertResultPsta.setObject(39, list.get(7));
                insertResultPsta.setObject(40, list.get(7));
                insertResultPsta.setObject(41, list.get(7));
                
                insertResultPsta.addBatch();

                updateplnPsta.setObject(1, "2");
                updateplnPsta.setObject(2, plnNo);
                updateplnPsta.addBatch();
            }
            insertResultPsta.executeBatch();
            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 deletePlnBillResult(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("FrmPlnZyJgWorkBill.DeletePlnBillResult");// 删除工单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            String updatepln = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.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);
                // 获取计划实体了
                PlnZyJgEntity pln = PlanService.getSendPlanCInfo(con, plnNo);
                if (pln == null) {
                    cor.setV_errMsg("无炉次计划信息");
                    return cor;
                }
                // 查询工单状态
                String zyBillStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.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("0")) {
                    cor.setV_errMsg("该工单不是编辑状态,不允许删除！");
                    return cor;
                }
                insertResultPsta.setObject(1, plnNo);
                insertResultPsta.setObject(2, woId);
                insertResultPsta.addBatch();
                insertResultPsta.executeBatch();

                // 根据炉次计划号查询工单表是否还有数据
                int count = 0;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.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) {
                    updateplnPsta.setObject(1, "1");
                    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 undPlnBillResult(ArrayList<Object> pram) {
        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", Locale.CHINA);
        String systime = dateFormat.format(now);
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertResultsql = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.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 user = list.get(2);
                // 查询工单状态
                String zyBillStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.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("0")) {
                    cor.setV_errMsg("该工单不是编辑状态,不允许下发！");
                    return cor;
                }if (zyBillStrat.equals("1")) {
                    cor.setV_errMsg("该工单已下发！");
                    return cor;
                }
                insertResultPsta.setObject(1, "1");
                insertResultPsta.setObject(2, user);
                insertResultPsta.setObject(3, systime);
                insertResultPsta.setObject(4, plnNo);
                insertResultPsta.setObject(5, 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 downPlnBillResult(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("FrmPlnZyJgWorkBill.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 zyBillStrat = "";
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.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("1")) {
                    cor.setV_errMsg("该工单不是下发状态,不允许撤销！");
                    return cor;
                }
                insertResultPsta.setObject(1, "0");
                insertResultPsta.setObject(2, "");
                insertResultPsta.setObject(3, "");
                insertResultPsta.setObject(4, plnNo);
                insertResultPsta.setObject(5, 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 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("FrmPlnZyJgWorkBill.UpdateClosePlnBillStrat");// 关闭工单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            String updatepln = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.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("FrmPlnZyJgWorkBill.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, list.get(2));
                insertResultPsta.setObject(3, list.get(3));
                insertResultPsta.setObject(4, plnNo);
                insertResultPsta.setObject(5, woId);
                insertResultPsta.addBatch();
                insertResultPsta.executeBatch();
                // 根据炉次计划号查询工单表是否还有数据
                int count = 0;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.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 downClosePlnBillResult(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("FrmPlnZyJgWorkBill.UpdateClosePlnBillStrat");// 撤销关闭工单
            PreparedStatement insertResultPsta = con
                    .prepareStatement(insertResultsql);
            String updatepln = CoreXmlSqlParsersFactory
                    .getSql("FrmPlnZyJgWorkBill.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("FrmPlnZyJgWorkBill.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("4")) {
                    cor.setV_errMsg("该工单不是关闭状态,不允许撤销！");
                    return cor;
                }
                insertResultPsta.setObject(1, "3");
                insertResultPsta.setObject(2, list.get(2));
                insertResultPsta.setObject(3, list.get(3));
                insertResultPsta.setObject(4, plnNo);
                insertResultPsta.setObject(5, woId);
                insertResultPsta.addBatch();
                insertResultPsta.executeBatch();
                // 根据炉次计划号查询工单表是否还有数据
                int count = 0;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.QueryPlnBillCount2");
                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, "2");
                    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("FrmPlnZyJgWorkBill.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("FrmPlnZyJgWorkBill.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("FrmPlnZyJgWorkBill.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("FrmPlnZyJgWorkBill.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;
                }
                
                int count = 0;
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPlnZyJgWorkBill.QueryEntrustedStrat");
                psta = con.prepareStatement(sql);
                psta.setObject(1, plnNo);
                psta.setObject(2, woId);
                rs = psta.executeQuery();
                if (rs.next()) {
                    count = Integer.parseInt(rs.getString("COUNT"));
                }
                rs.close();
                psta.close();

                if(count!=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;
    }
}
