<?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.HotakeSocialUserMapper">

    <resultMap type="com.vetti.hotake.domain.HotakeSocialUser" id="HotakeSocialUserResult">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="provider" column="provider"/>
        <result property="providerUserId" column="provider_user_id"/>
        <result property="email" column="email"/>
        <result property="name" column="name"/>
        <result property="avatar" column="avatar"/>
        <result property="accessToken" column="access_token"/>
        <result property="refreshToken" column="refresh_token"/>
        <result property="tokenExpireTime" column="token_expire_time"/>
        <result property="rawUserInfo" column="raw_user_info"/>
        <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="selectColumns">
        id, user_id, provider, provider_user_id, email, name, avatar, access_token, refresh_token,
        token_expire_time, raw_user_info, del_flag, create_by, create_time, update_by, update_time, remark
    </sql>

    <select id="selectByProviderAndProviderUserId" resultMap="HotakeSocialUserResult">
        select <include refid="selectColumns"/>
        from hotake_social_user
        where provider = #{provider} and provider_user_id = #{providerUserId} and del_flag = '0'
    </select>

    <select id="selectByUserIdAndProvider" resultMap="HotakeSocialUserResult">
        select <include refid="selectColumns"/>
        from hotake_social_user
        where user_id = #{userId} and provider = #{provider} and del_flag = '0'
    </select>

    <select id="selectByUserId" resultMap="HotakeSocialUserResult">
        select <include refid="selectColumns"/>
        from hotake_social_user
        where user_id = #{userId} and del_flag = '0'
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into hotake_social_user (
            user_id, provider, provider_user_id, email, name, avatar, access_token, refresh_token,
            token_expire_time, raw_user_info, del_flag, create_by, create_time, remark
        ) values (
            #{userId}, #{provider}, #{providerUserId}, #{email}, #{name}, #{avatar}, #{accessToken}, #{refreshToken},
            #{tokenExpireTime}, #{rawUserInfo}, '0', #{createBy}, now(), #{remark}
        )
    </insert>

    <update id="update">
        update hotake_social_user
        <set>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="email != null">email = #{email},</if>
            <if test="name != null">name = #{name},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="accessToken != null">access_token = #{accessToken},</if>
            <if test="refreshToken != null">refresh_token = #{refreshToken},</if>
            <if test="tokenExpireTime != null">token_expire_time = #{tokenExpireTime},</if>
            <if test="rawUserInfo != null">raw_user_info = #{rawUserInfo},</if>
            update_by = #{updateBy},
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <update id="deleteById">
        update hotake_social_user set del_flag = '2', update_time = now() where id = #{id}
    </update>

</mapper>
