<?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.MonitorMapper">
    <resultMap type="cn.roadm.bo.AlarmSms" id="monitorMap">
        <id column="id" property="id" />
        <result column="phone" property="phone" />
    </resultMap>

    <resultMap type="cn.roadm.bo.AlarmEmail" id="monitorEmailMap">
        <id column="id" property="id" />
        <result column="send_email" property="sendEmail" />
        <result column="server_host" property="serverHost" />
        <result column="mail_password" property="mailPassword" />
        <result column="subject" property="subject" />
        <result column="receive_email" property="receiveEmail" />
    </resultMap>

    <resultMap type="cn.roadm.bo.AlarmPaReport" id="monitorReportMap">
        <id column="id" property="id" />
        <result column="channel_id" property="channelId" />
        <result column="report_succ_rate" property="reportSuccRate" />
        <result column="send_num_limit" property="sendNumLimit" />
        <result column="alarm_type" property="alarmType" />
        <result column="alarm_num" property="alarmNum" />
        <result column="alarm_content" property="alarmContent" />
        <result column="alarm_interval_time" property="alarmIntervalTime" />
        <result column="data_scan_time" property="dataScanTime" />
    </resultMap>

    <resultMap type="cn.roadm.bo.AlarmChannelQueueSize" id="monitorQueueSizeMap">
        <id column="id" property="id" />
        <result column="channel_id" property="channelId" />
        <result column="alarm_queue_size" property="alarmQueueSize" />
        <result column="alarm_object_type" property="alarmObjectType" />
        <result column="alarm_interval_time" property="alarmIntervalTime" />
        <result column="alarm_content" property="alarmContent" />
        <result column="alarm_flag" property="alarmFlag" />
    </resultMap>

    <resultMap type="cn.roadm.bo.AlarmChannelNetwork" id="monitorChannelNetworkMap">
        <id column="id" property="id" />
        <result column="channel_id" property="channelId" />
        <result column="continuous_num" property="continuousNum" />
        <result column="alarm_object_type" property="alarmObjectType" />
        <result column="alarm_content" property="alarmContent" />
        <result column="network_interval_time" property="networkIntervalTime" />
        <result column="alarm_flag" property="alarmFlag" />
    </resultMap>
     <resultMap type="cn.roadm.bo.ChannelSendSpeed" id="ChannelSendSpeedMap">
        <result column="channelid" property="channelid" />
        <result column="speed" property="speed" />
        <result column="monitortime" property="monitortime" />
        <result column="createtime" property="createTime" />
    </resultMap>
    <resultMap type="cn.roadm.bo.AlarmChannelErrcode" id="AlarmChannelErrcodeMap">
        <result column="channelid" property="channelId" />
        <result column="alarm_type" property="alarmType" />
        <result column="alarm_content" property="alarmContent" />
        <result column="errcode" property="errcode" />
        <result column="alarm_interval_time" property="alarmIntervalTime" />
        <result column="alarm_flag" property="alarm_flag" />
    </resultMap>


    <update id="updateMonitorChannelNetwork" parameterType="cn.roadm.bo.AlarmChannelNetwork" >
    	update alarm_channel_network set continuous_num = #{continuousNum, jdbcType=INTEGER}, alarm_object_type = #{alarmObjectType, jdbcType=VARCHAR},
    			alarm_content = #{alarmContent, jdbcType=VARCHAR} ,network_interval_time =#{networkIntervalTime},alarm_flag=#{alarmFlag},
    	data_scan_time=#{dataScanTime, jdbcType=INTEGER} where  channel_id = #{channelId, jdbcType=INTEGER}
    </update>

    <update id="updateChannelErr" parameterType="cn.roadm.bo.AlarmChannelErrcode" >
        update alarm_channel_errcode set errcode = #{errcode, jdbcType=VARCHAR},
                alarm_type = #{alarmType, jdbcType=VARCHAR},
                alarm_content = #{alarmContent, jdbcType=VARCHAR},
                alarm_interval_time = #{alarmIntervalTime, jdbcType=INTEGER},
                alarm_flag = #{alarm_flag, jdbcType=INTEGER}
        where  channelid = #{channelId, jdbcType=INTEGER}
    </update>

    <insert id="insertMonitorChannelNetwork" parameterType="cn.roadm.bo.AlarmChannelNetwork" >
        insert into alarm_channel_network
        (channel_id, continuous_num, alarm_object_type, alarm_content,network_interval_time,alarm_flag,data_scan_time)
        values ( #{channelId, jdbcType=INTEGER}, #{continuousNum, jdbcType=INTEGER}, #{alarmObjectType, jdbcType=VARCHAR}
            , #{alarmContent, jdbcType=VARCHAR} ,#{networkIntervalTime},#{alarmFlag},#{dataScanTime} )
    </insert>

    <select id="listMonitorChannelNetwork" parameterType="cn.roadm.bo.AlarmChannelNetwork"  resultMap="monitorChannelNetworkMap">
        select id id, channel_id channelId, continuous_num continuousNum, alarm_object_type alarmObjectType,
        alarm_content alarmContent ,network_interval_time,alarm_flag alarmFlag ,data_scan_time dataScanTime
         from alarm_channel_network where channel_id = #{channelId, jdbcType=INTEGER}
    </select>

    <insert id="insertMonitorQueueSize" parameterType="cn.roadm.bo.AlarmChannelQueueSize">
        insert into alarm_channel_queue_size
        (id, channel_id, alarm_queue_size, alarm_object_type, alarm_interval_time, alarm_content,alarm_flag,data_scan_time)
        values (null, #{channelId, jdbcType=INTEGER}, #{alarmQueueSize, jdbcType=INTEGER}, #{alarmObjectType, jdbcType=VARCHAR}
            , #{alarmIntervalTime, jdbcType=INTEGER}, #{alarmContent, jdbcType=VARCHAR}, #{alarmFlag},#{dataScanTime, jdbcType=INTEGER})
    </insert>

    <select id="listMonitorChannelQueueSize" parameterType="cn.roadm.bo.AlarmChannelQueueSize"  resultMap="monitorQueueSizeMap">
        select id id, channel_id channelId, alarm_queue_size alarmQueueSize, alarm_object_type alarmObjectType,
        alarm_interval_time alarmIntervalTime, alarm_content alarmContent, alarm_flag alarmFlag ,data_scan_time dataScanTime
         from alarm_channel_queue_size where channel_id = #{channelId, jdbcType=INTEGER}
    </select>


    <update id="updateMonitorQueueSize" parameterType="cn.roadm.bo.AlarmChannelQueueSize" >
    	update alarm_channel_queue_size set alarm_queue_size = #{alarmQueueSize, jdbcType=INTEGER}, alarm_object_type = #{alarmObjectType, jdbcType=VARCHAR},
    			alarm_content = #{alarmContent, jdbcType=VARCHAR}, alarm_interval_time = #{alarmIntervalTime, jdbcType=INTEGER}, alarm_flag=#{alarmFlag},
    	data_scan_time=#{dataScanTime, jdbcType=INTEGER} where  channel_id = #{channelId, jdbcType=INTEGER}
    </update>

    <update id="updateMonitorStatus" parameterType="cn.roadm.bo.AlarmPaReport" >
    	update alarm_pa_report set report_succ_rate = #{reportSuccRate, jdbcType=VARCHAR}, send_num_limit = #{sendNumLimit, jdbcType=VARCHAR},
    			alarm_type = #{alarmType, jdbcType=INTEGER}, alarm_num =  #{alarmNum, jdbcType=INTEGER},
    			alarm_content = #{alarmContent, jdbcType=VARCHAR}, alarm_interval_time = #{alarmIntervalTime, jdbcType=INTEGER},
    			data_scan_time = #{dataScanTime, jdbcType=INTEGER}
    			,alarm_flag = #{alarm_flag, jdbcType=INTEGER}
    			,delay_time = #{delay_time, jdbcType=INTEGER}
    			, alarm_cycle = #{alarm_cycle, jdbcType=INTEGER}
    			,receipt_rate = #{receiptRate, jdbcType=INTEGER}
    			,email = #{email, jdbcType=VARCHAR}
    			,phone = #{phone, jdbcType=VARCHAR}
    	where  channel_id = #{channelId, jdbcType=INTEGER}
    </update>

    <select id="listMonitorStatusInfo" parameterType="cn.roadm.bo.AlarmPaReport"  resultMap="monitorReportMap">
        select id id, channel_id channelId, report_succ_rate reportSuccRate, send_num_limit sendNumLimit,
        		alarm_type alarmType, alarm_num alarmNum, alarm_content alarmContent, alarm_interval_time alarmIntervalTime,
        		data_scan_time dataScanTime
        		,alarm_flag,delay_time,alarm_cycle,phone,email,receipt_rate receiptRate
        		from alarm_pa_report where channel_id = #{channelId, jdbcType=INTEGER}
    </select>

    <select id="getChannelAlarmConfig" parameterType="cn.roadm.bo.AlarmPaReport"  resultMap="monitorReportMap">
    	select * from channel_alarm_config where channelid = #{channelId, jdbcType=INTEGER}
    </select>

    <insert id="insertChannelAlarmConfig">
    	insert into channel_alarm_config(channelid,email,phone) values(#{channelId},#{email},#{phone})
    </insert>

    <update id="updateCahnnelAlarmConfig">
    	update channel_alarm_config set email=#{email},phone=#{phone} where channelid=#{channelId}
    </update>

    <select id="listChannelErrInfo" parameterType="cn.roadm.bo.AlarmChannelErrcode"  resultMap="AlarmChannelErrcodeMap">
        select id id, channelid channelId, errcode errcode,
                alarm_type alarmType, alarm_content alarmContent, alarm_interval_time alarmIntervalTime, alarm_flag alarm_flag
                from alarm_channel_errcode where channelid = #{channelId, jdbcType=INTEGER}
    </select>

    <insert id="insertMonitorStatus" parameterType="cn.roadm.bo.AlarmPaReport">
        insert into alarm_pa_report
        (id, channel_id, report_succ_rate, send_num_limit, alarm_type, alarm_num, alarm_content, alarm_interval_time, data_scan_time,alarm_flag,delay_time,alarm_cycle,receipt_rate,email,phone)
        values (null, #{channelId, jdbcType=INTEGER}, #{reportSuccRate, jdbcType=VARCHAR}, #{sendNumLimit, jdbcType=VARCHAR}
            , #{alarmType, jdbcType=VARCHAR}, #{alarmNum, jdbcType=INTEGER}, #{alarmContent, jdbcType=VARCHAR}
            , #{alarmIntervalTime, jdbcType=INTEGER}, #{dataScanTime, jdbcType=INTEGER}
            , #{alarm_flag, jdbcType=INTEGER}
            , #{delay_time, jdbcType=INTEGER}
            , #{alarm_cycle, jdbcType=INTEGER}
            , #{receiptRate, jdbcType=INTEGER}
            , #{email, jdbcType=VARCHAR}
            , #{phone, jdbcType=VARCHAR})
    </insert>

    <insert id="insertChannelError" parameterType="cn.roadm.bo.AlarmChannelErrcode">
        insert into alarm_channel_errcode
        (channelid, errcode, alarm_interval_time, alarm_type, alarm_content, alarm_flag)
        values (#{channelId, jdbcType=INTEGER}, #{errcode, jdbcType=VARCHAR}, #{alarmIntervalTime, jdbcType=INTEGER}
            , #{alarmType, jdbcType=VARCHAR},  #{alarmContent, jdbcType=VARCHAR} , #{alarm_flag, jdbcType=INTEGER})
    </insert>

    <delete id="delMonitorEmail" parameterType="cn.roadm.bo.AlarmEmail">
        delete from alarm_email where id = #{id, jdbcType=INTEGER}
    </delete>

    <select id="listMonitorEmail" parameterType="cn.roadm.bo.AlarmEmail" resultMap="monitorEmailMap">
        select s.send_email sendEmail, s.id id, s.server_host serverHost, s.mail_password mailPassword
        , s.subject subject , s.receive_email receiveEmail
        from alarm_email s order by s.id desc
    </select>

    <select id="listPageAlarmLog" parameterType="cn.roadm.bo.AlarmLog" resultType="cn.roadm.bo.AlarmLog">
        select id,alarm_type as alarmType,alarm_object_type as alarmObjectType,alarm_content as alarmContent,
        alarm_time as alarmTime,alarm_object as alarmObject,status
        from alarm_log
        <where>
            <if test="alarmType!=null and alarmType!=''">
                alarm_type = #{alarmType}
            </if>
            <if test="alarmObjectType!=null and alarmObjectType!=''">
               AND alarm_object_type like concat('%',#{alarmObjectType},'%')
            </if>
            <if test="alarmContent!=null and alarmContent!=''">
                and alarm_content like concat('%',#{alarmContent},'%')
            </if>
            <if test="alarmObject!=null and alarmObject!=''">
                and alarm_object like concat('%',#{alarmObject},'%')
            </if>
            <if test="status!=null">
                and status = #{status}
            </if>
        </where>
         order by id desc
    </select>

    <update id="updateAlarmLog" parameterType="integer">
        update alarm_log set status = 1 where id = #{id}
    </update>

    <update id="updateAlarmLogByType" parameterType="integer">
        update alarm_log set status = 1 where status = 0
        <if test="alarmType!=null">
            and alarm_type = #{alarmType}
        </if>
    </update>

    <select id="getAlarmLogBystatus" resultType="cn.roadm.bo.DelayDistributionVO">
        select alarm_type name , count(1) value
        from alarm_log
        where  status = 0
        group by alarm_type
    </select>

    <select id="listPageSpDelayMinRatioAlarm" resultType="cn.roadm.bo.DelayRatio">
        select
        <if test="typeName !=null and typeName!='' and typeName == 'sp'">
            sp_id,
            sp_name,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'channelid'">
            channel_name,
            channelid,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'spAndChannel'">
            sp_id,
            sp_name,
            channel_name,
            channelid,
        </if>
        all_number,
        submit_fail,
        arrive_succ,
        arrive_fail,
        five_less,
        ten_less,
        ten_to_twenty,
        twenty_to_thirty,
        thirty_to_sisty,
        sisty_more,
        batch_id,
        createtime
        from (
        select
        <if test="typeName !=null and typeName!='' and typeName == 'sp'">
            sp.id sp_id,
            sp.sp_name,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'channelid'">
            si.channel_name,
            sr.channelid,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'spAndChannel'">
            sp.id sp_id,
            sp.sp_name,
            si.channel_name,
            sr.channelid,
        </if>
        sr.batch_id,
        sum(IFNULL(sr.all_number,0)) all_number,
        sum(IFNULL(sr.all_number, 0))+sum(IFNULL(sr.submit_fail, 0))  number,
        sum(IFNULL(sr.submit_fail,0)) submit_fail,
        sum(IFNULL(sr.arrive_succ,0)) arrive_succ,
        sum(IFNULL(sr.arrive_fail,0)) arrive_fail,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) )/ sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100, 2 ) sucess_rate,
        CONCAT(CONCAT(sum(sr.five_less), '(' ),(CONCAT(Round(sum(sr.five_less)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) five_less,
        CONCAT(CONCAT(sum(sr.ten_less), '(' ),(CONCAT(Round(sum(sr.ten_less)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) ten_less,
        CONCAT(CONCAT(sum(sr.ten_to_twenty), '(' ),(CONCAT(Round(sum(sr.ten_to_twenty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) ten_to_twenty,
        CONCAT(CONCAT(sum(sr.twenty_to_thirty), '(' ),(CONCAT(Round(sum(sr.twenty_to_thirty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) twenty_to_thirty,
        CONCAT(CONCAT(sum(sr.thirty_to_sisty), '(' ),(CONCAT(Round(sum(sr.thirty_to_sisty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) thirty_to_sisty,
        CONCAT(CONCAT(sum(sr.sisty_more), '(' ),(CONCAT(Round(sum(sr.sisty_more)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) sisty_more,
        date_format(sr.createtime,'%Y-%m-%d %H:%i') createtime
        from sms_alarm_minute_record sr, sms_sp_info sp,sms_send_channel_info si
        where sr.spid = sp.id and si.id=sr.channelid
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="channelid!=null and channelid!=''">
            and si.id = #{channelid}
        </if>
        <if test="sp_id!=null and sp_id>0">
            and sr.spid=#{sp_id}
        </if>
        <if test="createTime!=null and createTime!=''">
            and sr.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            sr.createtime&lt;str_to_date(#{createTime1},'%Y-%m-%d %H:%i')
        </if>
        <if test="batch_id!=null and batch_id!=''">
            and batch_id = #{batch_id}
        </if>
        group by
        <if test="typeName !=null and typeName!='' and typeName == 'sp'">
            sp.id,
            sp.sp_name,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'channelid'">
            si.channel_name,
            sr.channelid,
        </if>
        <if test="typeName !=null and typeName!='' and typeName == 'spAndChannel'">
            sp.id,
            sp.sp_name,
            si.channel_name,
            sr.channelid,
        </if>
        date_format( sr.createtime, '%Y-%m-%d %H:%i' )
        order by date_format( sr.createtime, '%Y-%m-%d %H:%i' ) desc,number desc
        ) m
        <where>
            <if test="sucess_rate!= null and sucess_rate >= 0">
                m.sucess_rate >= #{sucess_rate}
            </if>
        </where>
    </select>

    <select id="getSpDelayMinRatioAlarmSum" resultType="cn.roadm.bo.DelayRatio">
        select
        sum(all_number) all_number,
        sum(submit_fail) submit_fail,
        sum(arrive_succ) arrive_succ,
        sum(arrive_fail) arrive_fail,
        sum(IFNULL(five_less,0)) five_less,
        sum(IFNULL(ten_less,0)) ten_less,
        sum(IFNULL(ten_to_twenty,0)) ten_to_twenty,
        sum(IFNULL(twenty_to_thirty,0)) twenty_to_thirty,
        sum(IFNULL(thirty_to_sisty,0)) thirty_to_sisty,
        sum(IFNULL(sisty_more,0)) sisty_more
        from (
        select sp.id sp_id,
        sp.sp_name,
        si.channel_name,
        sr.channelid,
        sum(IFNULL(sr.all_number,0)) all_number,
        sum(IFNULL(sr.all_number, 0))+sum(IFNULL(sr.submit_fail, 0))  number,
        sum(IFNULL(sr.submit_fail,0)) submit_fail,
        sum(IFNULL(sr.arrive_succ,0)) arrive_succ,
        sum(IFNULL(sr.arrive_fail,0)) arrive_fail,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) )/ sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100, 2 ) sucess_rate,
        sum(sr.five_less) five_less,
        sum(sr.ten_less) ten_less,
        sum(sr.ten_to_twenty) ten_to_twenty,
        sum(sr.twenty_to_thirty) twenty_to_thirty,
        sum(sr.thirty_to_sisty) thirty_to_sisty,
        sum(sr.sisty_more) sisty_more
        from sms_alarm_minute_record sr, sms_sp_info sp,sms_send_channel_info si
        where sr.spid = sp.id and si.id=sr.channelid
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="channelid!=null and channelid!=''">
            and si.id = #{channelid}
        </if>
        <if test="sp_id!=null and sp_id>0">
            and sr.spid=#{sp_id}
        </if>
        <if test="createTime!=null and createTime!=''">
            and sr.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            sr.createtime&lt;str_to_date(#{createTime1},'%Y-%m-%d %H:%i')
        </if>
        <if test="batch_id!=null and batch_id!=''">
            and batch_id = #{batch_id}
        </if>
        group by sp.id, sp.sp_name,si.channel_name,sr.channelid,date_format( sr.createtime, '%Y-%m-%d %H:%i' )
        order by date_format( sr.createtime, '%Y-%m-%d %H:%i' ) desc, number desc
        ) m
        <where>
            <if test="sucess_rate!= null and sucess_rate >= 0">
                m.sucess_rate >= #{sucess_rate}
            </if>
        </where>
    </select>

    <select id="findAlarmLogTotals" resultType="cn.roadm.bo.AlarmLogTotalNumber">
		SELECT COUNT(*) AS alarmNumber, any_value(alarm_object_type) AS alarmObjectType
        FROM alarm_log
		WHERE alarm_time > DATE_ADD(NOW(), INTERVAL - #{daysToQuery} DAY)
		GROUP BY alarm_object_type
    </select>

    <select id="listTopAlarmLog" resultType="cn.roadm.bo.AlarmLog">
        SELECT id, alarm_type as alarmType, alarm_object_type as alarmObjectType,
        alarm_content as alarmContent, alarm_time as alarmTime
        FROM alarm_log
         WHERE alarm_time > DATE_ADD(NOW(), INTERVAL - #{daysToQuery} DAY)
         ORDER BY id DESC
         LIMIT 0, 5
    </select>

    <select id="hasMonitorEmail" parameterType="cn.roadm.bo.AlarmEmail" resultType="int">
        select count(1) from alarm_email s where s.receive_email = #{receiveEmail, jdbcType=VARCHAR}
    </select>

    <insert id="insertMonitorEmail" parameterType="cn.roadm.bo.AlarmEmail">
        insert into alarm_email
        (id, send_email, server_host, mail_password, subject, receive_email) values
            (null, #{sendEmail, jdbcType=VARCHAR}, #{serverHost, jdbcType=VARCHAR}, #{mailPassword, jdbcType=VARCHAR}
            , #{subject, jdbcType=VARCHAR}, #{receiveEmail, jdbcType=VARCHAR}  )
    </insert>

    <select id="listPagePhone" parameterType="cn.roadm.bo.AlarmSms" resultMap="monitorMap">
        select s.phone phone, s.id id
        from alarm_sms s order by s.id desc
    </select>

    <select id="hasMonitorPhone" parameterType="cn.roadm.bo.AlarmSms" resultType="int">
        select count(1) from alarm_sms s where s.phone = #{phone, jdbcType=VARCHAR}
    </select>

    <insert id="insertMonitorPhone" parameterType="cn.roadm.bo.AlarmSms">
        insert into alarm_sms
        (id, phone) values
        ( null, #{phone, jdbcType=VARCHAR} )
    </insert>

    <delete id="deleteMonitorPhone" parameterType="cn.roadm.bo.AlarmSms">
        delete from  alarm_sms where id = #{id, jdbcType=INTEGER}
    </delete>


    <select id="listPageBackCode" parameterType="cn.roadm.bo.BackCode" resultType="cn.roadm.bo.BackCode" >
        SELECT a.id,a.back_code as backCode ,a.code_type as codeType,a.pa_id as paId ,a.send_time as sendTime,
        a.`status`,a.descp,a.create_time as createTime ,a.resend_pa as resendPa,a.resend_time,a.sp_id,b.sp_name,
        a.mobile_pa,a.unicom_pa,a.telecom_pa,a.is_triple,a.is_sign_group,a.proSignId,a.replace_sign_group_id as replaceSignGroupId from sms_back_code as a left join sms_sp_info as b
        on a.sp_id = b.id
        <where>
            <if test="backCode != null and backCode != ''">
                a.back_code like CONCAT('%',#{backCode},'%')
            </if>
            <if test="codeType != null and codeType!=''">
                and a.code_type = #{codeType}
            </if>
            <if test="status!=null and status!=''">
                and a.`status` = #{status}
            </if>
            <if test="paId !=null and paId !=''">
                and a.pa_id = #{paId}
            </if>
            <if test="resendPa !=null and resendPa !=''">
                and (
                a.resend_pa = #{resendPa} or
                a.mobile_pa = #{resendPa} or
                a.unicom_pa = #{resendPa} or
                a.telecom_pa = #{resendPa}
                )
            </if>
            <if test="sp_id !=null and sp_id != ''">
                and a.sp_id = #{sp_id}
            </if>
            <if test="sp_name != null and sp_name!= ''">
                and b.sp_name like CONCAT('%',#{sp_name},'%')
            </if>
            <if test=" mms_flag != null and mms_flag == 2">
                and a.mms_flag = 2
            </if>
            <if test=" mms_flag == null or mms_flag != 2">
                and (a.mms_flag is null or a.mms_flag != 2)
            </if>

    	</where>
        order by id desc
    </select>

    <insert id="saveBackCode" parameterType="cn.roadm.bo.BackCode" useGeneratedKeys="true" keyProperty="id">
        insert into sms_back_code (back_code,code_type,pa_id,send_time,status,descp,create_time,resend_pa,resend_time,sp_id,null_error_code
        ,is_triple,is_group,mobile_pa,unicom_pa,telecom_pa,is_provincial_group,proGroupId,rounting_number_flag,rount_interface,sp_white,shunt_white,mms_flag,is_sign_group,proSignId,replace_sign_group_id)
        VALUES (#{backCode},#{codeType},#{paId},#{sendTime},#{status},#{descp},#{createTime},#{resendPa},#{resend_time},#{sp_id},#{nullErrorCode},
        #{is_triple},#{is_group},#{mobile_pa},#{unicom_pa},#{telecom_pa},#{is_provincial_group},#{proGroupId},#{rounting_number_flag},#{rount_interface},#{sp_white},#{shunt_white},#{mms_flag},#{is_sign_group},#{proSignId},#{replaceSignGroupId})
    </insert>

    <select id="findBackCodeId" resultType="cn.roadm.bo.BackCode" parameterType="int">
        SELECT id,back_code as backCode ,code_type as codeType,pa_id as paId ,send_time as sendTime,resend_time,
        `status`,descp,create_time as createTime,resend_pa as resendPa ,sp_id,null_error_code nullErrorCode,
         is_triple,is_group,mobile_pa,unicom_pa,telecom_pa,is_provincial_group,proGroupId,rounting_number_flag,rount_interface,
         sp_white,shunt_white,is_sign_group,proSignId,replace_sign_group_id as replaceSignGroupId
        from sms_back_code where id = #{id}
    </select>

    <select id="getBackCode" parameterType="cn.roadm.bo.BackCode" resultType="cn.roadm.bo.BackCode" >
        SELECT a.id,a.back_code as backCode ,a.code_type as codeType,a.pa_id as paId ,a.send_time as sendTime,
        a.`status`,a.descp,a.create_time as createTime ,a.resend_pa as resendPa,a.resend_time,a.sp_id,
        a.null_error_code nullErrorCode,a.mobile_pa,a.unicom_pa,a.telecom_pa,a.is_triple,a.is_group,rounting_number_flag,a.is_sign_group,a.proSignId,a.replace_sign_group_id as replaceSignGroupId from sms_back_code as a
        <where>
            <if test="backCode != null and backCode != ''">
                a.back_code like CONCAT('%',#{backCode},'%')
            </if>
            <if test="codeType != null and codeType!=''">
                and a.code_type = #{codeType}
            </if>
            <if test="status!=null and status!=''">
                and a.`status` = #{status}
            </if>
            <if test="paId !=null and paId !=''">
                and a.pa_id = #{paId}
            </if>
            <if test="resendPa !=null and resendPa !=''">
                and (
                a.resend_pa = #{resendPa} or
                a.mobile_pa = #{resendPa} or
                a.unicom_pa = #{resendPa} or
                a.telecom_pa = #{resendPa}
                )
            </if>
            <if test="sp_id !=null and sp_id != ''">
                and a.sp_id = #{sp_id}
            </if>
            <if test="sp_id ==null or sp_id==''">
            	and a.sp_id is null
            </if>
            <if test="sp_name != null and sp_name!= ''">
                and b.sp_name like CONCAT('%',#{sp_name},'%')
            </if>

    	</where>
        order by id desc
    </select>
    <update id="updateBackCodeById" parameterType="cn.roadm.bo.BackCode">
        update sms_back_code set back_code = #{backCode},code_type = #{codeType},resend_time = #{resend_time},
        pa_id = #{paId},send_time = #{sendTime},status = #{status},descp = #{descp},create_time = #{createTime}
        ,resend_pa = #{resendPa} ,sp_id = #{sp_id},null_error_code=#{nullErrorCode},is_triple=#{is_triple},
        is_group=#{is_group},mobile_pa=#{mobile_pa},unicom_pa=#{unicom_pa},telecom_pa=#{telecom_pa},is_provincial_group=#{is_provincial_group},proGroupId=#{proGroupId},rounting_number_flag=#{rounting_number_flag},
        rount_interface=#{rount_interface},is_sign_group=#{is_sign_group},proSignId=#{proSignId},replace_sign_group_id=#{replaceSignGroupId} where id = #{id}
    </update>

    <delete id="deleteBackCodeById" parameterType="int">
      delete from sms_back_code where id =#{id};
    </delete>

    <resultMap id="paChannelMap" type="cn.roadm.bo.PaChannel">
        <id column="id" property="id"/>
        <result property="name" column="name"/>
        <result property="queryTime" column="queryTime"/>
        <result property="pingTime" column="pingTime"/>
        <result property="autoChange" column="autoChange"/>
        <result property="is_support_unicom" column="is_support_unicom"/>
        <result property="is_support_mobile" column="is_support_mobile"/>
        <result property="is_support_telecom" column="is_support_telecom"/>
        <result property="ip" column="ip"/>
        <collection property="listBackIp" ofType="cn.roadm.bo.BackupIp">
            <id column="backId" property="id"/>
            <result column="backIp" property="backupIp" />
            <result column="paId" property="paId" />
        </collection>
    </resultMap>

    <select id="listChannel" resultMap="paChannelMap">
       SELECT
            a.id,
            a.CHANNEL_NAME AS NAME,
            a.query_time AS queryTime,
            a.ping_time pingTime,
            a.auto_change AS autoChange,
          b.ip,
          c.backup_ip as backIp,
          c.id as backId,
          c.pa_id as paId,
          a.is_support_unicom,
          a.is_support_mobile,
          a.is_support_telecom
        FROM
            sms_send_channel_info a
          LEFT JOIN sms_carrier_channel_info b on a.ID = b.id
          LEFT JOIN sms_channel_backup_ip c on c.pa_id = b.id
          where a.`STATUS` = '1' and a.protocol_type &lt; 20
        ORDER BY
            id DESC
    </select>

 <!--test begin-->
    <select id="listSpChannelSpeed" parameterType="cn.roadm.bo.ChannelSendSpeed" resultMap="ChannelSendSpeedMap">
        SELECT
        s.channelid,
        s.speed,
        s.monitortime,
        DATE_FORMAT(s.createtime,'%Y-%m-%d %H:%i:%S') as createtime
        FROM
        sms_channel_speed_monitor  s
        where 1=1
        <if test="channelid!=null and channelid!=''">
            and s.channelid= #{channelid}
        </if>
        <if test="type!=null and type>=0">
            and s.type= #{type}
        </if>
        <if test="createTime!=null and createTime!=''">
        	and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
            <!-- and DATE_FORMAT(s.createtime,'%Y-%m-%d')=#{createTime} -->
        </if>
		<if test="createTime1!=null and createTime1!=''">
        	and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
        </if>
    </select>

    <select id="listSpReportSpeed" parameterType="cn.roadm.bo.ChannelSendSpeed" resultMap="ChannelSendSpeedMap">
        SELECT
        s.channelid,
        s.speed,
        s.monitortime,
        DATE_FORMAT(s.createtime,'%Y-%m-%d %H:%i:%S') as createtime
        FROM
        sms_channel_report_speed  s
        where 1=1
        <if test="channelid!=null and channelid!=''">
            and s.channelid= #{channelid}
        </if>
        <if test="type!=null and type>=0">
            and s.type= #{type}
        </if>
        <if test="createTime!=null and createTime!=''">
            and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
            <!-- and DATE_FORMAT(s.createtime,'%Y-%m-%d')=#{createTime} -->
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
        </if>
    </select>

    <!--test end-->

     <insert id="insertMonitorSpApi" parameterType="cn.roadm.bo.AlarmSpApi">
        insert into alarm_sp_api
        (sp_id, alarm_content, no_send_time, submit_succ_ratetime, alarm_type, alarm_num, submit_succ_rate, alarm_interval_time, audit_num_type,audit_num_rate,audit_num_time
        ,audit_num,email,phone,alarm_flag,delay_time,no_send_time_json)
        values (#{sp_id, jdbcType=INTEGER}, #{alarmContent, jdbcType=VARCHAR}, #{noSendTime, jdbcType=INTEGER}, #{submitSuccRateTime, jdbcType=INTEGER}
            , #{alarmType, jdbcType=INTEGER}, #{alarmNum, jdbcType=INTEGER}, #{submitSuccRate, jdbcType=INTEGER}
            , #{alarmIntervalTime, jdbcType=INTEGER}, #{auditNumType, jdbcType=INTEGER} , #{auditNumRate, jdbcType=INTEGER}
            , #{auditNumTime, jdbcType=INTEGER} , #{auditNum, jdbcType=INTEGER}
            , #{email, jdbcType=VARCHAR}, #{phone, jdbcType=VARCHAR}

            , #{alarm_flag}
            , #{delay_time}
            , #{noSendTimeJson}
            )
    </insert>

    <select id="listMonitorSpApiInfo" parameterType="cn.roadm.bo.AlarmSpApi"  resultType="cn.roadm.bo.AlarmSpApi">
        select id , sp_id, alarm_content alarmContent, no_send_time noSendTime, submit_succ_ratetime submitSuccRateTime, alarm_type alarmType, alarm_num alarmNum, submit_succ_rate submitSuccRate
        , alarm_interval_time alarmIntervalTime, audit_num_type auditNumType,audit_num_rate auditNumRate,audit_num_time auditNumTime,audit_num  auditNum,email,phone
        		 ,alarm_flag,delay_time,no_send_time_json noSendTimeJson
        		 from alarm_sp_api

        		where sp_id = #{sp_id, jdbcType=INTEGER}
    </select>

     <update id="updateMonitorSpApi" parameterType="cn.roadm.bo.AlarmSpApi" >
    	update alarm_sp_api set alarm_content = #{alarmContent, jdbcType=VARCHAR}, alarm_type = #{alarmType, jdbcType=VARCHAR},
    			no_send_time = #{noSendTime, jdbcType=INTEGER}, alarm_num =  #{alarmNum, jdbcType=INTEGER},
    			submit_succ_ratetime = #{submitSuccRateTime, jdbcType=INTEGER}, alarm_interval_time = #{alarmIntervalTime, jdbcType=INTEGER},
    			submit_succ_rate = #{submitSuccRate, jdbcType=INTEGER} ,audit_num_type = #{auditNumType, jdbcType=INTEGER} ,audit_num_rate = #{auditNumRate, jdbcType=INTEGER}
    			,audit_num_time = #{auditNumTime, jdbcType=INTEGER} ,audit_num = #{auditNum, jdbcType=INTEGER}
    			, email = #{email, jdbcType=VARCHAR}
    			, phone = #{phone, jdbcType=VARCHAR}
    			, alarm_flag = #{alarm_flag, jdbcType=INTEGER}
    			, delay_time = #{delay_time, jdbcType=INTEGER}
    			, no_send_time_json = #{noSendTimeJson}
    	where  sp_id = #{sp_id, jdbcType=INTEGER}
    </update>

     <insert id="insertMonitorSpArrive" parameterType="cn.roadm.bo.AlarmSpArrive">
        insert into alarm_sp_arrive
        (sp_id, alarm_content, arrive_succ_ratetime, submit_succ_rateTtme, alarm_type, submit_succ_rate, alarm_interval_time, arrive_succ_rate
        ,telecom_channel,unicom_prefix,mobile_prefix,telecom_prefix,email,phone,unicom_channel,mobile_channel,auto_switch_channel,alarm_flag,delay_time,min_num
         ,receipt_rate,receipt_rate_time)
        values (#{sp_id, jdbcType=INTEGER}, #{alarmContent, jdbcType=VARCHAR}, #{arriveSuccRateTime, jdbcType=INTEGER}, #{submitSuccRateTime, jdbcType=INTEGER}
            , #{alarmType, jdbcType=INTEGER}, #{submitSuccRate, jdbcType=INTEGER}
            , #{alarmIntervalTime, jdbcType=INTEGER}, #{arriveSuccRate, jdbcType=INTEGER}
            ,#{telecom_channel, jdbcType=INTEGER} ,#{unicom_prefix, jdbcType=VARCHAR},#{mobile_prefix, jdbcType=VARCHAR},#{telecom_prefix, jdbcType=VARCHAR}
            ,#{email, jdbcType=VARCHAR},#{phone, jdbcType=VARCHAR}, #{unicom_channel, jdbcType=INTEGER}, #{mobile_channel, jdbcType=INTEGER}
            ,#{auto_switch_channel, jdbcType=VARCHAR}
            , #{alarm_flag, jdbcType=INTEGER}
            , #{delay_time, jdbcType=INTEGER},#{minNum, jdbcType=INTEGER},#{receiptRate,jdbcType=INTEGER},#{receiptRateTime,jdbcType=INTEGER}
            )
    </insert>
    <update id="updateMonitorSpArrive" parameterType="cn.roadm.bo.AlarmSpArrive" >
    	update alarm_sp_arrive set alarm_content = #{alarmContent, jdbcType=VARCHAR}, alarm_type = #{alarmType, jdbcType=VARCHAR},
    			arrive_succ_ratetime = #{arriveSuccRateTime, jdbcType=INTEGER}, submit_succ_rateTtme =  #{submitSuccRateTime, jdbcType=INTEGER},
    			arrive_succ_rate = #{arriveSuccRate, jdbcType=INTEGER}, alarm_interval_time = #{alarmIntervalTime, jdbcType=INTEGER},
    			submit_succ_rate = #{submitSuccRate, jdbcType=INTEGER} ,unicom_channel = #{unicom_channel, jdbcType=INTEGER}
    			,mobile_channel = #{mobile_channel, jdbcType=INTEGER},telecom_channel = #{telecom_channel, jdbcType=INTEGER}

    			,phone = #{phone, jdbcType=VARCHAR},email = #{email, jdbcType=VARCHAR}
    			,telecom_prefix = #{telecom_prefix, jdbcType=VARCHAR},mobile_prefix = #{mobile_prefix, jdbcType=VARCHAR}
    			,unicom_prefix = #{unicom_prefix, jdbcType=VARCHAR}
    			,auto_switch_channel = #{auto_switch_channel, jdbcType=VARCHAR}
    			,alarm_flag = #{alarm_flag}
    			,delay_time = #{delay_time},min_num=#{minNum},receipt_rate=#{receiptRate},receipt_rate_time=#{receiptRateTime}
    	where  sp_id = #{sp_id, jdbcType=INTEGER}
    </update>

    <select id="listMonitorSpArriveInfo" parameterType="cn.roadm.bo.AlarmSpArrive"  resultType="cn.roadm.bo.AlarmSpArrive">
        select id , sp_id,alarm_content alarmContent,arrive_succ_ratetime arriveSuccRateTime,submit_succ_rateTtme submitSuccRateTime,alarm_type alarmType
        ,submit_succ_rate submitSuccRate,alarm_interval_time alarmIntervalTime,arrive_succ_rate arriveSuccRate,unicom_channel,mobile_channel
        ,telecom_channel,unicom_prefix,mobile_prefix,telecom_prefix,email,phone,auto_switch_channel
         ,alarm_flag,delay_time,min_num minNum, receipt_rate as receiptRate, receipt_rate_time as receiptRateTime
        from alarm_sp_arrive where sp_id = #{sp_id, jdbcType=INTEGER}
    </select>

    <select id="getMonitorSpArriveBySpId" resultType="int" parameterType="cn.roadm.bo.AlarmSpArrive">
        SELECT count(1)  from alarm_sp_arrive where sp_id = #{sp_id, jdbcType=INTEGER}
    </select>
    <select id="getMonitorSpApiBySpId" resultType="int" parameterType="cn.roadm.bo.AlarmSpApi">
        SELECT count(1)  from alarm_sp_api where sp_id = #{sp_id, jdbcType=INTEGER}
    </select>

     <select id="getMonitorSpUrlBySpId" resultType="int" parameterType="cn.roadm.bo.AlarmSpUrl">
        SELECT count(1)  from sms_url_sp_config where spid = #{sp_id, jdbcType=INTEGER}
    </select>

    <insert id="insertMonitorSpUrl" parameterType="cn.roadm.bo.AlarmSpUrl">
        insert into sms_url_sp_config
        (spid,isopen,word_check_flag,word_check_type,ip_domain_flag,check_priority,url_status_flag,
        url_status_strategy,url_redirect_flag,screenshot_flag,ip_region_flag,
        ip_region_strategy,polling_flag,polling_num,polling_interval,createtime,
         alarm_content, alarm_type, email,phone)

        values (#{sp_id, jdbcType=INTEGER}, #{isopen, jdbcType=INTEGER} ,
        #{word_check_flag, jdbcType=INTEGER} ,#{word_check_type, jdbcType=VARCHAR} ,
        #{ip_domain_flag, jdbcType=INTEGER} ,#{check_priority, jdbcType=INTEGER} ,
        #{url_status_flag, jdbcType=INTEGER} ,#{url_status_strategy, jdbcType=INTEGER} ,
        #{url_redirect_flag, jdbcType=INTEGER} ,#{screenshot_flag, jdbcType=INTEGER} ,
        #{ip_region_flag, jdbcType=INTEGER} ,#{ip_region_strategy, jdbcType=INTEGER} ,
        #{polling_flag, jdbcType=INTEGER} ,#{polling_num, jdbcType=INTEGER} ,
        #{polling_interval, jdbcType=INTEGER}, sysdate(), #{alarmContent, jdbcType=VARCHAR},  #{alarmType, jdbcType=INTEGER}
            ,#{email, jdbcType=VARCHAR},#{phone, jdbcType=VARCHAR} )
    </insert>

     <update id="updateMonitorSpUrl" parameterType="cn.roadm.bo.AlarmSpUrl" >
    	update sms_url_sp_config set alarm_content = #{alarmContent, jdbcType=VARCHAR}, alarm_type = #{alarmType, jdbcType=VARCHAR},
    			 isopen = #{isopen, jdbcType=INTEGER},word_check_flag = #{word_check_flag, jdbcType=INTEGER},
    			 word_check_type = #{word_check_type, jdbcType=VARCHAR},ip_domain_flag = #{ip_domain_flag, jdbcType=INTEGER},
    			 check_priority = #{check_priority, jdbcType=INTEGER},url_status_flag = #{url_status_flag, jdbcType=INTEGER},
    			 url_status_strategy = #{url_status_strategy, jdbcType=INTEGER},polling_flag = #{polling_flag, jdbcType=INTEGER},
    			 polling_num = #{polling_num, jdbcType=INTEGER},polling_interval = #{polling_interval, jdbcType=INTEGER},
    			 ip_region_flag = #{ip_region_flag, jdbcType=INTEGER},polling_interval = #{polling_interval, jdbcType=INTEGER},
    			 polling_num = #{polling_num, jdbcType=INTEGER},
    			phone = #{phone, jdbcType=VARCHAR},email = #{email, jdbcType=VARCHAR}
    	where  spid = #{sp_id, jdbcType=INTEGER}
    </update>

     <select id="getMonitorSpUrlInfo" parameterType="cn.roadm.bo.AlarmSpUrl"  resultType="cn.roadm.bo.AlarmSpUrl">
        select spid sp_id,alarm_content alarmContent,alarm_type alarmType,
        email,phone ,isopen,word_check_flag,word_check_type,ip_domain_flag,check_priority,url_status_flag,
        url_status_strategy,url_redirect_flag,screenshot_flag,ip_region_flag,
        ip_region_strategy,polling_flag,polling_num,polling_interval,createtime,ip_region_flag
        		 from sms_url_sp_config
        		where spid = #{sp_id, jdbcType=INTEGER}
        		limit 1
    </select>

    <select id="getAlarmOverstack" resultType="cn.roadm.bo.AlarmOverstack">
    	select id,alarm_flag alarmFlag,alarm_type alarmType,cut_value cutValue,
    	scan_time scanTime,alarm_content alarmContent,phone,email,cut_value_queue cutValueQueue,cut_value_receiptc cutValueReceiptc,
    	cut_value_receiptp cutValueReceiptp from alarm_overstack limit 1
    </select>

    <select id="getAlarmXuni"  resultType="cn.roadm.bo.AlarmXuni">
        select id,alarm_flag alarmFlag,alarm_type alarmType,send_value sendValue,
               scan_time scanTime,alarm_content alarmContent,phone,email from alarm_xuni where id = #{id}
    </select>

    <update id="updateAlarmXuni" parameterType="cn.roadm.bo.AlarmXuni">
        UPDATE `alarm_xuni` SET `alarm_flag`=#{alarmFlag},`alarm_type`=#{alarmType},
                                     `send_value`=#{sendValue}, `scan_time`=#{scanTime}, `alarm_content`=#{alarmContent},
                                     `phone`=#{phone}, `email`=#{email} WHERE `id`=#{id}
    </update>
    <insert id="insertAlarmXuni" parameterType="cn.roadm.bo.AlarmXuni">
        INSERT INTO `alarm_xuni`
        ( id ,`alarm_flag`, `alarm_type`, `send_value`, `scan_time`, `alarm_content`, `phone`, `email`) VALUES
            (#{id},#{alarmFlag}, #{alarmType},#{sendValue} , #{scanTime}, #{alarmContent}, #{phone},#{email})
    </insert>

    <update id="updateAlarmOverstack" parameterType="cn.roadm.bo.AlarmOverstack">
    	UPDATE `alarm_overstack` SET `alarm_flag`=#{alarmFlag},`alarm_type`=#{alarmType},
    	`cut_value`=#{cutValue}, `scan_time`=#{scanTime}, `alarm_content`=#{alarmContent},
    	`phone`=#{phone}, `email`=#{email},cut_value_receiptp=#{cutValueReceiptp},cut_value_receiptc=#{cutValueReceiptc},
    	cut_value_queue=#{cutValueQueue} WHERE `id`=#{id}
    </update>
     <insert id="insertAlarmOverstack" parameterType="cn.roadm.bo.AlarmOverstack">
     	INSERT INTO `alarm_overstack`
     	(`alarm_flag`, `alarm_type`, `cut_value`, `scan_time`, `alarm_content`, `phone`, `email`,cut_value_receiptp,cut_value_receiptc,cut_value_queue) VALUES
     	(#{alarmFlag}, #{alarmType},#{cutValue} , #{scanTime}, #{alarmContent}, #{phone},#{email},#{cutValueReceiptp},#{cutValueReceiptc},#{cutValueQueue})
     </insert>


     <select id="getChannelChange" resultType="cn.roadm.bo.AlarmChannelChange">
     	select id,openflag,oldchannelid,newchannelid,reportsuccflag,overstackflag,
     	networkflag,date_format(createtime, '%y%m%d %h:%i:%s') createtime,
     	report_succ_rate reportSuccRate,send_num_limit sendNumLimit,data_scan_time dataScanTime,
     	weekdays_begin_time weekdaysBeginTime,weekdays_end_time weekdaysEndTime,
     	weekend_begin_time weekendBeginTime,weekend_end_time weekendEndTime,delay_time delayTime,
     	alarm_type alarmType,phone,email
     	from alarm_channel_change where oldchannelid=#{oldchannelid} limit 1
     </select>
     <select id="listPageChannelChange" resultType="cn.roadm.bo.AlarmChannelChange">
     	select ach.id,ach.openflag,ach.oldchannelid,ach.newchannelid,ach.reportsuccflag,ach.overstackflag,
     	ach.networkflag,date_format(ach.createtime, '%Y-%m-%d %H:%m:%s') createtime,
     	ach.report_succ_rate reportSuccRate,ach.send_num_limit sendNumLimit,ach.data_scan_time dataScanTime,
     	ach.weekdays_begin_time weekdaysBeginTime,ach.weekdays_end_time weekdaysEndTime,
     	ach.weekend_begin_time weekendBeginTime,ach.weekend_end_time weekendEndTime,ach.delay_time delayTime,
     	ach.alarm_type alarmType,ach.phone,ach.email
     	from alarm_channel_change ach
     	<where>
     		<if test="oldchannelid!=null and oldchannelid!=''">
     			and oldchannelid=#{oldchannelid}
     		</if>
     		<if test="newchannelid!=null and newchannelid!=''">
     			and newchannelid=#{newchannelid}
     		</if>
     		<if test="createtime!=null and createtime!=''">
     			and createtime &gt;= #{createtime}
     		</if>
     		<if test="createtime1!=null and createtime1!=''">
     			and createtime &gt;= #{createtime1}
     		</if>
     	</where>
     </select>

     <select id="getChannelChangeById" resultType="cn.roadm.bo.AlarmChannelChange">
     	select ach.id,ach.openflag,ach.oldchannelid,ach.newchannelid,ach.reportsuccflag,ach.overstackflag,
     	ach.networkflag,date_format(ach.createtime, '%Y-%m-%d %H:%m:%s') createtime,
     	ach.report_succ_rate reportSuccRate,ach.send_num_limit sendNumLimit,ach.data_scan_time dataScanTime,
     	ach.weekdays_begin_time weekdaysBeginTime,ach.weekdays_end_time weekdaysEndTime,
     	ach.weekend_begin_time weekendBeginTime,ach.weekend_end_time weekendEndTime,ach.delay_time delayTime,
     	ach.alarm_type alarmType,ach.phone,ach.email
     	from alarm_channel_change ach
     	where ach.id=#{id}
     </select>
     <select id="getChannelChangeRecord" resultType="cn.roadm.bo.AlarmChannelChange">
     	select sp_id spId from alarm_channel_change_record where change_id=#{id}
     </select>

    <insert id="insertChannelChange" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.AlarmChannelChange">
     	INSERT INTO `alarm_channel_change` (`openflag`, `oldchannelid`, `newchannelid`, `reportsuccflag`, `overstackflag`,
     	 `networkflag`, `createtime`,report_succ_rate,send_num_limit,data_scan_time,weekdays_begin_time,weekdays_end_time,
     	 weekend_begin_time,weekend_end_time,delay_time,alarm_type,phone,email)
     	VALUES (#{openflag}, #{oldchannelid}, #{newchannelid}, #{reportsuccflag}, #{overstackflag}, #{networkflag}, now(),
     	#{reportSuccRate},#{sendNumLimit},#{dataScanTime},#{weekdaysBeginTime},#{weekdaysEndTime},#{weekendBeginTime},#{weekendEndTime},
     	#{delayTime},#{alarmType},#{phone},#{email})
     </insert>
     <insert id="insertChangeRecord">
     	insert into alarm_channel_change_record(sp_id,change_id) value(#{spId},#{id})
     </insert>
    <delete id="delChangeRecord">
     	delete from alarm_channel_change_record where change_id=#{id}
     </delete>
    <update id="updateChannelChange" parameterType="cn.roadm.bo.AlarmChannelChange">
     	update alarm_channel_change set openflag=#{openflag},oldchannelid=#{oldchannelid},newchannelid=#{newchannelid},
     	reportsuccflag=#{reportsuccflag},overstackflag=#{overstackflag},networkflag=#{networkflag},report_succ_rate=#{reportSuccRate},
     	send_num_limit=#{sendNumLimit},data_scan_time=#{dataScanTime},weekdays_begin_time=#{weekdaysBeginTime},weekdays_end_time=#{weekdaysEndTime},
     	weekend_begin_time=#{weekendBeginTime},weekend_end_time=#{weekendEndTime},
     	delay_time=#{delayTime},alarm_type=#{alarmType},phone=#{phone},email=#{email} where id=#{id}
     </update>

    <delete id="deleteSwitchConfig">
        DELETE FROM fault_over where spid = #{spid}
    </delete>

    <update id="updateSwitchConfig">
        update fault_over set over_flag = #{overFlag},delay_time=#{delayTime},arrive_succ_rate=#{arriveSuccRate},arrive_succ_rate_time=#{arriveSuccRateTime},
                              submit_succ_rate=#{submitSuccRate},submit_succ_rate_time=#{submitSuccRateTime},spid=#{spid},old_channelid=#{oldChannelid},
                              yd_channelid=#{ydChannelid},dx_channelid=#{dxChannelid},lt_channelid=#{ltChannelid},alarm_type=#{alarmType},
                              trigger_cycle=#{triggerCycle},phone=#{phones},email=#{emails},content=#{content},
                              min_send_num=#{minSendNum}, receipt_rate = #{receiptRate}, receipt_rate_time = #{receiptRateTime} where id =#{id}
    </update>

    <delete id="deleteSwitchBaseData">
        DELETE FROM fault_over_record where spid = #{spid}
    </delete>

    <insert id="saveSwitchConfig" parameterType="cn.roadm.bo.ChannelSwitchConfigBO">
       replace into fault_over (over_flag,delay_time,arrive_succ_rate,arrive_succ_rate_time,submit_succ_rate,submit_succ_rate_time,spid,old_channelid,
                                yd_channelid,dx_channelid,lt_channelid,alarm_type,trigger_cycle,phone,email,content,operator,createtime,min_send_num,
        receipt_rate, receipt_rate_time)
        VALUES(#{overFlag},#{delayTime},#{arriveSuccRate},#{arriveSuccRateTime},#{submitSuccRate},#{submitSuccRateTime},
                                      #{spid},#{oldChannelid},#{ydChannelid},#{dxChannelid},#{ltChannelid},#{alarmType},
               #{triggerCycle},#{phones},#{emails},#{content},#{operator},#{createtime},#{minSendNum},#{receiptRate},#{receiptRateTime})
    </insert>
    <insert id="saveSwitchBaseData" parameterType="java.util.List">
        INSERT INTO `fault_over_record` (`spid`, `arrive_succ_rate`, `submit_succ_rate`, `old_channelid`, `new_channelid`, `otype`, `createtime`, `fault_time`)
        VALUES
               <foreach collection="list" item="item" index="index" separator=",">
                    (#{item.spId}, #{item.arriveSuccRate}, #{item.submitSuccRate}, #{item.oldChannelid},
                    #{item.newChannelid}, #{item.otype}, #{item.createtime}, #{item.faultTime})
               </foreach>
    </insert>
    <select id="listPageChannelSwitchTable" resultType="cn.roadm.bo.ChannelSwitchTableDataBO">
        select a.id,a.over_flag,a.delay_time,a.arrive_succ_rate,a.arrive_succ_rate_time,a.submit_succ_rate,a.submit_succ_rate_time,
               a.spid,a.old_channelid,a.yd_channelid,a.lt_channelid,a.dx_channelid,a.alarm_type,a.trigger_cycle,a.phone,a.email,a.content,
               a.operator,DATE_FORMAT(a.createtime, '%Y-%m-%d %H:%i:%s') as createtime,a.min_send_num,a.receipt_rate, a.receipt_rate_time,
               b.sp_name from fault_over a left join sms_sp_info b on a.spid = b.id
        <where>
            <if test="id != null and id != ''">
                a.id = #{id}
            </if>
            <if test="spid != null and spid != ''">
                and a.spid = #{spid}
            </if>
            <if test="sp_name != null and sp_name != ''">
                AND b.sp_name LIKE CONCAT( '%',#{sp_name}, '%' )
            </if>
            <if test="old_channelid != null and old_channelid != ''">
                AND a.old_channelid = #{old_channelid}
            </if>
            <if test="status != null and status != ''">
                and a.over_flag = #{status}
            </if>
            <if test="new_channelid != null and new_channelid != ''">
                and (yd_channelid = #{new_channelid} or lt_channelid = #{new_channelid} or dx_channelid = #{new_channelid})
            </if>
        </where>
        order by a.createtime desc
    </select>
    <select id="queryOneConfig" resultType="cn.roadm.bo.ChannelSwitchConfigBO">
        SELECT id, over_flag AS overFlag, delay_time AS delayTime, arrive_succ_rate AS arriveSuccRate, arrive_succ_rate_time AS arriveSuccRateTime,
                submit_succ_rate as submitSuccRate, submit_succ_rate_time as submitSuccRateTime, spid as spId, old_channelid as oldChannelid,
               yd_channelid as ydChannelid, lt_channelid as ltChannelid,dx_channelid as dxChannelid, alarm_type as alarmType, trigger_cycle as triggerCycle,
            phone as phones, email as emails, content, operator, createtime,min_send_num as minSendNum, receipt_rate as receiptRate, receipt_rate_time as receiptRateTime
        FROM
            fault_over where id=#{id} and spid = #{spId} and old_channelid = #{oldChannelId}
    </select>

    <update id="updateSwitchConfigEnable" parameterType="cn.roadm.bo.ChannelSwitchQueryBO">
        update fault_over set over_flag = #{over_flag} where id = #{id}
    </update>

    <select id="listPageChannelSwitchRecord" parameterType="cn.roadm.bo.ChannelSwitchQueryBO" resultType="cn.roadm.bo.ChannelSwitchTableDataBO">
        select id,spid,arrive_succ_rate,submit_succ_rate,old_channelid,new_channelid,otype,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
        DATE_FORMAT(fault_time, '%Y-%m-%d %H:%i:%s') as fault_time,`status`, receipt_rate from fault_over_record
        <where>
            <if test="spid != null and spid != ''">
                spid = #{spid}
            </if>
            <if test="old_channelid != null and old_channelid != '' and old_channelid != 'all'">
                and old_channelid = #{old_channelid}
            </if>
            <if test="otype != null and otype != ''">
                and otype = #{otype}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
            <if test="new_channelid != null and new_channelid != '-1'">
                and new_channelid = #{new_channelid}
            </if>
            <if test="recordIdList != null">
                and id in
                <foreach collection="recordIdList" index="index" item="item" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
        </where>
        order by createtime desc
    </select>

    <update id="deleteRecord" parameterType="cn.roadm.bo.ChannelSwitchQueryBO">
        update fault_over_record set status = 1 where id = #{id} and spid=#{spid}
    </update>

    <select id="queryCountBySpIdAndOldChannelId" parameterType="cn.roadm.bo.ChannelSwitchConfigBO" resultType="integer">
        select count(1) from fault_over
            <where>
                spid = #{spid}
                and old_channelid = #{oldChannelid}
                <if test="id != null and id != ''">
                    and id != #{id}
                </if>
            </where>
    </select>
    <select id="querySpAllConfig" resultType="java.lang.Integer">
        select count(1) from fault_over where spid = #{spid} and old_channelid = 'all'
    </select>

    <select id="listPageQueryFaultSummary" parameterType="cn.roadm.bo.ChannelSwitchSummaryVO" resultType="cn.roadm.bo.FaultSummaryEntity">
        SELECT a.id,a.spId,a.submitCount,a.submitSuccess,a.submitFail,a.submitRate,a.arriveCount,
        a.arriveSuccess,a.arriveFail,a.arriveRate,a.faultId,a.recordId,a.alarmMsg,DATE_FORMAT( a.createTime, '%Y-%m-%d %H:%i:%s' ) AS createTime,
        DATE_FORMAT( a.endTime, '%Y-%m-%d %H:%i:%s' ) AS endTime,a.alarmType,a.channelId,b.sp_name as spName, a.receiptRate
        FROM fault_over_summary a LEFT JOIN sms_sp_info b ON a.spId = b.id
            <where>
                <if test="spId != null">
                    a.spId = #{spId}
                </if>
                <if test="faultId != null and faultId != ''">
                    and a.faultId = #{faultId}
                </if>
                <if test="startTime != null and startTime != ''">
                    and a.createTime &gt;= str_to_date(#{startTime},'%Y-%m-%d')
                </if>
                <if test="endTime != null and endTime != ''">
                    and a.createTime &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
                </if>
                <if test="recordId != null and recordId != ''">
                    and a.recordId like CONCAT('%', #{recordId}, '%')
                </if>
            </where>
    </select>

    <select id="getAlarmDelayBySpId" resultType="cn.roadm.bo.AlarmDelayRatio">
    	select * from alarm_delay_ratio where sp_id=#{sp_id}
    </select>

    <insert id="insertAlarmDelayBySpId">
    	insert into alarm_delay_ratio(sp_id,alarm_type,alarm_content,phone,email,alarm_flag,ten_arrive_succ,
    	ten_scan_time,twen_arrive_succ,twen_scan_time,thir_arrive_succ,
    	thir_scan_time,six_arrive_succ,six_scan_time,min_num,unknow,unknow_scan_time,delay_time)
    	values(#{sp_id},#{alarm_type},#{alarm_content},#{phone},#{email},#{alarm_flag},
    	#{ten_arrive_succ},#{ten_scan_time},
    	#{twen_arrive_succ},#{twen_scan_time},
    	#{thir_arrive_succ},#{thir_scan_time},#{six_arrive_succ},#{six_scan_time},#{min_num},#{unknow},#{unknow_scan_time},#{delay_time})
    </insert>

    <update id="updateAlarmDelayBySpId">
    	update alarm_delay_ratio set alarm_type=#{alarm_type},alarm_content=#{alarm_content},
    	phone=#{phone},email=#{email},alarm_flag=#{alarm_flag},
    	ten_arrive_succ=#{ten_arrive_succ},ten_scan_time=#{ten_scan_time},
    	twen_arrive_succ=#{twen_arrive_succ},twen_scan_time=#{twen_scan_time},
    	thir_arrive_succ=#{thir_arrive_succ},thir_scan_time=#{thir_scan_time},
    	six_arrive_succ=#{six_arrive_succ},six_scan_time=#{six_scan_time},
    	min_num=#{min_num},unknow=#{unknow},unknow_scan_time=#{unknow_scan_time},delay_time=#{delay_time} where sp_id=#{sp_id}
    </update>

    <select id="getDeductArriveCount" resultType="java.lang.Long">
        SELECT count(1) from sms_deduct_arrive where `STATUS` = 0 and CREATE_TIME >= DATE_FORMAT(NOW(), '%Y-%m-%d')
    </select>


    <!--补发白名单修改保存-->
    <update id="saveWhiteBack" parameterType="cn.roadm.bo.BackCode">
        update sms_back_code set sp_white = #{sp_white},shunt_white = #{shunt_white} where id = #{id}
    </update>

    <select id="getAlarmOverStackNew" resultType="cn.roadm.bo.AlarmOverStackNewBO">
        select id, alarm_flag as alarmFlag,
               alarm_sleep as alarmSleep,
               alarm_type as alarmType,
               scan_time as scanTime,
               alarm_phone as alarmPhone,
               alarm_email as alarmEmail,
               channel_alarm_config as channelAlarmConfig,
               channel_alarm_msg as channelAlarmMsg,
               api_alarm_config as apiAlarmConfig,
               api_alarm_msg as apiAlarmMsg,
               report_alarm_config as reportAlarmConfig,
               report_alarm_msg as reportAlarmMsg,
               queue_alarm_config as queueAlarmConfig,
               queue_alarm_msg as queueAlarmMsg,
               alarm_flag_channel as alarmFlagChannel,
               alarm_flag_api as alarmFlagApi,
               alarm_flag_report as alarmFlagReport,
               alarm_flag_queue as alarmFlagQueue from alarm_overstack_new limit 1
    </select>

    <insert id="insertAlarmOverStackNew">
        insert into alarm_overstack_new(alarm_flag,alarm_type,scan_time,alarm_phone,alarm_email,
        channel_alarm_config,channel_alarm_msg,api_alarm_config,api_alarm_msg,report_alarm_config,
        report_alarm_msg,queue_alarm_config,queue_alarm_msg,alarm_flag_channel,alarm_flag_api,
        alarm_flag_report,alarm_flag_queue)
        values (#{alarmFlag},#{alarmType},#{scanTime},#{alarmPhone},#{alarmEmail},#{channelAlarmConfig},
        #{channelAlarmMsg},#{apiAlarmConfig},#{apiAlarmMsg},#{reportAlarmConfig},#{reportAlarmMsg},#{queueAlarmConfig},
        #{queueAlarmMsg},#{alarmFlagChannel},#{alarmFlagApi},#{alarmFlagReport},#{alarmFlagQueue})
    </insert>

    <update id="updateAlarmOverStackNew">
        update alarm_overstack_new set alarm_flag=#{alarmFlag}, alarm_sleep=#{alarmSleep}, alarm_type=#{alarmType},scan_time=#{scanTime},
        alarm_phone=#{alarmPhone},alarm_email=#{alarmEmail},channel_alarm_config=#{channelAlarmConfig},
        channel_alarm_msg=#{channelAlarmMsg},api_alarm_config=#{apiAlarmConfig},api_alarm_msg=#{apiAlarmMsg},
        report_alarm_config=#{reportAlarmConfig},report_alarm_msg=#{reportAlarmMsg},queue_alarm_config=#{queueAlarmConfig},
        queue_alarm_msg=#{queueAlarmMsg},alarm_flag_channel=#{alarmFlagChannel},alarm_flag_api=#{alarmFlagApi},
        alarm_flag_report=#{alarmFlagReport},alarm_flag_queue=#{alarmFlagQueue} where id=#{id}
    </update>
    <select id="listMonitorTime" resultType="cn.roadm.bo.ChannelSendSpeed">
        SELECT
        sum(s.speed) speed,
        s.monitortime
        FROM
        sms_channel_speed_monitor s
        <include refid="channelSpeedConditions"/>
        group by s.createtime,s.monitortime
        order by s.createtime
    </select>
    <select id="listTopTen" resultType="cn.roadm.bo.ChannelSendSpeed">
        select avg(s.speed) averageSpeed ,s.channelid from sms_channel_speed_monitor s
        <include refid="channelSpeedConditions"/>
        group by s.channelid order by averageSpeed desc limit 10
    </select>
    <sql id="channelSpeedConditions">
        <where>
            <if test="channelid !=null and channelid !=''">
                s.channelid= #{channelid}
            </if>
            <if test="createTime!=null and createTime!=''">
                and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </sql>

    <select id="selectBackCodeConfigBySpId" resultType="cn.roadm.bo.BackCode">
        select id, back_code as backCode, code_type as codeType, pa_id as paId, send_time as sendTime, status, descp,
        resend_pa as resendPa, resend_time, sp_id, null_error_code as nullErrorCode, mobile_pa, unicom_pa, telecom_pa, is_triple, is_group,
        is_provincial_group, proGroupId, rounting_number_flag, mms_flag, rount_interface, sp_white, shunt_white, is_sign_group, proSignId, replace_sign_group_id as replaceSignGroupId from sms_back_code where sp_id = #{spid}
    </select>

    <insert id="insertBackCodeConfig">
        insert into sms_back_code (back_code, code_type, pa_id, send_time, status, descp, create_time, resend_pa, resend_time, sp_id, null_error_code, mobile_pa, unicom_pa, telecom_pa, is_triple, is_group, is_provincial_group, proGroupId, rounting_number_flag, mms_flag, rount_interface, sp_white, shunt_white, is_sign_group, proSignId, replace_sign_group_id)
         VALUES
             <foreach collection="list" item="item" separator=",">
                 (#{item.backCode}, #{item.codeType}, #{item.paId}, #{item.sendTime}, #{item.status}, #{item.descp}, #{item.createTime}, #{item.resendPa}, #{item.resend_time}, #{item.sp_id}, #{item.nullErrorCode}, #{item.mobile_pa}, #{item.unicom_pa}, #{item.telecom_pa}, #{item.is_triple}, #{item.is_group}, #{item.is_provincial_group}, #{item.proGroupId}, #{item.rounting_number_flag}, #{item.mms_flag}, #{item.rount_interface}, #{item.sp_white}, #{item.shunt_white}, #{item.is_sign_group}, #{item.proSignId}, #{item.replaceSignGroupId})
             </foreach>
    </insert>
</mapper>
