<?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.cmvr.inspection.mapper.InspectionAlarmMapper">
    
    <resultMap type="InspectionAlarm" id="InspectionAlarmResult">
        <result property="id"    column="id"    />
        <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"    />
        <result property="alarmCode"    column="alarm_code"    />
        <result property="taskInstanceId"    column="task_instance_id"    />
        <result property="resultId"    column="result_id"    />
        <result property="alarmSource"    column="alarm_source"    />
        <result property="itemId"    column="item_id"    />
        <result property="nodeId"    column="node_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="robotId"    column="robot_id"    />
        <result property="robotName"    column="robot_name"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmTitle"    column="alarm_title"    />
        <result property="alarmContent"    column="alarm_content"    />
        <result property="alarmLocation"    column="alarm_location"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="handleStatus"    column="handle_status"    />
        <result property="handler"    column="handler"    />
        <result property="handleTime"    column="handle_time"    />
        <result property="handleRemark"    column="handle_remark"    />
        <result property="evidenceImage"    column="evidence_image"    />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionAlarmVo" id="InspectionAlarmVoResult">
        <result property="id"    column="id"    />
        <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"    />
        <result property="alarmCode"    column="alarm_code"    />
        <result property="taskInstanceId"    column="task_instance_id"    />
        <result property="resultId"    column="result_id"    />
        <result property="alarmSource"    column="alarm_source"    />
        <result property="itemId"    column="item_id"    />
        <result property="nodeId"    column="node_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="robotId"    column="robot_id"    />
        <result property="robotName"    column="robot_name"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmTitle"    column="alarm_title"    />
        <result property="alarmContent"    column="alarm_content"    />
        <result property="alarmLocation"    column="alarm_location"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="handleStatus"    column="handle_status"    />
        <result property="handler"    column="handler"    />
        <result property="handleTime"    column="handle_time"    />
        <result property="handleRemark"    column="handle_remark"    />
        <result property="evidenceImage"    column="evidence_image"    />
        <result property="createByName"    column="create_by_name"    />
        <result property="updateByName"    column="update_by_name"    />
    </resultMap>

    <sql id="selectInspectionAlarmVo">
        select id, create_by, create_time, update_by, update_time, remark, alarm_code,
               task_instance_id, result_id, alarm_source, item_id, node_id,
               task_id, task_name, robot_id, robot_name, alarm_level, alarm_type,
               alarm_title, alarm_content, alarm_location, alarm_time, handle_status,
               handler, handle_time, handle_remark, evidence_image from inspection_alarm
    </sql>

    <select id="selectInspectionAlarmList" parameterType="InspectionAlarm" resultMap="InspectionAlarmResult">
        <include refid="selectInspectionAlarmVo"/>
        <where>  
            <if test="alarmCode != null  and alarmCode != ''"> and alarm_code = #{alarmCode}</if>
            <if test="taskId != null  and taskId != ''"> and task_id = #{taskId}</if>
            <if test="robotId != null  and robotId != ''"> and robot_id = #{robotId}</if>
            <if test="alarmLevel != null  and alarmLevel != ''"> and alarm_level = #{alarmLevel}</if>
            <if test="alarmType != null  and alarmType != ''"> and alarm_type = #{alarmType}</if>
            <if test="handleStatus != null  and handleStatus != ''"> and handle_status = #{handleStatus}</if>
        </where>
    </select>
    
    <select id="selectInspectionAlarmById" parameterType="String" resultMap="InspectionAlarmResult">
        <include refid="selectInspectionAlarmVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionAlarm" parameterType="InspectionAlarm">
        insert into inspection_alarm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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>
            <if test="alarmCode != null">alarm_code,</if>
            <if test="taskInstanceId != null">task_instance_id,</if>
            <if test="resultId != null">result_id,</if>
            <if test="alarmSource != null">alarm_source,</if>
            <if test="itemId != null">item_id,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskName != null">task_name,</if>
            <if test="robotId != null">robot_id,</if>
            <if test="robotName != null">robot_name,</if>
            <if test="alarmLevel != null">alarm_level,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmTitle != null">alarm_title,</if>
            <if test="alarmContent != null">alarm_content,</if>
            <if test="alarmLocation != null">alarm_location,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="handleStatus != null">handle_status,</if>
            <if test="handler != null">handler,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="handleRemark != null">handle_remark,</if>
            <if test="evidenceImage != null">evidence_image,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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>
            <if test="alarmCode != null">#{alarmCode},</if>
            <if test="taskInstanceId != null">#{taskInstanceId},</if>
            <if test="resultId != null">#{resultId},</if>
            <if test="alarmSource != null">#{alarmSource},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="robotId != null">#{robotId},</if>
            <if test="robotName != null">#{robotName},</if>
            <if test="alarmLevel != null">#{alarmLevel},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmTitle != null">#{alarmTitle},</if>
            <if test="alarmContent != null">#{alarmContent},</if>
            <if test="alarmLocation != null">#{alarmLocation},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="handleStatus != null">#{handleStatus},</if>
            <if test="handler != null">#{handler},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="handleRemark != null">#{handleRemark},</if>
            <if test="evidenceImage != null">#{evidenceImage},</if>
         </trim>
    </insert>

    <update id="updateInspectionAlarm" parameterType="InspectionAlarm">
        update inspection_alarm
        <trim prefix="SET" suffixOverrides=",">
            <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>
            <if test="alarmCode != null">alarm_code = #{alarmCode},</if>
            <if test="taskInstanceId != null">task_instance_id = #{taskInstanceId},</if>
            <if test="resultId != null">result_id = #{resultId},</if>
            <if test="alarmSource != null">alarm_source = #{alarmSource},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="nodeId != null">node_id = #{nodeId},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="robotId != null">robot_id = #{robotId},</if>
            <if test="robotName != null">robot_name = #{robotName},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmTitle != null">alarm_title = #{alarmTitle},</if>
            <if test="alarmContent != null">alarm_content = #{alarmContent},</if>
            <if test="alarmLocation != null">alarm_location = #{alarmLocation},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="handleStatus != null">handle_status = #{handleStatus},</if>
            <if test="handler != null">handler = #{handler},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="handleRemark != null">handle_remark = #{handleRemark},</if>
            <if test="evidenceImage != null">evidence_image = #{evidenceImage},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionAlarmById" parameterType="String">
        delete from inspection_alarm where id = #{id}
    </delete>

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

    <select id="selectInspectionAlarmVoList" parameterType="InspectionAlarm" resultMap="InspectionAlarmVoResult">
        select a.id, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
               a.alarm_code, a.task_instance_id, a.result_id, a.alarm_source, a.item_id, a.node_id,
               a.task_id, t.task_name, a.robot_id, r.robot_name,
               a.alarm_level, a.alarm_type, a.alarm_title, a.alarm_content, a.alarm_location,
               a.alarm_time, a.handle_status, a.handler, a.handle_time, a.handle_remark, a.evidence_image,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_alarm a
        left join inspection_task t on a.task_id = t.id
        left join inspection_robot r on a.robot_id = r.id
        left join sys_user u1 on a.create_by = u1.user_name  <!-- 创建人关联用户 -->
        left join sys_user u2 on a.update_by = u2.user_name <!-- 更新人关联用户 -->
        <where>
            <if test="alarmCode != null and alarmCode != ''"> and a.alarm_code = #{alarmCode}</if>
            <if test="taskId != null and taskId != ''"> and a.task_id = #{taskId}</if>
            <if test="robotId != null and robotId != ''"> and a.robot_id = #{robotId}</if>
            <if test="alarmLevel != null and alarmLevel != ''"> and a.alarm_level = #{alarmLevel}</if>
            <if test="alarmType != null and alarmType != ''"> and a.alarm_type = #{alarmType}</if>
            <if test="handleStatus != null and handleStatus != ''"> and a.handle_status = #{handleStatus}</if>
        </where>
        order by a.alarm_time desc
    </select>
</mapper>
