<?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.HotakeSecuritySettingsMapper">
    
    <resultMap type="HotakeSecuritySettings" id="HotakeSecuritySettingsResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="twoFactorEnabled"    column="two_factor_enabled"    />
        <result property="twoFactorSecret"    column="two_factor_secret"    />
        <result property="twoFactorType"    column="two_factor_type"    />
        <result property="backupCodes"    column="backup_codes"    />
        <result property="passwordChangedAt"    column="password_changed_at"    />
        <result property="passwordResetToken"    column="password_reset_token"    />
        <result property="passwordResetExpires"    column="password_reset_expires"    />
        <result property="failedLoginAttempts"    column="failed_login_attempts"    />
        <result property="accountLockedUntil"    column="account_locked_until"    />
        <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="selectHotakeSecuritySettingsVo">
        select id, user_id, two_factor_enabled, two_factor_secret, two_factor_type, backup_codes,
               password_changed_at, password_reset_token, password_reset_expires, 
               failed_login_attempts, account_locked_until,
               del_flag, create_by, create_time, update_by, update_time, remark 
        from hotake_security_settings
    </sql>

    <select id="selectHotakeSecuritySettingsList" parameterType="HotakeSecuritySettings" resultMap="HotakeSecuritySettingsResult">
        <include refid="selectHotakeSecuritySettingsVo"/>
        <where>
            del_flag = '0'
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="twoFactorEnabled != null"> and two_factor_enabled = #{twoFactorEnabled}</if>
        </where>
    </select>
    
    <select id="selectHotakeSecuritySettingsById" parameterType="Long" resultMap="HotakeSecuritySettingsResult">
        <include refid="selectHotakeSecuritySettingsVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectHotakeSecuritySettingsByUserId" parameterType="Long" resultMap="HotakeSecuritySettingsResult">
        <include refid="selectHotakeSecuritySettingsVo"/>
        where user_id = #{userId} and del_flag = '0'
        limit 1
    </select>
        
    <insert id="insertHotakeSecuritySettings" parameterType="HotakeSecuritySettings" useGeneratedKeys="true" keyProperty="id">
        insert into hotake_security_settings
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="twoFactorEnabled != null">two_factor_enabled,</if>
            <if test="twoFactorSecret != null">two_factor_secret,</if>
            <if test="twoFactorType != null">two_factor_type,</if>
            <if test="backupCodes != null">backup_codes,</if>
            <if test="passwordChangedAt != null">password_changed_at,</if>
            <if test="passwordResetToken != null">password_reset_token,</if>
            <if test="passwordResetExpires != null">password_reset_expires,</if>
            <if test="failedLoginAttempts != null">failed_login_attempts,</if>
            <if test="accountLockedUntil != null">account_locked_until,</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="twoFactorEnabled != null">#{twoFactorEnabled},</if>
            <if test="twoFactorSecret != null">#{twoFactorSecret},</if>
            <if test="twoFactorType != null">#{twoFactorType},</if>
            <if test="backupCodes != null">#{backupCodes},</if>
            <if test="passwordChangedAt != null">#{passwordChangedAt},</if>
            <if test="passwordResetToken != null">#{passwordResetToken},</if>
            <if test="passwordResetExpires != null">#{passwordResetExpires},</if>
            <if test="failedLoginAttempts != null">#{failedLoginAttempts},</if>
            <if test="accountLockedUntil != null">#{accountLockedUntil},</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="updateHotakeSecuritySettings" parameterType="HotakeSecuritySettings">
        update hotake_security_settings
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="twoFactorEnabled != null">two_factor_enabled = #{twoFactorEnabled},</if>
            <if test="twoFactorSecret != null">two_factor_secret = #{twoFactorSecret},</if>
            <if test="twoFactorType != null">two_factor_type = #{twoFactorType},</if>
            <if test="backupCodes != null">backup_codes = #{backupCodes},</if>
            <if test="passwordChangedAt != null">password_changed_at = #{passwordChangedAt},</if>
            <if test="passwordResetToken != null">password_reset_token = #{passwordResetToken},</if>
            <if test="passwordResetExpires != null">password_reset_expires = #{passwordResetExpires},</if>
            <if test="failedLoginAttempts != null">failed_login_attempts = #{failedLoginAttempts},</if>
            <if test="accountLockedUntil != null">account_locked_until = #{accountLockedUntil},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</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="deleteHotakeSecuritySettingsById" parameterType="Long">
        update hotake_security_settings set del_flag = '2' where id = #{id}
    </delete>

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