package com.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Role;
import com.model.RoleUser;
import com.model.User;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 * 2018-10-18
 * zj
 */
public class RoleService<psvn> {
    public static final RoleService me = new RoleService();
    private Role dao = new Role().dao();

    private RoleUser daouserdao = new RoleUser().dao();

    public List<Role> findRoleByUser(int userid){
       return dao.find("SELECT * FROM t_role r inner join t_role_user u on u.roleid=r.id where userid=? ",userid);
    }
    public List<Role> findNoOwnerRoleByUser(int userid){
        return dao.find("SELECT * FROM t_role  where id not  " +
                "in ( select roleid from t_role_user where userid =?  )",userid);
    }


    public List<Role> findRole(){
        return dao.find("SELECT * FROM t_role r ");
    }

    public void addUserRole(List<RoleUser> roleUserList){
        for (RoleUser roleUser : roleUserList) {
            roleUser.save();
        }
    }

    public void delUserRole(int id){
        daouserdao.deleteById(id);
    }

    public void addOrUpdate(Role role){
        if (role.getId()==null){
            role.setCreatetime(new Date()).save();
        }else {
            role.update();
        }
    }

    public boolean roleHaveUser(int roleid){
        List<Record> list =Db.find("SELECT * FROM t_role_user where roleid =? limit 1",roleid);
        if (list != null && list.size()>0){
            return true;
        }
        return false;
    }

    public boolean roleHaveMenu(int roleid){
        List<Record> list =Db.find("SELECT * FROM t_role_menu where roleid =? limit 1",roleid);
        if (list != null && list.size()>0){
            return true;
        }
        return false;
    }





    public void delRole(int id){
        dao.deleteById(id);
    }
}
