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

    <resultMap id="BaseResultMap" type="cn.roadm.bo.SmsPhonepoolConfig">
        <result column="id" property="id"/>
        <result column="send_count" property="sendCount"/>
        <result column="fail_count" property="failCount"/>
        <result column="business_id" property="businessId"/>
        <result column="createtime" property="createtime"/>
    </resultMap>
    <sql id="Base_Column_List">
        `id`
        ,`send_count`,`fail_count`,`business_id`,`createtime`
    </sql>

    <select id="getSmsPhonepoolConfigByBusinessId" parameterType="String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from sms_phonepool_config
        <where>
            <if test="@cn.roadm.util.StringUtil@isNotEmpty(businessId)">
                and business_id = #{businessId}
            </if>
        </where>
    </select>

    <update id="updateSmsPhonepoolConfig" parameterType="cn.roadm.bo.SmsPhonepoolConfig">
        update sms_phonepool_config s
        <set>
            <if test="sendCount != null and sendCount > 0">
                s.send_count = #{sendCount},
            </if>
            <if test="failCount != null and failCount > 0">
                s.fail_count = #{failCount},
            </if>
            <if test="businessId != null and businessId != '' ">
                s.business_id = #{businessId},
            </if>
        </set>
        where id = #{id}
    </update>

    <insert id="insert" parameterType="cn.roadm.bo.SmsPhonepoolConfig">
        insert into sms_phonepool_config (send_count, fail_count, business_id, createtime)
            value (#{sendCount,jdbcType=INTEGER},#{failCount,jdbcType=INTEGER},#{businessId,jdbcType=VARCHAR},#{createtime,jdbcType=TIMESTAMP})
    </insert>
    
    
    <select id="listPagePhonepoolSummary" resultType="cn.roadm.bo.SmsPhonepoolSummary">
    	select 
    	sum(IFNULL(succ,0)) succ,
    	sum(IFNULL(fail,0)) fail,
    	sum(IFNULL(arrive_succ,0)) arrive_succ,
    	sum(IFNULL(arrive_fail,0)) arrive_fail,
    	sum(IFNULL(succ,0))+sum(IFNULL(fail,0))-sum(IFNULL(arrive_succ,0))-sum(IFNULL(arrive_fail,0)) unRecMsgnum,
    	city
    	<if test="isphone==1">
    		,phone
    	</if>
    	<if test="isotype==1">
    		,otype
    	</if>
    	<if test="ismon==1">
    		,DATE_FORMAT(stat_time, '%Y-%m-%d') stat_time
    	</if> 
    	from sms_phonepool_summary
    	<where>
    		<if test="createTime!=null and createTime!=''">
			and stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		<if test="phone!=null and phone!=''">
			and phone like CONCAT(#{phone},'%')
		</if>
		<if test="otype!=null and otype!=''">
				and otype=#{otype}
			</if>
		<if test="city!=null and city!=''">
			and city=#{city}
		</if>
    	</where>
    	GROUP BY city
    	<if test="isphone==1">
    		,phone
    	</if>
    	<if test="isotype==1">
    		,otype
    	</if>
    	<if test="ismon==1">
    		,stat_time
    	</if> 
    	order by succ desc
    </select>
    
    
    <select id="phonepoolSummaryCount" resultType="cn.roadm.bo.SmsPhonepoolSummary">
    	select sum(IFNULL(succ,0)) succ,
    	sum(IFNULL(fail,0)) fail,
    	sum(IFNULL(arrive_succ,0)) arrive_succ,
    	sum(IFNULL(arrive_fail,0)) arrive_fail,
    	sum(IFNULL(succ,0))+sum(IFNULL(fail,0))-sum(IFNULL(arrive_succ,0))-sum(IFNULL(arrive_fail,0)) unRecMsgnum
    	from sms_phonepool_summary
    	<where>
    		<if test="createTime!=null and createTime!=''">
				and stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
			</if>
			<if test="createTime1!=null and createTime1!=''">
				and
				stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
				1 DAY)
			</if>
			<if test="phone!=null and phone!=''">
				and phone like CONCAT(#{phone},'%')
			</if>
			<if test="city!=null and city!=''">
				and city=#{city}
			</if>
			<if test="otype!=null and otype!=''">
				and otype=#{otype}
			</if>
    	</where>
    </select>
    
    
     <select id="exportPhonepoolSummary" resultType="Map">
    	select 
    	sum(IFNULL(succ,0)) succ,
    	sum(IFNULL(fail,0)) fail,
    	sum(IFNULL(arrive_succ,0)) arrive_succ,
    	sum(IFNULL(arrive_fail,0)) arrive_fail,
    	sum(IFNULL(succ,0))+sum(IFNULL(fail,0))-sum(IFNULL(arrive_succ,0))-sum(IFNULL(arrive_fail,0)) unRecMsgnum,
    	city,
    	CONCAT(Round(sum(arrive_succ)/sum(succ)*100,2),'%') sp_succ_percent
    	<if test="isphone==1">
    		,phone
    	</if>
    	<if test="isotype==1">
    		,case when otype=1 then '联通'
			when otype=2 then '移动' 
			when otype=3 then '电信' end as 'otype'
    	</if>
    	<if test="ismon==1">
    		,DATE_FORMAT(stat_time, '%Y-%m-%d') stat_time
    	</if> 
    	from sms_phonepool_summary
    	<where>
    		<if test="createTime!=null and createTime!=''">
				and stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
			</if>
			<if test="createTime1!=null and createTime1!=''">
				and
				stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
				1 DAY)
			</if>
			<if test="phone!=null and phone!=''">
				and phone=#{phone}
			</if>
			<if test="otype!=null and otype!=''">
				and otype=#{otype}
			</if>
			<if test="city!=null and city!=''">
				and city=#{city}
			</if>
    	</where>
    	GROUP BY city
    	<if test="isphone==1">
    		,phone
    	</if>
    	<if test="isotype==1">
    		,otype
    	</if>
    	<if test="ismon==1">
    		,stat_time
    	</if> 
    	order by succ desc
    </select>
    
</mapper>