<?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.THiddenDangerStandingBookMapper">

    <resultMap type="THiddenDangerStandingBook" id="THiddenDangerStandingBookResult">
        <result property="hiddenId"    column="hidden_id"    />
        <result property="hiddenTitle"    column="hidden_title"    />
        <result property="hiddenContent"    column="hidden_content"    />
        <result property="hiddenLocation"    column="hidden_location"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="hiddenType"    column="hidden_type"    />
        <result property="hiddenFindPeople"    column="hidden_find_people"    />
        <result property="hiddenFindDate"    column="hidden_find_date"    />
        <result property="dealPlan"    column="deal_plan"    />
        <result property="dealPlanUrl"    column="deal_plan_url"    />
        <result property="remediation"    column="remediation"    />
        <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="beyondEnterpriseId"    column="beyondEnterprise_id"    />
        <result property="hiddenTroubleType"    column="hidden_trouble_type"    />
<!--        导出使用-->
        <result property="enterpriseName"    column="enterpriseName"    />
    </resultMap>

    <sql id="selectTHiddenDangerStandingBookVo">
        select book.hidden_id, book.hidden_title, book.hidden_content, book.hidden_location, book.longitude,
               book.latitude, book.hidden_type,book.hidden_find_people, book.hidden_find_date, book.deal_plan,
               book.deal_plan_url, book.remediation, book.create_by, book.create_time,book.update_by, book.update_time,
               book.is_del,book.beyondEnterprise_id, book.remarks,book.township,book.source,book.hidden_trouble_type,
               info.enterprise_name as enterpriseName
        from t_hidden_danger_standing_book book left join t_enterprise_info info on book.beyondEnterprise_id = info.enterprise_id
    </sql>

    <select id="selectTHiddenDangerStandingBookList" parameterType="THiddenDangerStandingBookForm" resultMap="THiddenDangerStandingBookResult">
        <include refid="selectTHiddenDangerStandingBookVo"/>
        <where>  book.is_del = '0'
            <if test="hiddenTitle != null  and hiddenTitle != ''"> and book.hidden_title like concat('%', #{hiddenTitle}, '%')</if>
            <if test="hiddenType != null  and hiddenType != ''"> and book.hidden_type = #{hiddenType}</if>
            <if test="remediation != null  and remediation != ''"> and book.remediation = #{remediation}</if>
            <if test="hiddenFindDateStart != null "> and book.hidden_find_date &gt;= #{hiddenFindDateStart}</if>
            <if test="hiddenFindDateEnd != null "> and book.hidden_find_date &lt;= #{hiddenFindDateEnd}</if>
            <if test="beyondEnterpriseId != null "> and book.beyondEnterprise_id = #{beyondEnterpriseId}</if>
        </where>
        order by book.create_time desc
    </select>

    <select id="selectTHiddenDangerStandingBookStatistic" resultType="THiddenDangerStandingBookVo">
        select COUNT(t.hidden_id) AS hiddenTotal,
               SUM(CASE t.hidden_type WHEN '1' THEN 1 ELSE 0 END) AS oneLevelTotal,
			   SUM(CASE t.hidden_type WHEN '2' THEN 1 ELSE 0 END) AS twoLevelTotal,
			   SUM(CASE t.hidden_type WHEN '3' THEN 1 ELSE 0 END) AS threeLevelTotal,
			   SUM(CASE t.remediation WHEN '1' THEN 1 ELSE 0 END) AS completedTotal,
			   SUM(CASE t.remediation WHEN '2' THEN 1 ELSE 0 END) AS incompleteTotal
        from (select * from t_hidden_danger_standing_book where is_del = '0') t
    </select>

    <select id="selectTHiddenDangerStandingBookById" parameterType="Long" resultMap="THiddenDangerStandingBookResult">
        <include refid="selectTHiddenDangerStandingBookVo"/>
        where hidden_id = #{hiddenId}
    </select>

    <insert id="insertTHiddenDangerStandingBook" parameterType="THiddenDangerStandingBook" useGeneratedKeys="true" keyProperty="hiddenId">
        insert into t_hidden_danger_standing_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hiddenTitle != null">hidden_title,</if>
            <if test="hiddenContent != null">hidden_content,</if>
            <if test="hiddenLocation != null">hidden_location,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="hiddenType != null">hidden_type,</if>
            <if test="hiddenFindPeople != null">hidden_find_people,</if>
            <if test="hiddenFindDate != null">hidden_find_date,</if>
            <if test="dealPlan != null">deal_plan,</if>
            <if test="dealPlanUrl != null">deal_plan_url,</if>
            <if test="remediation != null">remediation,</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="township != null">township,</if>
            <if test="source != null">source,</if>
            <if test="beyondEnterpriseId != null">beyondEnterprise_id,</if>
            <if test="hiddenTroubleType != null">hidden_trouble_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hiddenTitle != null">#{hiddenTitle},</if>
            <if test="hiddenContent != null">#{hiddenContent},</if>
            <if test="hiddenLocation != null">#{hiddenLocation},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="hiddenType != null">#{hiddenType},</if>
            <if test="hiddenFindPeople != null">#{hiddenFindPeople},</if>
            <if test="hiddenFindDate != null">#{hiddenFindDate},</if>
            <if test="dealPlan != null">#{dealPlan},</if>
            <if test="dealPlanUrl != null">#{dealPlanUrl},</if>
            <if test="remediation != null">#{remediation},</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="township != null">#{township},</if>
            <if test="source != null">#{source},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
            <if test="hiddenTroubleType != null">hidden_trouble_type = #{hiddenTroubleType},</if>
        </trim>
    </insert>

    <insert id="inserts" parameterType="THiddenDangerStandingBook" >
        insert into t_hidden_danger_standing_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            hidden_title,
            hidden_content,
            hidden_location,
            source,
            longitude,
            latitude,
            hidden_type,
            hidden_find_people,
            hidden_find_date,
            deal_plan,
            beyondEnterprise_id,
            remediation,
            create_time,
            remarks
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.hiddenTitle},
            #{item.hiddenContent},
            #{item.hiddenLocation},
            #{item.source},
            #{item.longitude},
            #{item.latitude},
            #{item.hiddenType},
            #{item.hiddenFindPeople},
            #{item.hiddenFindDate},
            #{item.dealPlan},
            #{item.beyondEnterpriseId},
            #{item.remediation},
            #{item.createTime},
            #{item.remarks})
        </foreach>
    </insert>

    <update id="updateTHiddenDangerStandingBook" parameterType="THiddenDangerStandingBook">
        update t_hidden_danger_standing_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="hiddenTitle != null">hidden_title = #{hiddenTitle},</if>
            <if test="hiddenContent != null">hidden_content = #{hiddenContent},</if>
            <if test="hiddenLocation != null">hidden_location = #{hiddenLocation},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="hiddenType != null">hidden_type = #{hiddenType},</if>
            <if test="hiddenFindPeople != null">hidden_find_people = #{hiddenFindPeople},</if>
            <if test="hiddenFindDate != null">hidden_find_date = #{hiddenFindDate},</if>
            <if test="dealPlan != null">deal_plan = #{dealPlan},</if>
            <if test="dealPlanUrl != null">deal_plan_url = #{dealPlanUrl},</if>
            <if test="remediation != null">remediation = #{remediation},</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="township != null">township = #{township},</if>
            <if test="source != null"> source= #{source},</if>
            <if test="hiddenTroubleType != null">hidden_trouble_type = #{hiddenTroubleType},</if>
        </trim>
        where hidden_id = #{hiddenId}
    </update>

    <delete id="deleteTHiddenDangerStandingBookById" parameterType="Long">
        delete from t_hidden_danger_standing_book where hidden_id = #{hiddenId}
    </delete>

    <delete id="deleteTHiddenDangerStandingBookByIds" parameterType="String">
        delete from t_hidden_danger_standing_book where hidden_id in
        <foreach item="hiddenId" collection="array" open="(" separator="," close=")">
            #{hiddenId}
        </foreach>
    </delete>

    <!--获取隐患整治台账统计信息-->
    <select id="hazardStatistics" resultType="com.zehong.system.domain.Statistics">
        SELECT
        COUNT( * ) AS 'count',
        DATE_FORMAT( create_time, '%Y-%m-%d' ) AS date
        FROM
        t_hidden_danger_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="selectCountByMonth" resultType="java.util.HashMap">
        SELECT d.`month`,SUM(IF(w.remediation=1,1,0)) AS `ycount`,COUNT(w.hidden_id) AS `zcount`
            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_hidden_danger_standing_book w
                ON DATE_FORMAT(w.create_time, '%Y-%m')=d.`month` AND w.`is_del` =0

                GROUP BY MONTH
    </select>
    <select id="selectCountByYear" resultType="java.util.HashMap">
        SELECT d.`years`, COUNT(w.`hidden_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_hidden_danger_standing_book w
                ON DATE_FORMAT(w.create_time, '%Y')=d.`years` AND w.`is_del` =0
                GROUP BY years
    </select>
    <select id="selectHiddenRanking" resultType="java.util.HashMap">
        SELECT COUNT(hidden_id) AS `value` ,d.`dict_label` AS `name`
        FROM sys_dict_data d
        LEFT JOIN t_hidden_danger_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="selectHiddenSource" resultType="java.util.HashMap">
        SELECT COUNT(hidden_id) AS `value` ,d.`dict_label` AS `name`
        FROM sys_dict_data d
        LEFT JOIN t_hidden_danger_standing_book t1 ON t1.`source` = d.`dict_value` AND t1.`is_del` = 0
        WHERE d.`dict_type` ='t_source'
        GROUP BY d.`dict_value`
        ORDER BY `value` DESC

    </select>

    <select id="hiddenBookStatistics" resultType="java.util.HashMap">
        SELECT
            IFNULL(SUM(CASE remediation WHEN '1' THEN 1 ELSE 0 END),0) AS finishNum,
            IFNULL(SUM(CASE remediation WHEN '2' THEN 1 ELSE 0 END),0) AS unFinishNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '1' THEN 1 ELSE 0 END),0) AS blowNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '2' THEN 1 ELSE 0 END),0) AS corrosionNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '3' THEN 1 ELSE 0 END),0) AS crackNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '4' THEN 1 ELSE 0 END),0) AS deviceNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '5' THEN 1 ELSE 0 END),0) AS distanceNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '6' THEN 1 ELSE 0 END),0) AS pipeNum,
            IFNULL(SUM(CASE hidden_trouble_type WHEN '7' THEN 1 ELSE 0 END),0) AS otherNum
        FROM
            t_hidden_danger_standing_book
        WHERE
            is_del = '0'
    </select>
</mapper>
