<?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.RiskManageMapper">
    <insert id="addDeblockingRecord">
        insert into yt_platform_deblocking
        (
         deblocking_id,
         banned_id,
         user_id,
         app_id,
         channel_id,
         agent_id,
         deblocking_time,
         deblocking_reason,
         operator,
         operator_name
        )
        values
        (
         #{deblockingId},
         #{bannedId},
         #{userId},
         #{appId},
         #{channelId},
         #{agentId},
         #{deblockingTime},
         #{deblockingReason},
         #{operator},
         #{operatorName}
        )
    </insert>
    <insert id="relativeApps" parameterType="com.ytpm.risk.param.RiskRelativeAppParam">
        insert into yt_cofig_app
            (
             template_id,
             app_id,
             app_name,
             operator,
             operator_name,
             operator_time
            )
        values
        <foreach collection="appList" separator="," index="index" item="item">
            (
             #{templateId},
             #{item.appId},
             #{item.appName},
             #{operator},
             #{operatorName},
             now()
            )
        </foreach>
    </insert>
    <delete id="deleteRelatives">
        delete from yt_cofig_app where template_id = #{templateId}
    </delete>
    <select id="getDeblockingList" resultType="com.ytpm.risk.view.RiskDeblockingListView">
        SELECT
            pd.deblocking_id,
            pd.banned_id,
            pd.user_id,
            pd.agent_id,
            pd.channel_id,
            pd.deblocking_time,
            pd.deblocking_reason,
            pd.operator,
            pd.operator_name,
            a.app_id,
            a.app_name,
            a.ditch_id,
            a.ditch_name
        FROM
            yt_platform_deblocking pd
            JOIN yt_app a ON pd.app_id = a.app_id
        WHERE
            pd.agent_id = #{agentId}
        <if test="appId != null and appId != ''">
            and a.app_id = #{appId}
        </if>
        <if test="userId != null and userId != ''">
            and pd.user_id = #{userId}
        </if>
        <if test="channelOrigin != null">
            and a.ditch_id = #{channelOrigin}
        </if>
        <if test="deblockingReason != null and deblockingReason != ''">
            and pd.deblocking_reason like concat('%',#{deblockingReason},'%')
        </if>
        <if test="deblockingTimeBegin != null">
            and DATE_FORMAT(pd.deblocking_time,'%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{deblockingTimeBegin}, '%Y-%m-%d')
        </if>
        <if test="deblockingTimeEnd != null">
            and DATE_FORMAT(pd.deblocking_time,'%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{deblockingTimeEnd}, '%Y-%m-%d')
        </if>
        ORDER BY pd.deblocking_time DESC
    </select>

    <select id="getBannedList" resultType="com.ytpm.risk.view.RiskBannedListView">
        SELECT
        pb.banned_id,
        pb.banned_time,
        pb.banned_reason,
        pb.banned_type,
        pb.banned_limit,
        pb.operator,
        pb.operator_name,
        pb.user_id,
        pb.agent_id,
        a.app_id,
        a.app_name,
        a.ditch_id,
        a.ditch_name
        FROM
        yt_platform_banned pb
        JOIN yt_app a ON pb.app_id = a.app_id
        WHERE
        pb.agent_id = #{agentId}
        <if test="appId != null and appId != ''">
            and a.app_id = #{appId}
        </if>
        <if test="userId != null and userId != ''">
            and pb.user_id = #{userId}
        </if>
        <if test="channelOrigin != null">
            and a.ditch_id = #{channelOrigin}
        </if>
        <if test="bannedReason != null and bannedReason != ''">
            and pb.banned_reason like concat('%',#{bannedReason},'%')
        </if>
        <if test="bannedTimeBegin != null">
            and DATE_FORMAT(pb.banned_time,'%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{bannedTimeBegin}, '%Y-%m-%d')
        </if>
        <if test="bannedTimeEnd != null">
            and DATE_FORMAT(pb.banned_time,'%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{bannedTimeEnd}, '%Y-%m-%d')
        </if>
        ORDER BY pb.banned_time DESC
    </select>
    <select id="getLastBanned" resultType="com.ytpm.agent.model.YtPlatformBanned">
        select
            banned_id, app_id, user_id, channel_id,agent_id, banned_time, banned_reason, banned_type, banned_limit, operator, operator_name
        from yt_platform_banned
        where user_id = #{userId}
        order by banned_time desc
        limit 1
    </select>

    <select id="queryBannedRecord" resultType="com.ytpm.agent.model.YtPlatformBanned">
        select
        banned_id, user_id, channel_id,agent_id, banned_time, banned_reason, banned_type, banned_limit, operator, operator_name
        from yt_platform_banned
        <where>
            <if test="startTime != null">
                AND banned_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND banned_time &lt;= #{endTime}
            </if>

            <choose>
                <when test="appIdList == null or appIdList.isEmpty()">
                    <!-- 为空直接查询不到数据 -->
                    AND 1 = 0
                </when>
                <otherwise>
                    AND app_id IN
                    <foreach collection="appIdList" item="item"
                             open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </otherwise>
            </choose>

        </where>
    </select>


    <select id="queryBannedRecordGroupByUserId" resultType="com.ytpm.agent.model.YtPlatformBanned">
        select
        user_id
        from yt_platform_banned
        <where>
            <if test="startTime != null">
                AND banned_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND banned_time &lt;= #{endTime}
            </if>
            <choose>
                <when test="appIdList == null or appIdList.isEmpty()">
                    <!-- 为空直接查询不到数据 -->
                    AND 1 = 0
                </when>
                <otherwise>
                    AND app_id IN
                    <foreach collection="appIdList" item="item"
                             open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </otherwise>
            </choose>
        </where>
        group by user_id
    </select>
    <select id="queryTodayDeblock" resultType="com.ytpm.risk.view.RiskDeblockingListView">
        select
            deblocking_id, banned_id, user_id, app_id, channel_id, agent_id, deblocking_time, deblocking_reason, operator, operator_name
        from yt_platform_deblocking
        where user_id = #{userId}
        and DATE(deblocking_time) = CURRENT_DATE()
    </select>


</mapper>