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

    <resultMap type="TaskInfo" id="taskResultMap">
        <result column="sp_id" property="sp_id" />
        <result column="sp_name" property="sp_name" />
        <result column="price" property="price" />
        <result column="channelid" property="channelid" />
        <result column="chinese" property="chinese" />
        <result column="allnumber" property="allnumber" />
        <result column="succ" property="succ" />
        <result column="fail" property="fail" />
        <result column="arrive_succ" property="arrive_succ" />
        <result column="arrive_fail" property="arrive_fail" />
        <result column="unRecMsgnum" property="unRecMsgnum" />
        <result column="stat_time" property="stat_time"/>
        <result column="country_code" property="chinese"/>
    </resultMap>

    <select id="getCountryTaskTotal"
            parameterType="TaskInfo" resultType="TaskInfo">
        SELECT
        CONCAT(Round(sum(spp.arrive_succ)/sum(spp.submit_succ)*100,2),'%') sp_succ_percent,
        sum( spp.submit_succ ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( spp.arrive_succ ) arrive_succ,
        sum( spp.arrive_fail ) arrive_fail
        FROM
        inter_sp_country_summary spp, sms_sp_info sp
        WHERE
        spp.spid=sp.id
        <if test="createTime!=null and createTime!=''">
            and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
            1 DAY)
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and spp.spid=#{sp_id}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>
        <if test="chinese!=null and chinese!=''">
            and spp.country_code like CONCAT('%',#{chinese},'%')
        </if>
        <if test="type_channel!=null and type_channel!='' and channelid!=null and channelid >=0">
            and spp.channelid=#{channelid}
        </if>
        <if test="saId!=null and saId > 0">
            and sp.sa_id = #{saId}
        </if>
    </select>

    <select id="listPageCountryTaskTotal"
            parameterType="TaskInfo" resultMap="taskResultMap">
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            SELECT
            inter.sp_name,
            inter.sp_id,
            inter.country_code,
            p.price,
            <if test="type_channel!=null and type_channel!=''">
                inter.channelid,
            </if>
            <if test="type_day!=null and type_day!=''">
                inter.stat_time,
            </if>
            inter.succ,
            inter.fail,
            inter.arrive_succ,
            inter.arrive_fail
            FROM
            (
        </if>
        SELECT
        <if test="(type_sp!=null and type_sp!='') or (sp_name!=null and sp_name!='')">
            sp.sp_name,
        </if>
        <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
            sp.id sp_id,
        </if>
        <if test="(type_country!=null and type_country!='') or (chinese!=null and chinese!='')">
            spp.country_code,
        </if>
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            sp.inter_price_group,
        </if>
        <if test="type_channel!=null and type_channel!=''">
            spp.channelid,
        </if>
        <if test="type_day!=null and type_day!=''">
            spp.stat_time,
        </if>
        sum( spp.submit_succ ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( spp.arrive_succ ) arrive_succ,
        sum( spp.arrive_fail ) arrive_fail
        FROM
        inter_sp_country_summary spp, sms_sp_info sp, sms_send_channel_info sc
        WHERE
        spp.spid=sp.id and sc.id=spp.channelid
        <if test="createTime!=null and createTime!=''">
            and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
            1 DAY)
        </if>
        <if test="chinese!=null and chinese!=''">
            and spp.country_code like CONCAT('%',#{chinese},'%')
        </if>
        <if test="type_channel!=null and type_channel!='' and channelid!=null and channelid >=0">
            and spp.channelid=#{channelid}
        </if>
        <if test="type_channel!=null and type_channel!='' and channel_name!=null and channel_name !=''">
            and sc.CHANNEL_NAME like CONCAT('%', #{channel_name}, '%')
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and spp.spid=#{sp_id}
        </if>
        <if test="saId!=null and saId > 0">
            and sp.sa_id = #{saId}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(type_sp!=null and type_sp!='') or (sp_name!=null and sp_name!='')">
                sp.sp_name,
            </if>
            <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
                sp.id,
            </if>
            <if test="(type_country!=null and type_country!='') or (chinese!=null and chinese!='')">
                spp.country_code,
            </if>
            <if test="type_channel!=null and type_channel!=''">
                spp.channelid,
            </if>
            <if test="type_day!=null and type_day!=''">
                spp.stat_time,
            </if>
        </trim>
        order by sum(spp.submit_succ) desc
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            ) inter
            LEFT JOIN inter_sms_price p ON inter.inter_price_group = p.gid AND inter.country_code = p.country
            ORDER BY inter.succ desc
        </if>
    </select>

    <select id="exportCountryTotalList"
            parameterType="TaskInfo" resultType="Map">
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            SELECT
            inter.SP_NAME SP_NAME,
            inter.SP_ID SP_ID,
            inter.CHINESE CHINESE,
            p.price PRICE,
            <if test="type_channel!=null and type_channel!=''">
                inter.CHANNELID CHANNELID,
            </if>
            <if test="type_day!=null and type_day!=''">
                inter.STAT_TIME STAT_TIME,
            </if>
            inter.SP_SUCC_PERCENT SP_SUCC_PERCENT,
            inter.SUCC SUCC,
            inter.FAIL FAIL,
            inter.ARRIVE_SUCC ARRIVE_SUCC,
            inter.ARRIVE_FAIL ARRIVE_FAIL
            FROM
            (
        </if>
        SELECT
        <if test="(type_sp!=null and type_sp!='') or (sp_name!=null and sp_name!='')">
            sp.sp_name SP_NAME,
        </if>
        <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
            sp.id SP_ID,
        </if>
        <if test="type_channel!=null and type_channel!=''">
            sc.channel_name CHANNELID,
        </if>
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            sp.inter_price_group,
        </if>
        <if test="(type_country!=null and type_country!='') or (chinese!=null and chinese!='')">
            spp.country_code CHINESE,
        </if>
        <if test="type_day!=null and type_day!=''">
            date_format(spp.stat_time,'%Y-%m-%d') STAT_TIME,
        </if>
        CONCAT(Round(sum(spp.arrive_succ)/sum(spp.submit_succ)*100,2),'%') SP_SUCC_PERCENT,
        sum( spp.submit_succ ) SUCC,
        sum( IFNULL( spp.submit_fail, 0 ) ) FAIL,
        sum( spp.arrive_succ ) ARRIVE_SUCC,
        sum( spp.arrive_fail ) ARRIVE_FAIL
        FROM
        inter_sp_country_summary spp, sms_sp_info sp, sms_send_channel_info sc
        WHERE
        spp.spid=sp.id and sc.id=spp.channelid
        <if test="createTime!=null and createTime!=''">
            and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
            1 DAY)
        </if>
        <if test="chinese!=null and chinese!=''">
            and spp.country_code like CONCAT('%',#{chinese},'%')
        </if>
        <if test="type_channel!=null and type_channel!='' and channelid!=null and channelid >=0">
            and spp.channelid=#{channelid}
        </if>
        <if test="type_channel!=null and type_channel!='' and channel_name!=null and channel_name !=''">
            and sc.CHANNEL_NAME like CONCAT('%', #{channel_name}, '%')
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and spp.spid=#{sp_id}
        </if>
        <if test="saId!=null and saId > 0">
            and sp.sa_id = #{saId}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(type_sp!=null and type_sp!='') or (sp_name!=null and sp_name!='')">
                sp.sp_name,
            </if>
            <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
                sp.id,
            </if>
            <if test="type_channel!=null and type_channel!=''">
                spp.channelid,
            </if>
            <if test="(type_country!=null and type_country!='') or (chinese!=null and chinese!='')">
                spp.country_code,
            </if>
            <if test="type_day!=null and type_day!=''">
                spp.stat_time,
            </if>
        </trim>
        order by sum(spp.submit_succ) desc
        <if test="(type_country!=null and type_country!='') and (type_sp!=null and type_sp!='')">
            ) inter
            LEFT JOIN inter_sms_price p ON inter.inter_price_group = p.gid AND inter.CHINESE = p.country
            ORDER BY inter.SUCC desc
        </if>
    </select>

    <select id="getCountryPriceBygId" parameterType="String" resultType="CountryPrice">
        select
            id, country, price, currency
        from inter_sms_price
        where gid=#{id}
    </select>
</mapper>