<?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="com.iformall.mapper.WxUserVisitMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxUserVisit">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="ref_date" jdbcType="VARCHAR" property="refDate" />
		<result column="day_date" jdbcType="TIMESTAMP" property="dayDate" />
		<result column="session_cnt" jdbcType="INTEGER" property="sessionCnt" />
		<result column="visit_pv" jdbcType="INTEGER" property="visitPv" />
		<result column="visit_uv" jdbcType="INTEGER" property="visitUv" />
		<result column="visit_uv_new" jdbcType="INTEGER" property="visitUvNew" />
		<result column="stay_time_uv" jdbcType="VARCHAR" property="stayTimeUv" />
		<result column="stay_time_session" jdbcType="VARCHAR" property="stayTimeSession" />
		<result column="visit_depth" jdbcType="VARCHAR" property="visitDepth" />
		<result column="app_id" jdbcType="VARCHAR" property="appId" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`ref_date`,`day_date`,`session_cnt`,`visit_pv`,`visit_uv`,`visit_uv_new`,`stay_time_uv`,`stay_time_session`,`visit_depth`,`app_id`,`create_date`
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
	    
		<if test=" null != id "> 
		and `id` = #{id} 
       	
		</if>
		<if test=" null != tenantId ">
			and `tenant_id`  =#{tenantId}

		</if>
	    
		<if test=" null != refDate "> 
		and `ref_date`  = #{refDate}
       	
		</if>    
	    
		<if test=" null != dayDate "> 
		and `day_date` = #{dayDate} 
       	
		</if>    
	    
		<if test=" null != sessionCnt "> 
		and `session_cnt` = #{sessionCnt} 
       	
		</if>    
	    
		<if test=" null != visitPv "> 
		and `visit_pv` = #{visitPv} 
       	
		</if>    
	    
		<if test=" null != visitUv "> 
		and `visit_uv` = #{visitUv} 
       	
		</if>    
	    
		<if test=" null != visitUvNew "> 
		and `visit_uv_new` = #{visitUvNew} 
       	
		</if>    
	    
		<if test=" null != stayTimeUv "> 
		and `stay_time_uv`  like concat('%', #{stayTimeUv},'%')
       	
		</if>    
	    
		<if test=" null != stayTimeSession "> 
		and `stay_time_session`  like concat('%', #{stayTimeSession},'%')
       	
		</if>    
	    
		<if test=" null != visitDepth "> 
		and `visit_depth`  like concat('%', #{visitDepth},'%')
       	
		</if>    
	    
		<if test=" null != appId "> 
		and `app_id`  = #{appId}
       	
		</if>    
	    
		<if test=" null != createDate "> 
		and `create_date` = #{createDate} 
       	
		</if>    
		<if test=" null != ids ">
		 	and id in  
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
			#{idItem}
			</foreach>
		</if>		
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.WxUserVisit" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_user_visit
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="touchUsersReportList" resultType="com.iformall.domain.vo.TouchUsersReportVo" parameterType="com.iformall.domain.dto.MarkingCouponDataReportDto">
		SELECT DATE_FORMAT(day_date,'%Y-%m-%d') as xTime,visit_pv as pv,visit_uv as uv
		from wx_user_visit
		where tenant_id=#{tenantId}
		<if test="startTime != null">
		and day_date &gt;= #{startTime}
		</if>
		<if test="endTime != null">
		and day_date &lt;= #{endTime}
		</if>

		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by day_date desc </if>
	</select>
	
	<select id="touchUsersReportData" resultType="com.iformall.domain.vo.TouchUsersReportVo" parameterType="hashmap">
		SELECT DATE_FORMAT(day_date,'%m/%d') as xTime,visit_pv as pv,visit_uv as uv
		from wx_user_visit
		where tenant_id=#{tenantId}
		<if test="startTime != null">
			and day_date &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and day_date &lt;= #{endTime}
		</if>
		order by day_date desc
	</select>

	<select id="queryVisitUv" resultType="Long" parameterType="hashmap">
		select count(visit_uv) from wx_user_visit where tenant_id=#{tenantId}
		<if test="startTime != null">
			and day_date &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and day_date &lt;= #{endTime}
		</if>
	</select>

</mapper>
