<?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.ruoyi.ehsIndexStatistics.mapper.EhsIndexStatisticsMapper">
    <select id="getEhsIndexStatistics" resultType="com.ruoyi.ehsIndexStatistics.vo.EhsIndexStatistics">
        SELECT
        count(distinct enterprise_name) unitNum,
        count( 1 ) hiddenDangerNum,
        sum( CASE WHEN STATUS = 10 THEN 1 ELSE 0 END ) haveRectifiedNum,
        sum( CASE WHEN STATUS != 10 THEN 1 ELSE 0 END ) notRectifiedNum,
        sum( CASE WHEN STATUS != 10 AND rectify_term_time &lt; NOW() THEN 1 ELSE 0 END ) exceedNotRectifiedNum,
        sum( rectify_money ) rectifyMoneySum,
        CONCAT(round(
        sum( CASE WHEN STATUS = 10 THEN 1 ELSE 0 END )/ count( 1 ),
        2
        )*100,'%') rectifyRate
        FROM
        `ehs_hidden_danger`
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
    </select>
    <select id="getSuperviseAndHandle" resultType="com.ruoyi.ehsIndexStatistics.vo.EhsIndexStatistics">
        SELECT
        count( 1 ) hiddenDangerNum,
        sum( CASE WHEN STATUS = 10 THEN 1 ELSE 0 END ) haveRectifiedNum,
        sum( CASE WHEN STATUS != 10 THEN 1 ELSE 0 END ) notRectifiedNum,
        sum( CASE WHEN STATUS != 10 AND rectify_term_time &lt; NOW() THEN 1 ELSE 0 END ) exceedNotRectifiedNum,
        sum( rectify_money ) rectifyMoneySum,
        CONCAT(round(
        sum( CASE WHEN STATUS = 10 THEN 1 ELSE 0 END )/ count( 1 ),
        2
        )*100,'%') rectifyRate
        FROM
        `ehs_hidden_danger`
        <where>
            is_supervise = 1
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
    </select>
    <select id="getEhsIndexStatisticsList" resultType="com.ruoyi.ehsIndexStatistics.vo.EhsIndexStatistics">
        SELECT
        r.hidden_danger_grade hiddenDangerGrade,
        d.dict_label name,
        sum( CASE WHEN r.hidden_danger_id is not null THEN 1 ELSE 0 END ) hiddenDangerNum,
        sum( CASE WHEN r.STATUS = 10 THEN 1 ELSE 0 END ) haveRectifiedNum,
        sum( CASE WHEN r.STATUS != 10 THEN 1 ELSE 0 END ) notRectifiedNum,
        sum( CASE WHEN r.STATUS != 10 AND r.rectify_term_time &lt; NOW() THEN 1 ELSE 0 END ) exceedNotRectifiedNum,
        ifnull(sum( r.rectify_money ),0) rectifyMoneySum,
        CONCAT(ifnull(round(
        sum( CASE WHEN r.STATUS = 10 THEN 1 ELSE 0 END )/ sum( CASE WHEN r.hidden_danger_id is not null THEN 1 ELSE 0 END ),
        2
        )* 100,0),'%') rectifyRate
        FROM
        sys_dict_data d
        LEFT JOIN ehs_hidden_danger r ON r.hidden_danger_grade = d.dict_value
        <where>
            d.dict_type = 'hidden_danger_grade'
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
        GROUP BY
        r.hidden_danger_grade
    </select>
    <select id="getHiddenDangerVoList" parameterType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerVo" resultType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerVo">
        SELECT
            *
        FROM
            ehs_hidden_danger
        <where>
            <if test="vo.deptId != null and vo.deptId != ''">and dept_id = #{vo.deptId}</if>
            <if test="vo.enterpriseName != null and vo.enterpriseName != ''">and enterprise_name like CONCAT('%',#{vo.enterpriseName},'%')</if>
            <if test="vo.hiddenDangerGrade != null and vo.hiddenDangerGrade != ''">and hidden_danger_grade = #{vo.hiddenDangerGrade}</if>
            <if test="vo.status != null and vo.status != ''">and status = #{vo.status}</if>
            <if test="vo.params.qxStartTime != null and vo.params.qxStartTime != ''">and rectify_term_time >= #{vo.params.qxStartTime}</if>
            <if test="vo.params.qxEndTime != null and vo.params.qxEndTime != ''">and rectify_term_time &lt; #{vo.params.qxEndTime}</if>
            <if test="vo.params.pcStartTime != null and vo.params.pcStartTime != ''">and check_time >= #{vo.params.pcStartTime}</if>
            <if test="vo.params.pcEndTime != null and vo.params.pcEndTime != ''">and check_time &lt; #{vo.params.pcEndTime}</if>
            <if test="vo.params.wcStartTime != null and vo.params.wcStartTime != ''">and rectify_time >= #{vo.params.wcStartTime}</if>
            <if test="vo.params.wcEndTime != null and vo.params.wcEndTime != ''">and rectify_time &lt; #{vo.params.wcEndTime}</if>
            <if test="vo.isOverTime != null ">and STATUS != 10 AND  rectify_term_time &lt; NOW()</if>
            <if test="vo.isSupervise != null ">and  is_supervise = 1</if>
        </where>
    </select>
    <select id="getHiddenDangerSituationList"
            resultType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerStatisticsVo">

select c.dict_label 'name',ifnull(a.thisUnitNum,0) thisUnitNum,ifnull(a.totalUnitNum,0) totalUnitNum,ifnull(a.thisReportNum,0) thisReportNum,ifnull(a.totalReportNum,0) totalReportNum
,ifnull(a.thisHaveNum,0)thisHaveNum,ifnull(a.totalHaveNum,0)totalHaveNum,ifnull(a.thisNotNum,0)thisNotNum,ifnull(a.totalNotNum,0)totalNotNum,ifnull(a.thisRate,0)thisRate,ifnull(a.totalRate,0)totalRate
from  sys_dict_data c  left join(
SELECT
          r.area_id ,
                (select count(1) from ehs_enterprise e where e.area_id = d.dict_value and e.create_time>= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' )) thisUnitNum,
            (select count(1) from ehs_enterprise e where e.area_id = d.dict_value) totalUnitNum,
            sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) THEN 1 ELSE 0 END ) 'thisReportNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ) 'totalReportNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status = 10 THEN 1 ELSE 0 END ) 'thisHaveNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status = 10 THEN 1 ELSE 0 END ) 'totalHaveNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status != 10 THEN 1 ELSE 0 END ) 'thisNotNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status != 10 THEN 1 ELSE 0 END ) 'totalNotNum',
                CONCAT(round(ifnull(sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status = 10 THEN 1 ELSE 0 END )/sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) THEN 1 ELSE 0 END ),0)*100,2),'%') 'thisRate',
                CONCAT(round(ifnull(sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status = 10 THEN 1 ELSE 0 END )/sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ),0)*100,2),'%') 'totalRate'
        FROM
            sys_dict_data d
                LEFT JOIN ehs_hidden_danger r ON r.area_id = d.dict_value
        WHERE
            d.dict_type = 'area_id'
        GROUP BY
            r.area_id) a  on  a.area_id = c.dict_value
        WHERE
            c.dict_type = 'area_id'
    </select>
    <select id="getHiddenDangerSituationByEnterpriseNameList"
            resultType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerStatisticsVo">
        SELECT
            d.dept_name 'name',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) THEN 1 ELSE 0 END ) 'thisReportNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ) 'totalReportNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status = 10 THEN 1 ELSE 0 END ) 'thisHaveNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status = 10 THEN 1 ELSE 0 END ) 'totalHaveNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status != 10 THEN 1 ELSE 0 END ) 'thisNotNum',
                sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status != 10 THEN 1 ELSE 0 END ) 'totalNotNum',
                CONCAT(round(ifnull(sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) and r.status = 10 THEN 1 ELSE 0 END )/sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.check_time >= DATE_FORMAT( now(), '%Y-%m-01 00:00:00' ) THEN 1 ELSE 0 END ),0)*100,2),'%') 'thisRate',
                CONCAT(round(ifnull(sum( CASE WHEN r.hidden_danger_id IS NOT NULL and r.status = 10 THEN 1 ELSE 0 END )/sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ),0)*100,2),'%') 'totalRate'
        FROM
            sys_dept d left join ehs_hidden_danger r on r.dept_id = d.dept_id
        GROUP BY
            d.dept_id,d.dept_name
    </select>
    <select id="getHiddenDangerRankVoList"
            resultType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerRankVo">
        SELECT
            s.*
        FROM
            (
                SELECT
                    d.dict_label 'areaName',
                        sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ) 'reportHiddenNum'
                FROM
                    sys_dict_data d
                        LEFT JOIN ehs_hidden_danger r ON r.area_id = d.dict_value
                WHERE
                    d.dict_type = 'area_id'
                GROUP BY
                    r.area_id
            ) s
        ORDER BY
            s.reportHiddenNum DESC
    </select>
    <select id="getHiddenDangerRankVoByDeptIdList"
            resultType="com.ruoyi.ehsIndexStatistics.vo.HiddenDangerRankVo">
        SELECT
            s.*
        FROM
            (
                   SELECT
                    d.dept_name 'areaName',
                        count(r.dept_id) 'reportHiddenNum'
                FROM
                   sys_dept d left join  ehs_hidden_danger r on r.dept_id = d.dept_id
                GROUP BY
                    d.dept_id,d.dept_name
                    ) s
        ORDER BY
            s.reportHiddenNum DESC
    </select>
    <select id="getEnterpriseMapList"
            resultType="map">
        SELECT
        ifnull(d.dict_label,'未知') 'name',
        count( 1 ) 'value'
        FROM
        ehs_hidden_danger e
        LEFT JOIN sys_dict_data d ON e.enterprise_type = d.dict_value
        AND d.dict_type = 'enterprise_type'
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
        GROUP BY
            e.enterprise_type
    </select>
    <select id="geteconomicTypeMapList"
            resultType="map">
        SELECT
        ifnull(d.dict_label,'未知') 'name',
        count( 1 ) 'value'
        FROM
        ehs_hidden_danger e
        LEFT JOIN sys_dict_data d ON e.economic_type = d.dict_value
        AND d.dict_type = 'economic_type'
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
        GROUP BY
            e.economic_type
    </select>
    <select id="getenterpriseScaleMapList"
            resultType="map">
        SELECT
        ifnull(d.dict_label,'未知') 'name',
        count( 1 ) 'value'
        FROM
        ehs_hidden_danger e
        LEFT JOIN sys_dict_data d ON e.enterprise_scale = d.dict_value
        AND d.dict_type = 'enterprise_scale'
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
        GROUP BY
            e.enterprise_scale
    </select>
    <select id="getHiddenDangerTypeMapList"
            resultType="map">
        SELECT
        ifnull(d.dict_label,'未知') 'name',
        count( 1 ) 'value'
        FROM
        ehs_hidden_danger e
        LEFT JOIN sys_dict_data d ON e.hidden_danger_type = d.dict_value
        AND d.dict_type = 'hidden_danger_type'
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
        </where>
        GROUP BY
            e.hidden_danger_type
    </select>
    <select id="getHiddenDangerDataMapList"
            resultType="map">
        SELECT
        g.date_time 'dayStr',
        g.dict_label 'name',
        CAST(ifnull( h.value, 0 ) AS CHAR) 'dataNum'
        FROM
        (
        SELECT
        s.date_time,
        d.dict_code,
        d.dict_label,
        d.dict_value
        FROM
        (
        SELECT
        DATE_FORMAT( date_add( CONCAT( #{yearStr}, '-01-01' ), INTERVAL ROW MONTH ), '%Y-%m' ) date_time
        FROM
        (
        SELECT
        @ROW := @ROW + 1 AS ROW
        FROM
        (
        SELECT
        0 UNION ALL
        SELECT
        1 UNION ALL
        SELECT
        2 UNION ALL
        SELECT
        3 UNION ALL
        SELECT
        4 UNION ALL
        SELECT
        5 UNION ALL
        SELECT
        6 UNION ALL
        SELECT
        7 UNION ALL
        SELECT
        8 UNION ALL
        SELECT
        9
        ) t,
        (
        SELECT
        0 UNION ALL
        SELECT
        1 UNION ALL
        SELECT
        2 UNION ALL
        SELECT
        3 UNION ALL
        SELECT
        4 UNION ALL
        SELECT
        5 UNION ALL
        SELECT
        6 UNION ALL
        SELECT
        7 UNION ALL
        SELECT
        8 UNION ALL
        SELECT
        9
        ) t2,
        ( SELECT @ROW :=- 1 ) r
        ) se
        WHERE
        DATE_FORMAT( date_add( CONCAT( #{yearStr}, '-01-01' ), INTERVAL ROW MONTH ), '%Y-%m' ) &lt;= DATE_FORMAT( CONCAT( #{yearStr}, '-12-01' ), '%Y-%m' )) s,
        sys_dict_data d
        WHERE
        d.dict_type = 'hidden_danger_grade'
        ) g
        LEFT JOIN (
        SELECT
        DATE_FORMAT( r.check_time, '%Y-%m' ) date_time,
        r.hidden_danger_grade,
        sum( CASE WHEN r.hidden_danger_id IS NOT NULL THEN 1 ELSE 0 END ) 'value'
        FROM
        ehs_hidden_danger r
        <where>
            <if test="deptId != null and deptId != ''">and r.dept_id = #{deptId}</if>
        </where>
        GROUP BY
        r.hidden_danger_grade,
        DATE_FORMAT( r.check_time, '%Y-%m' )) h ON g.date_time = h.date_time
        AND g.dict_value = h.hidden_danger_grade
        ORDER BY
        g.date_time,
        g.dict_value
    </select>
</mapper>