package com.steering.pss.qcm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;

/**
 * 基础信息管理类
 * @author wq 2014/10/13
 * @version v1.0
 * @modify
 */
public class CorePermissionConfigure extends DBCommon {

   
    /**
     * 角色信息查询
     */
    public CoreReturnObject doQuery(String name) {
    	Object[] obj = new Object[] { name};
        return this.getDataBySqlID("CorePermissionConfigure.doQuery", obj);
    }

    /**
     * 角色向下对应用户信息查询
     */
    public CoreReturnObject QueryUserInfo(String id) {
    	Object[] obj = new Object[] { id};
        return this.getDataBySqlID("CorePermissionConfigure.QueryUserInfo", obj);
    }
    
    /**
     * 角色权限信息查询
     */
    public CoreReturnObject getPageRole(String id) {
    	Object[] obj = new Object[] { id };
        return this.getDataBySqlID("CorePermissionConfigure.getPageRole", obj);
    }
    
	public CoreReturnObject doSave(String id,String userid,String userName, ArrayList<String> parm){
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			
			//先删除之前的数据
			String sql = CoreXmlSqlParsersFactory
	                .getSql("CorePermissionConfigure.doDelete");
	        PreparedStatement ps = con.prepareStatement(sql);
	        ps.setObject(1, id);
	        ps.executeUpdate();
	        
			//保存新数据
	        sql = CoreXmlSqlParsersFactory
	                .getSql("CorePermissionConfigure.doSave");
	        ps = con.prepareStatement(sql);
	        for(String pageId : parm){
	        	ps.setObject(1, id);
	        	ps.setObject(2, pageId);
	        	ps.setObject(3, userid);
	        	ps.setObject(4, userName);
		        ps.executeUpdate();
	        }
	        ps.close();
	        
			con.commit();
			return cro;
		}catch(Exception ex){
			try {
				con.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cro.setV_errMsg("程序异常: " + ex.getMessage());
			return cro;
		}finally{
			if(st != null){
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
