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

	<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_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="sign" property="sign"/>
    </resultMap>

	<select id="getSignTaskTotal"
		parameterType="TaskInfo" resultType="TaskInfo">
		SELECT
		CONCAT( Round( sum( spp.arrive_succ )/ sum( spp.succ )* 100, 2 ), '%' ) sp_succ_percent,
		sum( spp.succ ) as succ,
		sum( IFNULL( spp.fail, 0 ) ) as fail,
		sum( spp.arrive_succ ) as arrive_succ,
		sum( spp.arrive_fail ) as arrive_fail
		FROM (
		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,
		sign
		FROM
			sms_sp_sign_summary spp,sms_sp_info sp,sms_send_channel_info ssci
		WHERE
			spp.spid=sp.id and spp.channelid = ssci.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="managerId != null and managerId != ''">
			and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{managerId}) and sp.sp_saler != ''
		</if>
		<if test="business!=null and business!=''">
			and ssci.business_manager =#{business}
		</if>
		<if test="business_managerId != null and business_managerId != ''">
			and ssci.business_manager in
			(select ifnull(business, '') from business_manager bm where managerId=#{business_managerId})
			and ssci.business_manager != ''
		</if>
		<if test="sign!=null and sign!=''">
			and spp.sign=#{sign}
		</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>
		GROUP BY spp.sign
	) as spp
		<where>
			<if test="keyword != null and keyword != ''">
				<foreach item="word" collection="keyword.split(',')" separator="OR">
					spp.sign LIKE CONCAT('%', '${word}', '%')
				</foreach>
    		</if>
		</where>
	</select>

	<select id="listPageSignTaskTotal"
		parameterType="TaskInfo" resultMap="taskResultMap">
		select * 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,
		</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_day!=null and type_day!=''">
         	spp.stat_time,
         </if>
		sum( spp.submit_succ ) succ,
		sum( IFNULL( spp.submit_fail, 0 ) ) fail,
		sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0)) submitTotal,
		sum( spp.arrive_succ ) arrive_succ,
		sum( spp.arrive_fail ) arrive_fail,
		sum( IFNULL( spp.arrive_succ, 0) + IFNULL(spp.arrive_fail, 0)) arriveTotal,
		spp.sign sign
		FROM
			sms_sp_sign_summary spp,sms_sp_info sp,sms_send_channel_info ssci
		WHERE
			spp.spid=sp.id and spp.channelid = ssci.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_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 ssci.CHANNEL_NAME like CONCAT('%', #{channel_name}, '%')
		</if>
		<if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
		<if test="managerId != null and managerId != ''">
			and sp.sp_saler in (select ifnull(seller, '') from sale_manager sm where managerId=#{managerId}) and sp.sp_saler != ''
		</if>
		<if test="business!=null and business!=''">
			and ssci.business_manager =#{business}
		</if>
		<if test="business_managerId != null and business_managerId != ''">
			and ssci.business_manager in
			(select ifnull(business, '') from business_manager bm where managerId=#{business_managerId})
			and ssci.business_manager != ''
		</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="sign!=null and sign!=''">
			and spp.sign=#{sign}
		</if>
		<trim prefix="group by" suffixOverrides=",">
				spp.sign,
			<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_day!=null and type_day!=''">
	        	spp.stat_time,
	        </if>
        </trim>
		) as spp
		<where>
			 <if test="keyword != null and keyword != ''">
			 and
      		<foreach item="word" collection="keyword.split(',')" separator="OR">
				spp.sign LIKE CONCAT('%', '${word}', '%')
      		</foreach>
    	</if>
		</where>

		<if test="sort == 'asc'">
			<if test="col=='allRate'">
				order by arrive_succ/(succ+fail)
			</if>
			<if test="col=='rate'">
				order by arrive_succ/succ
			</if>
			<if test="col=='sp'">
				order by (spp.stat_time)
			</if>

			<if test="col=='total'">
				order by submitTotal
			</if>
			<if test="col=='arrive'">
				order by succ
			</if>

		</if>

		<if test="sort == 'desc'">
			<if test="col=='allRate'">
				order by arrive_succ/(succ+fail) desc
			</if>
			<if test="col=='rate'">
				order by arrive_succ/succ desc
			</if>
			<if test="col=='sp'">
				order by spp.stat_time desc
			</if>

			<if test="col=='total'">
				order by submitTotal desc
			</if>
			<if test="col=='arrive'">
				order by succ desc
			</if>
		</if>

		<if test="sort == null or sort == ''">
			order by submitTotal desc
		</if>

		<!-- <if test="type_day != null and type_day != '' and sort == 'asc'">
			order by (spp.stat_time) asc
		</if>
		<if test="type_day != null and type_day != '' and sort == 'desc'">
			order by (spp.stat_time) desc
		</if>
		<if test="type_day == null or type_day == ''">
			order by spp.succ desc
		</if> -->
	</select>

	<select id="exportSignTotalList"
        parameterType="TaskInfo" resultType="Map">
    select * from (
        SELECT
            MIN(spp.id) as id,
            <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!=''">
                CONCAT(spp.channelid, '-', ssci.channel_name) as CHANNELID,
            </if>
            <if test="type_otype!=null and type_otype!=''">
                (case when spp.otype = '1' then '联通'
                      when spp.otype = '2' then '移动'
                      else '电信' end) OTYPE_NAME,
            </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,
            IFNULL(SUM(spp.submit_succ) + SUM(IFNULL(spp.submit_fail, 0)) - SUM(spp.arrive_succ) - SUM(spp.arrive_fail),0) AS unknownTotal,
            CAST(SUM(IFNULL(spp.submit_succ, 0)) AS CHAR) AS SUCC,
  			CAST(SUM(IFNULL(spp.submit_fail, 0)) AS CHAR) AS FAIL,
  			CAST(SUM(IFNULL(spp.arrive_succ, 0)) AS CHAR) AS ARRIVE_SUCC,
  			CAST(SUM(IFNULL(spp.arrive_fail, 0)) AS CHAR) AS ARRIVE_FAIL,
            spp.sign SIGN
        FROM
            sms_sp_sign_summary spp
            JOIN sms_sp_info sp ON spp.spid = sp.id
            JOIN sms_send_channel_info ssci ON spp.channelid = ssci.id
        WHERE 1=1
            <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_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="sp_saler!=null and sp_saler!='' and sp_saler!= 'undefined'">
                and sp.sp_saler =#{sp_saler}
            </if>
            <if test="managerId != null and managerId != ''">
                and sp.sp_saler in (
                    select ifnull(seller,'') from sale_manager where managerId = #{managerId}
                ) and sp.sp_saler != ''
            </if>
            <if test="business!=null and business!=''">
                and ssci.business_manager =#{business}
            </if>
            <if test="business_managerId != null and business_managerId != ''">
                and ssci.business_manager in (
                    select ifnull(business, '') from business_manager bm where managerId=#{business_managerId}
                )
                and ssci.business_manager != ''
            </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="sign!=null and sign!=''">
                and spp.sign=#{sign}
            </if>

        <trim prefix="group by" suffixOverrides=",">
            spp.sign,
            <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, ssci.channel_name,
            </if>
            <if test="type_otype!=null and type_otype!=''">
                spp.otype,
            </if>
            <if test="type_day!=null and type_day!=''">
                spp.stat_time,
            </if>
        </trim>
        ORDER BY MIN(spp.id) DESC
    ) as spp
    <where>
        <if test="keyword != null and keyword != ''">
            <foreach collection="keyword.split(',')" item="item" separator="or">
                spp.sign like CONCAT('%',#{item},'%')
            </foreach>
        </if>
		<if test="endId != null and endId > 0">
                and spp.id &lt; #{endId}
		</if>
    </where>
    ORDER BY id DESC, SUCC DESC
    <if test="exportExcelSize != null and exportExcelSize > 0">
        LIMIT #{exportExcelSize}
    </if>
</select>



	<select id="listChannelTotalList"
        resultType="Map">
        select a.id channelid,a.channel_name channelName from sms_send_channel_info a order by a.id
    </select>
</mapper>
