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

	<resultMap type="ShortUrlInfo" id="ShortUrlResultMap">
		<result column="taskid" property="taskId" />
		<result column="spid" property="spId" />
		<result column="sp_name" property="spName" />
		<result column="mdnprovince" property="mdnProvince" />
		<result column="ipregion" property="ipRegion" />
		<result column="mdn" property="mdn" />
		<result column="ip" property="ip" />
		<result column="ua" property="ua" />
		<result column="longurl" property="longUrl" />
		<result column="shorturl" property="shortUrl" />
		<result column="createtime" property="create_time" />
		<result column="status" property="status" />
		<result column="type" property="type" />
	</resultMap>
	
	<resultMap type="ShortUrlInfo" id="UvResultMap">
		<result column="id" property="taskId" />
		<result column="sp_id" property="spId" />
		<result column="sp_name" property="spName" />
		<result column="createtime" property="create_time" />
		<result column="arrive_succ" property="arrive_succ" />
		<result column="uvCount" property="uvCount" />
	</resultMap>
	
	<resultMap type="Domain" id="DomainResultMapper">
        <id column="id" property="id" />
        <result column="domain" property="domain" />
        <result column="status" property="status" />
    </resultMap>
	
    <!-- 访问记录 -->
	<select id="listPageShortUrlInfo" parameterType="ShortUrlInfo" resultMap="ShortUrlResultMap">
		SELECT suv.taskid, suv.spid, sp.sp_name, suv.mdnprovince, suv.ipregion, suv.mdn, suv.ip, suv.ua, suv.longurl, suv.shorturl, suv.createtime
		from short_url_visit suv, sms_sp_info sp
		WHERE suv.spid = sp.id
		<if test="status != null and status == 1">
            and suv.taskid != '1'
        </if>
        <if test="status != null and status == 0">
            and suv.taskid = '1'
        </if>
		<if test="taskId != null and taskId != ''">
			and suv.taskid = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and suv.spid = #{spId}
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="longUrl !=null and longUrl != ''">
			and suv.longurl = #{longUrl}
		</if>
		<if test="shortUrl !=null and shortUrl != ''">
            and suv.shorturl = #{shortUrl}
        </if>
		<if test="createTime!=null and createTime!=''">
			and suv.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			suv.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		ORDER BY suv.id DESC
	</select>
	
	<select id="getDistinctIpTotal" parameterType="ShortUrlInfo" resultType="int">
        SELECT COUNT(DISTINCT suv.ip)
        FROM short_url_visit suv, sms_sp_info sp
        WHERE suv.spid = sp.id
        <if test="status != null and status == 1">
            and suv.taskid != '1'
        </if>
        <if test="status != null and status == 0">
            and suv.taskid = '1'
        </if>
        <if test="taskId != null and taskId != ''">
            and suv.taskid = #{taskId}
        </if>
        <if test="spId != null and spId > 0">
            and suv.spid = #{spId}
        </if>
        <if test="spName != null and spName != ''">
            and sp.sp_name like CONCAT('%',#{spName},'%')
        </if>
        <if test="longUrl !=null and longUrl != ''">
            and suv.longurl = #{longUrl}
        </if>
        <if test="shortUrl !=null and shortUrl != ''">
            and suv.shorturl = #{shortUrl}
        </if>
        <if test="createTime!=null and createTime!=''">
            and suv.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            suv.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
            1 DAY)
        </if>
    </select>
	
	<!-- 短链列表 -->
	<select id="listPageShortUrlInfo1" parameterType="ShortUrlInfo" resultMap="ShortUrlResultMap">
		SELECT suv.taskid, suv.spid, sp.sp_name, suv.mdn, suv.longurl, suv.shorturl, suv.createtime, suv.status, suv.type
		from short_url suv, sms_sp_info sp
		WHERE suv.spid = sp.id
		<if test="status != null and status == 1">
            and suv.type = '1'
        </if>
        <if test="status != null and status == 0">
            and suv.type = '0'
        </if>
		<if test="taskId != null and taskId != ''">
			and suv.taskid = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and suv.spid = #{spId}
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="longUrl !=null and longUrl != ''">
			and suv.longurl = #{longUrl}
		</if>
		<if test="shortUrl !=null and shortUrl != ''">
            and suv.shorturl = #{shortUrl}
        </if>
		<if test="createTime!=null and createTime!=''">
			and suv.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			suv.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		ORDER BY suv.createtime DESC
	</select>
	
	<select id="exportShortUrlVisitCount" parameterType="ShortUrlInfo" resultType="Long">
        SELECT COUNT(*)
        FROM short_url_visit suv, sms_sp_info sp
        WHERE suv.spid = sp.id
        <if test="status != null and status == 1">
            and suv.taskid != '1'
        </if>
        <if test="status != null and status == 0">
            and suv.taskid = '1'
        </if>
        <if test="taskId != null and taskId != ''">
            and suv.taskid = #{taskId}
        </if>
        <if test="spId != null and spId > 0">
            and suv.spid = #{spId}
        </if>
        <if test="spName != null and spName != ''">
            and sp.sp_name like CONCAT('%',#{spName},'%')
        </if>
        <if test="longUrl !=null and longUrl != ''">
            and suv.longurl = #{longUrl}
        </if>
        <if test="shortUrl !=null and shortUrl != ''">
            and suv.shorturl = #{shortUrl}
        </if>
        <if test="createTime!=null and createTime!=''">
            and suv.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            suv.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
            1 DAY)
        </if>
        ORDER BY suv.id DESC
    </select>
	
	<select id="exportShortUrlVisit" parameterType="ShortUrlInfo" resultType="Map">
		SELECT suv.id, suv.taskid, suv.spid, sp.sp_name, suv.mdnprovince, suv.ipregion, suv.mdn, suv.ip, suv.ua, suv.longurl, suv.shorturl,
		date_format(suv.createtime,'%Y-%m-%d %H:%i:%S') createtime
		from short_url_visit suv, sms_sp_info sp
		WHERE suv.spid = sp.id
		<if test="status != null and status == 1">
            and suv.taskid != '1'
        </if>
        <if test="status != null and status == 0">
            and suv.taskid = '1'
        </if>
		<if test="taskId != null and taskId != ''">
			and suv.taskid = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and suv.spid = #{spId}
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="longUrl !=null and longUrl != ''">
			and suv.longurl = #{longUrl}
		</if>
		<if test="shortUrl !=null and shortUrl != ''">
            and suv.shorturl = #{shortUrl}
        </if>
		<if test="createTime!=null and createTime!=''">
			and suv.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			suv.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		<if test="endId != null">
            and suv.id &lt; #{endId}
        </if>
		ORDER BY suv.id DESC
		<if test="exportExcelSize != null ">
            limit #{exportExcelSize}
        </if>
	</select>
	
	<select id="getUvTotal" parameterType="ShortUrlInfo" resultType="ShortUrlInfo">
		SELECT 
		sum(st.arrive_succ*st.total/st.allnumber) arrive_succ,
		sum(suv.uvCount) uvCount
		from sms_send_task st left join  
		(select spid, taskid, count(distinct ip) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
	</select>
	
	<select id="listPageUv" parameterType="ShortUrlInfo" resultMap="UvResultMap">
		SELECT st.id, st.sp_id, sp.sp_name, st.createtime, st.arrive_succ*st.total/st.allnumber arrive_succ,suv.uvCount
		from sms_send_task st left join
		(select spid, taskid, count(distinct ip) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		ORDER BY st.createtime DESC
	</select>
	
	<select id="listPageGroupUv" parameterType="ShortUrlInfo" resultMap="UvResultMap">
		SELECT st.sp_id, sp.sp_name, DATE_FORMAT(st.createtime,'%Y-%m-%d') createtime, 
		sum(st.arrive_succ*st.total/st.allnumber) arrive_succ, sum(suv.uvCount) uvCount
		from sms_send_task st left join
		(select spid, taskid, count(distinct ip) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		GROUP BY st.sp_id, createtime
		ORDER BY createtime DESC
	</select>
	
	<select id="exportGroupUv" parameterType="ShortUrlInfo" resultType="Map">
		SELECT st.sp_id, sp.sp_name, DATE_FORMAT(st.createtime,'%Y-%m-%d') createtime, 
	    floor(sum(st.arrive_succ*st.total/st.allnumber)) arrive_succ, sum(suv.uvCount) uvCount,
		CONCAT(Round(sum(suv.uvCount)/sum(st.arrive_succ)*100,2),'%') uvRate
		from sms_send_task st left join
		(select spid, taskid, count(distinct ip) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		GROUP BY st.sp_id, createtime
		ORDER BY createtime DESC
	</select>
	
	<select id="getPvTotal" parameterType="ShortUrlInfo" resultType="ShortUrlInfo">
		SELECT 
		sum(st.arrive_succ*st.total/st.allnumber) arrive_succ,
		sum(suv.uvCount) uvCount
		from sms_send_task st left join  
		(select spid, taskid, count(*) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="taskId != null and taskId != ''">
			and taskid = #{taskId}
		</if>
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 2 DAY)
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
	</select>
	
	<select id="listPagePv" parameterType="ShortUrlInfo" resultMap="UvResultMap">
		SELECT st.id, st.sp_id, sp.sp_name, st.createtime, st.arrive_succ*st.total/st.allnumber arrive_succ,suv.uvCount
		from sms_send_task st left join
		(select spid, taskid, count(*) uvCount 
		FROM short_url_visit 
		WHERE 1=1
		<if test="taskId != null and taskId != ''">
			and taskid = #{taskId}
		</if>
		<if test="createTime!=null and createTime!=''">
			and createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 2 DAY)
		</if>
		GROUP BY taskid, spid) suv
		on st.id = suv.taskid and st.sp_id=suv.spid
		inner join sms_sp_info sp
		on st.sp_id = sp.id
		where st.message_type = '10'
		<if test="taskId != null and taskId != ''">
			and st.id = #{taskId}
		</if>
		<if test="spId != null and spId > 0">
			and st.sp_id = #{spId}
		</if>
		<if test="content != null and content != ''">
			and st.content like CONCAT('%',#{content},'%')
		</if>
		<if test="spName != null and spName != ''">
			and sp.sp_name like CONCAT('%',#{spName},'%')
		</if>
		<if test="createTime!=null and createTime!=''">
			and st.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and st.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		ORDER BY st.createtime DESC
	</select>
	
	<select id="listPageDomain" parameterType="Domain" resultMap="DomainResultMapper">
        select
        id, domain, status, sp_id
        from sms_domain
        where 1=1
        <if test="status != null and status != -1">
            and status = #{status}
        </if>
        <if test="domain != null and domain != ''">
            and domain like CONCAT('%',#{domain},'%') 
        </if>
        <if test="sp_id != null and sp_id > 0">
            and sp_id = #{sp_id}
        </if>
    </select>
    
    <select id="getDomainById" parameterType="int" resultType="Domain">
        select
        id, domain, status, sp_id
        from sms_domain
		where id=#{id}
    </select>
    
    <insert id="insertDomain" useGeneratedKeys="true" keyProperty="id" parameterType="Domain">
        insert into sms_domain
        (domain, sp_id)
        values
        (#{domain}, #{sp_id})
    </insert>
    
    <update id="updateDomain" parameterType="Domain">
        update sms_domain set
        domain=#{domain}
        where id=#{id}
    </update>
    
    <update id="updateDomainStatus" parameterType="Domain">
        update sms_domain set
        status=#{status}
        where id=#{id}
    </update>
	<update id="disableMdnVisit">
		UPDATE short_url set status = 1 where taskid = #{taskId} and spid = #{spId} and mdn = #{mdn}
	</update>

	<delete id="deleteDomain" parameterType="int">
    	delete from sms_domain where id = #{id}
	</delete>
	
</mapper>