package com.steering.mes.signature;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

public class FrmTubeRollFileManage extends DBCommon {

    /**
     * 查询产线
     * @return
     */
    public CoreReturnObject GetPCode(String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTubeRollFileManage.GetPCode");
        sql += CoreConverter.toSqlConditionString("T.DEPARTMENT_CODE", arr);
        sql += " ORDER BY T.PLINE_NAME";
        return this.getData(sql);
    }
    
    /**
     * 查询产线(根据库号定义表找产线)
     * @return
     */
    public CoreReturnObject getPlineCode(String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTubeRollFileManage.getPlineCode");
        sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", arr);
        sql += " ORDER BY T.PLINE_NAME";
        return this.getData(sql);
    }

    /**
     * 查询轧管文件管理
     */
    public CoreReturnObject queryMinStgnature(String[] plineNo, String name) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTubeRollFileManage.queryMinStgnature");
         sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineNo);
        /*if (!name.equals("admin")) {
            sql += " AND PLINE_CODE='" + plineNo + "'";
        }*/
        sql += " ORDER BY PLINE_CODE,COL_GROUP,COL_USER";
        return this.getData(sql);
    }

    /**
     * 查询轧管文件管理
     */
    public CoreReturnObject queryMinStgnature1(String[] plineNo, String strgx) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTubeRollFileManage.queryMinStgnature1");
        sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineNo);
        sql += " AND PROCESS_NO LIKE '%'||'" + strgx + "'||'%'";
        sql += " ORDER BY PLINE_CODE,COL_GROUP,COL_USER";
        return this.getData(sql);
    }

    /**
     * 查询轧管文件管理
     */
    public CoreReturnObject queryMinStgnaturePlan(String plinc, String bz,
            String url) {
        return this.getDataBySqlID("FrmTubeRollFileManage.queryMinStgnaturePlan",
                new Object[] { plinc, bz, url });
    }

    /**
     * 查询轧管文件管理
     */
    public CoreReturnObject queryMinStgnature2(String ursId) {
        return this.getDataBySqlID("FrmTubeRollFileManage.queryMinStgnature2",
                new Object[] { ursId });
    }

    /**
     * 查询产线
     * @return
     */
    public CoreReturnObject queryPlineCode(String plineNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTubeRollFileManage.queryPlineCode");
        // sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineNo);
        if (!plineNo.equals("")) {
            sql += " AND T.PLINE_CODE='" + plineNo + "'";
        }
        sql += " ORDER BY T.PLINE_NAME";
        return this.getData(sql);
    }

    /**
     * 新增轧管文件管理
     */
    @SuppressWarnings( { "unchecked" })
    public CoreReturnObject addMinStgnature(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            // connection = ds.getConnection();
            connection.setAutoCommit(false); // 取消自动提交事务
            String addsql = CoreXmlSqlParsersFactory
                    .getSql("FrmTubeRollFileManage.addMinStgnature");
            psta = connection.prepareStatement(addsql);

            for (int j = 0; j < pram.size(); j++) {
                ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
                String plineCode = list.get(0).toString();
                String bz = list.get(1).toString();
                String url = list.get(2).toString();
                String username = list.get(3).toString();
                String memo = list.get(4).toString();
                String gx = list.get(5).toString();
                String level=list.get(7).toString();


                InputStream fil = new ByteArrayInputStream((byte[]) list.get(6));

                psta.setObject(1, plineCode);
                psta.setObject(2, bz);
                psta.setObject(3, url);
                psta.setObject(4, username);
                psta.setObject(5, memo);
                psta.setObject(6, gx);
                psta.setBinaryStream(7, fil, fil.available());
                psta.setObject(8, level);
                psta.addBatch();
                psta.executeBatch();
                cro.setV_errMsg("新增成功！");
                connection.commit();
            }

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

    /**
     * 删除轧管文件管理
     */
    public CoreReturnObject deleteMinStgnature(ArrayList<ArrayList<String>> pram) {

        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < pram.size(); i++) {
            sqllist.add(CoreXmlSqlParsersFactory
                    .getSql("FrmTubeRollFileManage.deleteMinStgnature"));
            parmlist.add(pram.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
    }

    /**
     * 修改轧管文件管理
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject updateMinStgnature(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            // connection = ds.getConnection();
            connection.setAutoCommit(false); // 取消自动提交事务
            String addsql = CoreXmlSqlParsersFactory
                    .getSql("FrmTubeRollFileManage.updateMinStgnature");
            psta = connection.prepareStatement(addsql);

            for (int j = 0; j < pram.size(); j++) {
                ArrayList<Object> list = (ArrayList<Object>) pram.get(j);

                // InputStream fil=new ByteArrayInputStream((byte[])
                // list.get(4));

                psta.setObject(1, list.get(0));
                psta.setObject(2, list.get(1));
                psta.setObject(3, list.get(2));
                psta.setObject(4, list.get(3));
                // psta.setBinaryStream(5, fil,fil.available());
                psta.setObject(5, list.get(5));
                psta.setObject(6, list.get(6));
                psta.setObject(7, list.get(4));
                psta.addBatch();
                psta.executeBatch();
            }
            cro.setV_errMsg("修改成功！");
            connection.commit();

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

    /**
     * 修改路径
     */
    public CoreReturnObject updateMinStgnature1(String url, byte[] filepath,
            String userId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String addsql = CoreXmlSqlParsersFactory
                    .getSql("FrmTubeRollFileManage.updateMinStgnature1");
            psta = connection.prepareStatement(addsql);
            // InputStream fil=new ByteArrayInputStream(filepath.getBytes());
            InputStream fil = new ByteArrayInputStream(filepath);
            // File file = new File(filepath);
            // FileInputStream fis = new FileInputStream(file);
            psta.setObject(1, url);
            psta.setBinaryStream(2, fil, fil.available());
            psta.setObject(3, userId);
            psta.addBatch();

            psta.executeBatch();
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     * 判断名字是否相同
     * @param name
     * @return
     */
    public CoreReturnObject querySameName(String name) {
        Object[] obj = new Object[] { name };
        return this.getDataBySqlID("FrmTubeRollFileManage.querySameName", obj);
    }

    /**
     * 修改时判断名字是否重复
     * @param name
     * @param userId
     * @return
     */
    public CoreReturnObject querySameNameByUpdate(String name, String userId) {
        Object[] obj = new Object[] { name, userId };
        return this.getDataBySqlID(
                "FrmTubeRollFileManage.querySameNameByUpdate", obj);
    }
}
