<?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.user.AdminUserMapper">
    <update id="updateSetOpenId">
		UPDATE system_users SET open_id = #{openId} WHERE id = #{userId}
	</update>
    <update id="updateCusInfo">
		UPDATE system_users
		SET nickname = #{entity.nickname},avatar = #{entity.avatar}
		WHERE id = #{entity.id}
	</update>

    <select id="getUsersByRoleRescue" resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		SELECT
		distinct su.*
		FROM
		system_users su
		INNER JOIN system_user_role ur on ur.user_id = su.id
		INNER JOIN system_role role on ur.role_id = role.id
		where role.code = #{roleCode}
		<if test="searchValue !=null and searchValue!=''">
			and (mobile like concat('%',#{searchValue},'%')  or nickname  like concat('%',#{searchValue},'%') )
		</if>
		<if test="deptIds !=null and deptIds.size()>0">
			AND su.dept_id in
			<foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
				#{deptId}
			</foreach>
		</if>
	</select>

	<select id="getUsersByRoleRescueTwo" resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		SELECT
		distinct su.*
		FROM
		system_users su
		INNER JOIN system_user_role ur on ur.user_id = su.id
		INNER JOIN system_role role on ur.role_id = role.id
		where role.code = #{roleCode}
		<if test="searchValue !=null and searchValue!=''">
			and (mobile like concat('%',#{searchValue},'%')  or nickname  like concat('%',#{searchValue},'%') )
		</if>
		<if test="deptIds !=null and deptIds.size()>0">
			AND su.dept_id in
			<foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
				#{deptId}
			</foreach>
		</if>
	</select>

	<select id="getStudentNumberByUserId" resultType="Integer">
		select distinct count(1) number from drive_school_reservation_course where coach_id = #{userId}
	</select>

	<select id="getStudentNumber" resultType="String">
		select distinct phone from drive_school_pay where jxId = #{deptId} and state = '3'
	</select>
	<select id="selectUserByPhone" resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		select su.*
		from system_users su where su.mobile = #{phone} and su.deleted = '0'
	</select>
    <select id="getUserByMobileWithoutTenant"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		SELECT
			*
		FROM
			system_users
		WHERE
			mobile = #{phoneNumber}
		  <if test="tenantId!=null">
			and  tenant_id = #{tenantId}
		  </if>
		  AND deleted = 0
		limit 1
	</select>
	<select id="getUserByMobileTenantIgnore"
			resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		SELECT
		*
		FROM
		system_users
		WHERE
		mobile = #{phoneNumber}
		AND deleted = 0
		limit 1
	</select>
    <select id="selectByRoleId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
		SELECT
		*
		FROM
		system_users
		WHERE
		id IN (
		SELECT
		user_id
		FROM
		system_user_role
		WHERE
		role_id = #{roleId}
		)
	</select>
    <select id="queryUserIgnoreTenant" resultType="java.util.Map">
		SELECT *
		FROM system_users
		WHERE
		username = #{username}
		AND deleted = 0 AND tenant_id != 0
		limit 1
	</select>
</mapper>
