<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.*" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.db.Logger" %>
<%!
    public static String getUserNames(ConnectionFactory cf,Connection conn,int startIndex,int n){
        Vector userListSize = cf.doQuery(conn,"select id from zb_user",null,new Object[]{});
        StringBuffer sql=new StringBuffer("select user_name from zb_user where 1=1 and order_num in(");
        List list= new ArrayList();
        String user_names="";
        int startIndex_2=startIndex;
        for(int i=0;i<n;i++){
            startIndex=startIndex+1;
            if(startIndex==userListSize.size()+1){
                startIndex=1;
            }
            if(i==n-1){
                sql.append(startIndex );
            }else{
                sql.append(startIndex + ",");
            }
        }
        sql.append(") order by field (order_num,");
        for(int i=0;i<n;i++){
            startIndex_2=startIndex_2+1;
            if(startIndex_2==userListSize.size()+1){
                startIndex_2=1;
            }
            if(i==n-1){
                sql.append(startIndex_2 );
            }else{
                sql.append(startIndex_2 + ",");
            }
        }
        sql.append(") ");
        Vector userList = cf.doQuery(conn,sql.toString(),null,list.toArray());
        if(userList!=null && userList.size()>0){
            for(int i=0;i<userList.size();i++) {
                Hashtable ht1 = (Hashtable) userList.get(i);
                String user_name = (String) ht1.get("user_name");
                user_names=user_names+user_name+",";
            }
        }else{
            System.out.println("方法查询出错"+cf.getMsg());
        }
        return startIndex+"_"+user_names;
    }
%>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Enumeration enu = request.getParameterNames();
    String action = pub.trimNull(request.getParameter("action"));
    String user_id = pub.trimNull(request.getParameter("user_id"));
    String role_num = pub.trimNull(request.getParameter("role_num"));
    String action_msg="";
    String role_id = pub.trimNull(request.getParameter("role_id"));
    if("".equals(role_id)){
        role_id=UUID.randomUUID().toString().trim().replaceAll("-", "");
    }
    String role_name = pub.trimNull(request.getParameter("role_name"));
    String order_num = pub.trimNull(request.getParameter("order_num"));
    String beizhu = pub.trimNull(request.getParameter("beizhu"));
    String[] userids = request.getParameterValues("userids");
    String add_user = pub.trimNull((String) session.getAttribute("session_login_id"));
    if ("".equals(add_user)) {
        System.out.println("当前没有用户信息");
    }
    String add_time = pub.getCurrentServerTime();
    StringBuffer sql_add=new StringBuffer("insert into zb_role (id,role_name,order_num,beizhu,add_time,add_user) values (?,?,?,?,?,?)");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        List list_add= new ArrayList();
        list_add.add(role_id);
        list_add.add(role_name);
        list_add.add(order_num);
        list_add.add(beizhu);
        list_add.add(add_time);
        list_add.add(add_user);
        conn.setAutoCommit(false);
        boolean is_oper = false;
        if("add".equals(action)){
            action_msg="新增";
            is_oper = cf.executeUpdate(conn, sql_add.toString(),list_add.toArray());
            if (!is_oper) {
                conn.rollback();
                System.out.println("新增zb_role表出错了" + cf.getMsg());
                return;
            }
        }else if("delete".equals(action)){
            action_msg="删除";
            is_oper = cf.executeUpdate(conn, "delete from zb_role where id=?",new Object[]{role_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_role表出错了" + cf.getMsg());
                return;
            }
            is_oper = cf.executeUpdate(conn, "delete from zb_role_user where role_id=?",new Object[]{role_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_role_user表出错了" + cf.getMsg());
                return;
            }
        }else if("addUser".equals(action)){
            action_msg="包含人员";
            is_oper = cf.executeUpdate(conn, "delete from zb_role_user where role_id=?",new Object[]{role_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_role表出错了" + cf.getMsg());
                return;
            }
            if(userids!=null && userids.length>0) {
                for (int i = 0; i < userids.length; i++) {
                    is_oper = cf.executeUpdate(conn, "insert into zb_role_user (role_id,user_id) values (?,?)",new Object[]{role_id,userids[i]});
                }
            }
            if (!is_oper) {
                conn.rollback();
                System.out.println("新增zb_role_user表出错了" + cf.getMsg());
                return;
            }
        }else if("optRoleNum".equals(action)){
            action_msg="设置角色内排序";
            is_oper = cf.executeUpdate(conn, "update zb_bgs_user set role_num=? where id=?",new Object[]{role_num,user_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("修改zb_bgs_user表出错了" + cf.getMsg());
                return;
            }
        }else{
            action_msg="修改";
            is_oper = cf.executeUpdate(conn, "update zb_role set role_name=?,order_num=?,beizhu=? where id=?", new Object[]{role_name,order_num,beizhu,role_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("修改zb_role表出错了" + cf.getMsg());
                return;
            }
        }
        if (!is_oper) {
            conn.rollback();
            System.out.println("操作出错了" + cf.getMsg());
            return;
        }
        Logger.log(conn, "值班角色",action_msg,  add_user, "", request);
        conn.commit();
        if (!is_oper) {
            conn.rollback();
            out.println(cf.getMsg());
        } else {
            out.println("success,"+role_id);
        }
    } catch (Exception e) {
        conn.rollback();
        System.out.println("有异常" + e);
        return;
    } finally {
        if (conn != null) {
            conn.setAutoCommit(true);

            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
