<?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.yanzhu.jh.video.mapper.SurProjectVideoConfigMapper">
    
    <resultMap type="SurProjectVideoConfig" id="SurProjectVideoConfigResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="projectName"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="videoName"    column="video_name"    />
        <result property="videoOnlyType"    column="video_only_type"    />
        <result property="videoDvrNumber"    column="video_dvr_number"    />
        <result property="videoDvrSecurity"    column="video_dvr_security"    />
        <result property="videoPassageCount"    column="video_passage_count"    />
        <result property="passagePassCount"    column="passage_pass_count"    />
        <result property="videoSort"    column="video_sort"    />
        <result property="signalType"    column="signal_type"    />
        <result property="signalState"    column="signal_state"    />
        <result property="signalCode"    column="signal_code"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="SurProjectVideoConfigSurProjectVideoPassageResult" type="SurProjectVideoConfig" extends="SurProjectVideoConfigResult">
        <collection property="surProjectVideoPassageList" notNullColumn="sub_id" javaType="java.util.List" resultMap="SurProjectVideoPassageResult" />
    </resultMap>

    <resultMap type="SurProjectVideoPassage" id="SurProjectVideoPassageResult">
        <result property="id"    column="sub_id"    />
        <result property="videoId"    column="sub_video_id"    />
        <result property="videoDvrNumber"    column="sub_video_dvr_number"    />
        <result property="passageName"    column="sub_passage_name"    />
        <result property="passageValue"    column="sub_passage_value"    />
        <result property="passageState"    column="sub_passage_state"    />
        <result property="passageType"    column="sub_passage_type"    />
    </resultMap>

    <sql id="selectSurProjectVideoConfigVo">
        select spvc.id, spvc.project_id, spvc.dept_id, spvc.video_name, spvc.video_only_type, spvc.video_dvr_number, spvc.video_dvr_security, spvc.video_passage_count, spvc.passage_pass_count, spvc.video_sort, spvc.signal_type, spvc.signal_state, spvc.signal_code, spvc.is_del, spvc.create_by, spvc.create_time, spvc.update_by, spvc.update_time, spvc.remark, sp.projectName, sd.dept_name from sur_project_video_config spvc
        left join sur_project sp on sp.id=spvc.project_id
        left join sys_dept sd on sd.dept_id= spvc.dept_id
    </sql>

    <select id="selectSurProjectVideoConfigList" parameterType="SurProjectVideoConfig" resultMap="SurProjectVideoConfigResult">
        <include refid="selectSurProjectVideoConfigVo"/>
        <where>
            and spvc.is_del='0'
            <if test="projectId != null "> and spvc.project_id = #{projectId}</if>
            <if test="projectName != null "> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null "> and spvc.dept_id = #{deptId}</if>
            <if test="deptName != null "> and sd.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="videoName != null "> and spvc.video_name like concat('%', #{videoName}, '%')</if>
            <if test="videoOnlyType != null  and videoOnlyType != ''"> and spvc.video_only_type = #{videoOnlyType}</if>
            <if test="videoDvrNumber != null  and videoDvrNumber != ''"> and spvc.video_dvr_number like concat('%', #{videoDvrNumber}, '%')</if>
            <if test="videoDvrSecurity != null  and videoDvrSecurity != ''"> and spvc.video_dvr_security = #{videoDvrSecurity}</if>
            <if test="signalType != null  and signalType != ''"> and spvc.signal_type = #{signalType}</if>
            <if test="signalState != null  and signalState != ''"> and spvc.signal_state = #{signalState}</if>
            <if test="signalCode != null  and signalCode != ''"> and spvc.signal_code = #{signalCode}</if>
            <if test="isDel != null  and isDel != ''"> and spvc.is_del = #{isDel}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and sp.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and sp.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
        </where>
        order by spvc.video_sort asc, spvc.video_name
    </select>
    
    <select id="selectSurProjectVideoConfigById" parameterType="Long" resultMap="SurProjectVideoConfigSurProjectVideoPassageResult">
        select a.id, a.project_id, p.projectName, a.dept_id, d.dept_name, a.video_name, a.video_only_type, a.video_dvr_number, a.video_dvr_security, a.video_passage_count, a.passage_pass_count, a.video_sort, a.signal_type, a.signal_state, a.signal_code, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark, p.projectName,
        b.id as sub_id, b.video_id as sub_video_id, b.video_dvr_number as sub_video_dvr_number, b.passage_name as sub_passage_name, b.passage_value as sub_passage_value
        from sur_project_video_config a
        left join sur_project p on a.project_id=p.id
        left join sys_dept d on a.dept_id=d.dept_id
        left join sur_project_video_passage b on b.video_id = a.id
        where a.id = #{id}
    </select>

    <select id="selectSurProjectVideoConfigByDvrNumber" parameterType="String" resultMap="SurProjectVideoConfigSurProjectVideoPassageResult">
        select a.id, a.project_id, a.video_name, a.video_only_type, a.video_dvr_number, a.video_dvr_security, a.video_passage_count, a.passage_pass_count, a.video_sort, a.signal_type, a.signal_state, a.signal_code, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark, p.projectName,
        b.id as sub_id, b.video_id as sub_video_id, b.video_dvr_number as sub_video_dvr_number, b.passage_name as sub_passage_name, b.passage_value as sub_passage_value, b.passage_state as sub_passage_state
        from sur_project_video_config a
        left join sur_project p on a.project_id=p.id
        left join sur_project_video_passage b on b.video_id = a.id
        where a.video_dvr_number = #{videoDvrNumber}
    </select>

    <select id="selectSurProjectVideoConfigByParams" parameterType="map" resultMap="SurProjectVideoConfigSurProjectVideoPassageResult">
        select a.id, a.project_id, a.video_name, a.video_only_type, a.video_dvr_number, a.video_dvr_security, a.video_passage_count, a.passage_pass_count, a.video_sort, a.signal_type, a.signal_state, a.signal_code, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark, p.projectName,
        b.id as sub_id, b.video_id as sub_video_id, b.video_dvr_number as sub_video_dvr_number, dacp.channel_name as sub_passage_name, b.passage_value as sub_passage_value, b.passage_state as sub_passage_state
        from sur_project_video_config a
        left join sur_project_video_passage b on b.video_id = a.id
        left join sur_project p on a.project_id=p.id
        left join dev_ai_project_config_passage dacp on dacp.video_passage_id = b.id
        where dacp.is_del=0 and dacp.channel_id is NOT NULL and dacp.channel_id!=''
        <if test="projectId != null "> and a.project_id = #{projectId}</if>
        <if test="deptId != null "> and p.deptId = #{deptId}</if>
        <if test='importance == "Y" '> and dacp.is_importance = #{importance}</if>
        <if test="passageType != null "> and FIND_IN_SET(#{passageType},dacp.passage_type) > 0</if>
        <!-- 查询条件-项目部门 -->
        <if test="projectDeptId != null "> and p.deptId = #{projectDeptId}</if>
        <!--子部门数据-->
        <if test='nowRole == "4"'> and p.deptId = #{nowDept}</if>
        <!--监理单位/总包公司/分包单位查询当前关联数据-->
        <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and p.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and p.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
        order by a.project_id asc,b.id asc
        <if test='importance != "Y" '> limit 6</if>
    </select>

    <select id="selectSurProjectVideoConfigByGroupCountParams" parameterType="map" resultMap="SurProjectVideoConfigSurProjectVideoPassageResult">
        select a.id, a.project_id, a.video_name, a.video_only_type, a.video_dvr_number, a.video_dvr_security, a.video_passage_count, a.passage_pass_count, a.video_sort, a.signal_type, a.signal_state, a.signal_code, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark, p.projectName,
        b.id as sub_id, b.video_id as sub_video_id, b.video_dvr_number as sub_video_dvr_number, dacp.channel_name as sub_passage_name, b.passage_value as sub_passage_value, b.passage_state as sub_passage_state, dacp.passage_type as sub_passage_type
        from sur_project_video_config a
        left join sur_project_video_passage b on b.video_id = a.id
        left join sur_project p on a.project_id=p.id
        left join dev_ai_project_config_passage dacp on dacp.video_passage_id = b.id
        where dacp.is_del=0 and dacp.channel_id is NOT NULL and dacp.channel_id!=''
        <if test="projectId != null "> and a.project_id = #{projectId}</if>
        <if test="deptId != null "> and p.deptId = #{deptId}</if>
        <!-- 查询条件-项目部门 -->
        <if test="projectDeptId != null "> and p.deptId = #{projectDeptId}</if>
        <!--子部门数据-->
        <if test='nowRole == "4"'> and p.deptId = #{nowDept}</if>
        <!--监理单位/总包公司/分包单位查询当前关联数据-->
        <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and p.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and p.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
        order by a.project_id asc,b.id asc
    </select>
        
    <insert id="insertSurProjectVideoConfig" parameterType="SurProjectVideoConfig" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_video_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="videoName != null">video_name,</if>
            <if test="videoOnlyType != null">video_only_type,</if>
            <if test="videoDvrNumber != null">video_dvr_number,</if>
            <if test="videoDvrSecurity != null">video_dvr_security,</if>
            <if test="videoPassageCount != null">video_passage_count,</if>
            <if test="passagePassCount != null">passage_pass_count,</if>
            <if test="videoSort != null">video_sort,</if>
            <if test="signalType != null">signal_type,</if>
            <if test="signalState != null">signal_state,</if>
            <if test="signalCode != null">signal_code,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="videoName != null">#{videoName},</if>
            <if test="videoOnlyType != null">#{videoOnlyType},</if>
            <if test="videoDvrNumber != null">#{videoDvrNumber},</if>
            <if test="videoDvrSecurity != null">#{videoDvrSecurity},</if>
            <if test="videoPassageCount != null">#{videoPassageCount},</if>
            <if test="passagePassCount != null">#{passagePassCount},</if>
            <if test="videoSort != null">#{videoSort},</if>
            <if test="signalType != null">#{signalType},</if>
            <if test="signalState != null">#{signalState},</if>
            <if test="signalCode != null">#{signalCode},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectVideoConfig" parameterType="SurProjectVideoConfig">
        update sur_project_video_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null and projectId>0">project_id = #{projectId},</if>
            <if test="projectId == 0">project_id = NULL,</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="videoName != null">video_name = #{videoName},</if>
            <if test="videoOnlyType != null">video_only_type = #{videoOnlyType},</if>
            <if test="videoDvrNumber != null">video_dvr_number = #{videoDvrNumber},</if>
            <if test="videoDvrSecurity != null">video_dvr_security = #{videoDvrSecurity},</if>
            <if test="videoPassageCount != null">video_passage_count = #{videoPassageCount},</if>
            <if test="passagePassCount != null">passage_pass_count = #{passagePassCount},</if>
            <if test="videoSort != null">video_sort = #{videoSort},</if>
            <if test="signalType != null">signal_type = #{signalType},</if>
            <if test="signalState != null">signal_state = #{signalState},</if>
            <if test="signalCode != null">signal_code = #{signalCode},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSurProjectVideoConfigById" parameterType="Long">
        update sur_project_video_config set is_del=1 where id = #{id}
    </update>

    <update id="deleteSurProjectVideoConfigByIds" parameterType="String">
        update sur_project_video_config set is_del=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
    <delete id="deleteSurProjectVideoPassageByVideoIds" parameterType="String">
        delete from sur_project_video_passage where video_id in 
        <foreach item="videoId" collection="array" open="(" separator="," close=")">
            #{videoId}
        </foreach>
    </delete>

    <delete id="deleteSurProjectVideoPassageByVideoId" parameterType="Long">
        delete from sur_project_video_passage where video_id = #{videoId}
    </delete>

    <insert id="batchSurProjectVideoPassage">
        insert into sur_project_video_passage( id, video_id, video_dvr_number, passage_name, passage_value,passage_state) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.videoId}, #{item.videoDvrNumber}, #{item.passageName}, #{item.passageValue}, #{item.passageState})
        </foreach>
    </insert>

    <update id="updateProjectVideoPassageState" parameterType="SurProjectVideoPassage">
        update sur_project_video_passage set passage_state=#{passageState} where id = #{id}
    </update>

</mapper>