<?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="township"    column="township"    />
        <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"    />
        <result property="enterpriseId"    column="enterprise_id"    />
        <result property="enterpriseName"    column="enterpriseName"    />
    </resultMap>

    <sql id="selectTTroubleStandingBookVo">
        select book.trouble_id, book.trouble_name, book.trouble_location,book.township, book.longitude, book.latitude,
               book.trouble_type,book.brief_process, book.trouble_reason, book.is_casualties, book.injury_num,
               book.death_num, book.responsible_unit,book.responsible_people, book.is_deal,book.happen_date,
               book.deal_date, book.create_by, book.create_time, book.update_by,book.update_time, book.is_del,
               book.remarks,book.enterprise_id,info.enterprise_name as enterpriseName
        from t_trouble_standing_book book
        left join t_enterprise_info info on book.enterprise_id = info.enterprise_id
    </sql>

    <select id="selectTTroubleStandingBookList" parameterType="TTroubleStandingBookForm" resultMap="TTroubleStandingBookResult">
        <include refid="selectTTroubleStandingBookVo"/>
        <where>  book.is_del = '0'
            <if test="troubleName != null  and troubleName != ''"> and book.trouble_name like concat('%', #{troubleName}, '%')</if>
            <if test="troubleType != null  and troubleType != ''"> and book.trouble_type = #{troubleType}</if>
            <if test="isDeal != null  and isDeal != ''"> and book.is_deal = #{isDeal}</if>
            <if test="happenDateStart != null "> and book.happen_date &gt;= #{happenDateStart}</if>
            <if test="happenDateEnd != null "> and book.happen_date &lt;= #{happenDateEnd}</if>
            <if test="dealDateStart != null "> and book.deal_date &gt;= #{dealDateStart}</if>
            <if test="dealDateEnd != null "> and book.deal_date &lt;= #{dealDateEnd}</if>
            <if test="enterpriseId != null and enterpriseId!= '' "> and book.enterprise_id = #{enterpriseId} </if>
        </where>
        order by book.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="township != null">township,</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>
            <if test="enterpriseId != null">enterprise_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="troubleName != null">#{troubleName},</if>
            <if test="troubleLocation != null">#{troubleLocation},</if>
            <if test="township != null">#{township},</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>
            <if test="enterpriseId != null">#{enterpriseId},</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="township != null">township = #{township},</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>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</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>


    <!--查询统计信息-->
    <select id="accidentLedger" resultType="com.zehong.system.domain.Statistics">
       SELECT
       COUNT( * ) AS 'count',
       DATE_FORMAT( create_time, '%Y-%m-%d' ) AS date
       FROM
        t_trouble_standing_book
       WHERE
        is_del='0'
        and
       DATE_FORMAT( create_time, '%Y-%m-%d' ) IN
       <foreach collection="list" item="sevenDate" separator="," open="(" close=")">
            #{sevenDate}
       </foreach>
       GROUP BY
       DATE_FORMAT( create_time, '%Y-%m-%d' )
       ORDER BY
       DATE_FORMAT( create_time, '%Y-%m-%d' ) DESC;
    </select>
    <select id="selectTrobleCountByMonth" resultType="java.util.HashMap">
        SELECT d.`month`, COUNT(w.`trouble_id`) AS `count`,
        IF(SUM(w.`injury_num`+w.`death_num`)IS NULL,0,SUM(w.`injury_num`+w.`death_num`)) AS hurtNum
            FROM(
              SELECT DATE_FORMAT((CURDATE()), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS `month`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS `month`
                )d
                LEFT JOIN t_trouble_standing_book w
                ON DATE_FORMAT(w.create_time, '%Y-%m')=d.`month` AND w.`is_del` =0
                GROUP BY MONTH
    </select>
    <select id="selectTrobleRanking" resultType="java.util.HashMap">
        SELECT COUNT(trouble_id) AS `value` ,d.`dict_label` AS `name`
        FROM sys_dict_data d
        LEFT JOIN t_trouble_standing_book t1 ON t1.`township` = d.`dict_value`  AND t1.`is_del` = 0
        WHERE d.`dict_type` ='t_township'
        GROUP BY d.`dict_value`
        ORDER BY `value` DESC
        LIMIT 10
    </select>
    <select id="selectTrobleCountByYear" resultType="java.util.HashMap">
        SELECT d.`years`, COUNT(w.`trouble_id`) AS `count`
            FROM(
              SELECT DATE_FORMAT((CURDATE()), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 YEAR), '%Y') AS `years`
                )d
                LEFT JOIN t_trouble_standing_book w
                ON DATE_FORMAT(w.create_time, '%Y')=d.`years` AND w.`is_del` =0
                GROUP BY years

    </select>
    <select id="selectTrobleReason" resultType="java.util.HashMap">
        SELECT  COUNT(trouble_id)  AS `value` ,d.`dict_label` AS `name`
        FROM sys_dict_data d
        LEFT JOIN t_trouble_standing_book t1 ON (t1.`trouble_reason` = d.`dict_value` AND t1.`is_del` = 0)

        WHERE d.`dict_type` ='t_trouble_reason'

        GROUP BY d.`dict_value`
    </select>
</mapper>
