<%@ 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" %>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Enumeration enu = request.getParameterNames();
    String action = pub.trimNull(request.getParameter("action"));
    String action_msg="";
    String user_id = pub.trimNull(request.getParameter("user_id"));
    String add_user = pub.trimNull((String) session.getAttribute("session_login_id"));
    if ("".equals(add_user)) {
        System.out.println("当前没有用户信息");
    }
    String[] role_ids = request.getParameterValues("role_ids");
    String add_time = pub.getCurrentServerTime();
    StringBuffer sql_add=new StringBuffer("insert into zb_user (add_user,add_time,");
    StringBuffer sql_update=new StringBuffer("update zb_user set update_user=?,update_time=?,");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        List list_add= new ArrayList();
        list_add.add(add_user);
        list_add.add(add_time);
        List list_update = new ArrayList();
        list_update.add(add_user);
        list_update.add(add_time);
        while (enu.hasMoreElements()) {
            String name = pub.trimNull((String) enu.nextElement());
            if ("action".equals(name) || "user_id".equals(name)||"role_ids".equals(name)) {
                continue;
            }
            String re_value = pub.trimNull(request.getParameter(name));
            if("order_num".equals(name)&&"".equals(re_value)){
                re_value="1";
            }
            sql_add.append(name + ",");
            list_add.add(re_value);
            sql_update.append(" " + name + "=?,");
            list_update.add(re_value);
        }

        conn.setAutoCommit(false);
        boolean is_oper = false;
        if("add".equals(action)){
            action_msg="新增";
            String table_cols = sql_add.toString().substring(0, sql_add.toString().length() - 1);
            sql_add = new StringBuffer(table_cols);
            sql_add.append(") values(");
            for (int k = 0; k < list_add.size(); k++) {
                sql_add.append("?");
                if (k < list_add.size() - 1) {
                    sql_add.append(",");
                }
            }
            sql_add.append(")");
            is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
            is_oper = cf.executeUpdate(conn, "delete from  t_book_user_role_rel  where login_id=?", new Object[]{user_id});
            if(role_ids!=null&&role_ids.length>0){
                for(int i=0;i<role_ids.length;i++) {
                    String role_id = pub.trimNull((String)role_ids[i]);
                    if(!"".equals(role_id)) {
                        is_oper = cf.executeUpdate(conn, "insert into  t_book_user_role_rel(login_id,role_id) values (?,?)", new Object[]{user_id, role_id});
                    }
                }
            }
        }else if("delete".equals(action)){
            action_msg="删除";
            is_oper = cf.executeUpdate(conn, "delete from zb_user where id=?",new Object[]{user_id});
        }else{
            action_msg="修改";
            String table_cols = sql_update.toString().substring(0, sql_update.toString().length() - 1);
            sql_update = new StringBuffer(table_cols);
            sql_update.append(" where id=? ");
            list_update.add(user_id);
            is_oper = cf.executeUpdate(conn, sql_update.toString(), list_update.toArray());
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除人员表出错了" + cf.getMsg());
                return;
            }
            is_oper = cf.executeUpdate(conn, "delete from  t_book_user_role_rel  where login_id=?", new Object[]{user_id});
            if(role_ids!=null&&role_ids.length>0){
                for(int i=0;i<role_ids.length;i++) {
                    String role_id = pub.trimNull((String)role_ids[i]);
                    if(!"".equals(role_id)) {
                        is_oper = cf.executeUpdate(conn, "insert into  t_book_user_role_rel(login_id,role_id) values (?,?)", new Object[]{user_id, role_id});
                    }
                }
            }
        }
        if (!is_oper) {
            conn.rollback();
            System.out.println("删除人员关系表出错了" + cf.getMsg());
            return;
        }
        Logger.log(conn, "用户人员管理",action_msg,  add_user, "用户名称："+pub.trimNull(request.getParameter("user_name")), request);
        conn.commit();
        if (!is_oper) {
            conn.rollback();
            out.println(cf.getMsg());
        } else {
            out.println("success,"+user_id);
        }
    } catch (Exception e) {
        conn.rollback();
        System.out.println("有异常" + e);
        return;
    } finally {
        if (conn != null) {
            conn.setAutoCommit(true);
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
