package com.steering.pss.qcm;

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;

/**
 * 测试类
 * @author Administrator
 *
 */
public class Test extends DBCommon {
    /**
     * 查询
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject query(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("Test.Query", list.toArray());//通过sqlID查询数据
        return cro;
    }

    /**
     * 修改
     * @param list 修改参数
     * @return obj
     */
    public CoreReturnObject update(ArrayList<String> list) {
        return this.setDataBySqlID("Test.Update", list.toArray());//通过sqlID修改数据
    }

    /**
     * 修改（多个非查询一起执行）
     * @param list 修改参数
     * @return obj
     */
    public CoreReturnObject update2(ArrayList<ArrayList<String>> list) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add(CoreXmlSqlParsersFactory.getSql("Test.Update"));
            parmlist.add(list.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist); //执行事务处理（多个非查询， 自动提交、回滚）
    }

    /**
     * 修改 (多个非查询语句中间参杂有查询语句，使用到这种方式操作数据的情况很少。)
     * @param list 修改参数
     * @param list2 修改参数
     * @return obj
     */
    public CoreReturnObject update3(ArrayList<String> list,
            ArrayList<String> list2) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); //取消自动提交事务
            String sqlString = CoreXmlSqlParsersFactory.getSql("Test.Update");
            psta = connection.prepareStatement(sqlString);
            psta.setString(1, list.get(0));
            psta.setString(2, list.get(1));
            psta.setString(3, list.get(2));
            int count = psta.executeUpdate();
            if (count > 0) {
                String sqlString2 = CoreXmlSqlParsersFactory
                        .getSql("Test.Query2");
                psta = connection.prepareStatement(sqlString2);
                psta.setString(1, "001");
                ResultSet resultSet = psta.executeQuery();
                int count2 = 0;
                while(resultSet.next()){
                    count2 = resultSet.getInt(1);
                }
                resultSet.close();
                if(count2 > 0){
                    psta = connection.prepareStatement(sqlString);
                    psta.setString(1, list2.get(0));
                    psta.setString(2, list2.get(1));
                    psta.setString(3, list2.get(2));
                    int count3 = psta.executeUpdate();
                    cro.setResult(count3);
                }
            }
            connection.commit();
        }
        catch (Exception e) {
            if(connection != null){
                try {
                    connection.rollback();//回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally{
            try {
                psta.close(); //关闭
                connection.close();
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }
}
