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

	<resultMap type="MontiorChannel" id="MontiorChannelMap">
        <id column="id" property="id" />
        <result column="channel_number" property="channel_number" />
        <result column="channel_grade" property="channel_grade" />
        <result column="createtime" property="createtime" />
        <collection property="content" ofType="MontiorContent">
			<result column="channel_id" property="channel_id" />
			<result column="link_num" property="link_num" />
			<result column="register_num" property="register_num" />
			<result column="pay_num" property="pay_num" />
		</collection>
        
    </resultMap>
    
	 <insert id="insertChannel"  useGeneratedKeys="true" keyProperty="id" parameterType="MontiorChannel" >
        insert into monitor_channel (channel_number,channel_grade,createtime) values (#{channel_number},#{channel_grade},now())
    </insert>
    
	<insert id="insertContent"  useGeneratedKeys="true" keyProperty="id" parameterType="MontiorContent" >
        insert into monitor_content (channel_id,link_num,register_num,pay_num,pay_grade) values
        <foreach item="item" index="index" collection="list" separator=",">
         	(#{item.channel_id},#{item.link_num},#{item.register_num},#{item.pay_num},#{item.pay_grade})
  		</foreach>
        
    </insert>
    
    <select id="listPageMonitor" resultMap="MontiorChannelMap">
    	SELECT ch.id,ch.channel_number,ch.channel_grade,DATE_FORMAT(ch.createtime, '%Y-%m-%d') createtime,
    	con.channel_id,con.link_num,con.register_num,con.pay_num,con.pay_grade
    	 from monitor_channel ch,monitor_content con where ch.id=con.channel_id
    	<if test="createtime!=null and createtime!=''">
    		and createtime &gt; #{createtime}
    	</if>
    	<if test="createtime1!=null and createtime1!=''">
    		and createtime &lt; #{createtime1}
    	</if>
    </select>
    
    <insert id="insertResSta" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.ResStatistics">
    	insert into res_statistics(productid,subproductid,time,game,starttime,endtime,androidregister,iosregister,purchase,roi1,roleName,
    	ltv1,ltv2,ltv3,createtime) 
    	values(#{productid},#{subproductid},#{time},#{game},#{starttime},#{endtime},#{androidregister},#{iosregister},#{purchase},#{roi1},#{roleName},
    	#{ltv1},#{ltv2},#{ltv3},now())
    </insert>
    
    <insert id="insertResStaAll"  useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.ResStatistics" >
        insert into res_statistics(productid,subproductid,time,game,starttime,endtime,androidregister,iosregister,purchase,roi1,
    	ltv,ltv2,ltv3,createtime)  values
        <foreach item="item" index="index" collection="list" separator=",">
         	(#{item.productid},#{item.subproductid},#{item.time},#{item.game},#{item.starttime},#{item.endtime},#{item.androidregister},
         	#{item.iosregister},#{item.purchase},#{item.roi1},
    		#{item.ltv1},#{item.ltv2},#{item.ltv3},now())
  		</foreach>
        
    </insert>
    
    <insert id="insertShortUrl" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.ShortUrlVisit">
    	insert into short_url_visit(spid,taskid,mdn,shorturl,longurl,ip,ua,createtime,mdntype,productname,phonetype,reason,ipregion,mdnprovince,browser) 
    	values(#{spid},#{taskid},#{mdn},#{shorturl},#{longurl},#{ip},#{ua},#{mdntype},#{productname},#{phonetype},#{reason},#{ipregion},#{mdnprovince},#{browser},now())
    </insert>
    
    <insert id="insertShortUrlAll"  useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.ShortUrlVisit" >
        insert into short_url_visit(spid,taskid,mdn,shorturl,longurl,ip,ua,createtime,mdntype,productname,phonetype,reason,ipregion,mdnprovince,browser)  values
        <foreach item="item" index="index" collection="list" separator=",">
         	(#{item.spid},#{item.taskid},#{item.mdn},#{item.shorturl},#{item.ip},#{item.ua},#{item.mdntype},#{item.productname},#{item.phonetype},#{item.reason},#{item.ipregion},
         	#{item.mdnprovince},#{item.browser},now())
  		</foreach>
    </insert>
    
    <insert id="insertCpaStatistics" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.CpaStatistics">
    	insert into res_statistics(id,productid,game,androidregister,iosregister,createtime,register) 
    	values(#{id},#{productid},#{game},#{androidregister},#{iosregister},#{createtime},#{register},now())
    </insert>
    
    <insert id="insertCpaStatisticsAll" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.CpaStatistics">
    	insert into cpa_statistics(id,productid,game,androidregister,iosregister,createtime) 
    	select id,productid,game,androidregister,iosregister,createtime from res_statistics
    </insert>
    
    <select id="listPageResSta" resultType="cn.roadm.bo.ResStatistics">
    	select id,productid,subproductid,time,game,DATE_FORMAT(starttime,'%Y-%m-%d %H:%i:%s') starttime,
    	DATE_FORMAT(endtime,'%Y-%m-%d %H:%i:%s') endtime,androidregister,iosregister,purchase,roi1,DATE_FORMAT(createtime,'%Y-%m-%d %H:%i:%s') createtime,
    	ltv1,ltv2,ltv3,(androidregister+iosregister)*ltv1 countLtv, roleName from res_statistics
    	where 1=1
    	<!-- <if test="roleName != null and roleName != ''">
   			and roleName=#{roleName}
   		</if> -->
   		<if test="productid!=null and productid!=''">
   			and productid=#{productid}
   		</if>
   		<if test="createtime!=null and createtime!=''">
   			and createtime &gt; #{createtime}
   		</if>
   		<if test="createtime1!=null and createtime1!=''">
   			and createtime &lt; #{createtime1}
   		</if>
    	order by id desc
    </select>
    
    
   
    <select id="listGames" resultType="cn.roadm.bo.ResStatistics">
    	select productid from res_statistics
    	where 1=1
    	<if test="roleName !=null and roleName != ''">
    		and roleName=#{roleName}
    	</if>
    	group by productid
    </select>
    
    <select id="listResStaCount" resultType="cn.roadm.bo.ResStatistics">
    	select androidregister,iosregister,purchase,(androidregister+iosregister)*ltv1 countLtv from res_statistics
    	where 1=1
    	<!-- <if test="roleName != null and roleName != ''">
   			and roleName=#{roleName}
   		</if> -->
   		<if test="productid!=null and productid!=''">
   			and productid=#{productid}
   		</if>
   		<if test="createtime!=null and createtime!=''">
   			and createtime &gt; #{createtime}
   		</if>
   		<if test="createtime1!=null and createtime1!=''">
   			and createtime &lt; #{createtime1}
   		</if>
    </select>
    
    
    <select id="getResStaByParam" resultType="Map">
    	select id,productid,subproductid,time,game,DATE_FORMAT(starttime,'%Y-%m-%d %H:%i:%s') starttime,
    	DATE_FORMAT(endtime,'%Y-%m-%d %H:%i:%s') endtime,androidregister,iosregister,purchase,roi1,DATE_FORMAT(createtime,'%Y-%m-%d %H:%i:%s') createtime,
    	ltv1,ltv2,ltv3 from res_statistics
    	<where>
    		<if test="productid!=null and productid!=''">
    			and productid=#{productid}
    		</if>
    		<if test="subproductid!=null and subproductid!=''">
    			and subproductid=#{subproductid}
    		</if>
    		<if test="game!=null and game!=''">
    			and game like concat('%',#{game},'%')
    		</if>
    		<if test="createtime!=null and createtime!=''">
    			and createtime &gt; #{createtime}
    		</if>
    		<if test="createtime1!=null and createtime1!=''">
    			and createtime &lt; #{createtime1}
    		</if>
    	</where>
    	order by id desc
    </select>
    
    <select id="getShortUrlByParam" resultType="Map">
    	select id,spid,taskid,mdn,shorturl,longurl,ip,ua,mdntype,productname,phonetype,reason,ipregion,mdnprovince,browser,DATE_FORMAT(createtime,'%Y-%m-%d %H:%i:%s') createtime from short_url_visit
    	<where>
    		<if test="shorturl!=null and shorturl!=''">
    			and shorturl=#{shorturl}
    		</if>
    		<if test="longurl!=null and longurl!=''">
    			and longurl=#{longurl}
    		</if>
    		<if test="createtime!=null and createtime!=''">
    			and createtime &gt; #{createtime}
    		</if>
    		<if test="createtime1!=null and createtime1!=''">
    			and createtime &lt; #{createtime1}
    		</if>
    	</where>
    	order by id desc
    </select>
    
    <select id="listPageShortUrl" resultType="cn.roadm.bo.ShortUrlVisit">
    	select id,spid,taskid,mdn,shorturl,longurl,ip,ua,mdntype,productname,phonetype,reason,ipregion,mdnprovince,browser,DATE_FORMAT(createtime,'%Y-%m-%d %H:%i:%s') createtime from short_url_visit
    	<where>
    		<if test="shorturl!=null and shorturl!=''">
    			and shorturl=#{shorturl}
    		</if>
    		<if test="longurl!=null and longurl!=''">
    			and longurl=#{longurl}
    		</if>
    		<if test="createtime!=null and createtime!=''">
    			and createtime &gt; #{createtime}
    		</if>
    		<if test="createtime1!=null and createtime1!=''">
    			and createtime &lt; #{createtime1}
    		</if>
    	</where>
    	order by id desc
    </select>
    
    <select id="listPageShortUrlCount" resultType="cn.roadm.bo.ShortUrlVisit">
    	select shorturl,DATE_FORMAT(createtime,'%Y-%m-%d') createtime, COUNT(1) pv,DATE_FORMAT(createtime,'%Y-%m-%d') time,COUNT(DISTINCT ip) uv from short_url_visit
    	<where>
    		<if test="shorturl!=null and shorturl!=''">
    			and shorturl=#{shorturl}
    		</if>
    		<if test="createtime!=null and createtime!=''">
    			and createtime &gt; #{createtime}
    		</if>
    		<if test="createtime1!=null and createtime1!=''">
    			and createtime &lt; #{createtime1}
    		</if>
    	</where>
    	GROUP BY DATE_FORMAT(createtime,'%Y-%m-%d'), shorturl
    </select>
    
    <select id="listPageCpaStatistics" resultType="cn.roadm.bo.CpaStatistics">
        select id,
        productid,
        game,
        DATE_FORMAT(createtime,'%Y-%m-%d') createtime,
        sum(androidregister+iosregister) register
        from res_statistics 
        <where>
        <if test="productid!=null and productid!=''">
            and productid=#{productid}
        </if>
        <if test="game!=null and game!=''">
    		and game like concat('%',#{game},'%')
    	</if>
        <if test="createtime!=null and createtime!=''">
    		and createtime &gt; #{createtime}
    	</if>
    	<if test="createtime1!=null and createtime1!=''">
    		and createtime &lt; #{createtime1}
    	</if>
        </where>
        group by productid,game,DATE_FORMAT(createtime,'%Y-%m-%d')
        order by id desc
    </select>

</mapper>