<?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.lframework.xingyun.template.inner.mappers.system.SysUserMapper">

    <sql id="SysUserDto_sql">
        SELECT
            u.*
        FROM sys_user AS u
    </sql>

    <select id="query" resultType="com.lframework.xingyun.template.core.entity.SysUser">
        <include refid="SysUserDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND u.code = #{vo.code}
                </if>
                <if test="vo.deptId != null and vo.deptId != ''">
                    AND u.id IN (SELECT user_id FROM sys_user_dept WHERE dept_id = #{vo.deptId})
                </if>
                <if test="vo.username != null and vo.username != ''">
                    AND u.username LIKE CONCAT('%', #{vo.username}, '%')
                </if>
                <if test="vo.name != null and vo.name != ''">
                    AND u.name LIKE CONCAT('%', #{vo.name}, '%')
                </if>
                <if test="vo.available != null">
                    AND u.available = #{vo.available}
                </if>
                <if test="vo.lockStatus != null">
                    AND u.lock_status = #{vo.lockStatus}
                </if>
            </if>
        </where>
        ORDER BY u.code
    </select>

    <select id="findById" resultType="com.lframework.xingyun.template.core.entity.SysUser">
        <include refid="SysUserDto_sql"/>
        WHERE u.id = #{id}
    </select>
    <select id="selector" resultType="com.lframework.xingyun.template.core.entity.SysUser">
        <include refid="SysUserDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND u.code = #{vo.code}
                </if>
                <if test="vo.username != null and vo.username != ''">
                    AND u.username LIKE CONCAT('%', #{vo.username}, '%')
                </if>
                <if test="vo.name != null and vo.name != ''">
                    AND u.name LIKE CONCAT('%', #{vo.name}, '%')
                </if>
                <if test="vo.available != null">
                    AND u.available = #{vo.available}
                </if>
            </if>
        </where>
        ORDER BY u.code
    </select>
    <update id="updatePassword">
        UPDATE sys_user
        SET password = #{password}
        WHERE id = #{userId}
    </update>

    <update id="updateEmail">
        UPDATE sys_user
        SET email = #{email}
        WHERE id = #{userId}
    </update>

    <update id="updateTelephone">
        UPDATE sys_user
        SET telephone = #{telephone}
        WHERE id = #{userId}
    </update>
    <update id="lockById">
        UPDATE sys_user
        SET lock_status = TRUE
        WHERE id = #{id}
    </update>
    <update id="unlockById">
        UPDATE sys_user
        SET lock_status = FALSE
        WHERE id = #{id}
    </update>

    <select id="getInfo" resultType="com.lframework.xingyun.template.inner.dto.UserInfoDto">
        SELECT
            id, username, code, name, email, telephone, gender
        FROM sys_user
        WHERE id = #{userId}
    </select>
</mapper>
