<?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.ruoyi.project.ehs.mapper.TIncidentRecordsMapper">
    
    <resultMap type="TIncidentRecords" id="TIncidentRecordsResult">
        <result property="id"    column="id"    />
        <result property="incidentDate"    column="incident_date"    />
        <result property="reportNo"    column="report_no"    />
        <result property="investigationNo"    column="investigation_no"    />
        <result property="company"    column="company"    />
        <result property="contractor"    column="contractor"    />
        <result property="classification"    column="classification"    />
        <result property="riskKind"    column="risk_kind"    />
        <result property="materials"    column="materials"    />
        <result property="leakedAmount"    column="leaked_amount"    />
        <result property="processSafety"    column="process_safety"    />
        <result property="processSystem"    column="process_system"    />
        <result property="oad"    column="oad"    />
        <result property="bridfing"    column="bridfing"    />
        <result property="actualClassification"    column="actual_classification"    />
        <result property="potential"    column="potential"    />
        <result property="potentialLevel"    column="potential_level"    />
        <result property="measures"    column="measures"    />
        <result property="aims"    column="aims"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="remarks"    column="remarks"    />
        <result property="probability"    column="probability"    />
        <result property="severity"    column="severity"    />
        <result property="riskClass"    column="risk_class"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTIncidentRecordsVo">
        select d.id, d.incident_date, d.report_no, d.investigation_no, d.company, d.contractor, d.classification, d.risk_kind, d.materials, d.leaked_amount, d.process_safety, d.process_system, d.oad, d.bridfing, d.actual_classification, d.potential, d.potential_level, d.measures, d.aims, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id, d.probability, d.severity, d.risk_class ,s.dept_name from t_incident_records d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTIncidentRecordsList" parameterType="TIncidentRecords" resultMap="TIncidentRecordsResult">
        <include refid="selectTIncidentRecordsVo"/>
        <where>  
            <if test="incidentYear != null "> and to_char(incident_date, 'yyyy' ) = #{incidentYear}</if>
            <if test="classification != null  and classification != ''"> and classification = #{classification}</if>
            <if test="riskClass != null  and riskClass != ''"> and risk_class = #{riskClass}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.incident_date desc
    </select>
    
    <select id="selectTIncidentRecordsById" parameterType="Long" resultMap="TIncidentRecordsResult">
        <include refid="selectTIncidentRecordsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTIncidentRecords" parameterType="TIncidentRecords">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_incident_records.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_incident_records
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="incidentDate != null">incident_date,</if>
            <if test="reportNo != null">report_no,</if>
            <if test="investigationNo != null">investigation_no,</if>
            <if test="company != null">company,</if>
            <if test="contractor != null">contractor,</if>
            <if test="classification != null">classification,</if>
            <if test="riskKind != null">risk_kind,</if>
            <if test="materials != null">materials,</if>
            <if test="leakedAmount != null">leaked_amount,</if>
            <if test="processSafety != null">process_safety,</if>
            <if test="processSystem != null">process_system,</if>
            <if test="oad != null">oad,</if>
            <if test="bridfing != null">bridfing,</if>
            <if test="actualClassification != null">actual_classification,</if>
            <if test="potential != null">potential,</if>
            <if test="potentialLevel != null">potential_level,</if>
            <if test="measures != null">measures,</if>
            <if test="aims != null">aims,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="probability != null">probability,</if>
            <if test="severity != null">severity,</if>
            <if test="riskClass != null">risk_class,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="incidentDate != null">#{incidentDate},</if>
            <if test="reportNo != null">#{reportNo},</if>
            <if test="investigationNo != null">#{investigationNo},</if>
            <if test="company != null">#{company},</if>
            <if test="contractor != null">#{contractor},</if>
            <if test="classification != null">#{classification},</if>
            <if test="riskKind != null">#{riskKind},</if>
            <if test="materials != null">#{materials},</if>
            <if test="leakedAmount != null">#{leakedAmount},</if>
            <if test="processSafety != null">#{processSafety},</if>
            <if test="processSystem != null">#{processSystem},</if>
            <if test="oad != null">#{oad},</if>
            <if test="bridfing != null">#{bridfing},</if>
            <if test="actualClassification != null">#{actualClassification},</if>
            <if test="potential != null">#{potential},</if>
            <if test="potentialLevel != null">#{potentialLevel},</if>
            <if test="measures != null">#{measures},</if>
            <if test="aims != null">#{aims},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="probability != null">#{probability},</if>
            <if test="severity != null">#{severity},</if>
            <if test="riskClass != null">#{riskClass},</if>
         </trim>
    </insert>

    <update id="updateTIncidentRecords" parameterType="TIncidentRecords">
        update t_incident_records
        <trim prefix="SET" suffixOverrides=",">
            <if test="incidentDate != null">incident_date = #{incidentDate},</if>
            <if test="reportNo != null">report_no = #{reportNo},</if>
            <if test="investigationNo != null">investigation_no = #{investigationNo},</if>
            <if test="company != null">company = #{company},</if>
            <if test="contractor != null">contractor = #{contractor},</if>
            <if test="classification != null">classification = #{classification},</if>
            <if test="riskKind != null">risk_kind = #{riskKind},</if>
            <if test="materials != null">materials = #{materials},</if>
            <if test="leakedAmount != null">leaked_amount = #{leakedAmount},</if>
            <if test="processSafety != null">process_safety = #{processSafety},</if>
            <if test="processSystem != null">process_system = #{processSystem},</if>
            <if test="oad != null">oad = #{oad},</if>
            <if test="bridfing != null">bridfing = #{bridfing},</if>
            <if test="actualClassification != null">actual_classification = #{actualClassification},</if>
            <if test="potential != null">potential = #{potential},</if>
            <if test="potentialLevel != null">potential_level = #{potentialLevel},</if>
            <if test="measures != null">measures = #{measures},</if>
            <if test="aims != null">aims = #{aims},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="probability != null">probability = #{probability},</if>
            <if test="severity != null">severity = #{severity},</if>
            <if test="riskClass != null">risk_class = #{riskClass},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTIncidentRecordsById" parameterType="Long">
        update t_incident_records set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTIncidentRecordsByIds" parameterType="String">
        update t_incident_records set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
</mapper>