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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylcm.sys.domain.AdTheatre">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="media_name" property="mediaName" />
        <result column="media_num" property="mediaNum" />
        <result column="first_theme" property="firstTheme" />
        <result column="second_theme" property="secondTheme" />
        <result column="show_date" property="showDate" />
        <result column="show_time" property="showTime" />
        <result column="type" property="type" />
        <result column="type_name" property="typeName" />
        <result column="jinxian" property="jinxian" />
        <result column="status" property="status" />
        <result column="crt_time" property="crtTime" />
        <result column="upd_time" property="updTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, name, media_name, media_num, first_theme, second_theme, show_date, show_time, type, type_name, jinxian, status, crt_time, upd_time
    </sql>

    <select id="getListByCondition" resultType="com.ylcm.sys.vo.AdTheatreVO">
        SELECT t.id, t.name, t.media_name, t.media_num, t.first_theme, t.second_theme, t.show_date, t.show_time, t.type, t.type_name, t.jinxian, t.crt_time, t.upd_time, t.duration
        FROM ad_theatre t
        <if test="form.mediaRank != null">
            LEFT JOIN ad_media m ON t.media_name = m.name
        </if>
        WHERE t.status = 0
        <if test="form.name != null and form.name != ''">
            AND t.name LIKE CONCAT('%',#{form.name},'%')
        </if>
        <if test="form.type != null">
            AND t.type = #{form.type}
        </if>
        <if test="form.typeName != null and form.typeName != ''">
            AND t.type_name LIKE CONCAT('%',#{form.typeName},'%')
        </if>
        <if test="form.firstTheme != null and form.firstTheme != ''">
            AND t.first_theme = #{form.firstTheme}
        </if>
        <if test="form.secondTheme != null and form.secondTheme != ''">
            AND t.second_theme = #{form.secondTheme}
        </if>
        <if test="form.mediaRank != null">
            AND m.rank = #{form.mediaRank}
        </if>
        <if test="form.mediaName != null and form.mediaName != ''">
            AND t.media_name = #{form.mediaName}
        </if>
        <if test="form.mediaNumMin != null">
            AND t.media_num >= #{form.mediaNumMin}
        </if>
        <if test="form.mediaNumMax != null">
            AND t.media_num <![CDATA[<=]]> #{form.mediaNumMax}
        </if>
        <if test="form.showDateStart != null and form.showDateStart != ''">
            AND t.show_date >= #{form.showDateStart}
        </if>
        <if test="form.showDateEnd != null and form.showDateEnd != ''">
            AND t.show_date <![CDATA[<=]]> #{form.showDateEnd}
        </if>
        <if test="form.showTimeStart != null and form.showTimeStart != ''">
            AND t.show_time >= #{form.showTimeStart}
        </if>
        <if test="form.showTimeEnd != null and form.showTimeEnd != ''">
            AND t.show_time <![CDATA[<=]]> #{form.showTimeEnd}
        </if>
        <if test="form.jinxianMin != null">
            AND t.jinxian >= #{form.jinxianMin}
        </if>
        <if test="form.jinxianMax != null">
            AND t.jinxian <![CDATA[<=]]> #{form.jinxianMax}
        </if>
        ORDER BY t.crt_time DESC
    </select>

    <select id="countTvNum" resultType="int">
        SELECT COUNT(*)
        FROM (
        SELECT max(id)
        FROM
            ad_theatre t
        <where>
            status = 0
            <if test="name != null and name != ''">
                AND t.name = #{name}
            </if>
            <if test="mediaName != null and mediaName != ''">
                AND t.media_name = #{mediaName}
            </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 t.name,t.media_num
        ) a
    </select>

    <select id="countThemeNum" resultType="int">
        SELECT COUNT(*)
        FROM (
            SELECT max(id)
            FROM
            ad_theatre b
            <where>
                status = 0
                <if test="mediaName != null and mediaName != ''">
                    AND b.media_name = #{mediaName}
                </if>
                <if test="firstTheme != null and firstTheme != ''">
                    AND b.first_theme = #{firstTheme}
                </if>
                <if test="timeQuery != null and timeQuery != ''">
                    AND ${timeQuery}
                </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 b.name
        ) a
    </select>

</mapper>
