<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.storlead.user.mapper.UserRoleMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.storlead.user.pojo.entity.UserRoleEntity">
		<id column="user_id" property="userId"/>
		<result column="role_id" property="roleId"/>
	</resultMap>

	<!-- 通用查询结果列 -->
	<sql id="Base_Column_List">
		user_id, role_id
	</sql>

	<!--初始化没有角色的用户 给 roleID = 2 普通用户-->
	<insert id="initUserRole">
		INSERT INTO
			user_role  (user_id, role_id)
		SELECT u.id AS userId, 2 AS roleId
		FROM user u
		WHERE u.status !=6 AND u.id NOT IN(SELECT user_id FROM user_role)
	</insert>


	<select id="selectUserRole" resultMap="BaseResultMap">
		SELECT ur.* FROM (select * from user_role ${ew.customSqlSegment}) AS ur
			LEFT JOIN role as r
			ON r.id = ur.role_id
		WHERE r.`enabled` = 1
	</select>

</mapper>
