package com.service;

import com.jfinal.plugin.activerecord.Db;
import com.model.Menu;
import com.model.RoleMenu;
import com.model.UserMenu;

import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;


public class MenuService<psvn> {
    public static final MenuService me = new MenuService();
    private Menu dao = new Menu().dao();
    private RoleMenu roleMenuDao = new RoleMenu().dao();

    public List<Menu> findMenuByRole(int roleid){
       return dao.find("SELECT m.* FROM  t_menu m inner join  t_role_menu rm on m.id=rm.menuid where rm.roleid=? ",roleid);
    }

    public List<Menu> findMenuByUser(int userid) {
        List<Menu> menuList2 = dao.find("SELECT t_menu.* FROM  t_menu inner join  t_role_menu  on t_menu.id=t_role_menu.menuid " +
                " inner join t_role_user on t_role_user.roleid=t_role_menu.roleid " +
                " where t_role_user.userid = ?  order by t_menu.pid,orderno,t_menu.id asc  ", userid);

        List<Menu> menuList1 = dao.find("select t_menu.* from t_user_menu inner join t_menu on t_menu.id= t_user_menu.menuid where userid=? order by t_menu.pid,orderno,t_menu.id asc  ", userid);
        /*if (menuList2.size()>menuList1.size()) {
            return menuList2;
        } else {
            return menuList1;
        }*/
        List<Menu> result = new ArrayList<>();
        result.addAll(menuList2);
        for(Menu menu : menuList1){
            boolean flag = true;
            for(Menu menu2: menuList2){
                if(menu.getId() .equals( menu2.getId())){
                    flag = false;
                }
            }
            if(flag == true){
                result.add(menu);
            }
        }
        return result;
    }


    public List<Menu> findMenuNoRole(int roleid){
        List<Menu> menuList= dao.find( "SELECT * FROM  t_menu where id not in (  SELECT menuid FROM t_role_menu where roleid =?  ) order by orderno",roleid);
        List<Integer> parentMenuIDList = new ArrayList<Integer>();
        List<Integer> menuIDList = new ArrayList<Integer>();
        for (Menu menu : menuList){
            parentMenuIDList.add(menu.getPid());
            menuIDList.add(menu.getId());
        }
        String menuidStr=" ";
        for (Integer pid : parentMenuIDList){
            if (!menuIDList.contains(pid)){
                menuidStr +=pid+",";
            }
        }
        menuidStr+=" -1 ";
        List<Menu> menus=dao.find("SELECT * FROM  t_menu where id in ("+menuidStr+")");
        menuList.addAll(menus);
        return menuList;
    }

    public List<Menu> findMenu(){
        return dao.find("SELECT m.* FROM  t_menu m ");
    }


    public Menu addOrUpdate(Menu menu){
        if (menu.getId() == null){
            menu.save();
        }else {
            menu.update();
        }
        return menu;
    }

    public List<Menu> findSubMenu(int menuid){
        return dao.find("SELECT * FROM t_menu where pid=?",menuid);
    }

    public void delete(int menuid){
        dao.deleteById(menuid);
    }

    public void saveRoleMenu(List<RoleMenu> roleMenuList) {
        for (RoleMenu roleMenu : roleMenuList) {
            RoleMenu r =roleMenuDao.findById(roleMenu.getMenuid(),roleMenu.getRoleid());
            if (r ==null){
                roleMenu.setCreatetime(new Date()).save();
            }
        }
    }

    public Menu findParentMenuByRole(int menuid,int roleid){
        return dao.findFirst("SELECT * FROM t_role_menu where roleid=? and menuid=?",roleid,menuid);
    }

    public Menu findParentMenuByUser(int menuid,int userid){
        return dao.findFirst("SELECT * FROM t_user_menu where userid=? and menuid=?",userid,menuid);
    }

    public void delUserMenu(int userid){
        Db.delete("delete from t_user_menu where userid=?",userid);
    }

    public void saveUserMenu(List<UserMenu> userMenuList) {
        for (UserMenu userMenu : userMenuList) {
            userMenu.setCreatetime(new Date()).save();
        }
    }

    public void delRoleMenu(List<RoleMenu> roleMenuList) {
        for (RoleMenu roleMenu : roleMenuList) {
            roleMenu.delete();
        }
    }


    public boolean userHasMenuAuth(int userid,String url){
        List list =Db.find("SELECT * FROM t_menu WHERE url =?",url);
        if (list.isEmpty()){
            return true;
        }
        List<Menu> menuList=dao.find("select t_menu.* from t_user_menu inner join t_menu on t_menu.id= t_user_menu.menuid where userid=? and  url=? ",userid,url);
        if (menuList !=null && menuList.size()==0 ){
            menuList=dao.find("SELECT t_menu.* FROM  t_menu inner join  t_role_menu  on t_menu.id=t_role_menu.menuid " +
                    " inner join t_role_user on t_role_user.roleid=t_role_menu.roleid " +
                    " where t_role_user.userid = ?  and  url=?  ",userid,url);
        }
        if (menuList.isEmpty()){
            return false;
        }
        return true;
    }
}
