<?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.TTroubleStandingBookMapper">
    
    <resultMap type="TTroubleStandingBook" id="TTroubleStandingBookResult">
        <result property="troubleId"    column="trouble_id"    />
        <result property="troubleName"    column="trouble_name"    />
        <result property="troubleLocation"    column="trouble_location"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="troubleType"    column="trouble_type"    />
        <result property="briefProcess"    column="brief_process"    />
        <result property="troubleReason"    column="trouble_reason"    />
        <result property="isCasualties"    column="is_casualties"    />
        <result property="injuryNum"    column="injury_num"    />
        <result property="deathNum"    column="death_num"    />
        <result property="responsibleUnit"    column="responsible_unit"    />
        <result property="responsiblePeople"    column="responsible_people"    />
        <result property="isDeal"    column="is_deal"    />
        <result property="happenDate"    column="happen_date"    />
        <result property="dealDate"    column="deal_date"    />
        <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="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTTroubleStandingBookVo">
        select trouble_id, trouble_name, trouble_location, longitude, latitude, trouble_type, brief_process, trouble_reason, is_casualties, injury_num, death_num, responsible_unit, responsible_people, is_deal,happen_date, deal_date, create_by, create_time, update_by, update_time, is_del, remarks from t_trouble_standing_book
    </sql>

    <select id="selectTTroubleStandingBookList" parameterType="TTroubleStandingBookForm" resultMap="TTroubleStandingBookResult">
        <include refid="selectTTroubleStandingBookVo"/>
        <where>  is_del = '0'
            <if test="troubleName != null  and troubleName != ''"> and trouble_name like concat('%', #{troubleName}, '%')</if>
            <if test="troubleType != null  and troubleType != ''"> and trouble_type = #{troubleType}</if>
            <if test="isDeal != null  and isDeal != ''"> and is_deal = #{isDeal}</if>
            <if test="happenDateStart != null "> and happen_date &gt;= #{happenDateStart}</if>
            <if test="happenDateEnd != null "> and happen_date &lt;= #{happenDateEnd}</if>
            <if test="dealDateStart != null "> and deal_date &gt;= #{dealDateStart}</if>
            <if test="dealDateEnd != null "> and deal_date &lt;= #{dealDateEnd}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTTroubleStandingBookStatistic" resultType="TTroubleStandingBookVo">
        select COUNT(t.trouble_id) AS troubleTotal,
			 SUM(t.injury_num) AS injutyTotal,
			 SUM(t.death_num) AS deathTotal,
			 ROUND(SUM(CASE t.is_deal WHEN '1' THEN 1 ELSE 0 END)/COUNT(t.trouble_id)*100, 1) AS completionRate
        from (select * from t_trouble_standing_book where is_del = '0') t
    </select>
    
    <select id="selectTTroubleStandingBookById" parameterType="Long" resultMap="TTroubleStandingBookResult">
        <include refid="selectTTroubleStandingBookVo"/>
        where trouble_id = #{troubleId}
    </select>
        
    <insert id="insertTTroubleStandingBook" parameterType="TTroubleStandingBook" useGeneratedKeys="true" keyProperty="troubleId">
        insert into t_trouble_standing_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="troubleName != null">trouble_name,</if>
            <if test="troubleLocation != null">trouble_location,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="troubleType != null">trouble_type,</if>
            <if test="briefProcess != null">brief_process,</if>
            <if test="troubleReason != null">trouble_reason,</if>
            <if test="isCasualties != null">is_casualties,</if>
            <if test="injuryNum != null">injury_num,</if>
            <if test="deathNum != null">death_num,</if>
            <if test="responsibleUnit != null">responsible_unit,</if>
            <if test="responsiblePeople != null">responsible_people,</if>
            <if test="isDeal != null">is_deal,</if>
            <if test="happenDate != null">happen_date,</if>
            <if test="dealDate != null">deal_date,</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="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="troubleName != null">#{troubleName},</if>
            <if test="troubleLocation != null">#{troubleLocation},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="troubleType != null">#{troubleType},</if>
            <if test="briefProcess != null">#{briefProcess},</if>
            <if test="troubleReason != null">#{troubleReason},</if>
            <if test="isCasualties != null">#{isCasualties},</if>
            <if test="injuryNum != null">#{injuryNum},</if>
            <if test="deathNum != null">#{deathNum},</if>
            <if test="responsibleUnit != null">#{responsibleUnit},</if>
            <if test="responsiblePeople != null">#{responsiblePeople},</if>
            <if test="isDeal != null">#{isDeal},</if>
            <if test="happenDate != null">#{happenDate},</if>
            <if test="dealDate != null">#{dealDate},</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="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTTroubleStandingBook" parameterType="TTroubleStandingBook">
        update t_trouble_standing_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="troubleName != null">trouble_name = #{troubleName},</if>
            <if test="troubleLocation != null">trouble_location = #{troubleLocation},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="troubleType != null">trouble_type = #{troubleType},</if>
            <if test="briefProcess != null">brief_process = #{briefProcess},</if>
            <if test="troubleReason != null">trouble_reason = #{troubleReason},</if>
            <if test="isCasualties != null">is_casualties = #{isCasualties},</if>
            <if test="injuryNum != null">injury_num = #{injuryNum},</if>
            <if test="deathNum != null">death_num = #{deathNum},</if>
            <if test="responsibleUnit != null">responsible_unit = #{responsibleUnit},</if>
            <if test="responsiblePeople != null">responsible_people = #{responsiblePeople},</if>
            <if test="isDeal != null">is_deal = #{isDeal},</if>
            <if test="happenDate != null">happen_date = #{happenDate},</if>
            <if test="dealDate != null">deal_date = #{dealDate},</if>
            <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="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where trouble_id = #{troubleId}
    </update>

    <delete id="deleteTTroubleStandingBookById" parameterType="Long">
        delete from t_trouble_standing_book where trouble_id = #{troubleId}
    </delete>

    <delete id="deleteTTroubleStandingBookByIds" parameterType="String">
        delete from t_trouble_standing_book where trouble_id in 
        <foreach item="troubleId" collection="array" open="(" separator="," close=")">
            #{troubleId}
        </foreach>
    </delete>
</mapper>