<?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="cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper">

	<select id="selectByRoleCode" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
		SELECT
			*
		FROM
			system_users
		WHERE
				id IN (
				SELECT
					user_id
				FROM
					system_user_role sur
				WHERE
					sur.tenant_id = #{tenantId}
				  AND sur.role_id = ( SELECT id FROM system_role WHERE `code` = #{roleCode} ))
	</select>
	<select id="selectListByRoleId" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO"
			parameterType="cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO">
		select distinct su.*,GROUP_CONCAT(DISTINCT sr2.name SEPARATOR ',') AS roleNames
		from system_users su
		left join system_user_role sr on su.id = sr.user_id
		left join system_role sr2 on sr.role_id = sr2.id
		<where>
			su.deleted = 0 and sr2.service_package_id = 'jiance' and sr2.code != 'jcyh'

			<if test="role.nickname != null">
				and (su.nickname like CONCAT('%',#{role.nickname},'%') OR su.username like CONCAT('%',#{role.nickname},'%'))
			</if>
		</where>
		group by su.id
		<if test="role.roleId != null">
			HAVING SUM(sr.role_id = #{role.roleId}) > 0
		</if>

		order by su.nickname
	</select>

	<select id="selectListByRoleIdJX" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO"
			parameterType="cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO">
        select
        su.id,
        su.username,
        su.nickname,
        su.user_type,
        su.remark,
        su.dept_id,
        su.mobile,
        su.password,
        su.sex,
        su.open_id,
        su.tenant_id,
        su.status,
        COALESCE(dsc.image, su.avatar) AS avatar,
        GROUP_CONCAT(DISTINCT sr2.name SEPARATOR ',') AS roleNames
        from system_users su
        left join system_user_role sr on su.id = sr.user_id and sr.deleted = 0
        left join system_role sr2 on sr.role_id = sr2.id
        INNER JOIN drive_school_coach dsc ON dsc.user_id = su.id
        <where>
            su.deleted = 0 and sr2.service_package_id = 'jiaxiao'

            <if test="role.nickname != null">
                and (su.nickname like CONCAT('%',#{role.nickname},'%') OR su.username like
                CONCAT('%',#{role.nickname},'%'))
            </if>
        </where>
        group by su.id
        <if test="role.roleId != null">
            HAVING SUM(sr.role_id = #{role.roleId}) > 0
        </if>

        order by su.nickname
    </select>

    <select id="userCodes" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserRoleDTO">
		SELECT
			su.id AS userId,sr.code AS roleCode,sr.id AS roleId, sr.service_package_id
		FROM
			system_users su
				LEFT JOIN system_user_role sur ON su.id = sur.user_id AND sur.deleted = 0
				LEFT JOIN system_role sr ON sur.role_id = sr.id AND sr.deleted = 0
		WHERE su.deleted = 0
		AND su.id IN
		<foreach item="item" collection="userIds" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>
	<select id="selectByRoleId" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
		SELECT
			*
		FROM
			system_users
		WHERE
				id IN (
				SELECT
					user_id
				FROM
					system_user_role sur
				WHERE
					sur.role_id = #{roleId})
	</select>
    <select id="selectByRoleIds" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
		SELECT
			*
		FROM
			system_users
		WHERE
				id IN (
				SELECT
					user_id
				FROM
					system_user_role sur
				WHERE
					sur.role_id IN
					<foreach item="item" collection="roleIds" open="(" separator="," close=")">
						#{item}
					</foreach>
				)
	</select>
	<select id="selectByRoleCodes" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
		SELECT
			*
		FROM
			system_users
		WHERE
				id IN (
				SELECT
					user_id
				FROM
					system_user_role sur
				WHERE
					sur.role_id IN (
					SELECT
						id
					FROM
						system_role
					WHERE
						code IN
						<foreach item="item" collection="codes" open="(" separator="," close=")">
							#{item}
						</foreach>
					) AND sur.deleted = 0
				) AND deleted = 0
	</select>
</mapper>
