<?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.qiaoba.module.system.mapper.SysUserMapper">

    <resultMap type="com.qiaoba.api.system.entity.vo.SysUserVo" id="SysUserVoResult">
        <id property="userId" column="user_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="username" column="username"/>
        <result property="nickname" column="nickname"/>
        <result property="email" column="email"/>
        <result property="phone" column="phone"/>
        <result property="sex" column="sex"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.qiaoba.module.system.templates.SysUserExport" id="ExportData">
        <id property="userId" column="user_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="username" column="username"/>
        <result property="nickname" column="nickname"/>
        <result property="email" column="email"/>
        <result property="phone" column="phone"/>
        <result property="gender" column="gender"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <collection property="roles" ofType="string" resultMap="RoleResult"/>
        <collection property="posts" ofType="string" resultMap="PostResult"/>
    </resultMap>

    <resultMap id="RoleResult" type="string">
        <result property="roleName" column="role_name"/>
    </resultMap>

    <resultMap id="PostResult" type="string">
        <result property="postName" column="post_name"/>
    </resultMap>

    <sql id="selectUserVo">
        SELECT u.user_id,u.dept_id,u.username,u.nickname,u.gender,u.phone,u.status,u.email,u.create_time,t2.dept_name
        FROM sys_user u
        left join sys_dept t2
        on u.dept_id = t2.dept_id
        <where>
            <if test="param.username != null and param.username != ''">
                AND u.username like concat('%', #{param.username}, '%')
            </if>
            <if test="param.nickname != null and param.nickname != ''">
                AND u.nickname like concat('%', #{param.nickname}, '%')
            </if>
            <if test="param.status != null and param.status != ''">
                AND u.status = #{param.status}
            </if>
            <if test="param.deptId != null and param.deptId != ''">
                AND u.dept_id = #{param.deptId}
            </if>
            <if test="param.phone != null and param.phone != ''">
                AND u.phone like concat('%', #{param.phone}, '%')
            </if>
            <if test="param.beginTime != null">
                AND u.create_time &gt;= #{param.beginTime}
            </if>
            <if test="param.endTime != null">
                AND u.create_time &lt;= #{param.endTime}
            </if>
            ${param.dataScope}
        </where>
    </sql>

    <select id="selectVoPageList" resultMap="SysUserVoResult">
        <include refid="selectUserVo"/>
    </select>

    <select id="selectExportData" resultMap="ExportData">
        select t1.user_id, t1.username, t1.nickname, t1.email, t1.phone, t1.gender, t1.status,t1.create_time,
        t2.dept_name,t4.role_name,t6.post_name
        from sys_user t1
        left join sys_dept t2 on t1.dept_id = t2.dept_id
        left join sys_user_role t3 on t1.user_id = t3.user_id
        left join sys_role t4 on t4.role_id = t3.role_id
        left join sys_user_post t5 on t5.user_id = t1.user_id
        left join sys_post t6 on t6.post_id = t5.post_id
        <where>
            <if test="param.username != null and param.username != ''">
                AND t1.username like concat('%', #{param.username}, '%')
            </if>
            <if test="param.nickname != null and param.nickname != ''">
                AND t1.nickname like concat('%', #{param.nickname}, '%')
            </if>
            <if test="param.status != null and param.status != ''">
                AND t1.status = #{param.status}
            </if>
            <if test="param.deptId != null and param.deptId != ''">
                AND t1.dept_id = #{param.deptId}
            </if>
            <if test="param.phone != null and param.phone != ''">
                AND t1.phone like concat('%', #{param.phone}, '%')
            </if>
            <if test="param.beginTime != null and param.beginTime != ''">
                AND t1.create_time &gt;= #{param.beginTime}
            </if>
            <if test="param.endTime != null and param.endTime != ''">
                AND t1.create_time &lt;= #{param.endTime}
            </if>
        </where>
    </select>

    <select id="selectAllocatedList" resultMap="SysUserVoResult">
        select distinct t1.user_id, t2.dept_name, t1.username, t1.nickname, t1.email, t1.phone, t1.status,t1.create_time
        from sys_user t1
        left join sys_dept t2
        on t1.dept_id = t2.dept_id
        left join sys_user_role t3
        on t1.user_id = t3.user_id
        left join sys_role t4
        on t4.role_id = t3.role_id
        where t4.role_id = #{param.roleId}
        <if test="param.username != null and param.username != ''">
            AND t1.username like concat('%', #{param.username}, '%')
        </if>
        <if test="param.phone != null and param.phone != ''">
            AND t1.phone like concat('%', #{param.phone}, '%')
        </if>
    </select>

    <select id="selectUnAllocatedList" resultMap="SysUserVoResult">
        select distinct t1.user_id, t2.dept_name, t1.username, t1.nickname, t1.email, t1.phone, t1.status,
        t1.create_time
        from sys_user t1
        left join sys_dept t2 on t1.dept_id = t2.dept_id
        left join sys_user_role t3 on t1.user_id = t3.user_id
        left join sys_role t4 on t4.role_id = t3.role_id
        where (t4.role_id != #{param.roleId} or t4.role_id IS NULL)
        and t1.user_id not in (select t1.user_id from sys_user t1 inner join sys_user_role t3 on t1.user_id = t3.user_id
        and
        t3.role_id = #{param.roleId})
        <if test="param.username != null and param.username != ''">
            AND t1.username like concat('%', #{param.username}, '%')
        </if>
        <if test="param.phone != null and param.phone != ''">
            AND t1.phone like concat('%', #{param.phone}, '%')
        </if>
    </select>

    <select id="selectVoList" resultMap="SysUserVoResult">
        <include refid="selectUserVo"/>
    </select>
    <select id="checkUsernameUnique" resultType="string">
        select username from sys_user where username = #{username}
        <if test="userId != null and userId != ''">and user_id != #{userId}</if>
    </select>

    <select id="checkPhoneUnique" resultType="string">
        select phone from sys_user where phone = #{phone}
        <if test="userId != null and userId != ''">and user_id != #{userId}</if>
    </select>

    <select id="checkEmailUnique" resultType="string">
        select email from sys_user where email = #{email}
        <if test="userId != null and userId != ''">and user_id != #{userId}</if>
    </select>

</mapper>
