<?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.vetti.hotake.mapper.HotakeSecurityPasswordHistoryMapper">
    
    <resultMap type="HotakeSecurityPasswordHistory" id="HotakeSecurityPasswordHistoryResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="passwordHash"    column="password_hash"    />
        <result property="changedAt"    column="changed_at"    />
        <result property="changedBy"    column="changed_by"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectHotakeSecurityPasswordHistoryVo">
        select id, user_id, password_hash, changed_at, changed_by, ip_address,
               del_flag, create_by, create_time, update_by, update_time, remark 
        from hotake_security_password_history
    </sql>

    <select id="selectHotakeSecurityPasswordHistoryList" parameterType="HotakeSecurityPasswordHistory" resultMap="HotakeSecurityPasswordHistoryResult">
        <include refid="selectHotakeSecurityPasswordHistoryVo"/>
        <where>
            del_flag = '0'
            <if test="userId != null"> and user_id = #{userId}</if>
        </where>
        order by changed_at desc
    </select>
    
    <select id="selectHotakeSecurityPasswordHistoryById" parameterType="Long" resultMap="HotakeSecurityPasswordHistoryResult">
        <include refid="selectHotakeSecurityPasswordHistoryVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectRecentPasswordHistory" resultMap="HotakeSecurityPasswordHistoryResult">
        <include refid="selectHotakeSecurityPasswordHistoryVo"/>
        where user_id = #{userId} and del_flag = '0'
        order by changed_at desc
        limit #{limit}
    </select>
        
    <insert id="insertHotakeSecurityPasswordHistory" parameterType="HotakeSecurityPasswordHistory" useGeneratedKeys="true" keyProperty="id">
        insert into hotake_security_password_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="passwordHash != null">password_hash,</if>
            <if test="changedAt != null">changed_at,</if>
            <if test="changedBy != null">changed_by,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="passwordHash != null">#{passwordHash},</if>
            <if test="changedAt != null">#{changedAt},</if>
            <if test="changedBy != null">#{changedBy},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateHotakeSecurityPasswordHistory" parameterType="HotakeSecurityPasswordHistory">
        update hotake_security_password_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="passwordHash != null">password_hash = #{passwordHash},</if>
            <if test="changedAt != null">changed_at = #{changedAt},</if>
            <if test="changedBy != null">changed_by = #{changedBy},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteHotakeSecurityPasswordHistoryById" parameterType="Long">
        update hotake_security_password_history set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteHotakeSecurityPasswordHistoryByIds" parameterType="String">
        update hotake_security_password_history set del_flag = '2' where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
