<?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.tuoheng.admin.mapper.UserMapper">
    
    <resultMap type="com.tuoheng.admin.entity.User" id="UserResult">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="code"    column="code"    />
        <result property="realname"    column="realname"    />
        <result property="nickname"    column="nickname"    />
        <result property="gender"    column="gender"    />
        <result property="avatar"    column="avatar"    />
        <result property="mobile"    column="mobile"    />
        <result property="email"    column="email"    />
        <result property="birthday"    column="birthday"    />
        <result property="deptId"    column="dept_id"    />
        <result property="provinceCode"    column="province_code"    />
        <result property="cityCode"    column="city_code"    />
        <result property="districtCode"    column="district_code"    />
        <result property="streetCode"    column="street_code"    />
        <result property="address"    column="address"    />
        <result property="cityName"    column="city_name"    />
        <result property="username"    column="username"    />
        <result property="password"    column="password"    />
        <result property="driverType"    column="driver_type"    />
        <result property="driverCode"    column="driver_code"    />
        <result property="salt"    column="salt"    />
        <result property="intro"    column="intro"    />
        <result property="status"    column="status"    />
        <result property="authorize"    column="authorize"    />
        <result property="subscribeTimes"    column="subscribe_times"    />
        <result property="note"    column="note"    />
        <result property="sort"    column="sort"    />
        <result property="loginNum"    column="login_num"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginTime"    column="login_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
        <result property="roleId"    column="role_id"    />
        <result property="dataPermission"    column="data_permission"    />
        <result property="clientId"    column="client_id"    />
    </resultMap>

    <sql id="selectUserVo">
        id, tenant_id, code, realname, nickname, gender, avatar, mobile, email, birthday, dept_id, province_code, city_code, district_code, street_code, address, city_name,
        username, password, driver_type, driver_code, salt, intro, status, authorize, subscribe_times, note, sort, login_num, login_ip, login_time, create_user, create_time, update_user, update_time,
        role_id, data_permission, client_id, mark
    </sql>

    <update id="updateByIdList" parameterType="hashmap">
        update th_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="mark != null"> mark = #{mark}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
        </trim>
        <where>
            <if test="idList != null and idList.size() > 0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>

    <update id="updateDeptIdByIdList" parameterType="hashmap">
        update th_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null"> dept_id = #{deptId}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
        </trim>
        <where>
            <if test="idList != null and idList.size() > 0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>

    <select id="selectPageList" parameterType="com.tuoheng.admin.request.user.QueryUserPageListRequest" resultMap="UserResult">
        select <include refid="selectUserVo"/>
        from th_user
        <where>
            <if test="1 == 1"> and mark = 1 and role_id != 1 </if>
            <if test="request.tenantId != null and request.tenantId != ''"> and tenant_id = #{request.tenantId} </if>
            <if test="request.username != null and request.username != ''"> and username like concat('%', #{request.username}, '%') </if>
            <if test="request.realname != null and request.realname != ''"> and realname like concat('%', #{request.realname}, '%') </if>
            <if test="request.roleId != null and request.roleId != 0"> and role_id = #{request.roleId} </if>
            <if test="request.deptId != null and request.deptId != ''"> and dept_id = #{request.deptId} </if>
            <if test="request.deptIdList != null and request.deptIdList.size() > 0">
                and dept_id in
                <foreach item="deptId" collection="request.deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

</mapper>