<?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.bunny.services.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.dao.entity.system.AdminUser">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="nick_name" property="nickName"/>
        <result column="email" property="email"/>
        <result column="phone" property="phone"/>
        <result column="password" property="password"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="summary" property="summary"/>
        <result column="ip_address" property="ipAddress"/>
        <result column="ip_region" property="ipRegion"/>
        <result column="status" property="status"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, username, nick_name, email, phone, password, avatar, sex, summary, ip_address, ip_region, status, create_user, create_time, update_time, update_user, is_deleted
    </sql>

    <!-- 分页查询用户信息内容 -->
    <select id="selectListByPage" resultType="cn.bunny.dao.entity.system.AdminUserAndDept">
        select
        user.*,user_dept.dept_id
        from sys_user user left join sys_user_dept user_dept on user.id = user_dept.user_id
        <where>
            <if test="dto.username != null and dto.username != ''">
                and username like CONCAT('%',#{dto.username},'%')
            </if>
            <if test="dto.nickName != null and dto.nickName != ''">
                and nick_name like CONCAT('%',#{dto.nickName},'%')
            </if>
            <if test="dto.email != null and dto.email != ''">
                and email like CONCAT('%',#{dto.email},'%')
            </if>
            <if test="dto.phone != null and dto.phone != ''">
                and phone like CONCAT('%',#{dto.phone},'%')
            </if>
            <if test="dto.sex != null and dto.sex != ''">
                and sex = #{dto.sex}
            </if>
            <if test="dto.summary != null and dto.summary != ''">
                and summary like CONCAT('%',#{dto.summary},'%')
            </if>
            <if test="dto.status != null and dto.status != ''">
                and status = #{dto.status}
            </if>
            <if test="dto.deptIds != null and dto.deptIds.size() > 0">
                and user_dept.dept_id in
                <foreach collection="dto.deptIds" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>

    </select>

    <!-- 查询用户 -->
    <select id="queryUser" resultType="cn.bunny.dao.entity.system.AdminUser">
        select *
        from sys_user
        <where>
            <if test="keyword != null and keyword != ''">
                username like CONCAT('%', #{keyword}, '%')
                or nick_name like CONCAT('%', #{keyword}, '%')
                or email like CONCAT('%', #{keyword}, '%')
                or phone like CONCAT('%', #{keyword}, '%')
            </if>
        </where>
    </select>

    <!-- 物理删除用户信息 -->
    <delete id="deleteBatchIdsWithPhysics">
        delete
        from sys_user
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

</mapper>
