<?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.middle.dao.RiskMapper">

    <insert id="insertConfigItem">
        insert into yt_risk_config
        (
         config_id,
         field_name,
         field_desc,
         config_type,
         config_val,
         multy,
         channel_id
        )
        values
        (
         #{configId},
         #{fieldName},
         #{fieldDesc},
         #{configType},
         #{configVal},
         #{multy},
         #{channelId}
        )
    </insert>
    <insert id="insertTempConfig">
        insert into yt_risk_template_config
        (
         template_id,
         config_id,
         operator,
         operator_time,
         operator_name
        )
        values
        (
         #{templateId},
         #{configId},
         #{operator},
         #{operatorTime},
         #{operatorName}
        );
    </insert>
    <insert id="insertTemplate">
        insert into yt_risk_template
        (
         template_id,
         template_name,
         template_content,
         template_code,
         channel_id,
         effect_node,
         app_id,
         all_satisfy,
         create_time,
         create_user_id,
         enabled,
         can_modify
         )
        values
        (
         #{templateId},
         #{templateName},
         #{templateContent},
         #{templateCode},
         #{channelId},
         #{effectNode},
         #{appId},
         #{allSatisfy},
         #{createTime},
         #{createUserId},
         #{enabled},
         #{canModify}
        );
    </insert>
    <insert id="relativeApp">
        insert into yt_cofig_app (template_id, app_id, app_name, operator, operator_name, operator_time)
        values (#{templateId},#{appId},#{appName},#{userId},#{nickName},now());
    </insert>
    <select id="getTemplateByAppId" resultType="com.ytpm.risk.model.YtRiskTemplate">
        select
            template_id, template_name, template_content, template_code, channel_id, effect_node, app_id, all_satisfy, create_time, create_user_id, update_time, update_user_id, enabled, can_modify
        from yt_risk_template
        where app_id = #{appId}
        limit 1
    </select>
    <select id="countBanned" resultType="java.lang.Integer">
        select
            count(distinct user_id)
        from yt_platform_banned
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(banned_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(banned_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(banned_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
    </select>
    <select id="countBannedHour" resultType="com.ytpm.middle.view.AppUserHourVO">
        select
        DATE_FORMAT(banned_time,'%Y-%m-%d %H:00:00') as `time`,
        count(distinct user_id)`count`
        from yt_platform_banned
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(banned_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(banned_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(banned_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
    </select>
</mapper>