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

    <resultMap id="SceneProResultMap" type="com.fdkankan.scene.entity.ScenePro" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="scene_name" property="sceneName" jdbcType="BIGINT" />
        <result column="scene_dec" property="sceneDec" jdbcType="VARCHAR" />
        <result column="web_site" property="webSite" jdbcType="VARCHAR" />
        <result column="thumb" property="thumb" jdbcType="VARCHAR" />
        <result column="user_id" property="userId" jdbcType="BIGINT" />
        <result column="camera_id" property="cameraId" jdbcType="BIGINT" />
        <result column="scene_logo" property="sceneLogo" jdbcType="VARCHAR" />
        <result column="scene_code" property="sceneCode" jdbcType="VARCHAR" />
        <result column="scene_type" property="sceneType" jdbcType="TINYINT" />
        <result column="view_count" property="viewCount" jdbcType="INTEGER" />
        <result column="shoot_count" property="shootCount" jdbcType="INTEGER" />
        <result column="videos" property="videos" jdbcType="VARCHAR" />
        <result column="gps" property="gps" jdbcType="VARCHAR" />
        <result column="scene_scheme" property="sceneScheme" jdbcType="TINYINT" />
        <result column="rec_status" property="recStatus" jdbcType="VARCHAR" />
        <result column="build_type" property="buildType" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
        <result column="tb_status" property="tbStatus" jdbcType="TINYINT" />
    </resultMap>

    <sql id="ScenePro_column">
        t.id, t.scene_name, t.scene_dec, t.web_site, t.thumb, t.user_id,
        t.camera_id, t.scene_logo, t.scene_code, t.scene_type, t.view_count,
        t.shoot_count, t.videos, t.gps, t.scene_scheme, t.rec_status, t.build_type,
        t.create_time, t.update_time, t.tb_status
    </sql>

    <resultMap id="SceneProExtResultMap" type="com.fdkankan.scene.entity.SceneProExt" >
        <result column="scene_pro_id" property="sceneProId" jdbcType="BIGINT" />
        <result column="scene_status" property="sceneStatus" jdbcType="INTEGER" />
        <result column="data_source" property="dataSource" jdbcType="VARCHAR" />
        <result column="pay_status" property="payStatus" jdbcType="TINYINT" />
        <result column="phone_id" property="phoneId" jdbcType="VARCHAR" />
        <result column="recommend" property="recommend" jdbcType="TINYINT" />
        <result column="files_name" property="filesName" jdbcType="VARCHAR" />
        <result column="algorithm" property="algorithm" jdbcType="VARCHAR" />
        <result column="ecs" property="ecs" jdbcType="VARCHAR" />
        <result column="space" property="space" jdbcType="BIGINT" />
        <result column="firmware_version" property="firmwareVersion" jdbcType="VARCHAR" />
        <result column="compute_time" property="computeTime" jdbcType="BIGINT" />
        <result column="scene_source" property="sceneSource" jdbcType="INTEGER" />
        <result column="vrnum" property="vrnum" jdbcType="VARCHAR" />
        <result column="unicode" property="unicode" jdbcType="VARCHAR" />
    </resultMap>

    <sql id="SceneProExt_column" >
        ext.scene_pro_id, ext.scene_status, ext.data_source, ext.pay_status,
        ext.phone_id, ext.recommend, ext.files_name, ext.algorithm, ext.ecs, ext.space,
        ext.firmware_version, ext.compute_time, ext.scene_source, ext.vrnum, ext.unicode
    </sql>

    <select id="findFolderIdScence">
        SELECT count(id)
        FROM t_scene_pro
        WHERE id in (SELECT scene_id from t_folder_scene WHERE folder_id = #{folderId})
        and user_id = #{userId}
    </select>

    <select id="findLikeNum" resultType="com.fdkankan.scene.entity.ScenePro">
        SELECT * FROM t_scene_pro
        WHERE scene_code like CONCAT(#{sceneCode}, '%')
        AND rec_status = 'A'
        ORDER BY id desc
        limit 1
    </select>

    <select id="findByNum" resultType="com.fdkankan.scene.entity.ScenePro">
        SELECT * FROM t_scene_pro WHERE scene_code = #{sceneCode} AND rec_status = 'A' LIMIT 1
    </select>

    <select id="getSceneStatusByUnicode" resultType="com.fdkankan.scene.entity.SceneProPO">
        select
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        from t_scene_pro t
        left join t_scene_pro_ext ext on t.id = ext.scene_pro_id
        WHERE ext.data_source LIKE CONCAT('%',#{unicode},'%') AND t.rec_status = #{recStatus}
        order by t.create_time desc
        LIMIT 1
    </select>

    <select id="findByUserIdAndCameraType" resultType="com.fdkankan.scene.entity.SceneProPO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE t.user_id = #{userId}
        AND ext.camera_type = #{cameraType}
        AND t.rec_status = 'A'
        AND ext.scene_status IN (1,-2)
        AND t.scene_type != 99
        ORDER BY t.create_time DESC
    </select>

    <select id="findByUserId" resultType="com.fdkankan.scene.entity.SceneProPO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE t.user_id = #{userId}
        AND t.rec_status = 'A'
        AND ext.scene_status in (1, -2)
        AND t.scene_type != 99
        AND ext.scene_source = 1
        <if test= 'excludeNums != null and excludeNums.size > 0'>
            and t.scene_code not in
            <foreach item='num' collection='excludeNums' open='(' separator=',' close=')'>
                #{num}
            </foreach>
        </if>
        order by t.id desc
    </select>

    <select id="findBySceneNums" resultType="com.fdkankan.scene.entity.SceneProPO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE ext.camera_type = #{cameraType}
        AND t.rec_status = 'A'
        AND ext.scene_status IN (1,-2)
        AND t.scene_type != 99
        <if test= 'sceneCodeList != null and sceneCodeList.size > 0'>
            and t.scene_code in
            <foreach item='sceneCode' collection='sceneCodeList' open='(' separator=',' close=')'>
                #{sceneCode}
            </foreach>
        </if>
        ORDER BY t.create_time DESC
    </select>

    <select id="findLatestOneByUserId" resultType="com.fdkankan.scene.vo.SceneVO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE t.user_id = #{userId}
        AND t.rec_status = 'A'
        AND t.camera_id is not null
        ORDER BY t.create_time DESC
        LIMIT 1
    </select>

    <select id="findByCameraIdPro" resultType="com.fdkankan.scene.entity.SceneProPO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE t.camera_id = #{cameraId}
        AND t.rec_status = 'A'
    </select>

    <select id="findByFileId" resultType="com.fdkankan.scene.entity.SceneProPO">
        SELECT
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE ext.data_source LIKE CONCAT('%',#{fileId},'%')
        AND t.rec_status = 'A'
        order by t.create_time desc
        LIMIT 1
    </select>

    <select id="findTempScenes" resultType="com.fdkankan.scene.entity.ScenePro">
        SELECT
        t.id, t.scene_code
        FROM t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        WHERE t.rec_status = 'A'
        and ext.pay_status = -2
        and (DATEDIFF(t.update_time,NOW()) <![CDATA[ <= ]]> -365)
    </select>

    <select id="getAppAllSceneByPage" resultType="com.fdkankan.scene.vo.SceneVO">
        SELECT * FROM (
        <if test= 'userId != null'>
            SELECT
            <include refid="ScenePro_column"/>,
            <include refid="SceneProExt_column"/>
            FROM t_scene_pro t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
            WHERE t.user_id = #{userId} AND ext.camera_type = #{cameraType}
            AND t.rec_status = 'A' AND ext.scene_status in (1, -2) AND t.scene_type != 99
        </if>
        <if test= 'userId != null and cameraId != null'>
            UNION
            SELECT
            <include refid="ScenePro_column"/>,
            <include refid="SceneProExt_column"/>
            FROM t_scene_pro t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
            WHERE t.camera_id = #{cameraId}
            AND ext.scene_status in (1, -2) AND t.scene_type != 99 AND t.rec_status = 'A'
        </if>
        <if test= 'sceneCodeList != null and sceneCodeList.size() > 0'>
            UNION
            SELECT
            <include refid="ScenePro_column"/>,
            <include refid="SceneProExt_column"/>
            FROM t_scene_pro t
            LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
            WHERE ext.scene_status in (1, -2)
            AND  t.scene_code IN
            <foreach collection="sceneCodeList" item="sceneCode" open="(" close=")" separator=",">
                #{sceneCode}
            </foreach>
        </if>
         ) a WHERE rec_status = 'A'
        <if test= 'sceneType != null'>
           AND scene_type = #{sceneType}
        </if>
        <if test= 'sceneName != null'>
           AND scene_name like CONCAT('%', #{sceneName}, '%')
        </if>
        <if test="orderBy != null and orderBy != ''">
            ${orderBy}
        </if>
    </select>

    <select id="findLastSceneByCameraId" resultType="com.fdkankan.scene.entity.SceneProPO">
        select
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        from t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        where t.rec_status = 'A' and t.camera_id = #{cameraId}
        order by t.create_time desc
        limit 1
    </select>

    <select id="findByNumWithOutCheckRecStatus" resultType="com.fdkankan.scene.entity.SceneProPO">
        select
        <include refid="ScenePro_column"/>,
        <include refid="SceneProExt_column"/>
        from t_scene_pro t
        LEFT JOIN t_scene_pro_ext ext ON t.id = ext.scene_pro_id
        where t.scene_code = #{sceneCode}
        limit 1
    </select>



</mapper>
