<?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="org.dromara.member.mapper.AppSocialAuthMapper">

    <resultMap type="org.dromara.member.domain.AppSocialAuth" id="AppSocialAuthResult">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="platform" column="platform"/>
        <result property="platformUid" column="platform_uid"/>
        <result property="openId" column="open_id"/>
        <result property="unionId" column="union_id"/>
        <result property="accessToken" column="access_token"/>
        <result property="expireIn" column="expire_in"/>
        <result property="refreshToken" column="refresh_token"/>
        <result property="scope" column="scope"/>
        <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"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppSocialAuthVo" id="AppSocialAuthVoResult">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="platform" column="platform"/>
        <result property="platformUid" column="platform_uid"/>
        <result property="openId" column="open_id"/>
        <result property="unionId" column="union_id"/>
        <result property="accessToken" column="access_token"/>
        <result property="expireIn" column="expire_in"/>
        <result property="refreshToken" column="refresh_token"/>
        <result property="scope" column="scope"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectAppSocialAuthVo">
        select id,
               user_id,
               platform,
               platform_uid,
               open_id,
               union_id,
               access_token,
               expire_in,
               refresh_token,
               scope,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from app_social_auth
    </sql>

    <!-- 查询社交认证列表 -->
    <select id="selectAppSocialAuthList" parameterType="AppSocialAuth" resultMap="AppSocialAuthVoResult">
        <include refid="selectAppSocialAuthVo"/>
        <where>
            <if test="userId != null">and user_id = #{userId}</if>
            <if test="platform != null and platform != ''">and platform = #{platform}</if>
            <if test="platformUid != null and platformUid != ''">and platform_uid = #{platformUid}</if>
            <if test="openId != null and openId != ''">and open_id = #{openId}</if>
            <if test="unionId != null and unionId != ''">and union_id = #{unionId}</if>
            and del_flag = '0'
        </where>
        order by create_time desc
    </select>

    <!-- 根据ID查询社交认证 -->
    <select id="selectAppSocialAuthById" parameterType="Long" resultMap="AppSocialAuthVoResult">
        <include refid="selectAppSocialAuthVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <!-- 根据用户ID查询社交认证列表 -->
    <select id="selectAppSocialAuthByUserId" parameterType="Long" resultMap="AppSocialAuthVoResult">
        <include refid="selectAppSocialAuthVo"/>
        where user_id = #{userId} and del_flag = '0'
        order by create_time desc
    </select>

    <!-- 根据平台和openId查询社交认证 -->
    <select id="selectAppSocialAuthByPlatformAndOpenId" resultMap="AppSocialAuthResult">
        <include refid="selectAppSocialAuthVo"/>
        where platform = #{platform} and open_id = #{openId} and del_flag = '0'
    </select>

    <!-- 根据平台和unionId查询社交认证 -->
    <select id="selectAppSocialAuthByPlatformAndUnionId" resultMap="AppSocialAuthResult">
        <include refid="selectAppSocialAuthVo"/>
        where platform = #{platform} and union_id = #{unionId} and del_flag = '0'
    </select>

    <!-- 新增社交认证 -->
    <insert id="insertAppSocialAuth" parameterType="org.dromara.member.domain.AppSocialAuth" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_social_auth
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="platform != null and platform != ''">platform,</if>
            <if test="platformUid != null and platformUid != ''">platform_uid,</if>
            <if test="openId != null and openId != ''">open_id,</if>
            <if test="unionId != null and unionId != ''">union_id,</if>
            <if test="accessToken != null and accessToken != ''">access_token,</if>
            <if test="expireIn != null">expire_in,</if>
            <if test="refreshToken != null and refreshToken != ''">refresh_token,</if>
            <if test="scope != null and scope != ''">scope,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="platform != null and platform != ''">#{platform},</if>
            <if test="platformUid != null and platformUid != ''">#{platformUid},</if>
            <if test="openId != null and openId != ''">#{openId},</if>
            <if test="unionId != null and unionId != ''">#{unionId},</if>
            <if test="accessToken != null and accessToken != ''">#{accessToken},</if>
            <if test="expireIn != null">#{expireIn},</if>
            <if test="refreshToken != null and refreshToken != ''">#{refreshToken},</if>
            <if test="scope != null and scope != ''">#{scope},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <!-- 修改社交认证 -->
    <update id="updateAppSocialAuth" parameterType="org.dromara.member.domain.AppSocialAuth">
        update app_social_auth
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="platform != null and platform != ''">platform = #{platform},</if>
            <if test="platformUid != null and platformUid != ''">platform_uid = #{platformUid},</if>
            <if test="openId != null and openId != ''">open_id = #{openId},</if>
            <if test="unionId != null and unionId != ''">union_id = #{unionId},</if>
            <if test="accessToken != null and accessToken != ''">access_token = #{accessToken},</if>
            <if test="expireIn != null">expire_in = #{expireIn},</if>
            <if test="refreshToken != null and refreshToken != ''">refresh_token = #{refreshToken},</if>
            <if test="scope != null and scope != ''">scope = #{scope},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 删除社交认证 -->
    <update id="deleteAppSocialAuthById" parameterType="Long">
        update app_social_auth
        set del_flag = '2'
        where id = #{id}
    </update>

    <!-- 批量删除社交认证 -->
    <update id="deleteAppSocialAuthByIds" parameterType="String">
        update app_social_auth set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!-- 根据用户ID删除社交认证 -->
    <update id="deleteAppSocialAuthByUserId" parameterType="Long">
        update app_social_auth
        set del_flag = '2'
        where user_id = #{userId}
    </update>

    <!-- 更新访问令牌 -->
    <update id="updateAccessToken">
        update app_social_auth
        set access_token  = #{accessToken},
            expire_in     = #{expireIn},
            refresh_token = #{refreshToken},
            update_time   = #{updateTime}
        where id = #{id}
    </update>

</mapper>