<?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.ylcm.sys.mapper.AdSpecialMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylcm.sys.domain.AdSpecial">
        <id column="id" property="id" />
        <result column="type" property="type" />
        <result column="product_name" property="productName" />
        <result column="brand_name" property="brandName" />
        <result column="adv_master" property="advMaster" />
        <result column="media_name" property="mediaName" />
        <result column="show_env" property="showEnv" />
        <result column="tv_name" property="tvName" />
        <result column="show_time" property="showTime" />
        <result column="show_date" property="showDate" />
        <result column="show_duration" property="showDuration" />
        <result column="price" property="price" />
        <result column="phone" property="phone" />
        <result column="status" property="status" />
        <result column="crt_time" property="crtTime" />
        <result column="upd_time" property="updTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, type, product_name, brand_name, adv_master, media_name, show_env, tv_name, show_time, show_date, show_duration, price, phone, status, crt_time, upd_time
    </sql>
    
    <select id="getPage" resultType="com.ylcm.sys.vo.AdSpecialAnalysisVO">
    	SELECT a.id, a.type, a.product_name, a.brand_name, a.adv_master, a.media_name, a.show_env, a.tv_name, a.show_time, a.show_date, a.show_duration, a.price, a.phone,
    			b.first_ind, b.second_ind
    	FROM ad_special a 
    	LEFT JOIN ad_product b on a.product_name = b.product_name
    	<where>
    		<if test="queryBean.type != null">
    			AND a.type= #{queryBean.type}
    		</if>
    	</where>
    </select>
    
    <select id="specialTop" resultType="com.ylcm.sys.vo.AdSpecialTopVO">
    	SELECT 
    		product_name,sum(show_duration) show_duration,SUM(1) frequency
		FROM
			ad_special
		<where>
			<if test="type != null">
				AND type = 0
			</if>
			<if test="startDate != null and startDate != ''">
				AND show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= show_date
			</if>
		</where> 

		GROUP BY product_name
		ORDER BY sum(show_duration) DESC LIMIT 30
    </select>

</mapper>
