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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylcm.sys.domain.AdTheatreTvStat">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="media_name" property="mediaName" />
        <result column="theatre_time" property="theatreTime" />
        <result column="first_theme" property="firstTheme" />
        <result column="second_theme" property="secondTheme" />
        <result column="channel_system" property="channelSystem" />
        <result column="season" property="season" />
        <result column="tv_num" property="tvNum" />
		<result column="tv_num_d" property="tvNumD" />
        <result column="rating_29" property="rating29" />
        <result column="dk_29" property="dk29" />
        <result column="rating_51" property="rating51" />
        <result column="dk_51" property="dk51" />
        <result column="show_date" property="showDate" />
        <result column="crt_time" property="crtTime" />
        <result column="upd_time" property="updTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, name, media_name, theatre_time, first_theme, second_theme, channel_system, season, tv_num, rating_29, dk_29, rating_51, dk_51, effect,show_date
    </sql>
    
	<select id="getPage" resultMap="BaseResultMap">
		SELECT *
		FROM (
			SELECT
				id, name, media_name, theatre_time, first_theme, second_theme, channel_system, season, sum(tv_num) tv_num, sum(rating_29)/sum(tv_num_d) rating_29, sum(dk_29)/sum(tv_num_d) dk_29, show_date
			FROM
				ad_theatre_tv_stat
			<where>
				<if test="queryBean.startDate != null and queryBean.startDate != ''">
					AND show_date >= #{queryBean.startDate}
				</if>
				<if test="queryBean.endDate != null and queryBean.endDate != ''">
					AND #{queryBean.endDate} >= show_date
				</if>
				<if test="queryBean.name != null and queryBean.name != ''">
					AND name like concat('%', #{queryBean.name}, '%')
				</if>
				<if test="queryBean.mediaName != null and queryBean.mediaName != ''">
					AND media_name = #{queryBean.mediaName}
				</if>
				<if test="queryBean.firstTheme != null and queryBean.firstTheme != ''">
					AND first_theme = #{queryBean.firstTheme}
				</if>
				<if test="queryBean.secondTheme != null and queryBean.secondTheme != ''">
					AND second_theme = #{queryBean.secondTheme}
				</if>
				<if test="queryBean.channelSystem != null">
					AND channel_system = #{queryBean.channelSystem}
				</if>
				<if test="queryBean.theatreTime != null">
					AND theatre_time = #{queryBean.theatreTime}
				</if>
			</where>
			GROUP BY media_name,name,season,theatre_time
		) a
		ORDER BY a.rating_29 DESC,id DESC
		
	</select>
	
	<select id="statByMedia" resultMap="BaseResultMap">
		SELECT
			sum(tv_num) tv_num, sum(rating_29)/sum(tv_num_d) rating_29, sum(dk_29)/sum(tv_num_d) dk_29
		FROM 
			ad_theatre_tv_stat
		<where>
			<if test="startDate != null and startDate != ''">
				AND show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= show_date
			</if>
			<if test="mediaName != null and mediaName != ''">
				AND media_name = #{mediaName}
			</if>
			<if test="theatreTime != null">
				AND theatre_time = #{theatreTime}
			</if>
		</where>
	</select>

	<select id="tvTop" resultType="com.ylcm.sys.vo.AdTheatreTopVO">
		SELECT *
		FROM
		(
			SELECT id,name, media_name mediaName, sum(tv_num) mediaNum, first_theme, sum(rating_29)/sum(tv_num_d) rating29,
					sum(dk_29)/sum(tv_num_d) dk29, count(*) total
			FROM

				ad_theatre_tv_stat
			<where>
				<if test="theatreTime != null">
					AND theatre_time = #{theatreTime}
				</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 name,media_name) A
		ORDER BY A.rating29 DESC
		limit 30

	</select>

	<select id="mediaTop" resultType="com.ylcm.sys.vo.AdTheatreTopVO">
		SELECT * ,rating29 as rating
		FROM (

		SELECT id,name, media_name mediaName, sum(tv_num) mediaNum, channel_system mediaRank, first_theme, sum(rating_29)/sum(tv_num_d) rating29,
			sum(dk_29)/sum(tv_num_d) dk29, count(*) total
		FROM
		ad_theatre_tv_stat
		<where>
			<if test="name != null and name != ''">
				AND name = #{name}
			</if>
			<if test="theatreTime != null">
				AND theatre_time = #{theatreTime}
			</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 media_name

		) t
		ORDER BY rating DESC
		limit #{limitSize}

	</select>
	
	<select id="tvRatioTrend" resultType="com.ylcm.sys.vo.AdTvRatioTrendVO">
		SELECT rating_29/tv_num_d as ratio, show_date as showDate
		FROM ad_theatre_tv_stat a
		<where>
			<if test="queryBean.name != null and queryBean.name != ''">
				AND a.name = #{queryBean.name}
			</if>
			<if test="queryBean.mediaName != null and queryBean.mediaName != ''">
				AND a.media_name = #{queryBean.mediaName}
			</if>
			AND a.theatre_time = #{queryBean.theatreTime}
			<if test="queryBean.startDate != null and queryBean.startDate != ''">
				AND a.show_date >= #{queryBean.startDate}
			</if>
			<if test="queryBean.endDate != null and queryBean.endDate != ''">
				AND #{queryBean.endDate} > a.show_date
			</if>

		</where>
		ORDER BY a.show_date DESC
		limit 10

	</select>
	
	<select id="tvRatioPeople" resultType="com.ylcm.sys.domain.AdTheatreTv">
		SELECT IFNULL(avg(people_man_29),0) peopleMan29,
				IFNULL(avg(people_women_29),0) peopleWomen29,
				IFNULL(avg(people_414_29),0) people41429,
				IFNULL(avg(people_1524_29),0) people152429,
				IFNULL(avg(people_2534_29),0) people253429,
				IFNULL(avg(people_3544_29),0) people354429,
				IFNULL(avg(people_4554_29),0) people455429,
				IFNULL(avg(people_5564_29),0) people556429,
				IFNULL(avg(people_65_29),0) people6529
		FROM ad_theatre_tv a JOIN ad_theatre b on a.ad_theatre_id = b.id
		<where>
			b.status = 0
			<if test="queryBean.name != null and queryBean.name != ''">
				AND b.name = #{queryBean.name}
			</if>
			<if test="queryBean.mediaName != null and queryBean.mediaName != ''">
				AND b.media_name = #{queryBean.mediaName}
			</if>
			<if test="queryBean.timeQuery != null and queryBean.timeQuery != ''">
				AND ${queryBean.timeQuery}
			</if>
			<if test="queryBean.startDate != null and queryBean.startDate != ''">
				AND b.show_date >= #{queryBean.startDate}
			</if>
			<if test="queryBean.endDate != null and queryBean.endDate != ''">
				AND #{queryBean.endDate} > b.show_date
			</if>
		</where>
	</select>

	<select id="tvRatioZone" resultType="com.ylcm.sys.domain.AdTheatreTv">
		SELECT avg(shandong) shandong,
		avg(liaoning) liaoning,
		avg(heilongjiang) heilongjiang,
		avg(jilin) jilin,
		avg(neimenggu) neimenggu,
		avg(beijing) beijing,
		avg(shanghai) shanghai,
		avg(tianjing) tianjing,
		avg(chongqing) chongqing,
		avg(shanxi) shanxi,
		avg(sichuan) sichuan,
		avg(yunnan) yunnan,
		avg(guizhou) guizhou,
		avg(gansu) gansu,
		avg(shaanxi) shaanxi,
		avg(hebei) hebei,
		avg(henan) henan,
		avg(fujian) fujian,
		avg(guangdong) guangdong,
		avg(zhejiang) zhejiang,
		avg(jiangsu) jiangsu,
		avg(jiangxi) jiangxi,
		avg(anhui) anhui,
		avg(hunan) hunan,
		avg(hubei) hubei,
		avg(guangxi) guangxi,
		avg(hainan) hainan,
		avg(ningxia) ningxia,avg(xinjiang) xinjiang

		FROM ad_theatre_tv a JOIN ad_theatre b on a.ad_theatre_id = b.id
		<where>
			b.status = 0
			<if test="queryBean.name != null and queryBean.name != ''">
				AND b.name = #{queryBean.name}
			</if>
			<if test="queryBean.mediaName != null and queryBean.mediaName != ''">
				AND b.media_name = #{queryBean.mediaName}
			</if>
			<if test="queryBean.timeQuery != null and queryBean.timeQuery != ''">
				AND ${queryBean.timeQuery}
			</if>
			<if test="queryBean.startDate != null and queryBean.startDate != ''">
				AND b.show_date >= #{queryBean.startDate}
			</if>
			<if test="queryBean.endDate != null and queryBean.endDate != ''">
				AND #{queryBean.endDate} > b.show_date
			</if>
		</where>
	</select>
	
	<select id="productDist" resultType="com.ylcm.sys.vo.AdTheatreProductDistVO">
		SELECT ifnull(sum(duration),0) duration,count(*) total,type_name name FROM
			ad_theatre b
		<where>
			b.`status` = 0 and (b.type_name is not null and b.type_name != '')
			<if test="queryBean.name != null and queryBean.name != ''">
				AND b.name = #{queryBean.name}
			</if>
			<if test="queryBean.timeQuery != null and queryBean.timeQuery != ''">
				AND ${queryBean.timeQuery}
			</if>
			<if test="queryBean.startDate != null and queryBean.startDate != ''">
				AND b.show_date >= #{queryBean.startDate}
			</if>
			<if test="queryBean.endDate != null and queryBean.endDate != ''">
				AND #{queryBean.endDate} > b.show_date
			</if>
		</where>

		GROUP BY type_name

	</select>

	<select id="themeDist" resultType="com.ylcm.sys.vo.AdThemeDistVO">
		SELECT
			first_theme,count(*) total,sum(rating_29)/sum(tv_num_d) ratio
		FROM
			ad_theatre_tv_stat

		<where>
			<if test="queryBean.mediaName != null and queryBean.mediaName != ''">
				AND media_name = #{queryBean.mediaName}
			</if>
			<if test="queryBean.startDate != null and queryBean.startDate != ''">
				AND show_date >= #{queryBean.startDate}
			</if>
			<if test="queryBean.endDate != null and queryBean.endDate != ''">
				AND #{queryBean.endDate} > show_date
			</if>
			<if test="queryBean.theatreTime != null">
				AND theatre_time = #{queryBean.theatreTime}
			</if>

		</where>
		GROUP BY first_theme

	</select>

</mapper>
