<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 12:54
  To change this template use File | Settings | File Templates.
--%>
<%@ 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.DBUtil" %>
<%@ page import="com.censoft.db.Logger" %>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    String action = pub.trimNull(request.getParameter("action"));
    String login_id = pub.trimNull(request.getParameter("login_id"));
    String passwd = pub.trimNull(request.getParameter("passwd"));
    String s_id = pub.trimNull(request.getParameter("s_id"));
    Map s_btn = (Map) session.getAttribute("s_btn");
    Map s_have_btn = (Map) session.getAttribute("s_have_btn");
    String tablename="zb_user";
    if ("login".equals(action)) {
        try {
            conn = com.censoft.db.DBUtil.getConnection();
            String ck_s_id = session.getId();
            if (!ck_s_id.equals(s_id)) {
                out.println("非法登录");
                return;
            }
            if ("".equals(login_id) || "".equals(passwd)) {
                out.println("用户名或密码不能为空");
                return;
            }
            //passwd = DBUtil.stringToMD5(passwd);
            Vector v_check = cf.doQuery(conn, "select * from zb_user where gh=? and pwd=?", null, new Object[]{login_id, passwd});
            Vector v_check_other = cf.doQuery(conn, "select * from zb_bgs_user where gh=? and pwd=?", null, new Object[]{login_id, passwd});
            System.out.println("v_check="+v_check+"v_check_other="+v_check_other);
            if(v_check == null || v_check_other==null){
                Logger.log("登录", "登录", login_id, "登录失败", request);
                out.println("用户名或密码不正确或用户不存在");
                return;
            }
            if(v_check_other.size()>0){
                tablename="zb_bgs_user";
            }
            if ( v_check.size() <= 0 &&v_check_other.size()<= 0) {
                Logger.log("登录", "登录", login_id, "登录失败", request);
                out.println("用户名或密码不正确或用户不存在");
                return;
            } else {
                if (s_btn == null || s_btn.isEmpty()) {
                    Vector v_btn = cf.doQuery(conn, "select * from t_book_btn order by menu_id");
                    Map map_btn = new HashMap();
                    if (v_btn != null && v_btn.size() > 0) {
                        for (int i = 0; i < v_btn.size(); i++) {
                            Hashtable ht = (Hashtable) v_btn.get(i);
                            String btn_id = pub.trimNull((String) ht.get("btn_id"));
                            String menu_id = pub.trimNull((String) ht.get("menu_id"));
                            if (map_btn.get(menu_id) != null) {
                                map_btn.put(menu_id, map_btn.get(menu_id) + "," + btn_id);
                            } else {
                                map_btn.put(menu_id, btn_id);
                            }
                        }
                        session.setAttribute("s_btn", map_btn);
                    }
                }
                if (s_have_btn == null || s_have_btn.isEmpty()) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(" select distinct btn.* from t_book_role_menu_rel rel  ");
                    sb.append(" join t_book_role role on role.id=rel.role_id ");
                    sb.append(" join t_book_user_role_rel ur on ur.role_id=role.id ");
                    sb.append(" join  "+tablename+" ad on ad.id=ur.login_id ");
                    sb.append(" join t_book_menu menu on menu.id=rel.menu_id ");
                    sb.append(" join t_book_role_menu_btn btn on menu.id=btn.menu_id and btn.role_id=role.id ");
                    sb.append(" where ad.gh=?  order by menu.menu_order asc");
                    System.out.println(sb.toString());
                    Vector v_list = cf.doQuery(conn, sb.toString(), null, new Object[]{login_id});
                    Map map_btn = new HashMap();
                    if (v_list != null && v_list.size() > 0) {
                        for (int i = 0; i < v_list.size(); i++) {
                            Hashtable ht = (Hashtable) v_list.get(i);
                            String btn_ids = pub.trimNull((String) ht.get("btn_ids"));
                            String menu_id = pub.trimNull((String) ht.get("menu_id"));
                            if(map_btn.get(menu_id)!=null){
                                map_btn.put(menu_id, btn_ids+","+map_btn.get(menu_id));
                            }else {
                                map_btn.put(menu_id, btn_ids);
                            }
                        }
                        session.setAttribute("s_have_btn", map_btn);
                    }
                }
                Hashtable ht =new Hashtable();
                if(v_check.size()>0){
                    ht = (Hashtable) v_check.get(0);
                }else if(v_check_other.size()>0){
                    ht = (Hashtable) v_check_other.get(0);
                }
                String login_name = pub.trimNull((String) ht.get("user_name"));
                String new_login_id = pub.trimNull((String) ht.get("id"));
                String gh = pub.trimNull((String) ht.get("gh"));
                session.setAttribute("session_login_id", new_login_id);
                session.setAttribute("session_gh", gh);
                session.setAttribute("session_login_name", login_name);
                Logger.log("登录", "登录", login_id, "登录成功", request);
                out.println("success"+","+tablename);
            }
            return;
        } catch (Exception e) {
            out.println("查询信息异常" + e);
            return;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
%>