<?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.hidd.mapper.ObjHiddMapper">
    <resultMap type="com.goldenwater.slaj.hidd.domain.ObjHiddVo" id="ObjHiddResult">
        <result property="guid" column="guid"/>
        <result property="hiddName" column="hidd_name"/>
        <result property="engGuid" column="eng_guid"/>
        <result property="tendGuid" column="tend_guid"/>
        <result property="tendName" column="tend_name"/>
        <result property="seGuid" column="se_guid"/>
        <result property="orgGuid" column="org_guid"/>
        <result property="hiddSour" column="hidd_sour"/>
        <result property="hiddGrad" column="hidd_grad"/>
        <result property="hiddClas" column="hidd_clas"/>
        <result property="ifFound" column="if_found"/>
        <result property="proPart" column="pro_part"/>
        <result property="partLong" column="part_long"/>
        <result property="partLat" column="part_lat"/>
        <result property="otlcolQiffFnvu" column="otlcol_qiff_fnvu"/>
        <result property="inspRecGuid" column="insp_rec_guid"/>
        <result property="seCheckItemGuid" column="se_check_item_guid"/>
        <result property="hiddStat" column="hidd_stat"/>
        <result property="hiddMergGuid" column="hidd_merg_guid"/>
        <result property="recOrgGuid" column="rec_org_guid"/>
        <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="hiddsGuid" column="hidds_guid"/>
        <result property="occuNum" column="occu_num"/>
        <result property="sinsScheGuid" column="sins_sche_guid"/>
        <result property="groupGuid" column="group_guid"/>
        <result property="temp" column="temp"/>
        <result property="toDate" column="to_date"/>
        <result property="dictGuid" column="dict_guid"/>
        <result property="hiddSort" column="hidd_sort"/>
        <result property="hazGuid" column="haz_guid"/>
        <result property="engName" column="eng_name"/>
        <result property="orgName" column="org_name"/>
        <result property="inspDate" column="insp_date"/>
        <result property="requCompDate" column="requ_comp_date"/>
        <result property="supStat" column="sup_stat"/>
        <result property="cancelState" column="cancel_state"/>
    </resultMap>
    <sql id="selectObjHiddVo">
        select h.guid, h.hidd_name, h.eng_guid, h.tend_guid, h.se_guid, h.org_guid, h.hidd_sour, h.hidd_grad, h.hidd_clas, h.if_found, h.pro_part, h.part_long, h.part_lat, h.otlcol_qiff_fnvu, h.insp_rec_guid, h.se_check_item_guid, h.hidd_stat, h.hidd_merg_guid, h.rec_org_guid, h.note, h.coll_time, h.upd_time, h.rec_pers, h.hidds_guid, h.occu_num, h.sins_sche_guid, h.group_guid, h.temp, h.to_date, h.dict_guid, h.hidd_sort, h.haz_guid,
               e.eng_name,
               o.org_name,
               bi.insp_date,
               ri.requ_comp_date,
               t.tend_name,
               COALESCE(CASE WHEN ms.hidd_guid IS NOT NULL THEN '1' ELSE '0' END, '0') as sup_stat,
               COALESCE(ra.cancel_state, '0') as cancel_state
        from obj_hidd h
        left join att_eng_base e on h.eng_guid = e.guid
        left join att_org_base o on h.org_guid = o.guid
        left join bis_hidd_inve bi on h.insp_rec_guid = bi.guid
        left join bis_hidd_rect_impl ri on h.guid = ri.hidd_guid
        left join bis_hidd_rect_acce ra on h.guid = ra.hidd_guid
        left join bis_maj_hidd_sup ms on h.guid = ms.hidd_guid
        left join obj_tend t on h.tend_guid = t.guid
    </sql>
    <select id="selectObjHiddByGuid" parameterType="String" resultMap="ObjHiddResult">
        <include refid="selectObjHiddVo"/>
        where h.guid = #{guid}
    </select>
    <select id="selectObjHiddList" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd" resultMap="ObjHiddResult">
        <include refid="selectObjHiddVo"/>
        <where>
            <if test="hiddName != null and hiddName != ''">AND h.hidd_name like concat('%', #{hiddName}, '%')</if>
            <if test="hiddGrad != null and hiddGrad != ''">AND h.hidd_grad = #{hiddGrad}</if>
            <if test="hiddStat != null and hiddStat != ''">AND h.hidd_stat = #{hiddStat}</if>
            <if test="orgGuid != null and orgGuid != ''">AND h.org_guid = #{orgGuid}</if>
            <if test="engGuid != null and engGuid != ''">AND h.eng_guid = #{engGuid}</if>
            <if test="hazGuid != null and hazGuid != ''">AND h.haz_guid = #{hazGuid}</if>
            <if test="guid != null and guid != ''">AND h.guid = #{guid}</if>
            <if test="hiddSort != null and hiddSort != ''">AND h.hidd_sort = #{hiddSort}</if>
            <if test="hiddSour != null and hiddSour != ''">AND h.hidd_sour = #{hiddSour}</if>
            <if test="orgName != null and orgName != ''">AND o.org_name like concat('%', #{orgName}, '%')</if>
            <if test="wiunProp != null and wiunProp != ''">AND EXISTS (SELECT 1 FROM att_org_ext ext WHERE ext.org_guid = h.org_guid AND ext.wiun_prop = #{wiunProp})</if>
        </where>
    </select>
    <insert id="insertObjHidd" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd">
        insert into obj_hidd (guid, hidd_name, eng_guid, tend_guid, se_guid, org_guid, hidd_sour, hidd_grad, hidd_clas, if_found, pro_part, part_long, part_lat, otlcol_qiff_fnvu, insp_rec_guid, se_check_item_guid, hidd_stat, hidd_merg_guid, rec_org_guid, note, coll_time, upd_time, rec_pers, hidds_guid, occu_num, sins_sche_guid, group_guid, temp, to_date, dict_guid, hidd_sort, haz_guid)
        values (#{guid}, #{hiddName}, #{engGuid}, #{tendGuid}, #{seGuid}, #{orgGuid}, #{hiddSour}, #{hiddGrad}, #{hiddClas}, #{ifFound}, #{proPart}, #{partLong}, #{partLat}, #{otlcolQiffFnvu}, #{inspRecGuid}, #{seCheckItemGuid}, #{hiddStat}, #{hiddMergGuid}, #{recOrgGuid}, #{note}, #{collTime}, #{updTime}, #{recPers}, #{hiddsGuid}, #{occuNum}, #{sinsScheGuid}, #{groupGuid}, #{temp}, #{toDate}, #{dictGuid}, #{hiddSort}, #{hazGuid})
    </insert>
    <update id="updateObjHidd" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd">
        update obj_hidd
        <set>
            <if test="hiddName != null">hidd_name = #{hiddName},</if>
            <if test="engGuid != null">eng_guid = #{engGuid},</if>
            <if test="tendGuid != null">tend_guid = #{tendGuid},</if>
            <if test="seGuid != null">se_guid = #{seGuid},</if>
            <if test="orgGuid != null">org_guid = #{orgGuid},</if>
            <if test="hiddSour != null">hidd_sour = #{hiddSour},</if>
            <if test="hiddGrad != null">hidd_grad = #{hiddGrad},</if>
            <if test="hiddClas != null">hidd_clas = #{hiddClas},</if>
            <if test="ifFound != null">if_found = #{ifFound},</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="otlcolQiffFnvu != null">otlcol_qiff_fnvu = #{otlcolQiffFnvu},</if>
            <if test="inspRecGuid != null">insp_rec_guid = #{inspRecGuid},</if>
            <if test="seCheckItemGuid != null">se_check_item_guid = #{seCheckItemGuid},</if>
            <if test="hiddStat != null">hidd_stat = #{hiddStat},</if>
            <if test="hiddMergGuid != null">hidd_merg_guid = #{hiddMergGuid},</if>
            <if test="recOrgGuid != null">rec_org_guid = #{recOrgGuid},</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="hiddsGuid != null">hidds_guid = #{hiddsGuid},</if>
            <if test="occuNum != null">occu_num = #{occuNum},</if>
            <if test="sinsScheGuid != null">sins_sche_guid = #{sinsScheGuid},</if>
            <if test="groupGuid != null">group_guid = #{groupGuid},</if>
            <if test="temp != null">temp = #{temp},</if>
            <if test="toDate != null">to_date = #{toDate},</if>
            <if test="dictGuid != null">dict_guid = #{dictGuid},</if>
            <if test="hiddSort != null">hidd_sort = #{hiddSort},</if>
            <if test="hazGuid != null">haz_guid = #{hazGuid},</if>
        </set>
        where guid = #{guid}
    </update>
    <delete id="deleteObjHiddByGuid" parameterType="String">
        delete from obj_hidd where guid = #{guid}
    </delete>
    <delete id="deleteObjHiddByGuids" parameterType="String">
        delete from obj_hidd where guid in
        <foreach item="guid" collection="array" open="(" separator="," close=")">
            #{guid}
        </foreach>
    </delete>

    <select id="selectObjHiddListByName" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd" resultMap="ObjHiddResult">
        <include refid="selectObjHiddVo"/>
        <where>
            <if test="hiddName != null and hiddName != ''">AND hidd_name like concat('%', #{hiddName}, '%')</if>
            <if test="hiddGrad != null and hiddGrad != ''">AND hidd_grad = #{hiddGrad}</if>
            <if test="hiddStat != null and hiddStat != ''">AND hidd_stat = #{hiddStat}</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="hiddSort != null and hiddSort != ''">AND hidd_sort = #{hiddSort}</if>
            <if test="wiunProp != null and wiunProp != ''">AND EXISTS (SELECT 1 FROM att_org_ext ext WHERE ext.org_guid = org_guid AND ext.wiun_prop = #{wiunProp})</if>
        </where>
        order by coll_time desc
    </select>

    <select id="selectNextDepHiddList" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd" resultMap="ObjHiddResult">
        <include refid="selectObjHiddVo"/>
        <where>
            <if test="orgGuid != null and orgGuid != ''">AND h.org_guid in (select guid from att_org_base where pguid = #{orgGuid})</if>
            <if test="hiddName != null and hiddName != ''">AND hidd_name like concat('%', #{hiddName}, '%')</if>
            <if test="hiddGrad != null and hiddGrad != ''">AND hidd_grad = #{hiddGrad}</if>
            <if test="hiddStat != null and hiddStat != ''">AND hidd_stat = #{hiddStat}</if>
            <if test="hiddSort != null and hiddSort != ''">AND hidd_sort = #{hiddSort}</if>
            <if test="wiunProp != null and wiunProp != ''">AND EXISTS (SELECT 1 FROM att_org_ext ext WHERE ext.org_guid = h.org_guid AND ext.wiun_prop = #{wiunProp})</if>
        </where>
        order by coll_time desc
    </select>

    <insert id="insertNextHidd" parameterType="com.goldenwater.slaj.hidd.domain.ObjHidd">
        insert into obj_hidd (guid, hidd_name, eng_guid, tend_guid, se_guid, org_guid, hidd_sour, hidd_grad, hidd_clas, if_found, pro_part, part_long, part_lat, otlcol_qiff_fnvu, insp_rec_guid, se_check_item_guid, hidd_stat, hidd_merg_guid, rec_org_guid, note, coll_time, upd_time, rec_pers, hidds_guid, occu_num, sins_sche_guid, group_guid, temp, to_date, dict_guid, hidd_sort, haz_guid)
        values (#{guid}, #{hiddName}, #{engGuid}, #{tendGuid}, #{seGuid}, #{orgGuid}, #{hiddSour}, #{hiddGrad}, #{hiddClas}, #{ifFound}, #{proPart}, #{partLong}, #{partLat}, #{otlcolQiffFnvu}, #{inspRecGuid}, #{seCheckItemGuid}, #{hiddStat}, #{hiddMergGuid}, #{recOrgGuid}, #{note}, #{collTime}, #{updTime}, #{recPers}, #{hiddsGuid}, #{occuNum}, #{sinsScheGuid}, #{groupGuid}, #{temp}, #{toDate}, #{dictGuid}, #{hiddSort}, #{hazGuid})
    </insert>

    <!-- 工程/非工程隐患聚合查询 -->
    <select id="selectHiddGroupBy" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
        SELECT
            e.eng_name AS engName,
            org.org_name AS orgName,
            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.hidd_stat = '2' then 1 else 0 end), 0) AS solvedCount
        FROM obj_hidd h
        LEFT JOIN obj_eng e ON h.eng_guid = e.guid
        LEFT JOIN att_org_base org ON h.org_guid = org.guid
        <where>
            <if test="hiddSort != null and hiddSort != ''">AND h.hidd_sort = #{hiddSort}</if>
            <if test="engName != null and engName != ''">AND e.eng_name LIKE '%' || #{engName} || '%'</if>
            <if test="orgName != null and orgName != ''">AND org.org_name LIKE '%' || #{orgName} || '%'</if>
            <if test="collTime != null and collTime != ''">AND h.coll_time LIKE #{collTime} || '%'</if>
        </where>
        GROUP BY e.eng_name, org.org_name
        ORDER BY totalCount DESC
    </select>
</mapper>
