<?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.goldenwater.slaj.hazard.mapper.ObjHazMapper">
    <resultMap type="com.goldenwater.slaj.hazard.domain.ObjHazVo" id="ObjHazResult">
        <result property="guid" column="guid"/>
        <result property="hazCode" column="haz_code"/>
        <result property="hazName" column="haz_name"/>
        <result property="orgGuid" column="org_guid"/>
        <result property="engGuid" column="eng_guid"/>
        <result property="tendGuid" column="tend_guid"/>
        <result property="tendName" column="tend_name"/>
        <result property="hiddGrad" column="hidd_grad"/>
        <result property="proPart" column="pro_part"/>
        <result property="partLong" column="part_long"/>
        <result property="partLat" column="part_lat"/>
        <result property="harmRisk" column="harm_risk"/>
        <result property="moniPrec" column="moni_prec"/>
        <result property="offiTel" column="offi_tel"/>
        <result property="patPers" column="pat_pers"/>
        <result property="supPers" column="sup_pers"/>
        <result property="ifLiceNoti" column="if_lice_noti"/>
        <result property="note" column="note"/>
        <result property="collTime" column="coll_time"/>
        <result property="updTime" column="upd_time"/>
        <result property="recPers" column="rec_pers"/>
        <result property="supPersDept" column="sup_pers_dept"/>
        <result property="safePrec" column="safe_prec"/>
        <result property="emerPrec" column="emer_prec"/>
        <result property="riskGrad" column="risk_grad"/>
        <result property="monitorMeans" column="monitor_means"/>
        <result property="monitorMeasure" column="monitor_measure"/>
        <result property="riskGuid" column="risk_guid"/>
        <result property="organizingPrec" column="organizing_prec"/>
        <result property="techniquePrec" column="technique_prec"/>
        <result property="systemPrec" column="system_prec"/>
        <result property="toDate" column="to_date"/>
        <result property="otlcolQgwNbokgir" column="otlcol_qgw_nbokgir"/>
        <result property="writCollTime" column="writ_coll_time"/>
        <result property="orgPers" column="org_pers"/>
        <result property="orgpersTel" column="orgpers_tel"/>
        <result property="onsitePers" column="onsite_pers"/>
        <result property="onsitepersTel" column="onsitepers_tel"/>
        <result property="hazStat" column="haz_stat"/>
        <result property="supTime" column="sup_time"/>
        <result property="supOrg" column="sup_org"/>
        <result property="regTime" column="reg_time"/>
        <result property="ifControl" column="if_control"/>
        <result property="major" column="major"/>
        <result property="project" column="project"/>
        <result property="type" column="type"/>
        <result property="regOrgGuid" column="reg_org_guid"/>
        <result property="acciCause" column="acci_cause"/>
        <result property="orgName" column="org_name"/>
        <result property="engName" column="eng_name"/>
    </resultMap>
    <sql id="selectObjHazVo">
        select h.guid, h.haz_code, h.haz_name, h.org_guid, h.eng_guid, h.tend_guid, h.hidd_grad,
               h.pro_part, h.part_long, h.part_lat, h.harm_risk, h.moni_prec, h.offi_tel,
               h.pat_pers, h.sup_pers, h.if_lice_noti, h.note, h.coll_time, h.upd_time, h.rec_pers,
               h.sup_pers_dept, h.safe_prec, h.emer_prec, h.risk_grad, h.monitor_means,
               h.monitor_measure, h.risk_guid, h.organizing_prec, h.technique_prec, h.system_prec,
               h.to_date, h.otlcol_qgw_nbokgir, h.writ_coll_time,
               h.org_pers, h.orgpers_tel, h.onsite_pers, h.onsitepers_tel,
               h.haz_stat, h.sup_time, h.sup_org, h.reg_time,
                h.if_control, h.major, h.project, h.type, h.reg_org_guid,
                h.acci_cause,
               o.org_name,
               e.eng_name,
               t.tend_name
        from obj_haz h
        left join att_org_base o on h.org_guid = o.guid
        left join att_eng_base e on h.eng_guid = e.guid
        left join obj_tend t on h.tend_guid = t.guid
    </sql>
    <select id="selectObjHazByGuid" parameterType="String" resultMap="ObjHazResult">
        <include refid="selectObjHazVo"/>
        where guid = #{guid}
    </select>
    <select id="selectObjHazList" parameterType="com.goldenwater.slaj.hazard.domain.ObjHazVo" resultMap="ObjHazResult">
        <include refid="selectObjHazVo"/>
        <where>
            <if test="hazName != null and hazName != ''">AND haz_name like '%' || #{hazName} || '%'</if>
            <if test="orgGuid != null and orgGuid != ''">AND org_guid = #{orgGuid}</if>
            <if test="engGuid != null and engGuid != ''">AND eng_guid = #{engGuid}</if>
            <if test="hiddGrad != null and hiddGrad != ''">AND hidd_grad = #{hiddGrad}</if>
            <if test="hazStat != null and hazStat != ''">AND haz_stat = #{hazStat}</if>
            <if test="riskGrad != null and riskGrad != ''">AND risk_grad = #{riskGrad}</if>
            <if test="type != null and type != ''">AND type = #{type}</if>
            <if test="guid != null and guid != ''">AND guid = #{guid}</if>
            <if test="orgName != null and orgName != ''">AND o.org_name like '%' || #{orgName} || '%'</if>
            <if test="engName != null and engName != ''">AND e.eng_name like '%' || #{engName} || '%'</if>
            <if test="ifControl != null and ifControl != ''">AND if_control = #{ifControl}</if>
            <if test="beginSupTime != null and beginSupTime != ''">AND sup_time >= #{beginSupTime}</if>
            <if test="endSupTime != null and endSupTime != ''">AND sup_time &lt;= #{endSupTime}</if>
        </where>
    </select>
    <insert id="insertObjHaz" parameterType="com.goldenwater.slaj.hazard.domain.ObjHaz">
        insert into obj_haz (
            guid, haz_code, haz_name, org_guid, eng_guid, tend_guid, hidd_grad,
            pro_part, part_long, part_lat, harm_risk, moni_prec, offi_tel,
            pat_pers, sup_pers, if_lice_noti, note, coll_time, upd_time, rec_pers,
            sup_pers_dept, safe_prec, emer_prec, risk_grad, monitor_means,
            monitor_measure, risk_guid, organizing_prec, technique_prec, system_prec,
            to_date, otlcol_qgw_nbokgir, writ_coll_time,
            org_pers, orgpers_tel, onsite_pers, onsitepers_tel,
            haz_stat, sup_time, sup_org, reg_time,
            if_control, major, project, type, reg_org_guid,
            acci_cause
        ) values (
            #{guid}, #{hazCode}, #{hazName}, #{orgGuid}, #{engGuid}, #{tendGuid}, #{hiddGrad},
            #{proPart}, #{partLong}, #{partLat}, #{harmRisk}, #{moniPrec}, #{offiTel},
            #{patPers}, #{supPers}, #{ifLiceNoti}, #{note}, #{collTime}, #{updTime}, #{recPers},
            #{supPersDept}, #{safePrec}, #{emerPrec}, #{riskGrad}, #{monitorMeans},
            #{monitorMeasure}, #{riskGuid}, #{organizingPrec}, #{techniquePrec}, #{systemPrec},
            #{toDate}, #{otlcolQgwNbokgir}, #{writCollTime},
            #{orgPers}, #{orgpersTel}, #{onsitePers}, #{onsitepersTel},
            #{hazStat}, #{supTime}, #{supOrg}, #{regTime},
            #{ifControl}, #{major}, #{project}, #{type}, #{regOrgGuid},
            #{acciCause}
        )
    </insert>
    <update id="updateObjHaz" parameterType="com.goldenwater.slaj.hazard.domain.ObjHaz">
        update obj_haz
        <set>
            <if test="hazCode != null">haz_code = #{hazCode},</if>
            <if test="hazName != null">haz_name = #{hazName},</if>
            <if test="orgGuid != null">org_guid = #{orgGuid},</if>
            <if test="engGuid != null">eng_guid = #{engGuid},</if>
            <if test="tendGuid != null">tend_guid = #{tendGuid},</if>
            <if test="hiddGrad != null">hidd_grad = #{hiddGrad},</if>
            <if test="proPart != null">pro_part = #{proPart},</if>
            <if test="partLong != null">part_long = #{partLong},</if>
            <if test="partLat != null">part_lat = #{partLat},</if>
            <if test="harmRisk != null">harm_risk = #{harmRisk},</if>
            <if test="moniPrec != null">moni_prec = #{moniPrec},</if>
            <if test="offiTel != null">offi_tel = #{offiTel},</if>
            <if test="patPers != null">pat_pers = #{patPers},</if>
            <if test="supPers != null">sup_pers = #{supPers},</if>
            <if test="ifLiceNoti != null">if_lice_noti = #{ifLiceNoti},</if>
            <if test="note != null">note = #{note},</if>
            <if test="collTime != null">coll_time = #{collTime},</if>
            <if test="updTime != null">upd_time = #{updTime},</if>
            <if test="recPers != null">rec_pers = #{recPers},</if>
            <if test="supPersDept != null">sup_pers_dept = #{supPersDept},</if>
            <if test="safePrec != null">safe_prec = #{safePrec},</if>
            <if test="emerPrec != null">emer_prec = #{emerPrec},</if>
            <if test="riskGrad != null">risk_grad = #{riskGrad},</if>
            <if test="monitorMeans != null">monitor_means = #{monitorMeans},</if>
            <if test="monitorMeasure != null">monitor_measure = #{monitorMeasure},</if>
            <if test="riskGuid != null">risk_guid = #{riskGuid},</if>
            <if test="organizingPrec != null">organizing_prec = #{organizingPrec},</if>
            <if test="techniquePrec != null">technique_prec = #{techniquePrec},</if>
            <if test="systemPrec != null">system_prec = #{systemPrec},</if>
            <if test="toDate != null">to_date = #{toDate},</if>
            <if test="otlcolQgwNbokgir != null">otlcol_qgw_nbokgir = #{otlcolQgwNbokgir},</if>
            <if test="writCollTime != null">writ_coll_time = #{writCollTime},</if>
            <if test="orgPers != null">org_pers = #{orgPers},</if>
            <if test="orgpersTel != null">orgpers_tel = #{orgpersTel},</if>
            <if test="onsitePers != null">onsite_pers = #{onsitePers},</if>
            <if test="onsitepersTel != null">onsitepers_tel = #{onsitepersTel},</if>
            <if test="hazStat != null">haz_stat = #{hazStat},</if>
            <if test="supTime != null">sup_time = #{supTime},</if>
            <if test="supOrg != null">sup_org = #{supOrg},</if>
            <if test="regTime != null">reg_time = #{regTime},</if>
            <if test="ifControl != null">if_control = #{ifControl},</if>
            <if test="major != null">major = #{major},</if>
            <if test="project != null">project = #{project},</if>
            <if test="type != null">type = #{type},</if>
            <if test="regOrgGuid != null">reg_org_guid = #{regOrgGuid},</if>
            <if test="acciCause != null">acci_cause = #{acciCause},</if>
        </set>
        where guid = #{guid}
    </update>
    <delete id="deleteObjHazByGuid" parameterType="String">
        delete from obj_haz where guid = #{guid}
    </delete>
    <delete id="deleteObjHazByGuids" parameterType="String">
        delete from obj_haz where guid in
        <foreach item="guid" collection="array" open="(" separator="," close=")">
            #{guid}
        </foreach>
    </delete>

    <select id="selectHazGroupBy" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
        SELECT
            e.eng_name AS engName,
            o.org_name AS orgName,
            t.tend_name AS tendName,
            count(h.guid) AS totalCount,
            coalesce(sum(case when h.hidd_grad = '1' then 1 else 0 end), 0) AS normalCount,
            coalesce(sum(case when h.hidd_grad = '2' then 1 else 0 end), 0) AS majorCount,
            coalesce(sum(case when h.if_control = 'Y' then 1 else 0 end), 0) AS controlledCount,
            coalesce(sum(case when (h.if_control is null or h.if_control != 'Y') then 1 else 0 end), 0) AS uncontrolledCount
        FROM obj_haz h
        LEFT JOIN obj_eng e ON h.eng_guid = e.guid
        LEFT JOIN att_org_base o ON h.org_guid = o.guid
        LEFT JOIN obj_tend t ON h.tend_guid = t.guid
        <where>
            <if test="hazSort != null and hazSort != ''">
                <choose>
                    <when test="hazSort == '1'">AND h.eng_guid IS NOT NULL</when>
                    <otherwise>AND h.eng_guid IS NULL</otherwise>
                </choose>
            </if>
            <if test="engName != null and engName != ''">AND e.eng_name like '%' || #{engName} || '%'</if>
            <if test="orgName != null and orgName != ''">AND o.org_name like '%' || #{orgName} || '%'</if>
        </where>
        GROUP BY                e.eng_name,
               t.tend_name,
               o.org_name
        ORDER BY totalCount DESC
    </select>
</mapper>
