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

    <resultMap type="cn.roadm.bo.AimSpSummaryBO" id="AimSpSummaryMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="spid" column="spid" jdbcType="INTEGER"/>
        <result property="allnumber" column="allnumber" jdbcType="INTEGER"/>
        <result property="succ" column="succ" jdbcType="INTEGER"/>
        <result property="fail" column="fail" jdbcType="INTEGER"/>
        <result property="arriveTotal" column="arrive_total" jdbcType="INTEGER"/>
        <result property="arriveSucc" column="arrive_succ" jdbcType="INTEGER"/>
        <result property="arriveFail" column="arrive_fail" jdbcType="INTEGER"/>
        <result property="statTime" column="stat_time" jdbcType="TIMESTAMP"/>
        <result property="modeid" column="modeId" jdbcType="VARCHAR"/>
        <result property="showSucc" column="show_succ" jdbcType="INTEGER"/>
        <result property="showFail" column="show_fail" jdbcType="INTEGER"/>
        <result property="bumpSucc" column="bump_succ" jdbcType="INTEGER"/>
        <result property="bumpFail" column="bump_fail" jdbcType="INTEGER"/>
    </resultMap>

    <resultMap type="TaskInfo" id="taskResultMap">
        <id column="id" property="task_id" />
        <result column="content" property="content" />
        <result column="createtime" property="createTime" />
        <result column="total" property="total" />
        <result column="allnumber" property="allnumber" />
        <result column="mobilenumber" property="mobilenumber" />
        <result column="unicomnumber" property="unicomnumber" />
        <result column="telecomnumer" property="telecomnumber" />
        <result column="succ" property="succ" />
        <result column="fail" property="fail" />
        <result column="arrive_total" property="arrive_total" />
        <result column="arrive_succ" property="arrive_succ" />
        <result column="arrive_fail" property="arrive_fail" />
        <result column="task_type" property="task_type" />
        <result column="status" property="status" />
        <result column="endtime" property="endTime" />
        <result column="send_type" property="send_type" />
        <result column="sp_id" property="sp_id" />
        <result column="unRecMsgnum" property="unRecMsgnum" />
        <result column="sp_name" property="sp_name" />
        <result column="time" property="time" />
        <result column="difference" property="difference" />
        <result column="stat_time" property="stat_time"/>
        <result column="descptions" property="descptions"/>
        <result column="task_name" property="taskName"/>
        <result column="ip" property="ip" />
        <result column="stat_time" property="stat_time" />
        <result column="saleprice" property="saleprice" />
        <result column="mobile_price" property="mobile_price" />
        <result column="unicom_price" property="unicom_price" />
        <result column="telecom_price" property="telecom_price" />
        <result column="full_name" property="fullName"/>
        <result column="eid" property="eid"/>
    </resultMap>

    <select id="listPageAimSpSummary" resultMap="AimSpSummaryMap">
        select
        spid,
        <if test="countTemplate != null and countTemplate == 1">
            modeId,
        </if>
        <if test="countDay!=null and countDay==1">
            date_format(stat_time, '%Y-%m-%d') as stat_time,
        </if>
        <if test="countOtype!=null and countOtype==1">
            otype,
        </if>
        sum(IFNULL(allnumber, 0)) allnumber,
        sum(IFNULL(succ, 0)) succ,
        sum(IFNULL(fail, 0)) fail,
        sum(IFNULL(arrive_succ, 0) + IFNULL(arrive_fail, 0)) arrive_total,
        sum(IFNULL(arrive_succ, 0)) arrive_succ,
        sum(IFNULL(arrive_fail, 0)) arrive_fail,
        sum(IFNULL(show_succ, 0)) show_succ,
        sum(IFNULL(show_fail, 0)) show_fail,
        sum(IFNULL(bump_succ, 0)) bump_succ,
        sum(IFNULL(bump_fail, 0)) bump_fail
        from
        aim_sp_summary
        <where>
            <if test="statTime != null and statTime!=''">
                and stat_time &gt;=concat(#{statTime},' 00:00:00')
            </if>
            <if test="statTimeEnd != null and statTimeEnd!=''">
                and stat_time &lt;
                DATE_ADD(str_to_date(#{statTimeEnd},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="modeid != null and modeid !=''">
                and modeId = #{modeid}
            </if>
            <if test="spid != null">
                and spid = #{spid}
            </if>
        </where>
        group by spid
        <if test="countTemplate != null and countTemplate == 1">
            ,modeId
        </if>
        <if test="countOtype!=null and countOtype==1">
            ,otype
        </if>
        <if test="countDay!=null and countDay==1">
            ,stat_time order by stat_time desc
        </if>
    </select>
    <select id="aimSpSummaryTotal" resultMap="AimSpSummaryMap">
        select
        sum(IFNULL(allnumber, 0)) allnumber,
        sum(IFNULL(succ, 0)) succ,
        sum(IFNULL(fail, 0)) fail,
        sum(IFNULL(arrive_succ, 0) + IFNULL(arrive_fail, 0)) arrive_total,
        sum(IFNULL(arrive_succ, 0)) arrive_succ,
        sum(IFNULL(arrive_fail, 0))  arrive_fail,
        sum(IFNULL(show_succ, 0)) show_succ,
        sum(IFNULL(show_fail, 0)) show_fail,
        sum(IFNULL(bump_succ, 0)) bump_succ,
        sum(IFNULL(bump_fail, 0)) bump_fail
        from
        aim_sp_summary
        <where>
            <if test="statTime != null and statTime!=''">
                and stat_time &gt;=concat(#{statTime},' 00:00:00')
            </if>
            <if test="statTimeEnd != null and statTimeEnd!=''">
                and stat_time &lt;
                DATE_ADD(str_to_date(#{statTimeEnd},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="modeid != null and modeid != ''">
                and modeId = #{modeid}
            </if>
            <if test="spid != null">
                and spid = #{spid}
            </if>
        </where>
    </select>

    <select id="getSysPcoTaskTotal"
            parameterType="TaskInfo" resultType="TaskInfo">
        select
        CONCAT(Round(sum(arrive_succ)/sum(succ)*100,2),'%') sp_succ_percent,
        sum( IFNULL(succ,0) ) succ,
        sum( IFNULL( fail, 0 ) ) fail,
        sum( IFNULL(arrive_succ,0) ) arrive_succ,
        sum( IFNULL(arrive_fail,0) ) arrive_fail
        from (
        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, sp.eid,
        </if>
        <if test="type_channel!=null and type_channel!=''">
            spp.channelid,
        </if>
        <if test="type_otype!=null and type_otype!=''">
            spp.otype,
        </if>
        <if test="type_province!=null and type_province!=''">
            spp.province,
        </if>
        <if test="type_day!=null and type_day!=''">
            spp.stat_time,
        </if>
        sum( IFNULL(spp.submit_succ, 0 ) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( spp.arrive_succ ) arrive_succ,
        sum( spp.arrive_fail ) arrive_fail
        FROM
        aim_sp_province_summary spp
        <if test="type_channel!=null and type_channel!='' and channel_name!=null and channel_name !=''">
            left join sms_send_channel_info sc on spp.channelid=sc.id
        </if>
        ,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="eid != null and eid != ''">
            and sp.eid = #{eid}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler like CONCAT('%', #{sp_saler}, '%')
        </if>
        <if test="salerList!=null and salerList.size()>0">
            and sp.sp_saler in
            <foreach collection="salerList" index="index"  item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </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_saler!=null and sp_saler!=''">
            and sp.sp_saler like CONCAT('%', #{sp_saler}, '%')
        </if>
        <if test="sp_type!=null and sp_type!=''">
            and sp.sp_type = #{sp_type}
        </if>
        <if test="star_level!=null and star_level>0">
            and sp.star_level =#{star_level}
        </if>
        <if test="type_province!=null and type_province!='' and province!=null and province!=''">
            and spp.province=#{province}
        </if>
        <if test="type_otype!=null and type_otype!='' and otype!=null and otype >0">
            and spp.otype=#{otype}
        </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>
        <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_otype!=null and type_otype!=''">
                spp.otype,
            </if>
            <if test="type_province!=null and type_province!=''">
                spp.province,
            </if>
            <if test="type_day!=null and type_day!=''">
                spp.stat_time,
            </if>
        </trim>
        ) t
        <where>
            <if test="succ!=null and succ!=''">
                and t.succ &gt; #{succ}
            </if>
        </where>
    </select>

    <select id="listPageSysPcoTaskTotal1"
            parameterType="TaskInfo" resultMap="taskResultMap">
        select
        <if test="(type_sp!=null and type_sp!='') or (sp_name!=null and sp_name!='')">
            t.sp_name,
        </if>
        <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
            t.sp_id, t.eid,
        </if>
        <if test="type_channel!=null and type_channel!=''">
            t.channelid,
        </if>
        <if test="type_otype!=null and type_otype!=''">
            t.otype,
        </if>
        <if test="type_province!=null and type_province!=''">
            t.province,
        </if>
        <if test="type_day!=null and type_day!=''">
            t.stat_time,
        </if>
        t.succ, t.fail, t.arrive_succ, t.arrive_fail
        <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
            ,s.clientid
        </if>
        from (
        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, sp.eid,
        </if>
        <if test="type_channel!=null and type_channel!=''">
            spp.channelid,
        </if>
        <if test="type_otype!=null and type_otype!=''">
            spp.otype,
        </if>
        <if test="type_province!=null and type_province!=''">
            spp.province,
        </if>
        <if test="type_day!=null and type_day!=''">
            spp.stat_time,
        </if>
        sum( IFNULL(spp.submit_succ, 0 ) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( spp.arrive_succ ) arrive_succ,
        sum( spp.arrive_fail ) arrive_fail
        FROM
        aim_sp_province_summary spp
        <if test="type_channel!=null and type_channel!='' and channel_name!=null and channel_name != ''">
            left join sms_send_channel_info sc on spp.channelid=sc.id
        </if>
        ,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="type_province!=null and type_province!='' and province!=null and province!=''">
            and spp.province=#{province}
        </if>
        <if test="type_otype!=null and type_otype!='' and otype!=null and otype >0">
            and spp.otype=#{otype}
        </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="eid != null and eid != ''">
            and sp.eid=#{eid}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler like CONCAT('%', #{sp_saler}, '%')
        </if>
        <if test="sp_type!=null and sp_type!=''">
            and sp.sp_type = #{sp_type}
        </if>
        <if test="star_level!=null and star_level>0">
            and sp.star_level =#{star_level}
        </if>
        <if test="salerList!=null and salerList.size()>0">
            and sp.sp_saler in
            <foreach collection="salerList" index="index"  item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </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>
        <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_otype!=null and type_otype!=''">
                spp.otype,
            </if>
            <if test="type_province!=null and type_province!=''">
                spp.province,
            </if>
            <if test="type_day!=null and type_day!=''">
                spp.stat_time,
            </if>
        </trim>
        <if test="sort != null and sort != ''"> order by ${sort} </if>
        <if test="sort == null"> order by sum(spp.submit_succ) desc </if>
        ) t
        <if test="(type_sp!=null and type_sp!='') or (sp_id!=null and sp_id>0)">
            left join sms_api_socket_sp s on t.sp_id = s.spid
        </if>
        <where>
            <if test="succ!=null and succ!=''">
                and t.succ &gt; #{succ}
            </if>
        </where>
        <if test="col != null and col != '' and col=='succ'">
            order by  t.succ ${orderType}
        </if>
        <if test="col != null and col != '' and col=='arriveSucc'">
            order by  t.arrive_succ ${orderType}
        </if>
    </select>
</mapper>

