<?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="cn.roadm.mapper.ContentInterceptStrategyMapper">

    <resultMap id="contentInterceptStrategyList" type="cn.roadm.bo.ContentInterceptStrategyBO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="content_intercept_flag" property="content_intercept_flag"/>
        <result column="content_intercept_content" property="content_intercept_content"/>
        <result column="content_intercept_time" property="content_intercept_time"/>
        <result column="create_time" property="create_time"/>
        <result column="update_time" property="update_time"/>
        <result column="state" property="state"/>
        <result column="sp_list" property="sp_list"/>
    </resultMap>

    <select id="listPageContentInterceptStrategy" resultMap="contentInterceptStrategyList">
        SELECT a.id,
               a.name,
               a.content_intercept_flag,
               a.content_intercept_content,
               a.content_intercept_time,
               a.create_time,
               a.update_time,
               a.state,
               b.sp_list
        FROM sms_content_intercept_strategy a
                 LEFT JOIN (SELECT strategy_id, GROUP_CONCAT(sp_id) as sp_list
                            FROM sms_content_intercept_sp
                            GROUP BY strategy_id) b ON a.id = b.strategy_id
        <where>
            state = 0
            <if test="name != null and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="sp_id != null and sp_id != ''">
                and sp_list like concat('%', #{sp_id}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="getContentInterceptStrategyById" resultMap="contentInterceptStrategyList">
        select id,
               name,
               content_intercept_flag,
               content_intercept_content,
               content_intercept_time,
               create_time,
               update_time,
               state
        from sms_content_intercept_strategy
        where id = #{id}
    </select>

    <insert id="insertContentInterceptStrategy" useGeneratedKeys="true" keyProperty="id">
        insert into sms_content_intercept_strategy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="content_intercept_flag != null">content_intercept_flag,</if>
            <if test="content_intercept_content != null and content_intercept_content != ''">content_intercept_content,</if>
            content_intercept_time,
            create_time,
            state
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="content_intercept_flag != null">#{content_intercept_flag},</if>
            <if test="content_intercept_content != null and content_intercept_content != ''">#{content_intercept_content},</if>
            #{content_intercept_time},
            now(),
            0
        </trim>
    </insert>

    <update id="updateContentInterceptStrategy">
        update sms_content_intercept_strategy
        <set>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="content_intercept_flag != null">content_intercept_flag = #{content_intercept_flag},</if>
            <if test="content_intercept_content != null">content_intercept_content = #{content_intercept_content},</if>
            content_intercept_time = #{content_intercept_time},
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <update id="deleteContentInterceptStrategy">
        update sms_content_intercept_strategy
        set state = 1
        where id = #{id}
    </update>

    <select id="queryContentInterceptStrategySp" resultType="cn.roadm.bo.ContentInterceptStrategySpBO">
        select sp_id as spId, strategy_id as strategyId from sms_content_intercept_sp where strategy_id = #{strategyId}
    </select>

    <select id="queryAllSp" resultType="cn.roadm.bo.ContentInterceptStrategySpBO">
        select id as spId, sp_name as spName from sms_sp_info where status = 1
    </select>

    <delete id="deleteThisStrategySp">
        delete from sms_content_intercept_sp where strategy_id = #{id}
    </delete>

    <insert id="addContentInterceptStrategySp">
        replace into sms_content_intercept_sp (sp_id, strategy_id) values
        <foreach collection="spList" item="sp" separator=",">
            (#{sp}, #{tid})
        </foreach>
    </insert>

    <select id="listContentInterceptStrategy" resultMap="contentInterceptStrategyList">
        select id, name, content_intercept_flag
        from sms_content_intercept_strategy
        where state = 0
        order by create_time desc
    </select>

</mapper> 