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

    <insert id="insertAll">
        insert into yt_risk_config
            (
             config_id,
             field_name,
             field_desc,
             config_type,
             config_val,
             multy,
             channel_id
            )
        values
        <foreach collection="configs" separator="," item="item" index="index">
            (
            #{item.configId},
            #{item.fieldName},
            #{item.fieldDesc},
            #{item.configType},
            #{item.configVal},
            #{item.multy},
            #{item.channelId}
            )
        </foreach>
    </insert>
    <insert id="saveTemplate">
        insert into yt_risk_template
            (
             template_id,
             template_name,
             template_content,
             template_code,
             effect_node,
             channel_id,
             all_satisfy,
             app_id,
             create_time,
             create_user_id,
             enabled
            )
        values
            (
                #{templateId},
                #{templateName},
                #{templateContent},
                #{templateCode},
                #{effectNode},
                #{channelId},
                #{allSatisfy},
                #{appId},
                #{createTime},
                #{createUserId},
                #{enabled}
            );
    </insert>
    <insert id="addRelease">
        insert into yt_risk_template_config
        (
         template_id,
         config_id,
         operator,
         operator_time,
         operator_name
        )
        values
        <foreach collection="configIds" item="item" separator="," index="index">
            (
             #{templateId},
             #{item},
             #{operator},
             now(),
             #{operatorName}
            )
        </foreach>
    </insert>
    <update id="updateByTempId">
        update yt_risk_template
        <set>
            <if test="templateName != null">
                template_name = #{templateName},
            </if>
            <if test="templateContent != null">
                template_content = #{templateContent},
            </if>
            <if test="templateCode != null">
                template_code = #{templateCode},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="effectNode != null">
                effect_node = #{effectNode},
            </if>
            <if test="appId != null">
                app_id = #{appId},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="enabled != null">
                enabled = #{enabled},
            </if>
        </set>
        where template_id = #{templateId}
    </update>
    <update id="updateConfigVal">
        update yt_risk_config set config_val = #{configVal} where config_id = #{configId}
    </update>
    <select id="getRiskConfigList" resultType="com.ytpm.risk.view.RiskConfigListView">
        SELECT
            group_concat(ca.app_id) appIds,
            group_concat(ca.app_name) appNames,
            t.template_name,
            t.create_time,
            t.template_id,
            t.template_code,
            t.template_content,
            u.nick_name as nickName,
            t.enabled,
            t.effect_node,
            t.all_satisfy,
            t.can_modify,
            t.update_time,
            u_update.nick_name as updateNickName
        FROM
            yt_risk_template t
                JOIN yt_platform_user u on t.create_user_id = u.user_id
                LEFT JOIN yt_cofig_app ca ON t.template_id = ca.template_id
                LEFT JOIN yt_platform_user u_update ON t.update_user_id = u_update.user_id
        WHERE
            t.channel_id = #{channelId}
        <if test="appId != null and appId!= ''">
            and ca.app_id = #{appId}
        </if>
        group by t.template_id
    </select>
    <select id="selectOneTemplate" resultType="com.ytpm.risk.model.YtRiskTemplate">
        select
            template_id, template_name, template_content, template_code,effect_node, channel_id, app_id, create_time, create_user_id, update_time, update_user_id, enabled
        from yt_risk_template
        where template_id = #{templateId}
    </select>
    <resultMap id="RiskTemplateViewMap" type="com.ytpm.risk.view.RiskTemplateView">
        <id column="template_id" property="templateId" />
        <result column="template_code" property="templateCode" />
        <result column="template_name" property="templateName" />
        <result column="template_content" property="templateContent" />
        <result column="enabled" property="enabled" />
        <result column="all_satisfy" property="allSatisfy" />
        <result column="effect_node" property="effectNode" />
        <collection property="configList" ofType="com.ytpm.risk.view.RiskConfigView">
            <id column="config_id" property="configId" />
            <result column="field_name" property="fieldName" />
            <result column="field_desc" property="fieldDesc" />
            <result column="config_type" property="configType" />
            <result column="config_val" property="configVal" />
            <result column="multy" property="multy" />
        </collection>
    </resultMap>
    <select id="getTemplateView" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.effect_node,
            rt.enabled,
            rt.all_satisfy,
            rc.config_id,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
               LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
               LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.template_id = #{templateId}
          AND rt.channel_id = #{channelId}
    </select>
    <select id="getAppConfig" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.effect_node,
            rt.enabled,
            rc.config_id,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
               LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
               LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.app_id = #{appId}
    </select>
    <select id="getAppClazz" resultType="com.ytpm.agent.view.AgentAppClassView">
        select
            app_id, full_name, class_desc, enabled
        from yt_app_class
        where enabled = 1
        <if test="appIds != null and appIds != ''">
            and app_id in
            <foreach collection="appIds.split(',')" item="item" separator="," open="(" close=")" >
                #{item}
            </foreach>
        </if>
    </select>
    <select id="getByCode" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.effect_node,
            rt.enabled,
            rc.config_id,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
                LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
                LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.template_code = #{riskCode}
    </select>
    <select id="getByNode" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.enabled,
            rc.config_id,
            rt.effect_node,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
                LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
                LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.effect_node = #{effectNode}
    </select>
    <select id="getApplicationOwner" resultType="java.lang.String">
        select
            user_id
        from yt_app
        where app_id = #{appId}
    </select>
    <select id="getCustomTempConfig" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.enabled,
            rc.config_id,
            rt.effect_node,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
                LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
                LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.enabled = 1
            and rt.can_modify = 1
            and rt.app_id = #{appId}
            and rt.effect_node = #{effectNode}
    </select>
    <select id="getTemplateByAppId" resultMap="RiskTemplateViewMap">
        SELECT
            rt.template_id,
            rt.template_code,
            rt.template_name,
            rt.template_content,
            rt.effect_node,
            rt.enabled,
            rc.config_id,
            rc.field_name,
            rc.field_desc,
            rc.config_type,
            rc.config_val,
            rc.multy
        FROM
            yt_risk_template rt
                LEFT JOIN yt_risk_template_config rtc ON rt.template_id = rtc.template_id
                LEFT JOIN yt_risk_config rc ON rtc.config_id = rc.config_id
        WHERE
            rt.app_id = #{appId}
    </select>
</mapper>