package com.kmall.admin.fromcomm.service.impl;

import com.kmall.admin.fromcomm.dao.SysRoleDao;
import com.kmall.admin.fromcomm.dao.SysUserRoleDao;
import com.kmall.admin.fromcomm.entity.SysRoleEntity;
import com.kmall.admin.fromcomm.entity.SysUserRoleEntity;
import com.kmall.admin.fromcomm.service.SysRoleDeptService;
import com.kmall.admin.fromcomm.service.SysRoleMenuService;
import com.kmall.admin.fromcomm.service.SysRoleService;
import com.kmall.admin.fromcomm.service.SysUserService;
import com.kmall.common.utils.Constant;
import com.kmall.common.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 角色
 *
 * @author Scott
 * @email
 * @date 2016年9月18日 上午9:45:12
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;

    @Override
    public SysRoleEntity queryObject(Long roleId) {
        return sysRoleDao.queryObject(roleId);
    }

    @Override
    public List<SysRoleEntity> queryList(Map<String, Object> map) {
        return sysRoleDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysRoleDao.queryTotal(map);
    }

    @Override
    @Transactional
    public void save(SysRoleEntity role) {
        if (role.getMenuIdList() == null || role.getMenuIdList().size() <= 0) {
            throw new RRException("功能权不能为空！");
        }

        role.setCreateTime(new Date());
        sysRoleDao.save(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        //sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional
    public void update(SysRoleEntity role) {
        if (role.getMenuIdList() == null || role.getMenuIdList().size() <= 0) {
            throw new RRException("功能权不能为空！");
        }

        sysRoleDao.update(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        //保存角色与部门关系
        //sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional
    public void deleteBatch(Long[] roleIds) {
        for (Long id : roleIds) {
            SysRoleEntity role = queryObject(id);

            Map<String, Object> map = new HashMap<>();
            map.put("roleId", id);
            List<SysUserRoleEntity> userEntityList = sysUserRoleDao.queryList(map);

            if (userEntityList != null && userEntityList.size() > 0) {
                throw new RRException("角色【" + role.getRoleName() + "】还有【" + userEntityList.size() + "】个管理员正在使用，删除失败！");
            }
            sysUserRoleDao.deleteByRoleId(id);
            sysRoleMenuService.delete(id);
            delete(id);
        }
    }

    @Override
    public void delete(Long roleId) {
        sysRoleDao.delete(roleId);
    }

    @Override
    public List<Long> queryRoleIdList(Long createUserId) {
        return sysRoleDao.queryRoleIdList(createUserId);
    }

    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRoleEntity role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if (role.getCreateUserId() == Constant.SUPER_ADMIN) {
            return;
        }

        //查询用户所拥有的菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new RRException("新增角色的权限，已超出你的权限范围");
        }
    }

    @Override
    public SysRoleEntity queryObjectByName(String roleName) {
        return sysRoleDao.queryObjectByName(roleName);
    }
}
