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

    <select id="listPageMmsSignSummary" resultType="cn.roadm.bo.MmsSignEntity">
        select  mpt.sign,
        <if test='bySpId != null and bySpId == "1"'>
            mpt.spid spid, sp.sp_name spName,
        </if>
        <if test='byStatTime != null and byStatTime == "1"'>
            mpt.stat_time stat_time,
        </if>
        <if test="byOtype != null and byOtype != '1'">
            mpt.otype otype,
        </if>
        <if test="byChannel != null and byChannel != '1'">
            mpt.channelid channelid,
        </if>
        <if test="byTemplate != null and byTemplate != '1'">
            mpt.content byTemplate,
        </if>
        sum(IFNULL(mpt.submit_succ, 0)) submitSucc,
        sum(IFNULL(mpt.submit_fail, 0)) submitFail,
        sum(IFNULL(mpt.arrive_succ, 0)) arriveSucc,
        sum(IFNULL(mpt.arrive_fail, 0)) arriveFail
        from mms_sp_tem_summary mpt left join sms_sp_info sp on mpt.spid = sp.id
        <where>
            <if test="spid != null">
                mpt.spid = #{spid}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%',#{spName},'%')
            </if>
            <if test="sign != null and sign != ''">
                and mpt.sign like CONCAT('%',#{sign},'%')
            </if>
            <if test="startTime != null and startTime != ''">
                and mpt.stat_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and mpt.stat_time &lt;= #{endTime}
            </if>
            <if test="content != null and content != ''">
                and mpt.content like CONCAT('%',#{content},'%')
            </if>
        </where>
        group by
        <if test='bySpId != null and bySpId == "1"'>
            spid, spName,
        </if>
        <if test='byStatTime != null and byStatTime == "1"'>
            mpt.stat_time,
        </if>
        <if test="byOtype != null and byOtype != '1'">
            mpt.otype,
        </if>
        <if test="byChannel != null and byChannel != '1'">
            mpt.channelid,
        </if>
        <if test="byTemplate != null and byTemplate != '1'">
            byTemplate,
        </if>
        mpt.sign
        order by submitSucc desc
        <if test='byStatTime != null and byStatTime == "1"'>, mpt.stat_time desc</if>
    </select>



    <select id="selectSumSign" resultType="cn.roadm.bo.MmsSignEntity">
        select
        sum(IFNULL(mpt.submit_succ, 0)) submitSucc,
        sum(IFNULL(mpt.submit_fail, 0)) submitFail,
        sum(IFNULL(mpt.arrive_succ, 0)) arriveSucc,
        sum(IFNULL(mpt.arrive_fail, 0)) arriveFail
        from mms_sp_tem_summary mpt left join sms_sp_info sp on mpt.spid = sp.id
        <where>
            <if test="spid != null">
                mpt.spid = #{spid}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%',#{spName},'%')
            </if>
            <if test="sign != null and sign != ''">
                and mpt.sign like CONCAT('%',#{sign},'%')
            </if>
            <if test="startTime != null and startTime != ''">
                and mpt.stat_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and mpt.stat_time &lt;= #{endTime}
            </if>
            <if test="content != null and content != ''">
                and mpt.content like CONCAT('%',#{content},'%')
            </if>
        </where>
    </select>



    <select id="exportPageMmsSignList" resultType="java.util.Map">
        select  mpt.sign,
        <if test='bySpId != null and bySpId == "1"'>
            mpt.spid spid, sp.sp_name spName,
        </if>
        <if test='byStatTime != null and byStatTime == "1"'>
            DATE_FORMAT(mpt.stat_time, '%Y-%m-%d') as stat_time,
        </if>
        <if test='byOtype != null and byOtype == "1"'>
            mpt.otype otype,
        </if>
        <if test='byChannel != null and byChannel == "1"'>
            mpt.channelid channelid,
        </if>
        <if test='byTemplate != null and byTemplate == "1"'>
            mpt.content byTemplate,
        </if>
        IFNULL(sum(mpt.submit_succ), 0) submitSucc,
        IFNULL(sum(mpt.submit_fail), 0) submitFail,
        IFNULL(sum(mpt.arrive_succ), 0) arriveSucc,
        IFNULL(sum(mpt.arrive_fail), 0) arriveFail,
        IFNULL(sum(mpt.submit_succ), 0)+IFNULL(sum(mpt.submit_fail), 0) submitNum,
        IFNULL(sum(mpt.arrive_succ), 0)+IFNULL(sum(mpt.arrive_fail), 0) arriveNum,
        IFNULL(sum(mpt.submit_succ), 0)+IFNULL(sum(mpt.submit_fail), 0)-IFNULL(sum(mpt.arrive_succ), 0)-IFNULL(sum(mpt.arrive_fail), 0) unknownNum,
        Round(IFNULL(sum( mpt.arrive_succ ), 0 )/  IFNULL (sum( mpt.submit_succ), 0)* 100, 2 ) gateWaySucessRate,
        Round(IFNULL(sum( mpt.arrive_succ), 0 ) / (IFNULL(sum( mpt.submit_succ), 0 ) + IFNULL(sum(mpt.submit_fail), 0))* 100, 2 ) sucessRate
        from mms_sp_tem_summary mpt left join sms_sp_info sp on mpt.spid = sp.id
        <where>
            <if test="spid != null">
                mpt.spid = #{spid}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%',#{spName},'%')
            </if>
            <if test="sign != null and sign != ''">
                and mpt.sign like CONCAT('%',#{sign},'%')
            </if>
            <if test="startTime != null and startTime != ''">
                and mpt.stat_time &gt;= str_to_date(#{startTime},'%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and mpt.stat_time &lt;= str_to_date(#{endTime},'%Y-%m-%d')
            </if>
        </where>
        group by
        <if test='bySpId != null and bySpId == "1"'>
            mpt.spid,
        </if>
        <if test='byStatTime != null and byStatTime == "1"'>
            mpt.stat_time,
        </if>
        <if test='byOtype != null and byOtype == "1"'>
            mpt.otype,
        </if>
        <if test='byChannel != null and byChannel == "1"'>
            mpt.channelid,
        </if>
        <if test='byTemplate != null and byTemplate == "1"'>
            mpt.content,
        </if>
        mpt.sign
        order by submitSucc desc
        <if test='byStatTime != null and byStatTime == "1"'>, mpt.stat_time desc</if>
    </select>
</mapper>
