<?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.ucenter.mapper.ISceneProMapper">

    <select id="findSceneProNumByCameraIds" resultType="com.fdkankan.ucenter.vo.response.GroupByCount">
        SELECT camera_id as id,count(id)  as count,sum(space)  as space  ,max(create_time) as lastTime FROM t_scene_pro
        WHERE rec_status = 'A'  AND is_upgrade = 0
        and camera_id in
        <foreach item="cameraId" collection="cameraIds" open="(" separator="," close=")">
            #{cameraId}
        </foreach>
        GROUP BY camera_id
    </select>
    <select id="findScenePlusNumByCameraIds" resultType="com.fdkankan.ucenter.vo.response.GroupByCount">
        SELECT camera_id as id,count(s.id)  as count ,sum(space)  as space ,max(s.create_time) as lastTime FROM t_scene_plus s left join  t_scene_plus_ext e on s.id = e.plus_id
        WHERE s.rec_status = 'A'
        and camera_id in
        <foreach item="cameraId" collection="cameraIds" open="(" separator="," close=")">
            #{cameraId}
        </foreach>
        GROUP BY camera_id
    </select>

    <select id="pageListAndFolder" resultType="com.fdkankan.ucenter.vo.response.SceneVo">
        <include refid="HasFolder"></include>
        SELECT * FROM (
        SELECT DISTINCT  p.id ,null as name ,0 as isFolder,null as type,null as parentId ,p.create_time,num,scene_name,scene_dec,
        p.status,pay_status,thumb,web_site,0 as is_upgrade,sn_code,view_count,p.build_type, null as removePortrait,p.scene_source,p.laser_title
        FROM t_scene_pro p
        <include refid="sceneJoinCamera"></include>
        WHERE is_upgrade = 0 and  p.rec_status = 'A'
        <include refid="commonWhere"></include>
        <include refid="commonSceneWhere"></include>
        <if test="param.sceneName !=null and param.sceneName !=''">
          and  p.scene_name like CONCAT('%',#{param.sceneName},'%')
        </if>
        <if test="param.isObj !=null and param.isObj ==1 and param.isLaser==1">
          and  p.is_obj  = #{param.isObj}
        </if>
        UNION ALL
        SELECT  DISTINCT  p.id as id ,null as name ,0 as isFolder,null as type,null as parentId,e.algorithm_time as createTime ,num,title as scene_name,description as scene_dec,
        scene_status as status,pay_status,thumb,web_site,1 as is_upgrade,sn_code,view_count,e.build_type,p.remove_portrait as removePortrait,p.scene_source,p.laser_title
        FROM t_scene_plus p
        LEFT JOIN t_scene_plus_ext e on p.id = e.plus_id
        <include refid="sceneJoinCamera"></include>
        WHERE p.rec_status = 'A'
        <include refid="commonWhere"></include>
        <include refid="commonSceneWhere"></include>
        <if test="param.sceneName !=null and param.sceneName !=''">
          and  p.title like CONCAT('%',#{param.sceneName},'%')
        </if>
        <if test="param.isObj !=null and param.isObj ==1 and param.isLaser==1">
            and  e.is_obj  = #{param.isObj}
        </if>
        ORDER BY create_time desc ,id desc
        ) as scene_tb
    </select>
    <select id="getCountByUserId" resultType="java.lang.Long">
           select count(*) from t_scene_pro s left join t_camera_detail d on s.camera_id = d.camera_id
        where s.rec_status = 'A' and s.is_upgrade = 0
        and s.user_id = #{userId} and d.type = #{cameraType}
    </select>

    <sql id="HasFolder">
        <if test="param.hasFolder == 1">
            select * from (
            SELECT id,name,1 as isFolder,type,parent_id as parentId,create_time,null as num,null as scene_name,null as
            scene_dec,
            null as status,null as pay_status,null as thumb,null as web_site,null as is_upgrade,null as sn_code,null as
            view_count, null build_type,null as removePortrait,null as scene_source,null as laserTitle
            FROM t_folder WHERE rec_status = 'A' and user_id =#{param.userId}
            <if test="param.folderType!=null and param.folderType == 0">
                and (type = #{param.folderType} or type = 3)
            </if>
            <if test="param.folderType!=null and param.folderType != 0">
                and type = #{param.folderType}
            </if>

            <if test="param.folderId!=null">
                AND parent_id = #{param.folderId}
            </if>
            <if test="param.folderId == null">
                AND parent_id is null
            </if>
            order by create_time desc
            limit 1000
            )as tb
            UNION ALL
        </if>
    </sql>

    <sql id="sceneJoinCamera">
        LEFT JOIN t_camera c on p.camera_id = c.id
        <if test="param.userName!=null and param.userName!=''">
            LEFT JOIN t_scene_cooperation coo on p.num = coo.scene_num
            LEFT JOIN t_user u on coo.user_id = u.id
        </if>
    </sql>

    <sql id="commonWhere">
        <if test="param.numList == null and param.userId != null ">
           and p.user_id =#{param.userId}
        </if>
        <if test="param.cameraId != null ">
           and p.camera_id =#{param.cameraId}
        </if>
            and p.num not in (select num from t_scene_not_display where rec_status= 'A')
    </sql>

    <sql id="commonSceneWhere">
        <if test="param.sourceList !=null and param.sourceList.size >0">
            AND scene_source in
            <foreach item="sourceId" collection="param.sourceList" open="(" separator="," close=")">
                #{sourceId}
            </foreach>
        </if>
        <if test="param.folderId == null and param.hasFolder == 1 ">
            AND p.id not in(SELECT scene_id FROM t_folder_scene where rec_status = 'A')
        </if>
        <if test="param.folderId != null and param.hasFolder == 1">
            AND p.id in (SELECT scene_id FROM t_folder_scene where rec_status= 'A' and folder_id =#{param.folderId} )
        </if>
        <if test="param.numList !=null and param.numList.size >0">
            and num in
            <foreach item="num" collection="param.numList" open="(" separator="," close=")">
                #{num}
            </foreach>
        </if>
        <if test="param.snCode !=null and param.snCode !=''">
            and c.sn_code like CONCAT('%',#{param.snCode},'%')
        </if>
        <if test="param.userName !=null and param.userName !=''">
            and u.user_name like CONCAT('%',#{param.userName},'%')
            and coo.rec_status = 'A' and coo.scene_type = 'mesh'
        </if>
        <if test="param.startTime !=null and param.startTime !=''">
            and p.create_time &gt;= #{param.startTime}
        </if>
        <if test="param.endTime !=null and param.endTime !=''">
            and p.create_time &lt;= #{param.endTime}
        </if>
        <if test="param.num !=null and param.num !=''">
            and p.num =  #{param.num}
        </if>
    </sql>


    <select id="getSpaceSumByCameraId" resultType="java.lang.Long">
        select sum(space) from t_scene_pro where rec_status= 'A' and status = -2 and is_upgrade = 0 and camera_id = #{cameraId}
    </select>
</mapper>
