package com.dgtis.dorm.manager.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dgtis.dorm.manager.db.dao.AdminRoleMapper;
import com.dgtis.dorm.manager.db.dao.RoleMapper;
import com.dgtis.dorm.manager.db.dao.RoleMenuMapper;
import com.dgtis.dorm.manager.db.domain.AdminRole;
import com.dgtis.dorm.manager.db.domain.Role;
import com.dgtis.dorm.manager.db.domain.RoleMenu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private AdminRoleMapper adminRoleMapper;


    /**
     * 添加角色
     *
     * @param record 角色
     * @return int 插入条数
     */
    public int insertSelective(Role record){
        return roleMapper.insertSelective(record);
    }

    /**
     * 根据id查询角色
     *
     * @param id 角色id
     * @return Role 角色
     */
    public Role selectByPrimaryKey(Integer id){
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     *
     * 根据id更新角色信息
     *
     * @param record 角色
     * @return int 跟新条数
     */
    public int updateByPrimaryKeySelective(Role record){
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    /**
     *
     * 分页查询角色表,如果roleName为空，则查询全部
     *
     * @param roleName 模糊查询
     * @param page
     * @param size
     * @return list<Role>
     */
    public Map<String,Object> selectByRoleName(String roleName, Integer page, Integer size){
        Role example = new Role();
        example.setRoleName(roleName);

        PageHelper.startPage(page, size);
        List<Role> list = roleMapper.selectByExample(example);

        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());

        return resMap;
    }


    /**
     * 添加角色和菜单关联
     *
     * @param roleId
     * @param menuList
     */
    public void insertRoleMenu(int roleId,List<String> menuList){
        RoleMenu example = new RoleMenu();
        example.setRoleId(roleId);

        roleMenuMapper.deleteByExample(example);

        for (String menu:menuList){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menu);
            roleMenuMapper.insertSelective(roleMenu);
        }

    }

    /**
     * 根据角色查询菜单
     *
     * @param roleId
     * @return List<String>
     */
    public List<String> selectMenuByRoleId(Integer roleId){

        RoleMenu example = new RoleMenu();
        example.setRoleId(roleId);

        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        List<String> resultList = new ArrayList<String>();
        for (RoleMenu rm : list){
            resultList.add(rm.getMenuId());
        }

        return resultList;

    }


    /**
     *
     * 添加admin用户和角色关系
     * @param adminId
     * @param roleList
     */
    public void insertAdminRole(Integer adminId, List<String> roleList) {
        AdminRole example = new AdminRole();
        example.setAdminId(adminId);
        adminRoleMapper.deleteByExample(example);

        for (String roleId:roleList){
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(Integer.parseInt(roleId));
            adminRoleMapper.insertSelective(adminRole);
        }
    }

    /**
     * 查询用户所拥有的菜单
     *
     * @param id
     * @return
     */
    public List<String> selectAdminUserMenuByAdminId(Integer id){
        List<String> list = roleMapper.selectAdminUserMenuByAdminId(id);
        return list;
    }

    /**
     * 查询用户拥有的角色
     *
     * @param adminId 用户id
     * @return
     */
    public List<String> selectAdminUserRoleByAdminId(Integer adminId) {
        AdminRole example = new AdminRole();
        example.setAdminId(adminId);

        List<AdminRole> list = adminRoleMapper.selectByExample(example);
        List<String> resList = new ArrayList<String>();
        for (AdminRole ar:list){
            resList.add(ar.getRoleId()+"");
        }

        return resList;
    }
}
