package com.jiayue.ssi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ssi.constant.UserConstants;
import com.jiayue.ssi.entity.SysRole;

import com.jiayue.ssi.entity.SysRoleMenu;
import com.jiayue.ssi.mapper.SysRoleMapper;

import com.jiayue.ssi.mapper.SysRoleMenuMapper;
import com.jiayue.ssi.mapper.SysUserRoleMapper;
import com.jiayue.ssi.service.SysRoleService;

import com.jiayue.ssi.util.RyStringUtils;
import com.jiayue.ssi.util.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 角色 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    SysRoleMapper roleMapper;
    @Autowired
    SysRoleMenuMapper roleMenuMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    /**
     * 新增保存角色信息
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertRole(SysRole role) {
        // 新增角色信息
        roleMapper.insertRole(role);
        return insertRoleMenu(role);
    }


    /**
     * 新增角色菜单信息
     *
     * @param role 角色对象
     */
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertRoleMenu(SysRole role) {
        int rows = 1;
        // 新增用户与角色管理
        List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
        for (Long menuId : role.getMenuIds()) {
            SysRoleMenu rm = new SysRoleMenu();
            rm.setRoleId(role.getRoleId());
            rm.setMenuId(menuId);
            rm.setDelFlag("0");
            rm.setCreateBy(SecurityContextUtil.getSysUser().getCreateBy());
            rm.setCreateTime(new Date());
            list.add(rm);
        }
        if (list.size() > 0) {
            rows = roleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }

    /**
     * 通过角色ID查询角色
     *
     * @param roleId 角色ID
     * @return 角色对象信息
     */
    @Override
    public SysRole selectRoleById(Long roleId) {
        return this.getById(roleId);
    }

    /**
     * 修改保存角色信息
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateRole(SysRole role) {
        // 修改角色信息
        this.updateById(role);
        // 删除角色与菜单关联
        roleMenuMapper.deleteRoleMenuByRoleId(SecurityContextUtil.getSysUser().getUsername(), role.getRoleId());
        return insertRoleMenu(role);
    }

    /**
     * 校验角色名称是否唯一
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkRoleNameUnique(SysRole role)
    {
        Long roleId = RyStringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        SysRole info = roleMapper.checkRoleNameUnique(role.getRoleName());
        if (RyStringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验角色权限是否唯一
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkRoleKeyUnique(SysRole role)
    {
        Long roleId = RyStringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        SysRole info = roleMapper.checkRoleKeyUnique(role.getRoleKey());
        if (RyStringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 根据角色名称查找
     * @param roleName
     * @return SysUser
     */
    @Override
    public SysRole queryRoleName(String roleName){
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getRoleName,roleName);
        SysRole sysRole = roleMapper.selectOne(queryWrapper);
        return sysRole;
    }
    /**
     * 根据角色权限查找
     * @param roleKey
     * @return SysRole
     */
    @Override
    public SysRole queryRoleKey(String roleKey){
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getRoleKey,roleKey);
        SysRole sysRole = roleMapper.selectOne(queryWrapper);
        return sysRole;
    }

    /**
     * 通过角色ID删除角色
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteRoleById(Long roleId) {
        String username = SecurityContextUtil.getSysUser().getUsername();
        // 删除角色与菜单关联
        roleMenuMapper.deleteRoleMenuByRoleId(username, roleId);
        return roleMapper.deleteRoleById(username, roleId);
    }

    /**
     * 根据用户id找对应的角色
     * @return 结果
     */
    @Override
    public SysRole selectRoleListByUserId() {
        Long userId = SecurityContextUtil.getSysUser().getId();
        SysRole sysRole = roleMapper.selectRoleListByUserId(userId);
        return sysRole;
    }

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    public int countUserRoleByRoleId(Long roleId)
    {
        return sysUserRoleMapper.countUserRoleByRoleId(roleId);
    }
}
