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

    <sql id="commonWhere">
        left join t_user u on p.user_id = u.id
        left join t_camera c on p.camera_id = c.id
        left join t_camera_detail d on c.id = d.camera_id
        where   p.rec_status = 'A'    AND p.scene_type != 99
        and p.num not in (select num from t_scene_not_display where rec_status= 'A')
        <if test="param.sceneType !=null and param.sceneType !=''">
            and  p.scene_type  = #{param.sceneType}
        </if>
        <if test= 'param.cameraType != null' >
            and d.type = #{param.cameraType}
        </if>
        <if test="param.copyNumSet !=null and param.copyNumSet.size >0">
            and  p.num not in
            <foreach collection="param.copyNumSet" item="num" open="("  separator="," close=")">
                #{num}
            </foreach>
        </if>

        <if test="param.cameraId != null and param.userId !=null and param.cooperationNumList !=null and param.cooperationNumList.size >0">
            and ( p.camera_id = #{param.cameraId} or p.user_id = #{param.userId}  or p.num in
            <foreach collection="param.cooperationNumList" item="coNum" open="(" separator="," close=")">
                #{coNum}
            </foreach>
            )
        </if>
        <if test="param.cameraId != null and param.userId !=null  and param.cooperationNumList !=null and param.cooperationNumList.size ==0 ">
            and  ( p.camera_id = #{param.cameraId} or p.user_id = #{param.userId} )
        </if>
        <if test="param.cameraId != null and param.userId ==null  and param.cooperationNumList !=null and param.cooperationNumList.size ==0 ">
            and   p.camera_id = #{param.cameraId}
        </if>
        <if test="param.cameraId != null and param.userId ==null  and param.cooperationNumList !=null and param.cooperationNumList.size >0 ">
            and (   p.camera_id = #{param.cameraId}  or p.num in
            <foreach collection="param.cooperationNumList" item="coNum" open="(" separator="," close=")">
                #{coNum}
            </foreach>
            )
        </if>
        <if test="param.cameraId == null and param.userId !=null  and param.cooperationNumList !=null and param.cooperationNumList.size ==0 ">
            and   p.user_id = #{param.userId}
        </if>
        <if test="param.cameraId == null and param.userId !=null  and param.cooperationNumList !=null and param.cooperationNumList.size >0 ">
            and (  p.user_id = #{param.userId}  or p.num in
            <foreach collection="param.cooperationNumList" item="coNum" open="(" separator="," close=")">
                #{coNum}
            </foreach>
            )
        </if>


    </sql>


    <select id="pageList" resultType="com.fdkankan.ucenter.vo.response.AppSceneVo">
        SELECT 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,view_count,
        p.camera_id,p.user_id ,p.data_source,p.scene_type,build_type,c.sn_code,c.child_name,4 as location
        FROM t_scene_pro p
        <include refid="commonWhere"></include>
        and is_upgrade = 0 AND ( p.status = 1 OR p.status = -2)
        <if test="param.sceneName !=null and param.sceneName !=''">
            and  p.scene_name like CONCAT('%',#{param.sceneName},'%')
        </if>
        UNION
        SELECT p.id as id ,null as name ,0 as isFolder,null as type,null as parentId,p.create_time ,num,title as scene_name,description as scene_dec,
        scene_status as status,pay_status,thumb,web_site,1 as is_upgrade,view_count,
        p.camera_id,p.user_id,e.data_source,p.scene_type,build_type,c.sn_code,c.child_name,e.location
        FROM t_scene_plus p
        LEFT JOIN t_scene_plus_ext e on p.id = e.plus_id
        <include refid="commonWhere"></include>
        <if test="param.queryVrPano !=null and param.queryVrPano == false">
            and  (e.location  != 7 or e.location is null )
        </if>
        AND ( p.scene_status = 1 OR p.scene_status = -2)
        <if test="param.sceneName !=null and param.sceneName !='' and param.isLaser == false">
            and  p.title like CONCAT('%',#{param.sceneName},'%')
        </if>
        <if test="param.sceneName !=null and param.sceneName !='' and param.isLaser == true">
            and  p.laser_title like CONCAT('%',#{param.sceneName},'%')
        </if>
        <if test="param.orderBy !=null and param.orderBy !=''">
            ORDER BY  ${param.orderBy}
        </if>
        <if test="param.orderBy ==null or param.orderBy ==''">
            ORDER BY   create_time desc
        </if>

    </select>


    <select id="getCountByUserId" resultType="java.lang.Long">
        select count(*) from t_scene_plus s left join t_camera_detail d on s.camera_id = d.camera_id
        where s.rec_status = 'A'
          and s.user_id = #{userId} and d.type = #{cameraType}
    </select>


    <select id="getSpaceSumByCameraId" resultType="java.lang.Long">
        select  sum(space) from  t_scene_plus p left join t_scene_plus_ext e on p.id = e.plus_id where p.rec_status = 'A' and p.scene_status = -2 and p.camera_id = #{cameraId}
    </select>

    <select id="getGroupBySceneSouceV3" resultType="com.fdkankan.ucenter.vo.response.SceneNumVo">
        SELECT scene_source as type,count(1) as sceneNum,s.is_obj from t_scene_pro s
        WHERE s.rec_status = 'A' and s.is_upgrade = 0 and s.user_id = #{userId}
        GROUP BY scene_source,is_obj
    </select>

    <select id="getGroupBySceneSouceV4" resultType="com.fdkankan.ucenter.vo.response.SceneNumVo">
        SELECT scene_source as type,count(1) as sceneNum,e.is_obj from t_scene_plus s LEFT JOIN t_scene_plus_ext e on s.id = e.plus_id
        WHERE s.rec_status = 'A' and s.user_id = #{userId}
        GROUP BY scene_source,is_obj
    </select>

    <select id="getCountByUserIdAndSceneSource" resultType="com.fdkankan.ucenter.entity.ScenePlus">
        select s.* from t_scene_plus s LEFT JOIN t_scene_plus_ext e on s.id = e.plus_id
        WHERE s.rec_status = 'A' and s.user_id = #{userId}
        <if test="sceneSourceList !=null and sceneSourceList.size >0">
            and s.scene_source in
            <foreach collection="sceneSourceList" item="sceneSource" open="(" separator="," close=")">
                #{sceneSource}
            </foreach>
        </if>
        <if test="isObj != null and isObj==1 ">
            and e.is_obj = 1
        </if>
    </select>


    <select id="getCountByAndIsObj" resultType="java.lang.Long">
        SELECT
        (select count(s.id) from t_scene_plus s LEFT JOIN t_scene_plus_ext e on s.id = e.plus_id
        WHERE s.rec_status = 'A' and s.user_id = #{userId}
        <if test="sceneSourceList !=null and sceneSourceList.size >0">
            and s.scene_source in
            <foreach collection="sceneSourceList" item="sceneSource" open="(" separator="," close=")">
                #{sceneSource}
            </foreach>
        </if>
        <if test="isObj != null and isObj==1 and isLaser ==1">
            and e.is_obj = 1
        </if>
        )
        +
        (select count(s.id) from t_scene_pro s
        WHERE s.rec_status = 'A' and s.user_id = #{userId} and is_upgrade = 0
        <if test="sceneSourceList !=null and sceneSourceList.size >0">
            and s.scene_source in
            <foreach collection="sceneSourceList" item="sceneSource" open="(" separator="," close=")">
                #{sceneSource}
            </foreach>
        </if>
        <if test="isObj != null and isObj==1 and isLaser ==1 ">
            and s.is_obj = 1
        </if>
        ) as total
    </select>

    <select id="pageLaserScene" resultType="com.fdkankan.ucenter.vo.RelicsSceneInitQueueDTO">
        select * from t_scene_plus s LEFT JOIN t_scene_plus_ext e on s.id = e.plus_id
        WHERE s.rec_status = 'A' and s.user_id = #{param.userId}
        <if test="param.sceneSource !=null">
            and s.scene_source = #{param.sceneSource}
        </if>
        <if test="param.location !=null">
            and e.location = #{param.location}
        </if>
        <if test="param.title !=null and param.title !=''">
            and  s.title like CONCAT('%',#{param.title},'%')
        </if>
        <if test="param.num !=null and param.num != ''">
            and s.num = #{param.num}
        </if>
        order by s.create_time desc
    </select>
    <select id="getVoByNumList" resultType="com.fdkankan.ucenter.vo.response.SceneVo">
        select * from t_scene_plus s left join  t_scene_plus_ext e on s.id = e.plus_id
        where s.rec_status= 'A'
        and s.num in
        <foreach collection="numList" item="num" open="(" separator="," close=")">
            #{num}
        </foreach>
    </select>
</mapper>
