<?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.zcxk.dao.CameraManageMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.CameraManage" id="CameraManageMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="tenantId" column="TENANT_ID" jdbcType="VARCHAR"/>
        <result property="companyOrgId" column="COMPANY_ORG_ID" jdbcType="INTEGER"/>
        <result property="deptOrgId" column="DEPT_ORG_ID" jdbcType="INTEGER"/>
        <result property="sceneId" column="SCENE_ID" jdbcType="INTEGER"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="geo" column="geo" jdbcType="VARCHAR"/>
        <result property="palyUrl" column="PALY_URL" jdbcType="VARCHAR"/>
        <result property="replayUrl" column="REPLAY_URL" jdbcType="VARCHAR"/>
        <result property="appkey" column="APPKEY" jdbcType="VARCHAR"/>
        <result property="scrert" column="SCRERT" jdbcType="VARCHAR"/>
        <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="DATE_UPDATE" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
        <result property="dateCreate" column="DATE_CREATE" jdbcType="TIMESTAMP"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="deviceSerial" column="device_serial" jdbcType="VARCHAR"/>
        <result property="channelNo" column="channel_no" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="sceneName" column="SCENE_NAME" jdbcType="VARCHAR"/>
        <result property="imagePath" column="IMAGE_PATH" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        a.ID, a.TENANT_ID, a.COMPANY_ORG_ID, a.DEPT_ORG_ID, SCENE_ID, a.address, geo, PALY_URL, REPLAY_URL, APPKEY, SCRERT, a.CREATE_BY, a.DATE_UPDATE, a.UPDATE_BY, a.DATE_CREATE, a.name, device_serial, channel_no    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="CameraManageMap">
        select
        <include refid="Base_Column_List"/>,b.SCENE_NAME
        from sms_camera_manage a join sms_scene b on a.SCENE_ID=b.ID
        where a.ID = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="CameraManageMap">
        select
        <include refid="Base_Column_List"/>
        from sms_camera_manage
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="CameraManageMap">
        select
        <include refid="Base_Column_List"/>
        from sms_camera_manage a
        <where>
            a.status=1
            <if test="permissonType == 5 or permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="permissonType == 4 or permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and TENANT_ID = #{tenantId}
            </if>
            <if test="companyOrgId != null">
                and COMPANY_ORG_ID = #{companyOrgId}
            </if>
            <if test="deptOrgId != null">
                and DEPT_ORG_ID = #{deptOrgId}
            </if>
            <if test="sceneId != null">
                and SCENE_ID = #{sceneId}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="geo != null and geo != ''">
                and geo = #{geo}
            </if>
            <if test="palyUrl != null and palyUrl != ''">
                and PALY_URL = #{palyUrl}
            </if>
            <if test="replayUrl != null and replayUrl != ''">
                and REPLAY_URL = #{replayUrl}
            </if>
            <if test="appkey != null and appkey != ''">
                and APPKEY = #{appkey}
            </if>
            <if test="scrert != null and scrert != ''">
                and SCRERT = #{scrert}
            </if>
            <if test="createBy != null and createBy != ''">
                and CREATE_BY = #{createBy}
            </if>
            <if test="dateUpdate != null">
                and DATE_UPDATE = #{dateUpdate}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and UPDATE_BY = #{updateBy}
            </if>
            <if test="dateCreate != null">
                and DATE_CREATE = #{dateCreate}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="deviceSerial != null and deviceSerial != ''">
                and device_serial = #{deviceSerial}
            </if>
            <if test="channelNo != null">
                and channel_no = #{channelNo}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into sms_camera_manage(ID, TENANT_ID, COMPANY_ORG_ID, DEPT_ORG_ID, SCENE_ID, address, geo, PALY_URL,
                                      REPLAY_URL, APPKEY, SCRERT, CREATE_BY, DATE_UPDATE, UPDATE_BY, DATE_CREATE, name,
                                      device_serial, channel_no, status)
        values (#{id}, #{tenantId}, #{companyOrgId}, #{deptOrgId}, #{sceneId}, #{address}, #{geo}, #{palyUrl},
                #{replayUrl}, #{appkey}, #{scrert}, #{createBy}, #{dateUpdate}, #{updateBy}, #{dateCreate}, #{name},
                #{deviceSerial}, #{channelNo}, #{status})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into sms_camera_manage(ID, TENANT_ID, COMPANY_ORG_ID, DEPT_ORG_ID, SCENE_ID, address, geo, PALY_URL,
        REPLAY_URL, APPKEY, SCRERT, CREATE_BY, DATE_UPDATE, UPDATE_BY, DATE_CREATE, name, device_serial, channel_no,
        status)
        values
        <foreach collection="cameraManages" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantId}, #{item.companyOrgId}, #{item.deptOrgId}, #{item.sceneId}, #{item.address},
            #{item.geo}, #{item.palyUrl}, #{item.replayUrl}, #{item.appkey}, #{item.scrert}, #{item.createBy},
            #{item.dateUpdate}, #{item.updateBy}, #{item.dateCreate}, #{item.name}, #{item.deviceSerial},
            #{item.channelNo}, #{item.status} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update sms_camera_manage
        <set>
            <if test="tenantId != null and tenantId != ''">
                TENANT_ID = #{tenantId},
            </if>
            <if test="companyOrgId != null">
                COMPANY_ORG_ID = #{companyOrgId},
            </if>
            <if test="deptOrgId != null">
                DEPT_ORG_ID = #{deptOrgId},
            </if>
            <if test="deptOrgId == null">
                DEPT_ORG_ID =null,
            </if>
            <if test="sceneId != null">
                SCENE_ID = #{sceneId},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="geo != null and geo != ''">
                geo = #{geo},
            </if>
            <if test="palyUrl != null and palyUrl != ''">
                PALY_URL = #{palyUrl},
            </if>
            <if test="replayUrl != null ">
                REPLAY_URL = #{replayUrl},
            </if>
            <if test="appkey != null and appkey != ''">
                APPKEY = #{appkey},
            </if>
            <if test="scrert != null and scrert != ''">
                SCRERT = #{scrert},
            </if>
            <if test="createBy != null and createBy != ''">
                CREATE_BY = #{createBy},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY = #{updateBy},
            </if>
            <if test="dateCreate != null">
                DATE_CREATE = #{dateCreate},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="deviceSerial != null and deviceSerial != ''">
                device_serial = #{deviceSerial},
            </if>
            <if test="channelNo != null">
                channel_no = #{channelNo},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from sms_camera_manage
        where ID = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from sms_camera_manage
    </select>

    <select id="appSelectPage" resultMap="CameraManageMap">
        select
         a.ID, a.TENANT_ID, a.COMPANY_ORG_ID, a.DEPT_ORG_ID, a.SCENE_ID, a.address, geo, PALY_URL, REPLAY_URL, APPKEY, SCRERT, a.CREATE_BY, a.DATE_UPDATE,
         a.UPDATE_BY, a.DATE_CREATE, a.name, device_serial, channel_no, b.SCENE_NAME, c.image_path
        from sms_camera_manage a join sms_scene b on a.SCENE_ID=b.ID
        left join sms_scene_image c on c.SCENE_ID=a.SCENE_ID and c.image_type=3 and c.STATUS =1
        <where>
            a.status=1
            <if test="cameraManage.permissonType == 5 or cameraManage.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="cameraManage.permissonType == 4 or cameraManage.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="cameraManage.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
            <if test="cameraManage.tenantId != null and cameraManage.tenantId != ''">
                and a.TENANT_ID = #{cameraManage.tenantId}
            </if>
            <if test="cameraManage.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{cameraManage.companyOrgId}
            </if>
            <if test="cameraManage.deptOrgId != null">
                and a.DEPT_ORG_ID = #{cameraManage.deptOrgId}
            </if>
            <if test="cameraManage.name != null and cameraManage.name != ''">
                and name LIKE concat('%',#{cameraManage.name},'%')
            </if>
            <if test="cameraManage.parentSceneId != null">
                and FIND_IN_SET( #{cameraManage.parentSceneId},b.PARENT_SCENE_IDS)
            </if>
        </where>
    </select>


    <select id="selectPage" resultMap="CameraManageMap">
        select
        <include refid="Base_Column_List"/> ,b.SCENE_NAME
        from sms_camera_manage a join sms_scene b on a.SCENE_ID=b.ID
        <where>
            a.status=1

            <if test="cameraManage.permissonType == 5 or cameraManage.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="cameraManage.permissonType == 4 or cameraManage.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="cameraManage.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="cameraManage.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
            <if test="cameraManage.id != null">
                and ID = #{cameraManage.id}
            </if>
            <if test="cameraManage.tenantId != null and cameraManage.tenantId != ''">
                and a.TENANT_ID = #{cameraManage.tenantId}
            </if>
            <if test="cameraManage.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{cameraManage.companyOrgId}
            </if>
            <if test="cameraManage.deptOrgId != null">
                and a.DEPT_ORG_ID = #{cameraManage.deptOrgId}
            </if>
            <if test="cameraManage.sceneId != null">
                and SCENE_ID = #{cameraManage.sceneId}
            </if>
            <if test="cameraManage.address != null and cameraManage.address != ''">
                and a.address = #{cameraManage.address}
            </if>
            <if test="cameraManage.geo != null and cameraManage.geo != ''">
                and geo = #{cameraManage.geo}
            </if>
            <if test="cameraManage.palyUrl != null and cameraManage.palyUrl != ''">
                and PALY_URL = #{cameraManage.palyUrl}
            </if>
            <if test="cameraManage.replayUrl != null and cameraManage.replayUrl != ''">
                and REPLAY_URL = #{cameraManage.replayUrl}
            </if>
            <if test="cameraManage.appkey != null and cameraManage.appkey != ''">
                and APPKEY = #{cameraManage.appkey}
            </if>
            <if test="cameraManage.scrert != null and cameraManage.scrert != ''">
                and SCRERT = #{cameraManage.scrert}
            </if>
            <if test="cameraManage.createBy != null and cameraManage.createBy != ''">
                and a.CREATE_BY = #{cameraManage.createBy}
            </if>
            <if test="cameraManage.dateUpdate != null">
                and DATE_UPDATE = #{cameraManage.dateUpdate}
            </if>
            <if test="cameraManage.updateBy != null and cameraManage.updateBy != ''">
                and UPDATE_BY = #{cameraManage.updateBy}
            </if>
            <if test="cameraManage.dateCreate != null">
                and DATE_CREATE = #{cameraManage.dateCreate}
            </if>
            <if test="cameraManage.name != null and cameraManage.name != ''">
                and name LIKE concat('%',#{cameraManage.name},'%')
            </if>
            <if test="cameraManage.deviceSerial != null and cameraManage.deviceSerial != ''">
                and device_serial = #{cameraManage.deviceSerial}
            </if>
            <if test="cameraManage.channelNo != null">
                and channel_no = #{cameraManage.channelNo}
            </if>
            <if test="cameraManage.status != null">
                and status = #{cameraManage.status}
            </if>
        </where>
    </select>
    <select id="getAllCameras" resultType="com.zcxk.entity.CameraManage">
        SELECT
            *
        FROM
            sms_camera_manage
        WHERE
            status=1 and    SCENE_ID IN (
                SELECT DATA
                           .id
                FROM
                    (
                        SELECT
                            @ids AS _ids,
                            ( SELECT @ids := GROUP_CONCAT( id ) FROM sms_scene WHERE
                        FIND_IN_SET( PARENT_SCENE_ID, @ids ) AND STATUS = 1 ) AS cids,
                            @l := @l + 1 AS LEVEL
                        FROM
                            sms_scene,
                            ( SELECT @ids := #{id}, @l := 0 ) b
                        WHERE
                            @ids IS NOT NULL
                    ) id,
                    sms_scene DATA
                WHERE
                    FIND_IN_SET( DATA.id, ID._ids )
            )
    </select>
    <select id="selectAllCameras" resultType="com.zcxk.entity.CameraManage">
        select
        <include refid="Base_Column_List"/> ,b.SCENE_NAME,b.PARENT_SCENE_IDS
        from sms_camera_manage a join sms_scene b on a.SCENE_ID=b.ID
        where a.status=1  and a.TENANT_ID = #{tenantId}
    </select>

    <update id="deleteAll">
        update sms_camera_manage set STATUS=0 where STATUS=1 and ID in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

</mapper>