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

    <resultMap id="spSendStrategyList" type="cn.roadm.bo.SpSendStrategyBO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="samemdn_nday" property="samemdn_nday"/>
        <result column="samemdn_oneday" property="samemdn_oneday"/>
        <result column="samemdn_threeday" property="samemdn_threeday"/>
        <result column="samemdn_nminute" property="samemdn_nminute"/>
        <result column="samemdn_onehour" property="samemdn_onehour"/>
        <result column="limit_days" property="limit_days"/>
        <result column="limit_minute" property="limit_minute"/>
        <result column="begin_send_time" property="begin_send_time"/>
        <result column="end_send_time" property="end_send_time"/>
        <result column="begin_effective_time" property="begin_effective_time"/>
        <result column="end_effective_time" property="end_effective_time"/>
        <result column="send_type" property="send_type"/>
        <result column="plan_send_time" property="plan_send_time"/>
        <result column="unsend_city" property="unsend_city"/>
        <result column="unsend_province" property="unsend_province"/>
        <result column="send_city" property="send_city"/>
        <result column="send_province" property="send_province"/>
        <result column="update_time" property="update_time"/>
        <result column="create_time" property="create_time"/>
        <result column="state" property="state"/>
        <result column="sp_list" property="sp_list"/>
        <result column="frequency_strategy" property="frequency_strategy"/>
        <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="audit_timeout" property="audit_timeout"/>
    </resultMap>

    <select id="listPageSpSendStrategy" resultMap="spSendStrategyList">
        SELECT a.id,
               a.NAME,
               a.samemdn_nday ,
               a.samemdn_oneday ,
               a.samemdn_threeday ,
               a.samemdn_nminute ,
               a.samemdn_onehour ,
               a.limit_days ,
               a.limit_minute ,
               a.begin_send_time ,
               a.end_send_time ,
               a.begin_effective_time ,
               a.end_effective_time ,
               a.send_type ,
               a.plan_send_time ,
               a.unsend_city ,
               a.unsend_province ,
               a.send_city ,
               a.send_province ,
               a.update_time ,
               a.create_time ,
               a.state,
               b.sp_list,
               a.frequency_strategy,
               a.content_intercept_flag,
               a.content_intercept_content,
               a.content_intercept_time,
               a.audit_timeout
        FROM sms_send_period_strategy a
                 LEFT JOIN (SELECT strategy_id, GROUP_CONCAT(sp_id) as sp_list
                            FROM sms_send_period_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="getSpSendStrategyById" resultMap="spSendStrategyList">
        select id,
               name,
               samemdn_nday ,
               samemdn_oneday ,
               samemdn_threeday ,
               samemdn_nminute ,
               samemdn_onehour ,
               limit_days ,
               limit_minute ,
               begin_effective_time ,
               end_effective_time ,
               begin_send_time ,
               end_send_time ,
               send_type ,
               plan_send_time ,
               unsend_city ,
               unsend_province ,
               send_city ,
               send_province ,
               update_time ,
               create_time ,
               state,
               frequency_strategy,
               content_intercept_flag,
               content_intercept_content,
               content_intercept_time,
               audit_timeout
        from sms_send_period_strategy
        where id = #{id}
    </select>

    <insert id="insertSpSendStrategy" useGeneratedKeys="true" keyProperty="id">
        insert into sms_send_period_strategy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            name,
            samemdn_nday,
            samemdn_oneday,
            samemdn_threeday,
            samemdn_nminute,
            samemdn_onehour,
            limit_days,
            limit_minute,
            begin_effective_time,
            end_effective_time,
            begin_send_time,
            end_send_time,
            send_type,
            plan_send_time,
            unsend_city,
            unsend_province,
            send_city,
            send_province,
            frequency_strategy,
            content_intercept_flag,
            content_intercept_content,
            content_intercept_time,
            audit_timeout,
            create_time,
            state
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{name},
            #{samemdn_nday},
            #{samemdn_oneday},
            #{samemdn_threeday},
            #{samemdn_nminute},
            #{samemdn_onehour},
            #{limit_days},
            #{limit_minute},
            <choose>
                <when test="begin_effective_time != null and begin_effective_time != ''">#{begin_effective_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            <choose>
                <when test="end_effective_time != null and end_effective_time != ''">#{end_effective_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            <choose>
                <when test="begin_send_time != null and begin_send_time != ''">#{begin_send_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            <choose>
                <when test="end_send_time != null and end_send_time != ''">#{end_send_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            #{send_type},
            <choose>
                <when test="plan_send_time != null and plan_send_time != ''">#{plan_send_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            #{unsend_city},
            #{unsend_province},
            #{send_city},
            #{send_province},
            #{frequency_strategy},
            #{content_intercept_flag},
            #{content_intercept_content},
            <choose>
                <when test="content_intercept_time != null and content_intercept_time != ''">#{content_intercept_time},</when>
                <otherwise>null,</otherwise>
            </choose>
            <choose>
                <when test="audit_timeout != null and audit_timeout != ''">#{audit_timeout},</when>
                <otherwise>null,</otherwise>
            </choose>
            now(),
            0
        </trim>
    </insert>

    <update id="updateSpSendStrategy">
        update sms_send_period_strategy
        <set>
            name = #{name},
            samemdn_nday = #{samemdn_nday},
            samemdn_oneday = #{samemdn_oneday},
            samemdn_threeday = #{samemdn_threeday},
            samemdn_nminute = #{samemdn_nminute},
            samemdn_onehour = #{samemdn_onehour},
            limit_days = #{limit_days},
            limit_minute = #{limit_minute},
            <if test="begin_effective_time != null">
                <choose>
                    <when test="begin_effective_time != ''">begin_effective_time = #{begin_effective_time},</when>
                    <otherwise>begin_effective_time = null,</otherwise>
                </choose>
            </if>
            <if test="end_effective_time != null">
                <choose>
                    <when test="end_effective_time != ''">end_effective_time = #{end_effective_time},</when>
                    <otherwise>end_effective_time = null,</otherwise>
                </choose>
            </if>
            <if test="begin_send_time != null">
                <choose>
                    <when test="begin_send_time != ''">begin_send_time = #{begin_send_time},</when>
                    <otherwise>begin_send_time = null,</otherwise>
                </choose>
            </if>
            <if test="end_send_time != null">
                <choose>
                    <when test="end_send_time != ''">end_send_time = #{end_send_time},</when>
                    <otherwise>end_send_time = null,</otherwise>
                </choose>
            </if>
            send_type = #{send_type},
            <if test="plan_send_time != null">
                <choose>
                    <when test="plan_send_time != ''">plan_send_time = #{plan_send_time},</when>
                    <otherwise>plan_send_time = null,</otherwise>
                </choose>
            </if>
            unsend_city = #{unsend_city},
            unsend_province = #{unsend_province},
            send_city = #{send_city},
            send_province = #{send_province},
            frequency_strategy = #{frequency_strategy},
            content_intercept_flag = #{content_intercept_flag},
            content_intercept_content = #{content_intercept_content},
            <if test="content_intercept_time != null">
                <choose>
                    <when test="content_intercept_time != ''">content_intercept_time = #{content_intercept_time},</when>
                    <otherwise>content_intercept_time = null,</otherwise>
                </choose>
            </if>
            <if test="audit_timeout != null">
                <choose>
                    <when test="audit_timeout != ''">audit_timeout = #{audit_timeout},</when>
                    <otherwise>audit_timeout = null,</otherwise>
                </choose>
            </if>
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteSpSendStrategy">
        update sms_send_period_strategy
        set state = 1
        where id = #{id}
    </delete>

    <select id="querySpSendStrategySp" resultType="cn.roadm.bo.SpSendStrategySpBO">
        select sp_id as spId, strategy_id as strategyId from `sms_send_period_sp` where strategy_id = #{strategyId}
    </select>

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

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

    <insert id="addSpSendStrategySp">
        replace into `sms_send_period_sp` (sp_id, strategy_id) values
        <foreach collection="spList" item="sp" separator=",">
            (${sp}, ${tid})
        </foreach>
    </insert>

    <select id="listSpSendStrategy" resultMap="spSendStrategyList">
        select id,name,begin_send_time,end_send_time from sms_send_period_strategy where state = 0 order by create_time
    </select>

</mapper>
