<?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.DevAiProjectDataMapper">
    
    <resultMap type="DevAiProjectData" id="DevAiProjectDataResult">
        <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="deptName"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceAddr0"    column="device_addr0"    />
        <result property="deviceAddr1"    column="device_addr1"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="ipcSerialNum"    column="ipc_serial_num"    />
        <result property="ipcAddr"    column="ipc_addr"    />
        <result property="imageUrl"    column="image_url"    />
        <result property="channelId"    column="channel_id"    />
        <result property="channelName"    column="channel_name"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmTypeName"    column="alarm_type_name"    />
        <result property="alarmId"    column="alarm_id"    />
        <result property="reportRate"    column="report_rate"    />
        <result property="timestamp"    column="timestamp"    />
        <result property="leftTopX"    column="left_top_x"    />
        <result property="leftTopY"    column="left_top_y"    />
        <result property="rightBtmX"    column="right_btm_x"    />
        <result property="rightBtmY"    column="right_btm_y"    />
        <result property="personNum"    column="person_num"    />
        <result property="inNum"    column="in_num"    />
        <result property="outNum"    column="out_num"    />
        <result property="plateNo"    column="plate_no"    />
        <result property="valueType"    column="value_type"    />
        <result property="alarmVideourl"    column="alarm_videoURL"    />
        <result property="alarmVideoName"    column="alarm_video_name"    />
        <result property="compareResult"    column="compare_result"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectDevAiProjectDataVo">
        select dapd.id, dapd.project_id, sp.projectName, dapd.dept_id, sd.dept_name as deptName, dapd.device_id, dapd.device_name, dapd.device_addr0, dapd.device_addr1, dapd.serial_number, dapd.ipc_serial_num, dapd.ipc_addr, dapd.image_url, dapd.channel_id, dapd.channel_name, dapd.alarm_type, sdd.dict_label as alarm_type_name, dapd.alarm_id, dapd.report_rate, dapd.timestamp, dapd.left_top_x, dapd.left_top_y, dapd.right_btm_x, dapd.right_btm_y, dapd.person_num, dapd.in_num, dapd.out_num, dapd.plate_no, dapd.value_type, dapd.alarm_videoURL, dapd.alarm_video_name, dapd.compare_result, dapd.create_time, dapd.is_del from dev_ai_project_data dapd
        left join sur_project sp on sp.id=dapd.project_id
        left join sys_dept sd on sd.dept_id=dapd.dept_id
        left join sys_dict_data sdd on sdd.dict_type='aibox_alarm_type' and sdd.dict_value=dapd.alarm_type
    </sql>

    <select id="selectDevAiProjectDataList" parameterType="DevAiProjectData" resultMap="DevAiProjectDataResult">
        <include refid="selectDevAiProjectDataVo"/>
        <where>
            and dapd.is_del='0'
            <if test="projectId != null "> and dapd.project_id = #{projectId}</if>
            <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null "> and dapd.dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and sd.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deviceName != null  and deviceName != ''"> and dapd.device_name like concat('%', #{deviceName}, '%')</if>
            <if test="alarmType != null "> and dapd.alarm_type = #{alarmType}</if>
            <if test="plateNo != null "> and dapd.plate_no = #{plateNo}</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 spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept} and spui.del_flag=0 )</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'>
                and sp.id in (select spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)
            </if>
            <if test="params.date != null"> and date(dapd.create_time) = date(#{params.date})</if>
        </where>
        order by dapd.create_time desc
    </select>
    
    <select id="selectDevAiProjectDataById" parameterType="Long" resultMap="DevAiProjectDataResult">
        <include refid="selectDevAiProjectDataVo"/>
        where dapd.id = #{id}
    </select>

    <select id="groupCountByAlarmType" parameterType="DevAiProjectData" resultType="map">
        select dapd.alarm_type as type,sdd.dict_label as name, count(1) as value from dev_ai_project_data dapd
        left join sur_project sp on sp.id=dapd.project_id
        left join sys_dept sd on sd.dept_id=dapd.dept_id
        left join sys_dict_data sdd on sdd.dict_type='aibox_alarm_type' and sdd.dict_value=dapd.alarm_type
        <where>
            and dapd.is_del='0'
            <if test="projectId != null "> and dapd.project_id = #{projectId}</if>
            <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null "> and dapd.dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and sd.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deviceName != null  and deviceName != ''"> and dapd.device_name like concat('%', #{deviceName}, '%')</if>
            <if test="alarmType != null "> and dapd.alarm_type = #{alarmType}</if>
            <if test="plateNo != null "> and dapd.plate_no = #{plateNo}</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 spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept} and spui.del_flag=0 )</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'>
                and sp.id in (select spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)
            </if>
            <if test="params.date != null"> and date(dapd.create_time) = date(#{params.date})</if>
        </where>
        GROUP BY dapd.alarm_type
    </select>
        
    <insert id="insertDevAiProjectData" parameterType="DevAiProjectData" useGeneratedKeys="true" keyProperty="id">
        insert into dev_ai_project_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceAddr0 != null">device_addr0,</if>
            <if test="deviceAddr1 != null">device_addr1,</if>
            <if test="serialNumber != null">serial_number,</if>
            <if test="ipcSerialNum != null">ipc_serial_num,</if>
            <if test="ipcAddr != null">ipc_addr,</if>
            <if test="imageUrl != null">image_url,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="channelName != null">channel_name,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmId != null">alarm_id,</if>
            <if test="reportRate != null">report_rate,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="leftTopX != null">left_top_x,</if>
            <if test="leftTopY != null">left_top_y,</if>
            <if test="rightBtmX != null">right_btm_x,</if>
            <if test="rightBtmY != null">right_btm_y,</if>
            <if test="personNum != null">person_num,</if>
            <if test="inNum != null">in_num,</if>
            <if test="outNum != null">out_num,</if>
            <if test="plateNo != null">plate_no,</if>
            <if test="valueType != null">value_type,</if>
            <if test="alarmVideourl != null">alarm_videoURL,</if>
            <if test="alarmVideoName != null">alarm_video_name,</if>
            <if test="compareResult != null">compare_result,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceAddr0 != null">#{deviceAddr0},</if>
            <if test="deviceAddr1 != null">#{deviceAddr1},</if>
            <if test="serialNumber != null">#{serialNumber},</if>
            <if test="ipcSerialNum != null">#{ipcSerialNum},</if>
            <if test="ipcAddr != null">#{ipcAddr},</if>
            <if test="imageUrl != null">#{imageUrl},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="channelName != null">#{channelName},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmId != null">#{alarmId},</if>
            <if test="reportRate != null">#{reportRate},</if>
            <if test="timestamp != null">#{timestamp},</if>
            <if test="leftTopX != null">#{leftTopX},</if>
            <if test="leftTopY != null">#{leftTopY},</if>
            <if test="rightBtmX != null">#{rightBtmX},</if>
            <if test="rightBtmY != null">#{rightBtmY},</if>
            <if test="personNum != null">#{personNum},</if>
            <if test="inNum != null">#{inNum},</if>
            <if test="outNum != null">#{outNum},</if>
            <if test="plateNo != null">#{plateNo},</if>
            <if test="valueType != null">#{valueType},</if>
            <if test="alarmVideourl != null">#{alarmVideourl},</if>
            <if test="alarmVideoName != null">#{alarmVideoName},</if>
            <if test="compareResult != null">#{compareResult},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateDevAiProjectData" parameterType="DevAiProjectData">
        update dev_ai_project_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceAddr0 != null">device_addr0 = #{deviceAddr0},</if>
            <if test="deviceAddr1 != null">device_addr1 = #{deviceAddr1},</if>
            <if test="serialNumber != null">serial_number = #{serialNumber},</if>
            <if test="ipcSerialNum != null">ipc_serial_num = #{ipcSerialNum},</if>
            <if test="ipcAddr != null">ipc_addr = #{ipcAddr},</if>
            <if test="imageUrl != null">image_url = #{imageUrl},</if>
            <if test="channelId != null">channel_id = #{channelId},</if>
            <if test="channelName != null">channel_name = #{channelName},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmId != null">alarm_id = #{alarmId},</if>
            <if test="reportRate != null">report_rate = #{reportRate},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="leftTopX != null">left_top_x = #{leftTopX},</if>
            <if test="leftTopY != null">left_top_y = #{leftTopY},</if>
            <if test="rightBtmX != null">right_btm_x = #{rightBtmX},</if>
            <if test="rightBtmY != null">right_btm_y = #{rightBtmY},</if>
            <if test="personNum != null">person_num = #{personNum},</if>
            <if test="inNum != null">in_num = #{inNum},</if>
            <if test="outNum != null">out_num = #{outNum},</if>
            <if test="plateNo != null">plate_no = #{plateNo},</if>
            <if test="valueType != null">value_type = #{valueType},</if>
            <if test="alarmVideourl != null">alarm_videoURL = #{alarmVideourl},</if>
            <if test="alarmVideoName != null">alarm_video_name = #{alarmVideoName},</if>
            <if test="compareResult != null">compare_result = #{compareResult},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDevAiProjectDataById" parameterType="Long">
        delete from dev_ai_project_data where id = #{id}
    </delete>

    <delete id="deleteDevAiProjectDataByIds" parameterType="String">
        delete from dev_ai_project_data where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>