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

    <resultMap id="timePeriodStrategyList" type="cn.roadm.bo.TimePeriodStrategyBO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="strategy" property="strategy"/>
        <result column="config_time" property="configTime"/>
        <result column="config_value" property="configValue"/>
        <result column="create_time" property="createTime"/>
        <result column="state" property="state"/>
        <result column="alarm_mdn" property="alarmMdn"/>
        <result column="alarm_email" property="alarmEmail"/>
        <result column="alarm_type" property="alarmType"/>
        <result column="alarm_content" property="alarmContent"/>
        <result column="client_login_option" property="clientLoginOption"/>
        <result column="sp_list" property="spList"/>
    </resultMap>

    <select id="listPageTimePeriodStrategy" resultMap="timePeriodStrategyList">
        SELECT a.id,
               a.NAME,
               a.start_time,
               a.end_time,
               a.config_value,
               a.strategy,
               a.create_time,
               a.state,
               a.alarm_mdn,
               a.alarm_email,
               a.alarm_type,
               a.alarm_content,
               a.client_login_option,
               b.sp_list
        FROM sms_time_period_strategy a
                 LEFT JOIN (SELECT strategy_id, GROUP_CONCAT(sp_id) as sp_list
                            FROM sms_time_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>
        </where>
        order by create_time desc
    </select>

    <select id="getTimePeriodStrategyById" resultMap="timePeriodStrategyList">
        select id,
               name,
               start_time,
               end_time,
               config_value,
               strategy,
               create_time,
               state,
               alarm_mdn,
               alarm_email,
               alarm_type,
               alarm_content,
               client_login_option
        from sms_time_period_strategy
        where id = #{id}
    </select>

    <insert id="insertTimePeriodStrategy" useGeneratedKeys="true" keyProperty="id">
        insert into sms_time_period_strategy
        (name, start_time, end_time, config_value, strategy, create_time, alarm_mdn, alarm_email, alarm_type,
         alarm_content, client_login_option)
        values (#{name}, #{startTime}, #{endTime}, #{configValue}, #{strategy}, now(), #{alarmMdn}, #{alarmEmail},
                #{alarmType}, #{alarmContent}, #{clientLoginOption})
    </insert>

    <update id="updateTimePeriodStrategy">
        update sms_time_period_strategy
        set name = #{name},
            start_time = #{startTime},
            end_time = #{endTime},
            config_value = #{configValue},
            strategy = #{strategy},
            alarm_mdn = #{alarmMdn},
            alarm_email = #{alarmEmail},
            alarm_type = #{alarmType},
            alarm_content = #{alarmContent},
            client_login_option = #{clientLoginOption}
        where id = #{id}
    </update>

    <delete id="deleteTimePeriodStrategy">
        update sms_time_period_strategy
        set state = 1
        where id = #{id}
    </delete>

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

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

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

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

    <select id="listTimePeriodStrategy" resultMap="timePeriodStrategyList">
        select id,name,config_value,start_time,end_time from sms_time_period_strategy where state = 0 order by config_value
    </select>

    <select id="listSelectConfigList" resultType="cn.roadm.bo.TimeSlotSendMonitorBO">
        select id,
               start_hour                                    as startHour,
               end_hour                                      as endHour,
               config_name                                   as configName,
               delay_time                                    as delayTime,
               count_base                                    as countBase,
               count_incr                                    as countIncr,
               count_rate                                    as countRate,
               speed_base                                    as speedBase,
               speed_incr                                    as speedIncr,
               speed_rate                                    as speedRate,
               alarm_type                                    as alarmType,
               alarm_content                                 as alarmContent,
               alarm_sleep                                   as alarmSleep,
               alarm_phone                                   as alarmPhone,
               alarm_email                                   as alarmEmail,
               exclude_sp                                    as excludeSp,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               alarm_speed_content as speedAlarmContent
        from time_slot_send_monitor
        where status = 0
    </select>

    <select id="getTimeSlotMonitorConfigById" resultType="cn.roadm.bo.TimeSlotSendMonitorBO">
            select id,
               start_hour                                    as startHour,
               end_hour                                      as endHour,
               config_name                                   as configName,
               delay_time                                    as delayTime,
               count_base                                    as countBase,
               count_incr                                    as countIncr,
               count_rate                                    as countRate,
               speed_base                                    as speedBase,
               speed_incr                                    as speedIncr,
               speed_rate                                    as speedRate,
               alarm_type                                    as alarmType,
               alarm_content                                 as alarmContent,
               alarm_speed_content                           as speedAlarmContent,
               alarm_sleep                                   as alarmSleep,
               alarm_phone                                   as alarmPhone,
               alarm_email                                   as alarmEmail,
               exclude_sp                                    as excludeSp,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime,
            alarm_speed_content as speedAlarmContent
        from time_slot_send_monitor
        where id = #{id}
    </select>

    <insert id="insertTimeSlotMonitorConfig">
        insert into time_slot_send_monitor(start_hour, end_hour, config_name, delay_time, count_base, count_incr,
                                           count_rate, speed_base, speed_incr, speed_rate, alarm_type, alarm_content,
                                           alarm_sleep, alarm_phone, alarm_email, exclude_sp, create_time, alarm_speed_content)
        values (#{startHour}, #{endHour}, #{configName}, #{delayTime}, #{countBase}, #{countIncr}, #{countRate},
                #{speedBase}, #{speedIncr}, #{speedRate}, #{alarmType}, #{alarmContent}, #{alarmSleep}, #{alarmPhone},
                #{alarmEmail}, #{excludeSp}, now(), #{speedAlarmContent})
    </insert>

    <update id="updateTimeSlotMonitorConfig">
        update time_slot_send_monitor set
            start_hour = #{startHour},
            end_hour = #{endHour},
            config_name = #{configName},
            delay_time = #{delayTime},
            count_base = #{countBase},
            count_incr = #{countIncr},
            count_rate = #{countRate},
            speed_base = #{speedBase},
            speed_incr = #{speedIncr},
            speed_rate = #{speedRate},
            alarm_type = #{alarmType},
            alarm_content = #{alarmContent},
            alarm_sleep = #{alarmSleep},
            alarm_phone = #{alarmPhone},
            alarm_email = #{alarmEmail},
            exclude_sp = #{excludeSp},
            alarm_speed_content = #{speedAlarmContent}
        where id = #{id}
    </update>

    <select id="listPageSameContentStrategy" resultMap="timePeriodStrategyList">
        SELECT a.id,
               a.NAME,
               a.start_time,
               a.end_time,
               a.config_time,
               a.config_value,
               a.strategy,
               a.create_time,
               a.state,
               b.sp_list
        FROM sms_time_period_same_content_strategy a
                 LEFT JOIN (SELECT strategy_id, GROUP_CONCAT(sp_id) as sp_list
                            FROM sms_time_period_same_content_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>
        </where>
        order by create_time desc
    </select>

    <select id="getTimSameContentStrategyById" resultMap="timePeriodStrategyList">
        select id,
               name,
               start_time,
               end_time,
               config_time,
               config_value,
               strategy,
               create_time,
               state
        from sms_time_period_same_content_strategy
        where id = #{id}
    </select>

    <insert id="insertTSameContentStrategy">
         insert into sms_time_period_same_content_strategy
        (name, start_time, end_time, config_time, config_value, strategy, create_time)
        values (#{name}, #{startTime}, #{endTime}, #{configTime}, #{configValue}, #{strategy}, now())
    </insert>

    <update id="updateSameContentStrategy">
         update sms_time_period_same_content_strategy
        set name = #{name},
            start_time = #{startTime},
            end_time = #{endTime},
            config_time = #{configTime},
            config_value = #{configValue},
            strategy = #{strategy}
        where id = #{id}
    </update>

    <update id="deleteSameContentStrategy">
        update sms_time_period_same_content_strategy
        set state = 1
        where id = #{id}
    </update>

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

    <delete id="deleteThisSameContentStrategySp">
        delete from `sms_time_period_same_content_sp` where strategy_id = #{id}
    </delete>

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

    <select id="queryTimeSendCountAlarmStrategySp" resultType="cn.roadm.bo.TimePeriodStrategySpBO">
        select sp_id as spId, strategy_id as strategyId from time_slot_send_monitor_sp where strategy_id = #{id}
    </select>

    <delete id="deleteSendCountAlarmDistributionSp">
        delete from time_slot_send_monitor_sp where strategy_id = #{id}
    </delete>

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

    <delete id="deleteTimeSlotMonitorConfig">
        delete from time_slot_send_monitor where id = #{id}
    </delete>

    <select id="getTimePeriodConfigIdBySpId" resultType="java.lang.Integer">
        select strategy_id as id from sms_time_period_sp where sp_id = #{spid}
    </select>

    <insert id="addTimePeriodStrategy">
        replace into sms_time_period_sp (strategy_id, sp_id) values (#{configId}, #{newSpid})
    </insert>
</mapper>
