<?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.dk.mdm.mapper.core.UserLockedMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        user_id, is_locked, miss_count, locked_time
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.core.UserLocked">
        <id column="user_id" property="userId"/>
                <result column="is_locked" property="isLocked"/>
                <result column="miss_count" property="missCount"/>
            <result column="locked_time" property="lockedTime"
                    typeHandler="com.dk.common.infrastructure.handler.TimestampTypeHandler"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="isLocked != null">
                AND is_locked = #{isLocked}
            </if>
            <if test="missCount != null and missCount != ''">
                AND miss_count = #{missCount}
            </if>
            <if test="lockedTime != null">
                AND locked_time = #{lockedTime}
            </if>
        </where>
    </sql>

    <!-- 查询表t_user_locked，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM core.t_user_locked
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_user_locked，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM core.t_user_locked
        <include refid="Condition"/>
    </select>

    <!--    添加密码错误次数-->
    <update id="addMissCount">
        update core.t_user_locked
        set miss_count = miss_count + 1
        where user_id = #{userId}
    </update>

    <!--    锁定用户-->
    <update id="lockUser">
        update core.t_user_locked
        set is_locked   = true,
            locked_time = now()
        where user_id = #{userId}
    </update>

    <!--    用户解锁-->
    <update id="unLockUser">
        update core.t_user_locked
        set is_locked   = false,
            locked_time = null,
            miss_count  = 0
        where user_id = #{userId}
    </update>
</mapper>
