<?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.zehong.system.mapper.TInsSpotHazardRefMapper">
    
    <resultMap type="TInsSpotHazardRef" id="TInsSpotHazardRefResult">
        <result property="fInsSpotHazardRefId"    column="f_ins_spot_hazard_ref_id"    />
        <result property="fHazardUniqueCode"    column="f_hazard_unique_code"    />
        <result property="fHazardOutUniqueCode"    column="f_hazard_out_unique_code"    />
        <result property="fInvolveEnterpriseCode"    column="f_involve_enterprise_code"    />
        <result property="fObjType"    column="f_obj_type"    />
        <result property="fObjCode"    column="f_obj_code"    />
        <result property="fObjBelongRegionId"    column="f_obj_belong_region_id"    />
        <result property="fCheckTime"    column="f_check_time"    />
        <result property="fHazardTypeLevelId"    column="f_hazard_type_level_id"    />
        <result property="fHazardDesc"    column="f_hazard_desc"    />
        <result property="fBeforePicture"    column="f_before_picture"    />
        <result property="fIsEnforcement"    column="f_is_enforcement"    />
        <result property="fRemark"    column="f_remark"    />
        <result property="fLastTime"    column="f_last_time"    />

        <result property="afterPicture"    column="f_after_picture"    />
        <result property="disposalDetails"    column="f_disposal_details"    />
        <result property="disposalTime"    column="f_disposal_time"    />
    </resultMap>

    <sql id="selectTInsSpotHazardRefVo">
       SELECT
            spot.f_ins_spot_hazard_ref_id,
            spot.f_hazard_unique_code,
            spot.f_hazard_out_unique_code,
            spot.f_involve_enterprise_code,
            spot.f_obj_type,
            spot.f_obj_code,
            spot.f_obj_belong_region_id,
            spot.f_check_time,
            spot.f_hazard_type_level_id,
            spot.f_hazard_desc,
            spot.f_before_picture,
            spot.f_is_enforcement,
            spot.f_remark,
            spot.f_last_time,
            spot.f_after_picture,
            spot.f_disposal_details,
            spot.f_disposal_time,
            (select rec.f_name from t_county_level_region rec where rec.f_id = spot.f_obj_belong_region_id) AS countyName,
            (select en.enterprise_name from t_enterprise_info en where enterprise_id = spot.f_involve_enterprise_code)AS enterpriseName,
            (select inf.f_hazard_name from t_ins_sta_infor inf where inf.f_ins_sta_infor_id = spot.f_hazard_type_level_id)AS levelName
        FROM
            t_ins_spot_hazard_ref spot
    </sql>

    <select id="selectTInsSpotHazardRefList" parameterType="TInsSpotHazardRef" resultMap="TInsSpotHazardRefResult">
        <include refid="selectTInsSpotHazardRefVo"/>
        <where>  
            <if test="fHazardUniqueCode != null  and fHazardUniqueCode != ''"> and spot.f_hazard_unique_code like concat('%', #{fHazardUniqueCode}, '%')</if>
            <if test="fObjType != null  and fObjType != ''"> and spot.f_obj_type = #{fObjType}</if>
            <if test="fIsEnforcement != null  and fIsEnforcement != ''"> and spot.f_is_enforcement = #{fIsEnforcement}</if>
        </where>
        ORDER BY f_last_time DESC
    </select>
    
    <select id="selectTInsSpotHazardRefById" parameterType="Long" resultMap="TInsSpotHazardRefResult">
        <include refid="selectTInsSpotHazardRefVo"/>
        where spot.f_ins_spot_hazard_ref_id = #{fInsSpotHazardRefId}
    </select>
        
    <insert id="insertTInsSpotHazardRef" parameterType="TInsSpotHazardRef" useGeneratedKeys="true" keyProperty="fInsSpotHazardRefId">
        insert into t_ins_spot_hazard_ref
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fHazardUniqueCode != null">f_hazard_unique_code,</if>
            <if test="fHazardOutUniqueCode != null">f_hazard_out_unique_code,</if>
            <if test="fInvolveEnterpriseCode != null">f_involve_enterprise_code,</if>
            <if test="fObjType != null">f_obj_type,</if>
            <if test="fObjCode != null">f_obj_code,</if>
            <if test="fObjBelongRegionId != null">f_obj_belong_region_id,</if>
            <if test="fCheckTime != null">f_check_time,</if>
            <if test="fHazardTypeLevelId != null">f_hazard_type_level_id,</if>
            <if test="fHazardDesc != null">f_hazard_desc,</if>
            <if test="fBeforePicture != null">f_before_picture,</if>
            <if test="fIsEnforcement != null">f_is_enforcement,</if>
            <if test="fRemark != null">f_remark,</if>
            <if test="fLastTime != null">f_last_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fHazardUniqueCode != null">#{fHazardUniqueCode},</if>
            <if test="fHazardOutUniqueCode != null">#{fHazardOutUniqueCode},</if>
            <if test="fInvolveEnterpriseCode != null">#{fInvolveEnterpriseCode},</if>
            <if test="fObjType != null">#{fObjType},</if>
            <if test="fObjCode != null">#{fObjCode},</if>
            <if test="fObjBelongRegionId != null">#{fObjBelongRegionId},</if>
            <if test="fCheckTime != null">#{fCheckTime},</if>
            <if test="fHazardTypeLevelId != null">#{fHazardTypeLevelId},</if>
            <if test="fHazardDesc != null">#{fHazardDesc},</if>
            <if test="fBeforePicture != null">#{fBeforePicture},</if>
            <if test="fIsEnforcement != null">#{fIsEnforcement},</if>
            <if test="fRemark != null">#{fRemark},</if>
            <if test="fLastTime != null">#{fLastTime},</if>
         </trim>
    </insert>

    <update id="updateTInsSpotHazardRef" parameterType="TInsSpotHazardRef">
        update t_ins_spot_hazard_ref
        <trim prefix="SET" suffixOverrides=",">
            <if test="fHazardUniqueCode != null">f_hazard_unique_code = #{fHazardUniqueCode},</if>
            <if test="fHazardOutUniqueCode != null">f_hazard_out_unique_code = #{fHazardOutUniqueCode},</if>
            <if test="fInvolveEnterpriseCode != null">f_involve_enterprise_code = #{fInvolveEnterpriseCode},</if>
            <if test="fObjType != null">f_obj_type = #{fObjType},</if>
            <if test="fObjCode != null">f_obj_code = #{fObjCode},</if>
            <if test="fObjBelongRegionId != null">f_obj_belong_region_id = #{fObjBelongRegionId},</if>
            <if test="fCheckTime != null">f_check_time = #{fCheckTime},</if>
            <if test="fHazardTypeLevelId != null">f_hazard_type_level_id = #{fHazardTypeLevelId},</if>
            <if test="fHazardDesc != null">f_hazard_desc = #{fHazardDesc},</if>
            <if test="fBeforePicture != null">f_before_picture = #{fBeforePicture},</if>
            <if test="fIsEnforcement != null">f_is_enforcement = #{fIsEnforcement},</if>
            <if test="fRemark != null">f_remark = #{fRemark},</if>
            <if test="fLastTime != null">f_last_time = #{fLastTime},</if>
        </trim>
        where f_ins_spot_hazard_ref_id = #{fInsSpotHazardRefId}
    </update>
    <update id="disposeOfInsSpotHazardRef" parameterType="TInsSpotHazardRef">
        update t_ins_spot_hazard_ref
        <trim prefix="SET" suffixOverrides=",">
            <if test="afterPicture != null and afterPicture != ''">f_after_picture = #{afterPicture},</if>
            <if test="disposalDetails != null and disposalDetails != ''">f_disposal_details = #{disposalDetails},</if>
            <if test="disposalTime != null">f_disposal_time = #{disposalTime},</if>
        </trim>
        where f_ins_spot_hazard_ref_id = #{fInsSpotHazardRefId}
    </update>

    <delete id="deleteTInsSpotHazardRefById" parameterType="Long">
        delete from t_ins_spot_hazard_ref where f_ins_spot_hazard_ref_id = #{fInsSpotHazardRefId}
    </delete>

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

    <insert id="batchInsertTInsSpotHazardRef" parameterType="TInsSpotHazardRef" useGeneratedKeys="true" keyProperty="fInsSpotHazardRefId">
        insert into t_ins_spot_hazard_ref(
            f_hazard_unique_code,
            f_hazard_out_unique_code,
            f_involve_enterprise_code,
            f_obj_type,
            f_obj_code,
            f_obj_belong_region_id,
            f_check_time,
            f_hazard_type_level_id,
            f_hazard_desc,
            f_before_picture,
            f_is_enforcement,
            f_remark,
            f_last_time
        )VALUES
        <foreach collection="list" separator="," item="item">
            (
                #{item.fHazardUniqueCode},
                #{item.fHazardOutUniqueCode},
                #{item.fInvolveEnterpriseCode},
                #{item.fObjType},
                #{item.fObjCode},
                #{item.fObjBelongRegionId},
                #{item.fCheckTime},
                #{item.fHazardTypeLevelId},
                #{item.fHazardDesc},
                #{item.fBeforePicture},
                #{item.fIsEnforcement},
                #{item.fRemark},
                #{item.fLastTime}
            )
        </foreach>
    </insert>
</mapper>