package cn.qinys.platform.upms.api.mapper;

import cn.qinys.platform.upms.api.dto.RolesDTO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qinys.platform.entity.upms.UpmsRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 租户角色配置表(UpmsRole)表数据库访问层
 *
 * @author makejava
 * @since 2024-02-27 11:03:32
 */
@Repository
@Mapper
public interface UpmsRoleMapper extends BaseMapper<UpmsRole> {

    /**
     * 角色分页
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT * FROM upms_role ${ew.customSqlSegment}")
    Page<UpmsRole> selectRolePage(Page<UpmsRole> page, @Param(Constants.WRAPPER) Wrapper<UpmsRole> wrapper);

    /**
     * 获取默认角色
     *
     * @param userId
     * @return
     */
    @Select("SELECT * FROM upms_role r INNER JOIN upms_user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId} AND active = '1' LIMIT 1")
    UpmsRole selectActiveRole(@Param("userId") String userId);

    /**
     * 用户登录的角色
     *
     * @param userId
     * @return
     */
    @Select("SELECT r.id, r.role_cn_name as name FROM upms_user_role ur LEFT JOIN upms_role r ON ur.role_id = r.id WHERE ur.user_id = #{userId}")
    List<RolesDTO> getRoleListDTO(@Param("userId") String userId);

    /**
     * 获取默认角色
     *
     * @param TenantId
     * @return
     */
    @Select("SELECT * FROM upms_role WHERE tenant_id = #{TenantId}")
    List<UpmsRole> selectByTenantId(@Param("TenantId") String TenantId);


}

