package com.chushang.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.system.entity.bo.CancelUserRole;
import com.chushang.system.entity.dto.ListRoleDTO;
import com.chushang.system.entity.po.SysRole;
import com.chushang.system.entity.po.SysUser;

import java.util.List;

/**
 * <p>
 * 角色用于控制菜单 及 用户的显示 服务类
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
public interface ISysRoleService extends IService<SysRole> {

    List<SysRole> selectRoleAll(SysRole sysRole);

    List<SysRole> selectRolesByUserId(SysUser user);

    AjaxResult selectRoleList(ListRoleDTO role);

    void checkRoleDataScope(SysRole role);

    SysRole selectRoleById(Long roleId);

    default boolean checkRoleNameUnique(SysRole role){
        long roleId = ObjectUtil.isNull(role.getRoleId()) ? -1 : role.getRoleId();
        SysRole info = getOne(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleName, role.getRoleName())
                .last(Operator.LIMIT_ONE.getCharacter()));
        return ObjectUtil.isNotNull(info) && info.getRoleId() != roleId;
    }

    default boolean checkRoleKeyUnique(SysRole role){
        long roleId = ObjectUtil.isNull(role.getRoleId()) ? -1 : role.getRoleId();
        SysRole info = getOne(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleKey, role.getRoleKey())
                .last(Operator.LIMIT_ONE.getCharacter()));
        return ObjectUtil.isNotNull(info) && info.getRoleId() != roleId;
    }

    void saveRole(SysRole role);

    default void checkRoleAllowed(SysRole role){
        Long roleId = role.getRoleId();
        if (StringUtils.isNotNull(roleId) && role.isAdmin())
        {
            throw new ResultException("不允许操作超级管理员角色");
        }
    }

    void updateRole(SysRole role);

    void authDataScope(SysRole role);

    default void updateRoleStatus(SysRole role){
        updateById(role);
    }

    void deleteRoleByIds(Long[] roleIds);

    void deleteAuthUser(CancelUserRole cancelUserRole);

    void insertAuthUsers(Long roleId, Long[] userIds);

    String selectRolesByUserName(String username);
}
