<?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.yida.data.system.mapper.UserMapper">
    <select id="findUserDetailPage" parameterType="com.yida.data.common.core.entity.system.SystemUser"
            resultType="com.yida.data.common.core.entity.system.SystemUser">
        SELECT
        u.user_id userId,
        u.username,
        u.nickname,
        u.email,
        u.mobile,
        u.status,
        u.create_time createDate,
        u.ssex sex,
        u.dept_id deptId,
        u.last_login_time lastLoginTime,
        u.modify_time updateDate,
        u.description,
        u.avatar,
        u.agent_id,
        u.user_origin,
        d.dept_name deptName,
        GROUP_CONCAT(r.role_id) roleId,
        GROUP_CONCAT(r.role_name) roleName
        FROM
        t_user u
        LEFT JOIN t_dept d ON (u.dept_id = d.dept_id)
        LEFT JOIN t_user_role ur ON (u.user_id = ur.user_id)
        LEFT JOIN t_role r ON (r.role_id = ur.role_id)
        WHERE 1 = 1
        <if test="user.username != null and user.username != ''">
            AND u.username like concat('%', #{user.username},'%')
        </if>
        <if test="user.nickname != null and user.nickname != ''">
            AND u.nickname like concat('%', #{user.nickname},'%')
        </if>
        <if test="user.deptId != null and user.deptId != ''">
            AND d.dept_id = #{user.deptId}
        </if>
        <if test="user.sex != null and user.sex != ''">
            AND u.ssex = #{user.sex}
        </if>
        <if test="user.status != null and user.status != ''">
            AND u.status = #{user.status}
        </if>
        <if test="user.mobile != null and user.mobile != ''">
            AND u.mobile = #{user.mobile}
        </if>
        <if test="user.deptName != null and user.deptName != ''">
            AND d.dept_name like CONCAT('%',#{user.deptName},'%')
        </if>
        <if test="user.createTimeFrom != null and user.createTimeFrom != ''">
            And DATE_FORMAT(u.create_time, '%Y-%m-%d') &gt;= #{user.createTimeFrom}
        </if>
        <if test="user.createTimeTo != null and user.createTimeTo != ''">
            And DATE_FORMAT(u.create_time, '%Y-%m-%d') &lt;= #{user.createTimeTo}
        </if>
        <if test='user.userOrigin != null and user.userOrigin != ""'>
            AND u.user_origin = #{user.userOrigin}
        </if>
        group by u.username,u.user_id,u.email,u.mobile,u.status,u.create_time,u.ssex,
        u.dept_id,u.last_login_time,u.modify_time,u.description,u.avatar
        order by u.CREATE_TIME desc
    </select>

    <select id="findUserDetail" parameterType="com.yida.data.common.core.entity.system.SystemUser"
            resultType="com.yida.data.common.core.entity.system.SystemUser">
        SELECT
        u.user_id userId,
        u.username,
        u.password,
        u.email,
        u.mobile,
        u.status,
        u.create_time createTime,
        u.ssex sex,
        u.dept_id deptId,
        u.last_login_time lastLoginTime,
        u.modify_time modifyTime,
        u.description,
        u.avatar,
        d.dept_name deptName,
        GROUP_CONCAT(r.role_id) roleId,
        GROUP_CONCAT(r.ROLE_NAME) roleName
        FROM
        t_user u
        LEFT JOIN t_dept d ON (u.dept_id = d.dept_id)
        LEFT JOIN t_user_role ur ON (u.user_id = ur.user_id)
        LEFT JOIN t_role r ON r.role_id = ur.role_id
        WHERE 1 = 1
        <if test="user.username != null and user.username != ''">
            AND u.username = #{user.username}
        </if>
        <if test="user.deptId != null and user.deptId != ''">
            AND d.dept_id = #{user.deptId}
        </if>
        <if test="user.sex != null and user.sex != ''">
            AND u.ssex = #{user.sex}
        </if>
        <if test="user.status != null and user.status != ''">
            AND u.status = #{user.status}
        </if>
        <if test="user.mobile != null and user.mobile != ''">
            AND u.mobile = #{user.mobile}
        </if>
        <if test="user.createTimeFrom != null and user.createTimeFrom != ''">
            And u.create_time &gt; #{user.createTimeFrom}
        </if>
        <if test="user.createTimeTo != null and user.createTimeTo != ''">
            And u.create_time &lt; #{user.createTimeTo}
        </if>
        group by u.username,u.user_id,u.email,u.mobile,u.status,u.create_time,u.ssex,
        u.dept_id,u.last_login_time,u.modify_time,u.description,u.avatar
    </select>

    <select id="findUserByUsername" resultType="com.yida.data.common.core.entity.system.SystemUser">
        select a.*, group_concat(b.ROLE_ID) roleId
        from t_user a
        left join t_user_role b on a.USER_ID = b.USER_ID
        where a.USERNAME=#{username}
        group by a.USER_ID
    </select>
    
    <select id="findUserByMobile" resultType="com.yida.data.common.core.entity.system.SystemUser">
        select a.*, group_concat(b.ROLE_ID) roleId
        from t_user a
        left join t_user_role b on a.USER_ID = b.USER_ID
        where a.MOBILE=#{mobile}
        group by a.USER_ID
    </select>
    
    <!-- 获取代理商管理员账号详情 -->
	<select id="getAgentAdminAccountInfo"
            resultType="com.yida.data.common.core.entity.system.SystemUser">
        SELECT
            *
        FROM
            t_user u
        LEFT JOIN t_user_role ur ON ( u.user_id = ur.user_id )
        LEFT JOIN t_role r ON ( r.role_id = ur.role_id )
        WHERE
            u.agent_id = #{agentId}
          AND r.ROLE_PERMS = #{rolePerm}
    </select>

    <!-- 代理商查询学校管理员账号列表数据 -->
	<select id="listSchoolAdminAccountPage"
			resultType="com.yida.data.system.vo.SchoolAdminAccountSelectPageVO">
        SELECT
            u.USER_ID as user_id,
            u.USERNAME as username,
            u.nickname as nickname,
            u.SSEX as sex,
            u.EMAIL as email,
            u.STATUS as status,
            u.CREATE_TIME as create_date
        FROM
            t_user u
        LEFT JOIN t_user_role ur ON ( u.user_id = ur.user_id )
        LEFT JOIN t_role r ON ( r.role_id = ur.role_id )
        WHERE
            u.DEPT_ID = #{dto.schoolId}
        <if test='dto.userName != null and dto.userName != ""'>
            and u.USERNAME LIKE CONCAT('%', #{dto.userName}, '%')
        </if>
        AND r.ROLE_PERMS = #{rolePerm}
    </select>
    <select id="findRoleId" resultType="java.lang.Long">
    SELECT DISTINCT
	    MENU_ID
    FROM
	    `t_role_menu`
    WHERE
	    ROLE_ID IN
	    <foreach collection="roles" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="findUserPage"
            resultType="com.yida.data.common.core.entity.system.SystemUser">
        SELECT
        u.user_id userId,
        u.username,
        u.nickname,
        u.email,
        u.mobile,
        u.status,
        u.create_time createDate,
        u.ssex sex,
        u.dept_id deptId,
        u.last_login_time lastLoginTime,
        u.modify_time updateDate,
        u.description,
        u.avatar,
        u.agent_id,
        u.user_origin,
        d.dept_name deptName,
        GROUP_CONCAT(r.role_id) roleId,
        GROUP_CONCAT(r.role_name) roleName
        FROM
        (
            SELECT DISTINCT
                *
            FROM
                t_user
            WHERE
                DEPT_ID IN
            <foreach collection="childIdList" open="(" close=")" separator="," item="deptId">
                #{deptId}
            </foreach>
        ) u
        LEFT JOIN t_dept d ON (u.dept_id = d.dept_id)
        LEFT JOIN t_user_role ur ON (u.user_id = ur.user_id)
        LEFT JOIN t_role r ON (r.role_id = ur.role_id)
        <where>
            <if test="user.username != null and user.username != ''">
                AND u.username like concat('%', #{user.username},'%')
            </if>
            <if test="user.nickname != null and user.nickname != ''">
                AND u.nickname like concat('%', #{user.nickname},'%')
            </if>
            <if test="user.deptId != null and user.deptId != ''">
                AND d.dept_id = #{user.deptId}
            </if>
            <if test="user.sex != null and user.sex != ''">
                AND u.ssex = #{user.sex}
            </if>
            <if test="user.status != null and user.status != ''">
                AND u.status = #{user.status}
            </if>
            <if test="user.mobile != null and user.mobile != ''">
                AND u.mobile = #{user.mobile}
            </if>
            <if test="user.deptName != null and user.deptName != ''">
                AND d.dept_name like CONCAT('%',#{user.deptName},'%')
            </if>
            <if test="user.createTimeFrom != null and user.createTimeFrom != ''">
                And DATE_FORMAT(u.create_time, '%Y-%m-%d') &gt;= #{user.createTimeFrom}
            </if>
            <if test="user.createTimeTo != null and user.createTimeTo != ''">
                And DATE_FORMAT(u.create_time, '%Y-%m-%d') &lt;= #{user.createTimeTo}
            </if>
            <if test='user.userOrigin != null'>
                AND u.user_origin = #{user.userOrigin}
            </if>
        </where>
        group by u.username,u.user_id,u.email,u.mobile,u.status,u.create_time,u.ssex,
        u.dept_id,u.last_login_time,u.modify_time,u.description,u.avatar
        order by u.CREATE_TIME desc
    </select>

</mapper>
