<?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.fdkankan.scene.mapper.ISceneMapper">

    <sql id="Scene_column">
        id, scene_name, scene_dec, web_site, thumb, scene_status,
        data_source, user_id, pay_status, camera_id, phone_id,
        scene_type, scene_code, scene_key, recommend, view_count,
        version, thumb_status, floor_logo, floor_logo_size, rec_status,
        create_time, update_time, tb_status
    </sql>

    <sql id="SceneExt_column" >
        hots_ids, entry, shoot_count, style,
        gps, scene_scheme, bg_music, screencap_voice_src, screencap_music, screencap_len,
        screencap_voice_sound, screencap_voice_type, play_data, algorithm, oss,floor_edit_ver,floor_publish_ver,space,screencap_thumb
    </sql>


    <select id="findByNum" resultType="com.fdkankan.scene.entity.ScenePO">
        SELECT
        <include refid="Scene_column" />,
        <include refid="SceneExt_column" />
        FROM t_scene t
        left join  t_scene_ext ext on t.id = ext.scene_id
        WHERE t.scene_code = #{sceneCode}
        AND t.rec_status = 'A'
        LIMIT 1
    </select>

    <select id="queryByParam" resultType="com.fdkankan.scene.vo.SceneVO">
        SELECT * FROM (
            SELECT t.id, t.thumb, t.scene_code, t.scene_type, t.scene_name, t.create_time, ext.gps,
            t.scene_key, t.view_count, t.scene_status, ext.scene_scheme, t.web_site, t.rec_status, t.user_id, t.camera_id
            FROM t_scene t
            left join  t_scene_ext ext on t.id = ext.scene_id
            UNION
            SELECT t.id, t.thumb, t.scene_code, t.scene_type, t.scene_name, t.create_time, t.gps,
            'pro' as scene_key, t.view_count, ext.scene_status, t.scene_scheme, t.web_site, t.rec_status, t.user_id, t.camera_id
            FROM t_scene_pro t
            left join t_scene_pro_ext ext on t.id = ext.scene_pro_id
        ) a
         WHERE rec_status = 'A'
        <if test= "type != null">
            and status = #{type}
        </if>
        <if test= "searchKey != null" >
            and (scene_name like CONCAT('%',#{searchKey},'%') or user_id in (${userIds}) or camera_id in (${cameraIds}) )
        </if>
        order by create_time desc
    </select>

    <select id="queryByParamNew" resultType="com.fdkankan.scene.vo.SceneVO">
        SELECT * FROM (
            SELECT t.id, t.thumb, t.scene_code, t.scene_type, t.scene_name, t.create_time, ext.gps,
            t.scene_key, t.view_count, t.scene_status, ext.scene_scheme, t.web_site, t.rec_status, t.user_id, t.camera_id
            FROM t_scene t
            left join  t_scene_ext ext on t.id = ext.scene_id
            UNION
            SELECT t.id, t.thumb, t.num, t.scene_type, t.scene_name, t.create_time, t.gps,
            'pro' as scene_key, t.view_count, ext.scene_status, t.scene_scheme, t.web_site, t.rec_status, t.user_id, t.camera_id
            FROM t_scene_pro t
            left join t_scene_pro_ext ext on t.id = ext.scene_pro_id
         ) a
         WHERE rec_status = 'A'
         <if test= 'type != null'>
           and status = #{type}
         </if>
         <if test= 'cameraIds != null' >
           and camera_id in (${cameraIds})
         </if>
         <if test= 'ownCameraIds != null' >
           and camera_id in (${ownCameraIds})
         </if>
         <if test= 'sceneName != null' >
           and scene_name like CONCAT('%',#{sceneName},'%')
         </if>
         <if test= 'sceneSource != null' >
           and scene_name = #{sceneSource}
         </if>
    </select>

    <select id="findAllByYesterday" resultType="com.fdkankan.scene.entity.ScenePO">
        SELECT
        <include refid="Scene_column" />,
        <include refid="SceneExt_column" />
        FROM t_scene t
        left join  t_scene_ext ext on t.id = ext.scene_id
        WHERE (DATEDIFF(t.create_time,NOW())=-1)
        AND t.scene_code in (1, -2)
        AND pay_status = 0
        AND scene_scheme != 4
        AND rec_status = 'A'
    </select>

    <select id="findAllScene" parameterType="com.fdkankan.scene.vo.SceneParamVO"
            resultType="com.fdkankan.scene.entity.SceneProPO">
        select a.* from (
            SELECT t.id,scene_name, scene_dec, web_site, thumb, scene_type,
            t.create_time, scene_status, user_id, pay_status, camera_id, scene_code, scene_scheme, shoot_count,
            rec_status, SPACE, recommend, view_count, 'V1' AS build_type
            FROM `t_scene` t
            LEFT JOIN  t_scene_ext ext ON t.id = ext.scene_id
            UNION
            SELECT t.id,t.scene_name, t.scene_dec, t.web_site, t.thumb, t.scene_type,
            t.create_time, ext.scene_status, t.user_id, ext.pay_status, t.camera_id, t.scene_code, t.scene_scheme, t.shoot_count,
            t.rec_status, ext.space, ext.recommend, t.view_count, t.build_type
            FROM `t_scene_pro` t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        ) a
        where 1 = 1 and rec_status = 'A' and a.scene_type != 99
        <if test="sceneType != null and sceneType!= ''">
            and a.scene_type = #{sceneType}
        </if>
        <if test="sceneScheme != null">
            <choose>
                <when test="sceneScheme >= 4">
                    and a.scene_scheme >= #{sceneScheme}
                </when>
                <otherwise>
                    and a.scene_scheme <![CDATA[ < ]]> #{sceneScheme}
                </otherwise>
            </choose>
        </if>
        <if test="type ！= null and type == '11'">
            and a.scene_status in (1, -2)
            and a.pay_status = 1
            and a.build_type = 'V3'
        </if>
        <if test="userId != null">
            and a.user_id = #{userId}
        </if>
        <if test="userId == null">
            and a.scene_status = 1
            and a.pay_status = 1
        </if>
        <choose>
            <when test="nums != null and nums.size() > 0">
                <choose>
                    <when test="sceneName != null and sceneName != '' and cameraId != null">
                        and (a.scene_name like CONCAT('%',#{sceneName},'%')
                            or a.scene_code like #{likeNum}
                            or a.camera_id = #{cameraId}
                            or a.scene_code in
                            <foreach collection="numList" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                        )
                    </when>
                    <when test="sceneName != null and sceneName !=''">
                        and (a.scene_name like CONCAT('%',#{sceneName},'%')
                            or a.scene_code like #{likeNum}
                            or a.scene_code in
                            <foreach collection="numList" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                        )
                    </when>
                    <otherwise>
                        and (a.camera_id = #{cameraId}
                            or a.scene_code in
                            <foreach collection="numList" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                        )
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                <choose>
                    <when test="sceneName != null and sceneName != '' and cameraId != null">
                        and (a.scene_name like CONCAT('%',#{sceneName},'%')
                            or a.scene_code like #{likeNum}
                            or a.camera_id = #{cameraId}
                            or a.scene_code in
                        )
                    </when>
                    <when test="sceneName != null and sceneName !=''">
                        and (a.scene_name like CONCAT('%',#{sceneName},'%')
                            or a.scene_code like #{likeNum}
                        )
                    </when>
                    <otherwise>
                        and a.camera_id = #{cameraId}
                    </otherwise>
                </choose>
            </otherwise>
        </choose>
        <if test="startTime != null">
            and a.create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and a.create_time <![CDATA[ <= ]]> #{endDate}
        </if>
        <if test="num != null and num != ''">
            and a.scene_code = #{num}
        </if>
        <if test="order != null and order != ''">
            order by ${order}
        </if>
    </select>

    <select id="unionSearchBySceneName" resultType="com.fdkankan.scene.entity.ScenePO">
        select a.* from (
            SELECT t.id,scene_name, scene_dec, web_site, thumb, scene_type,
            t.create_time, scene_status, user_id, pay_status, camera_id, scene_code, scene_scheme, shoot_count,
            rec_status, SPACE, recommend, view_count, 'V1' AS build_type
            FROM `t_scene` t
            LEFT JOIN  t_scene_ext ext ON t.id = ext.scene_id
            UNION
            SELECT t.id,t.scene_name, t.scene_dec, t.web_site, t.thumb, t.scene_type,
            t.create_time, ext.scene_status, t.user_id, ext.pay_status, t.camera_id, t.scene_code, t.scene_scheme, t.shoot_count,
            t.rec_status, ext.space, ext.recommend, t.view_count, t.build_type
            FROM `t_scene_pro` t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        ) a
        where scene_status = 1 and rec_status = 'A' and pay_status = 1
        <if test="searchKey != null and searchKey!= ''">
            AND scene_name LIKE CONCAT('%',#{searchKey},'%')
        </if>
    </select>
    
    <select id="getOnlySceneList" parameterType="com.fdkankan.scene.vo.SceneParamVO"
            resultType="com.fdkankan.scene.entity.SceneProPO">
        select a.* from (
            SELECT t.id,scene_name, scene_dec, web_site, thumb, scene_type,
            t.create_time, scene_status, user_id, pay_status, camera_id, scene_code, scene_scheme, shoot_count,
            rec_status, SPACE, recommend, view_count, 'V1' AS build_type, '2' AS scene_source
            FROM `t_scene` t
            LEFT JOIN  t_scene_ext ext ON t.id = ext.scene_id
            UNION
            SELECT t.id,t.scene_name, t.scene_dec, t.web_site, t.thumb, t.scene_type,
            t.create_time, ext.scene_status, t.user_id, ext.pay_status, t.camera_id, t.scene_code, t.scene_scheme, t.shoot_count,
            t.rec_status, ext.space, ext.recommend, t.view_count, t.build_type, ext.scene_source
            FROM `t_scene_pro` t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        ) a 
        WHERE a.rec_status = 'A' AND a.scene_source != 11 and a.user_id = #{userId}
        <if test="sceneSource != null"> 
          and a.scene_source = #{sceneSource}
        </if> 
        <if test="sceneType != null"> 
          and a.scene_type = #{sceneType}
        </if> 
        <if test="startTime != null"> 
          and a.create_time &gt;= #{startTime}
        </if> 
        <if test="endTime != null"> 
          and a.create_time &lt;= #{endTime}
        </if> 
        <if test="nums != null"> 
          and a.scene_code in (${nums})
        </if> 
        <if test="calculated  != null and calculated == true"> 
          and (a.status = 1 or a.status = -2)
        </if> 
        <if test="cameraIds != null"> 
          and a.camera_id in (${cameraIds})
        </if> 
        <if test="sceneName != null"> 
          and a.scene_name like CONCAT('%',#{sceneName},'%')
        </if> 
        order by a.create_time desc
        
    </select>

</mapper>
