package org.jeecg.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.system.entity.SysUserRole;

import java.util.List;

/**
 * <p>
 * 用户角色表 Mapper 接口
 * </p>
 *
 * @Author scott
 * @since 2018-12-21
 */
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

	@Select("select role_code from sys_role where id in (select role_id from sys_user_role where user_id = (select id from sys_user where username=#{username}))")
	List<String> getRoleByUserName(@Param("username") String username);

	@Select("select id from sys_role where id in (select role_id from sys_user_role where user_id = (select id from sys_user where username=#{username}))")
	List<String> getRoleIdByUserName(@Param("username") String username);
	@Select("SELECT\n" +
			"\tt3.user_id\n" +
			"FROM\n" +
			"\t(\n" +
			"\t\tSELECT\n" +
			"\t\t\tt2.user_id,\n" +
			"\t\t\tt2.role_id\n" +
			"\t\tFROM\n" +
			"\t\t\tsys_user_depart t1\n" +
			"\t\tLEFT JOIN sys_user_role t2 ON t1.user_id = t2.user_id\n" +
			"\t\tWHERE\n" +
			"\t\t\tt1.dep_id = (\n" +
			"\t\t\t\tSELECT\n" +
			"\t\t\t\t\tdep_id\n" +
			"\t\t\t\tFROM\n" +
			"\t\t\t\t\tsys_user_depart\n" +
			"\t\t\t\tWHERE\n" +
			"\t\t\t\t\tuser_id = #{userId}\n" +
			"\t\t\t)\n" +
			"\t) t3\n" +
			"LEFT JOIN sys_role t4 ON t3.role_id = t4.id\n" +
			"WHERE\n" +
			"\tt4.role_code =  #{roleCode}\n")
	String getLeaderIdByUser(@Param("userId") String userId, @Param("roleCode") String roleCode);

}
