<?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.testor.module.iam.dao.IamSysUserDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.iam.model.domain.IamSysUser">
        <id column="id" property="id"/>
        <id column="user_id" property="userId"/>
        <id column="mobile" property="mobile"/>
        <id column="name" property="name"/>
        <id column="main_department" property="mainDepartment"/>
        <id column="email" property="email"/>
        <id column="gender" property="gender"/>
        <id column="enable" property="enable"/>
        <id column="ad_account" property="adAccount"/>
        <id column="telephone" property="telephone"/>
        <id column="polity" property="polity"/>
        <id column="id_type" property="idType"/>
        <id column="id_card" property="idCard"/>
        <id column="level_code" property="levelCode"/>
        <id column="level_name" property="levelName"/>
        <id column="psn_cl" property="psnCl"/>
    </resultMap>

    <select id="querySysUserList" resultType="com.testor.module.sys.model.dto.SysUserListResponse">
        SELECT
            u.user_id,u.org_id,u.login_name,u.user_name,u.user_type,u.photo_url,u.sex,u.address,
            u.zip_code,u.phone_no,u.fax,u.email,u.status,u.org_name,u.age,u.dept_id,i.iam_user_id
        FROM t_sys_user u
        LEFT JOIN iam_user_mapping i ON u.user_id = i.user_id
        <where>
            <if test="param.loginName != null and param.loginName !='' ">
                and u.login_name like concat('%',#{param.loginName},'%')
            </if>
            <if test="param.userName != null and param.userName !='' ">
                and u.user_name like concat('%',#{param.userName},'%')
            </if>
            <if test="param.phoneNo != null and param.phoneNo !='' ">
                and u.phone_no like concat('%',#{param.userName},'%')
            </if>
            <if test="param.userType != null and param.userType !='' ">
                and u.user_type = #{param.userType}
            </if>
            <if test="param.orgIdList != null">
                and u.org_id in
                <foreach collection="param.orgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.statusList != null">
                and u.status in
                <foreach collection="param.statusList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.iamUserId != null and param.iamUserId !='' ">
                and i.iam_user_id like concat('%',#{param.iamUserId},'%')
            </if>
            <if test="param.orderBy != null and param.orderBy !='' ">
                ORDER BY concat(#{param.orderBy},' ',#{param.orderBy})
            </if>
        </where>
        <if test="param.limit!=null and param.limit!=''">
            LIMIT #{param.limit} OFFSET #{param.offset}
        </if>
    </select>

    <select id="querySysUserListCount" resultType="java.lang.Long">
        SELECT
        count(u.user_id)
        FROM t_sys_user u
        LEFT JOIN iam_user_mapping i ON u.user_id = i.user_id
        <where>
            <if test="param.loginName != null and param.loginName !='' ">
                and u.login_name like concat('%',#{param.loginName},'%')
            </if>
            <if test="param.userName != null and param.userName !='' ">
                and u.user_name like concat('%',#{param.userName},'%')
            </if>
            <if test="param.phoneNo != null and param.phoneNo !='' ">
                and u.phone_no like concat('%',#{param.userName},'%')
            </if>
            <if test="param.userType != null and param.userType !='' ">
                and u.user_type = #{param.userType}
            </if>
            <if test="param.iamUserId != null and param.iamUserId !='' ">
                and i.iam_user_id like concat('%',#{param.iamUserId},'%')
            </if>
            <if test="param.orgIdList != null">
                and u.org_id in
                <foreach collection="param.orgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.statusList != null">
                and u.status in
                <foreach collection="param.statusList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="pageUserList" resultType="com.testor.module.sys.model.dto.SysUserListResponse">
        SELECT
        u.user_id,
        u.org_id,
        u.login_name,
        u.user_name,
        u.user_type,
        ue.user_type_code,
        u.photo_url,
        u.sex,
        u.address,
        u.zip_code,
        u.phone_no,
        u.fax,
        u.email,
        u.STATUS,
        so.org_name,
        u.age,
        u.dept_id,
        d.org_name AS deptName,
        i.iam_user_id
        FROM
        t_sys_user u
        LEFT JOIN iam_user_mapping i ON u.user_id = i.user_id
        LEFT JOIN t_sys_org so ON so.org_id = u.org_id AND so.is_dept = 0
        LEFT JOIN t_sys_org d ON d.org_id = u.dept_id AND d.is_dept = 1
        LEFT JOIN t_sys_user_extend ue ON u.user_id = ue.user_id
        <where>
            u.STATUS != '1'
            <if test="param.loginName != null and param.loginName !='' ">
                and u.login_name like concat('%',#{param.loginName},'%')
            </if>
            <if test="param.userName != null and param.userName !='' ">
                and u.user_name like concat('%',#{param.userName},'%')
            </if>
            <if test="param.phoneNo != null and param.phoneNo !='' ">
                and u.phone_no like concat('%',#{param.phoneNo},'%')
            </if>
            <if test="param.userType != null and param.userType !='' ">
                and u.user_type = #{param.userType}
            </if>
            <if test="param.orgIdList != null">
                and u.org_id in
                <foreach collection="param.orgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.statusList != null">
                and u.status in
                <foreach collection="param.statusList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.iamUserId != null and param.iamUserId !='' ">
                and i.iam_user_id like concat('%',#{param.iamUserId},'%')
            </if>
            <if test="param.orderBy != null and param.orderBy !='' ">
                ORDER BY concat(#{param.orderBy},' ',#{param.orderType})
            </if>
        </where>
    </select>
</mapper>
