<?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.ytpm.dao.RiskUserMapper">
    <insert id="addBannedRecord">
        insert yt_platform_banned
        (
         banned_id,
         user_id,
         app_id,
         agent_id,
         channel_id,
         banned_time,
         banned_reason,
         banned_type,
         banned_limit,
         operator,
         operator_name
        )
        values
        (
            #{bannedId},
            #{userId},
            #{appId},
            #{agentId},
            #{channelId},
            #{bannedTime},
            #{bannedReason},
            #{bannedType},
            #{bannedLimit},
            #{operator},
            #{operatorName}
        )
    </insert>
    <update id="updateByUserId">
        update yt_app_user
        <set>
            <if test="appId !=null and appId!=''">
                app_id = {appId},
            </if>
            <if test="nickName !=null and nickName!=''">
                nick_name = #{nickName},
            </if>
            <if test="registryTime !=null">
                registry_time = #{registryTime},
            </if>
            <if test="lastLoginTime !=null">
                last_login_time = #{lastLoginTime},
            </if>
            <if test="lastLoginIp !=null and lastLoginIp!=''">
                last_login_ip = #{lastLoginIp},
            </if>
            <if test="loginDays !=null">
                login_days = #{loginDays},
            </if>
            <if test="channelType !=null">
                channel_type = #{channelType},
            </if>
            <if test="channelOrigin !=null">
                channel_origin = #{channelOrigin},
            </if>
            <if test="userType !=null">
                user_type = #{userType},
            </if>
            <if test="totalVideo !=null">
                total_video = #{totalVideo},
            </if>
            <if test="totalIncome !=null">
                total_income = #{totalIncome},
            </if>
            <if test="redPacketBalance !=null">
                red_packet_balance = #{redPacketBalance},
            </if>
            <if test="redPacketAmount !=null">
                red_packet_amount = #{redPacketAmount},
            </if>
            <if test="pointsBalance !=null">
                points_balance = #{pointsBalance},
            </if>
            <if test="pointsTotal !=null">
                points_total = #{pointsTotal},
            </if>
            <if test="withdrawTotal !=null">
                withdraw_total = #{withdrawTotal},
            </if>
            <if test="signDays !=null">
                sign_days = #{signDays},
            </if>
            <if test="userStatus !=null">
                user_status = #{userStatus},
            </if>
            <if test="lockReason !=null">
                lock_reason = #{lockReason},
            </if>
        </set>
        where user_id = #{userId}
    </update>
    <update id="banned">
        update yt_app_user set user_status = 3
        where user_id in
        <foreach collection="userIdList" separator="," item="item" index="index" open="(" close=")">
            #{item}
        </foreach>
    </update>
    <update id="unBanned">
        update yt_app_user set user_status = 1
        where user_id in
        <foreach collection="userIdList" separator="," item="item" index="index" open="(" close=")">
            #{item}
        </foreach>
    </update>

    <select id="selectByUserId" resultType="com.ytpm.agent.model.YtAppUser">
        select
            user_id, app_id, channel_id, platform_id, nick_name,lock_reason, registry_time, last_login_time, last_login_ip, login_days, channel_type, channel_origin, user_type, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status
        from yt_app_user
        where user_id = #{userId}
    </select>
    <select id="getBannedUserList" resultType="com.ytpm.agent.model.YtAppUser">
        select
            user_id, app_id, channel_id, platform_id, nick_name, registry_time, last_login_time, last_login_ip, login_days, channel_type, channel_origin, user_type, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, wx_open_id, platform_id, channel_id
        from yt_app_user
        where user_id = #{userId}
        <if test="bannedTargetId != null and bannedTargetId != ''">
            <choose>
                <when test="bannedType == 1 ">
                    and channel_id = #{bannedTargetId}
                </when>
                <otherwise>
                    and platform_id = #{bannedTargetId}
                </otherwise>
            </choose>
        </if>
    </select>
    <select id="selectByWx" resultType="com.ytpm.agent.model.YtAppUser">
        select user_id, app_id, nick_name, registry_time, last_login_time, last_login_ip, login_days, channel_type, channel_origin, user_type, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, lock_reason, wx_open_id, platform_id, channel_id from yt_app_user where wx_open_id = #{openid}
    </select>
    <select id="selectByAppId" resultType="com.ytpm.risk.view.RiskAccessView">
        select
            a.app_id,
            a.app_name,
            pua.access_id,
            pua.access_secret,
            pua.user_id
        from yt_app a
        join yt_platform_user_access pua on a.user_id = pua.user_id and pua.enabled = 1
        where a.app_id = #{appId}
    </select>
    <select id="getTimedUser" resultType="java.lang.String">
        SELECT
            user_id
        FROM
            yt_platform_banned
        GROUP BY
            user_id
        HAVING
            TIMESTAMPDIFF(
                    HOUR,
                    max( banned_time ),
                    NOW()) > 24
    </select>
</mapper>