<?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.ho.user.mapper.SysUserMapper">


    <sql id="Base_Column_List">
        id
        , username, salt, `password`, phone, dept_id, group_id, remark,real_name, nick_name, email, `status`,
    sex, deleted, create_id, update_id, create_where, create_time, update_time,user_level,period_of_validity
    </sql>

    <!--  <select id="getUserInfoByName" resultMap="BaseResultMap">-->
    <!--    SELECT <include refid="Base_Column_List"></include>-->
    <!--    FROM sys_user-->
    <!--    WHERE username=#{userName}-->
    <!--    AND deleted=1-->
    <!--  </select>-->

    <select id="getUserInfoByName" resultType="com.ho.user.api.entity.SysUser">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys_user
        <where>
            and username = #{username}
            AND deleted=1
        </where>
    </select>

    <select id="getUserInfoListByName" resultType="com.ho.user.api.entity.SysUser">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys_user
        <where>
            and username LIKE concat('%',#{username},'%')
            <if test="groupId !=null">
                and group_id = #{groupId}
            </if>
            AND deleted=1
        </where>

    </select>

    <select id="getUserInfoByPhone" resultType="com.ho.user.api.entity.SysUser">
        SELECT
        <include refid="Base_Column_List"></include>
        FROM sys_user
        WHERE phone=#{phone}
        AND deleted=1
    </select>

    <select id="selectByPrimaryKey" parameterType="java.lang.String" resultType="com.ho.user.api.entity.SysUser">
        select
        <include refid="Base_Column_List"/>
        from sys_user
        where id = #{id}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete
        from sys_user
        where id = #{id}
    </delete>
    <insert id="insert">
        insert into sys_user (id, username, salt,
                              `password`, phone, dept_id,
                              real_name, nick_name, email,
                              `status`, sex, deleted, remark,
                              create_id, update_id, create_where,
                              create_time, update_time)
        values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{salt,jdbcType=VARCHAR},
                #{password,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{deptId,jdbcType=INTEGER},
                #{realName,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
                #{status}, #{sex}, #{deleted}, #{remark}, #{createId,jdbcType=VARCHAR},
                #{updateId,jdbcType=VARCHAR}, #{createWhere},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
    </insert>

    <insert id="insertSelective">
        insert into sys_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="username != null">
                username,
            </if>
            <if test="salt != null">
                salt,
            </if>
            <if test="password != null">
                `password`,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="deptId != null">
                dept_id,
            </if>
            <if test="groupId != null">
                group_id,
            </if>
            <if test="realName != null">
                real_name,
            </if>
            <if test="nickName != null">
                nick_name,
            </if>
            <if test="email != null">
                email,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="sex != null">
                sex,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="createId != null">
                create_id,
            </if>
            <if test="updateId != null">
                update_id,
            </if>
            <if test="createWhere != null">
                create_where,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="userLevel != null">
                user_level,
            </if>
            <if test="periodOfValidity != null">
                period_of_validity,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="username != null">
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="salt != null">
                #{salt,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                #{phone,jdbcType=VARCHAR},
            </if>
            <if test="deptId != null">
                #{deptId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                #{groupId,jdbcType=INTEGER},
            </if>
            <if test="realName != null">
                #{realName,jdbcType=VARCHAR},
            </if>
            <if test="nickName != null">
                #{nickName,jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                #{email,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="sex != null">
                #{sex},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="createId != null">
                #{createId,jdbcType=VARCHAR},
            </if>
            <if test="updateId != null">
                #{updateId,jdbcType=VARCHAR},
            </if>
            <if test="createWhere != null">
                #{createWhere},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="userLevel != null">
                #{userLevel},
            </if>
            <if test="periodOfValidity != null">
                #{periodOfValidity},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.user.api.entity.SysUser">
        update sys_user
        <set>
            <if test="username != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="salt != null">
                salt = #{salt,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                `password` = #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=VARCHAR},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                group_id = #{groupId},
            </if>
            <if test="realName != null">
                real_name = #{realName,jdbcType=VARCHAR},
            </if>
            <if test="nickName != null">
                nick_name = #{nickName,jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                email = #{email,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="sex != null">
                sex = #{sex},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="createId != null">
                create_id = #{createId,jdbcType=VARCHAR},
            </if>
            <if test="updateId != null">
                update_id = #{updateId,jdbcType=VARCHAR},
            </if>
            <if test="createWhere != null">
                create_where = #{createWhere},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="userLevel != null">
                user_level = #{userLevel},
            </if>
            <if test="periodOfValidity != null">
                period_of_validity = #{periodOfValidity},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.user.api.entity.SysUser">
        update sys_user
        set username     = #{username,jdbcType=VARCHAR},
            salt         = #{salt,jdbcType=VARCHAR},
            `password`   = #{password,jdbcType=VARCHAR},
            phone        = #{phone,jdbcType=VARCHAR},
            dept_id      = #{deptId,jdbcType=INTEGER},
            real_name    = #{realName,jdbcType=VARCHAR},
            nick_name    = #{nickName,jdbcType=VARCHAR},
            email        = #{email,jdbcType=VARCHAR},
            `status`     = #{status},
            sex          = #{sex},
            deleted      = #{deleted},
            create_id    = #{createId,jdbcType=VARCHAR},
            update_id    = #{updateId,jdbcType=VARCHAR},
            create_where = #{createWhere},
            create_time  = #{createTime,jdbcType=TIMESTAMP},
            update_time  = #{updateTime,jdbcType=TIMESTAMP},
            remark       = #{remark}
        where id = #{id,jdbcType=VARCHAR}
    </update>


    <select id="selectAll" resultType="com.ho.user.api.entity.SysUser" parameterType="com.ho.user.vo.req.UserPageReqVO">
        select
        <include refid="Base_Column_List"></include>
        from sys_user
        <where>
            deleted=1
            <if test="username!=null and username!=''">
                AND username LIKE concat('%',#{username},'%')
            </if>
            <if test="nickName!=null and nickName!=''">
                AND nick_name LIKE concat('%',#{nickName},'%')
            </if>
            <if test="startTime!=null and startTime!=''">
                AND create_time &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!=''">
                AND create_time &lt;= #{endTime}
            </if>
            <if test="userId!=null and userId!='' ">
                AND id=#{userId}
            </if>
            <if test="status!=null and status!='' or status==0 ">
                AND `status`=#{status}
            </if>
            <if test="superRole!=null and superRole!='' ">
                AND group_id= 0
            </if>
        </where>
    </select>

    <select id="selectUserInfoByDeptIds" resultType="com.ho.user.api.entity.SysUser">
        select
        <include refid="Base_Column_List"/>
        from sys_user
        <where>
            <if test="list!=null and list.size() &gt; 0">
                and dept_id in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="vo.userIds != null and vo.userIds.size() &gt; 0">
                and id in
                <foreach collection="vo.userIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="vo.username != null">
                and username LIKE CONCAT('%',#{vo.username},'%')
            </if>
            <if test="vo.realName != null">
                and real_name LIKE CONCAT('%',#{vo.realName},'%')
            </if>
            <if test="vo.phone != null">
                and phone LIKE CONCAT('%',#{vo.phone},'%')
            </if>
            <if test="vo.status != null">
                and `status` = #{vo.status}
            </if>
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            and deleted=1
        </where>
    </select>


    <select id="getUserListByDeptId" resultType="com.ho.user.api.entity.SysUser">
        select
        <include refid="Base_Column_List"></include>
        from sys_user
        where dept_id=#{deptId}
        and deleted = 1
        and status = 1
    </select>

    <select id="selectByIds" resultType="com.ho.user.api.entity.SysUser">
        select
        <include refid="Base_Column_List"></include>
        from sys_user
        <where>
            deleted=1
            <if test="list != null and list.size() != 0">
                and id in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="vo.depts != null and vo.depts.size() &gt; 0">
                and dept_id in
                <foreach collection="vo.depts" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="vo.groupId !=null">
                and group_id = #{vo.groupId}
            </if>
        </where>
    </select>

    <select id="selectPostUserIds" resultType="com.ho.user.api.entity.SysUser">
        select
        user.*,post.name as post_name ,post.id as post_id
        from sys_user user
        left JOIN sys_user_position user_post on user_post.user_id = user.id
        left JOIN sys_position post on post.id = user_post.position_id
        <where>
            user.deleted = 1
            <if test="userIds != null and userIds.size() != 0">
                and user.id in
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="postIds != null and postIds.size() != 0">
                and post.id in
                <foreach collection="postIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getUserTheme" resultType="com.ho.user.vo.resp.SysUserThemeRespVo">
        select b.theme,b.title from sys_user_theme a left join sys_theme b on a.theme = b.title  where a.id = #{userId}
    </select>

    <select id="getSysTheme" resultType="com.ho.user.entity.SysTheme">
        select title,theme,name from sys_theme
    </select>

    <delete id="deletedUserTheme">
        delete from sys_user_theme where id = #{userId}
    </delete>

    <insert id="addUserTheme" parameterType="com.ho.user.vo.req.SysUserThemeReqVo">
        insert into sys_user_theme (id,theme) values(#{userId},#{title})
    </insert>


    <update id="deletedUsers" parameterType="com.ho.user.api.entity.SysUser">
        update sys_user
        <set>
            deleted = 0,
            <if test="sysUser.updateId != null">
                update_id = #{sysUser.updateId},
            </if>
            <if test="sysUser.updateTime != null">
                update_time = #{sysUser.updateTime},
            </if>
        </set>
        where id in
        <foreach collection="list" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </update>
</mapper>
