package com.steering.mes.signature;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.common.StringTool;

public class FrmMilCraftworkBase extends DBCommon {

    /**
     * 查询工艺
     * @return
     */
    public CoreReturnObject queryCrafBase(String farctor, String val) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMilCraftworkBase.queryCrafBase");
        if (!farctor.equals("")) {
            sql += " AND T.FACTOR_NAME LIKE '%'||" + farctor + "||'%'";
        }
        if (val.equals("1")) {
            sql += " AND T.VALIDFLAG='1' ";
        }
        sql += " ORDER BY T.CREATE_TIME  DESC";
        return this.getData(sql);
    }

    /**
     * 查询工艺是否引用
     * @return
     */
    public CoreReturnObject queryCrafSet(String farctId) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMilCraftworkBase.queryCrafSet");
        return this.getData(sql, new Object[] { farctId });
    }

    /**
     * 查询表名
     * @return
     */
    public CoreReturnObject queryTbleName() {
        return this.getDataBySqlID("FrmMilCraftworkBase.queryTbleName");
    }

    /**
     * 查询表字段
     * @return
     */
    public CoreReturnObject queryTableColumnName(String tableName) {
        return this.getDataBySqlID("FrmMilCraftworkBase.queryTableColumnName",
                new Object[] { tableName });
    }

    /**
     * 查询表字段备注
     * @return
     */
    public CoreReturnObject queryTableColumecomments(String tableName,
            String colName) {
        return this.getDataBySqlID(
                "FrmMilCraftworkBase.queryTableColumecomments", new Object[] {
                        tableName, colName });
    }

    /**
     * 查询表字段类型
     * @return
     */
    public CoreReturnObject queryTableDataType(String tableName, String colName) {
        return this.getDataBySqlID("FrmMilCraftworkBase.queryTableDataType",
                new Object[] { tableName, colName });
    }

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

            String factID = "";
            String sqlquery = CoreXmlSqlParsersFactory
                    .getSql("FrmMilCraftworkBase.queryCrafBaseMaxId"); // 查询最大工艺ID
            PreparedStatement psta1 = connection.prepareStatement(sqlquery);
            psta1.setObject(1, "C");
            ResultSet rs = psta1.executeQuery();
            if (rs.next()) {
                factID = rs.getString("FACTOR_ID");
            }
            rs.close();
            psta1.close();

            int factNo = Integer.parseInt(factID.substring(factID.length() - 6,
                    factID.length()));
            for (int j = 0; j < pram.size(); j++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(j);
                String factName = list.get(0);
                String tableName = list.get(1);
                String colKey = list.get(2);
                String colType = list.get(3);
                String name = list.get(4);
                String time = list.get(5);

                psta.setObject(1, "C"
                        + StringTool.formatDecimal(factNo, "000000"));
                psta.setObject(2, factName);
                psta.setObject(3, tableName);
                psta.setObject(4, colKey);
                psta.setObject(5, colType);
                psta.setObject(6, name);
                psta.setObject(7, time);
                psta.setObject(8, list.get(6));
                psta.addBatch();
                factNo++;
            }
            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 deleteCrafBase(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("FrmMilCraftworkBase.deleteCrafBase"));
            parmlist.add(pram.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
    }

    /**
     *修改
     */
    public CoreReturnObject updateCrafBase(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("FrmMilCraftworkBase.updateCrafBase"));
            parmlist.add(pram.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
    }

    /**
     * 作废
     * @param pram
     * @return
     */
    public CoreReturnObject updateValCrafBase(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("FrmMilCraftworkBase.updateValCrafBase"));
            parmlist.add(pram.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
    }
}
