<?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.wisenergy.chnmuseum.party.mapper.TUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.chnmuseum.party.model.TUser">
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="password_hash" property="passwordHash"/>
        <result column="password_salt" property="passwordSalt"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="org_id" property="orgId"/>
        <result column="permanent" property="permanent"/>
        <result column="effective_date" property="effectiveDate"/>
        <result column="exired_date" property="exiredDate"/>
        <result column="status" property="status"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="we_chat" property="weChat"/>
        <result column="telephone" property="telephone"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="real_name" property="realName"/>
        <result column="type" property="type"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="area_id" property="areaId"/>
        <result column="password" property="password"/>
        <result column="org_name" property="orgName"/>
        <result column="area_name" property="areaName"/>
        <result column="org_code" property="orgCode"/>
        <collection fetchType="eager"  property="roleList" ofType="java.lang.String"
                    select="selectRoles" column="user_name">
            <result column="rid"/>
        </collection>
    </resultMap>


<!--    <resultMap id="roleMap" type="java.lang.String">-->
<!--        <result column="id"/>-->
<!--    </resultMap>-->

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_name, password_hash, password_salt, create_time, update_time, org_id, permanent, effective_date, exired_date, status, phone, email, we_chat, telephone, is_deleted, real_name,type
    </sql>

    <select id="selectByUsername" resultMap="BaseResultMap">
        select u.*,o.name org_name,a.full_name area_name,o.code org_code
        from t_user u
        left join t_organ o on o.id = u.org_id
        left join t_area a on u.area_id = a.id
        where u.user_name =#{userName}
    </select>

    <select id="getById" resultMap="BaseResultMap">
        select u.*,o.name org_name,a.full_name area_name
        from t_user u
        left join t_organ o on o.id = u.org_id
        left join t_area a on u.area_id = a.id
        where u.id =#{id}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        select u.*,o.name org_name,a.full_name area_name
        from t_user u
        left join t_organ o on o.id = u.org_id
        left join t_area a on u.area_id = a.id
        where 1 = 1
        <if test=" user.userName != null and user.userName != '' ">
            and u.user_name LIKE concat('%', #{user.userName}, '%')
        </if>
        <if test=" user.status != null and user.status != '' ">
            and u.status = #{user.status}
        </if>
        <if test=" user.auditStatus != null and user.auditStatus != '' ">
            and u.audit_status LIKE "TBC%"
        </if>
        <if test=" user.type != null and user.type != '' ">
            and u.type = #{user.type}
        </if>
        <if test=" user.orgId != null and user.orgId != '' ">
            and u.org_id = #{user.orgId}
        </if>
        <if test=" user.orgCode != null and user.orgCode != '' ">
            and o.code LIKE concat(#{user.orgCode}, '%')
        </if>
        <if test=" user.areaId != null and user.areaId != '' ">
            and u.area_id = #{user.areaId}
        </if>
        <if test=" user.areaName != null and user.areaName != '' ">
            and u.area_id LIKE concat(#{user.areaName}, '%')
        </if>
        and u.is_deleted = false
        order by u.create_time desc
    </select>


    <select id="getUserList" resultMap="BaseResultMap">
        select u.*,o.name org_name,a.full_name area_name
        from t_user u
        left join t_organ o on o.id = u.org_id
        left join t_area a on u.area_id = a.id
        where 1 = 1
        <if test=" user.userName != null and user.userName != '' ">
            and u.user_name LIKE concat('%', #{user.userName}, '%')
        </if>
        <if test=" user.status != null and user.status != '' ">
            and u.status = #{user.status}
        </if>
        <if test=" user.auditStatus != null and user.auditStatus != '' ">
            and u.audit_status LIKE "TBC%"
        </if>
        <if test=" user.type != null and user.type != '' ">
            and u.type = #{user.type}
        </if>
        <if test=" user.orgId != null and user.orgId != '' ">
            and u.org_id = #{user.orgId}
        </if>
        <if test="user.orgCode != null and user.orgCode != '' ">
            and o.code LIKE concat(#{user.orgCode}, '%')
        </if>
        <if test=" user.areaId != null and user.areaId != '' ">
            and u.area_id = #{user.areaId}
        </if>
        and u.is_deleted = false
        order by u.create_time desc
    </select>

    <select id="selectRoles" resultType="java.lang.String">
        select r.id rid
        from t_user u
        left join t_organ o on o.id = u.org_id
        left join t_user_role_link l on l.user_id = u.id
        left join t_role r on l.role_id = r.id
        where 1=1
        <if test=" userName != null and userName != '' ">
            and u.user_name =#{userName}
        </if>
    </select>

</mapper>