<?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>
    
    <!-- 专题基本字段 -->
    <sql id="Page_Column_List">
        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
    </sql>
    
    <sql id="WHERE_GET_PAGE">
      <trim prefix="where" prefixOverrides="and|or">
          a.status = 0 AND b.status = 0
          <if test="queryBean.type != null">
              AND a.type= #{queryBean.type} 
          </if>  
          <if test="queryBean.keyword != null and queryBean.keyword !='' ">
              AND (position(#{queryBean.keyword} in COALESCE(a.product_name,'')) > 0 OR position(#{queryBean.keyword} in COALESCE(a.brand_name,'')) > 0
              OR position(#{queryBean.keyword} in COALESCE(a.adv_master,'')) > 0 OR position(#{queryBean.keyword} in COALESCE(a.tv_name,'')) > 0)
          </if>
          <if test="queryBean.mediaName != null and queryBean.mediaName != ''">
              AND a.media_name= #{queryBean.mediaName}
          </if>  
          <if test="queryBean.showEnv != null">
              AND a.show_env= #{queryBean.showEnv}
          </if>  
          <if test="queryBean.firstInd != null and queryBean.firstInd != ''">
              AND b.first_ind= #{queryBean.firstInd}
          </if>  
          <if test="queryBean.secondInd != null and queryBean.secondInd != ''">
              AND b.second_ind= #{queryBean.secondInd}
          </if>  
          <if test="queryBean.startDate != null and queryBean.startDate != ''">
              <![CDATA[ AND a.show_date  >= #{queryBean.startDate}]]>
          </if>
          <if test="queryBean.endDate != null and queryBean.endDate != ''">
              <![CDATA[ AND a.show_date  <= #{queryBean.endDate}]]>
          </if>      
      </trim>
    </sql>
    
    <select id="getPage" resultType="com.ylcm.sys.vo.AdSpecialAnalysisVO">
    	SELECT 
    	<include refid ="Page_Column_List"/>
    	FROM ad_special a 
        <if test="queryBean.type != null and queryBean.type == 0">
			JOIN ad_product b on a.product_name = b.product_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
		<if test="queryBean.type != null and queryBean.type == 1">
			JOIN ad_platform b on a.product_name = b.platform_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
        <include refid ="WHERE_GET_PAGE"/>
		-- 汉字首字母升序、日期降序、时间升序
		ORDER BY (media_name+0)
		,(lower(case when media_name REGEXP '^[a-zA-Z]' then LEFT(media_name, 1)
			when media_name REGEXP '^[0-9]' then LEFT(media_name, 1)
			else ELT(INTERVAL(CONV(HEX(LEFT(CONVERT(media_name USING gbk),1)),16,10),
			0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
			0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,
			0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
			'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P',
			'Q','R','S','T','W','X','Y','Z')
			end)
		) ASC
		,media_name ASC
		,a.show_date DESC,a.show_time
    </select>
    
    <select id="getALLPage" resultType="com.ylcm.sys.vo.AdSpecialAnalysisVO">
	     SELECT a.* FROM 
		 (
			 SELECT <include refid ="Page_Column_List"/>
		     FROM ad_special a 
			 JOIN ad_product b on a.product_name = b.product_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
			 <include refid ="WHERE_GET_PAGE"/>
			 UNION ALL
			 SELECT <include refid ="Page_Column_List"/>
		     FROM ad_special a 
			 JOIN ad_platform b on a.product_name = b.platform_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
			 <include refid ="WHERE_GET_PAGE"/>
		 ) a
		 -- 汉字首字母升序、日期降序、时间升序
		ORDER BY (media_name+0)
		,(lower(case when media_name REGEXP '^[a-zA-Z]' then LEFT(media_name, 1)
			when media_name REGEXP '^[0-9]' then LEFT(media_name, 1)
			else ELT(INTERVAL(CONV(HEX(LEFT(CONVERT(media_name USING gbk),1)),16,10),
			0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
			0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,
			0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
			'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P',
			'Q','R','S','T','W','X','Y','Z')
			end)
		) ASC
		,media_name ASC
		,a.show_date DESC,a.show_time
    </select>
    
    <select id="specialTop" resultType="com.ylcm.sys.vo.AdSpecialTopVO">
    	SELECT 
    		a.product_name as productName, b.second_ind as secondInd, sum(a.show_duration) duration, SUM(1) frequency
		FROM
			ad_special a
	    <if test="type == 0">
			JOIN ad_product b on a.product_name = b.product_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
		<if test="type == 1">
			JOIN ad_platform b on a.product_name = b.platform_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
		<where>
			a.status = 0 AND b.status = 0
			<if test="type != null">
				AND a.type = #{type}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= a.show_date
			</if>
		</where> 
		GROUP BY a.product_name,b.second_ind
		ORDER BY sum(a.show_duration) DESC LIMIT 30
    </select>

	<select id="mediaDist" resultType="com.ylcm.sys.vo.AdSpecialDistVO">
    	SELECT media_name name,SUM(1) total, SUM(show_duration) duration
		FROM ad_special 
		<where>
			status = 0
			<if test="type != null">
				AND type = #{type}
			</if>
			<if test="startDate != null and startDate != ''">
				AND show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= show_date
			</if>
			<if test="productName != null and productName != ''">
				AND product_name = #{productName}
			</if>
		</where>
		GROUP BY media_name
    </select>
    
    <select id="tvDist" resultType="com.ylcm.sys.vo.AdSpecialDistVO">
    	SELECT tv_name name,SUM(1) total, SUM(show_duration) duration
		FROM ad_special 
		<where>
			status = 0 and show_env = 1
			<if test="type != null">
				AND type = #{type}
			</if>
			<if test="startDate != null and startDate != ''">
				AND show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= show_date
			</if>
			<if test="productName != null and productName != ''">
				AND product_name = #{productName}
			</if>
		</where>
		GROUP BY tv_name
    </select>
    
    <select id="secondIndDist" resultType="com.ylcm.sys.vo.AdSpecialDistVO">
    	SELECT 
    	a.product_name name,
    	SUM(1) total, SUM(a.show_duration) duration
		FROM ad_special a 
		<if test="type == 0">
			JOIN ad_product b on a.product_name = b.product_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
		<if test="type == 1">
			JOIN ad_platform b on a.product_name = b.platform_name AND COALESCE(a.brand_name, '') =  COALESCE(b.brand_name, '') 
		</if>
		<where>
			a.status = 0
			<if test="type != null">
				AND a.type = #{type} AND b.status = 0
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.show_date >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND #{endDate} >= a.show_date
			</if>
			<if test="type != null and secondInd != null and secondInd != ''">
                AND b.second_ind = #{secondInd}
            </if>
		</where>	    
		GROUP BY a.product_name
		<if test="launch != null and launch == 0">
		    ORDER BY SUM(1) DESC
		</if>
		<if test="launch != null and launch == 1">
		    ORDER BY SUM(a.show_duration) DESC 
		</if>
    </select>
    
    <select id="benchmarkingPage" resultType="com.ylcm.sys.vo.AdSpecialAnalysisBenchmarkingVO">
    	SELECT a.id , a.product_name as productName
    	<if test="queryBean.type == 0">
    	    ,b.name_img_url as imgUrl
    	</if>
    	<if test="queryBean.type == 1">
    	    ,b.img_url as imgUrl
    	</if>
		FROM ad_special a 
		<if test="queryBean.type == 0">
			LEFT JOIN ad_product b on a.product_name = b.product_name
		</if>
		<if test="queryBean.type == 1">
			LEFT JOIN ad_platform b on a.product_name = b.platform_name
		</if>
		<where>
			a.status = 0 
			<if test="queryBean.type != null">
				AND a.type = #{queryBean.type} AND b.status = 0
			</if>
		    <if test="queryBean.productName != null and queryBean.productName !='' ">
				AND a.product_name LIKE CONCAT('%',#{queryBean.productName},'%')
			</if>
			<if test="queryBean.idList != null and queryBean.idList.size() > 0">
				<foreach collection="queryBean.idList" item="id" open="AND a.id in (" separator="," close=")" >
	                 #{id}
	            </foreach>
            </if>
            <if test="queryBean.productNameList != null and queryBean.productNameList.size() > 0">
				<foreach collection="queryBean.productNameList" item="productNameList" open="AND a.product_name not in (" separator="," close=")" >
	                 #{productNameList}
	            </foreach>	
		    </if>               
		</where>
		GROUP BY a.product_name
		ORDER BY a.crt_time DESC
    </select>
    
    <!-- 同步专题栏目/产品信息 -->
	<update id="upSyscAdSpecialProductName">
        UPDATE ad_special SET product_name = #{productName}, brand_name = #{brandName},upd_time = now()
		WHERE status = 0 AND type= #{type} AND product_name = #{oldProductName} 
		<choose>
			<when test="oldBrandName != null and oldBrandName !='' ">
				 AND brand_name = #{oldBrandName}
			</when>
			<otherwise>
				 AND (brand_name is null OR brand_name = '')
			</otherwise>
        </choose>     
	</update>
    
</mapper>
