<?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.bootdo.datas.dao.DataDao">


    <!--统计数据 省地图 根据数据级别-->
    <select id="mapDataList" resultType="com.bootdo.datas.dto.MapDataDTO">
        SELECT
         b.UNIT_CITY  as cityName,
         COUNT(DISTINCT(a.ID)) AS amount
        FROM
            gy_data AS a
            LEFT JOIN gy_unit AS b ON b.CREDIT_CODE = a.GY_NUIT_ID
        <where>
            <if test="dataLevel != null and dataLevel != ''"> and a.DATA_LEVEL = #{dataLevel}</if>
            <if test="auditStage != null and auditStage != ''">and a.AUDIT_STAGE  = #{auditStage}</if>
            and a.DATA_STATUS = '3'
            AND a.DELETE_STATUS != '1'

        </where>
        GROUP BY
            b.UNIT_CITY
        ORDER BY
            amount DESC
    </select>

    <!--统计数据近七天趋势-->
    <select id="trendData" resultType="com.bootdo.datas.dto.DateAndCountDTO">
        SELECT
            COUNT( a.ID ) AS amount,
            date(DATE_FORMAT( a.UPLOAD_TIME, '%Y-%m-%d' ))  AS date
        FROM
            gy_data AS a ,
            gy_unit AS b
        WHERE
            DATE_SUB( CURDATE(), INTERVAL 7 DAY ) &lt;= date(
            DATE_FORMAT( a.UPLOAD_TIME, '%Y-%m-%d' ))
            <if test="city != null and city != ''">  AND b.UNIT_CITY = #{city} </if>
           <if test="auditStage != null and auditStage != ''">and a.AUDIT_STAGE  = #{auditStage}</if>
            AND b.CREDIT_CODE = a.GY_NUIT_ID
            and a.DATA_STATUS = '3'
            AND a.DELETE_STATUS != '1'
        GROUP BY
            DATE_FORMAT(
            a.UPLOAD_TIME,
            '%Y-%m-%d')
        ORDER BY
	        date DESC
    </select>

    <!--统计数据所属行业子类-->
    <select id="industryData" resultType="com.bootdo.datas.dto.NameAndValueDTO">
        SELECT
            b.DATA_INDUSTRY_TWO AS name,
            COUNT( a.ID ) AS value
        FROM
            gy_data AS a ,
            gy_unit AS b
        <where>
            <if test="city != null and city != ''">  AND b.UNIT_CITY = #{city} </if>
            <if test="auditStage != null and auditStage != ''">and a.AUDIT_STAGE  = #{auditStage}</if>
            AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS = '3'
        </where>
        GROUP BY
            b.DATA_INDUSTRY_TWO
        ORDER BY
            value DESC
    </select>

    <!--统计数据所属级别-->
    <select id="dataLevelData" resultType="com.bootdo.datas.dto.DateAndCountDTO">
        SELECT
            COUNT( a.ID ) AS amount,
            date(DATE_FORMAT( a.UPLOAD_TIME, '%Y-%m-%d' ))  AS date
        FROM
            gy_data AS a ,
            gy_unit AS b
        WHERE
            DATE_SUB( CURDATE(), INTERVAL 7 DAY ) &lt;= date(
            DATE_FORMAT( a.UPLOAD_TIME, '%Y-%m-%d' ))
             <if test="dataLevel != null and dataLevel != ''">  AND a.DATA_LEVEL = #{dataLevel} </if>
             <if test="city != null and city != ''">  AND b.UNIT_CITY = #{city} </if>
            <if test="auditStage != null and auditStage != ''">and a.AUDIT_STAGE  = #{auditStage}</if>
             AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS = '3'
        GROUP BY
            DATE_FORMAT(
            a.UPLOAD_TIME,
            '%Y-%m-%d')
        ORDER BY
            date DESC
    </select>

    <!--各市时需要的统计  计算审核通过率-->
    <select id="dataAuditData" resultType="java.lang.Double">
        SELECT
            ROUND( currentCount / allData * 100, 1 )
        FROM
            ( SELECT COUNT( a.ID ) AS allData FROM gy_data AS a ,
                gy_unit AS b
        WHERE
                 b.UNIT_CITY = #{city}
                AND b.CREDIT_CODE = a.GY_NUIT_ID
                AND a.`DELETE_STATUS` != 1) AS allData,
            ( SELECT COUNT( a.ID ) AS currentCount FROM gy_data AS a ,
                gy_unit AS b
        WHERE
                 b.UNIT_CITY = #{city}
                AND b.CREDIT_CODE = a.GY_NUIT_ID
        <if test="auditStage != null and auditStage != ''">and a.AUDIT_STAGE  = #{auditStage}</if>
                AND a.DATA_STATUS = '3'
                AND a.DELETE_STATUS != '1'
              ) AS currentCount
    </select>

    <!--各市时需要的统计  统计数据所属类别-->
    <select id="dataCategoryData" resultType="com.bootdo.datas.dto.NameAndValueDTO">
        SELECT
            a.DATA_TYPE_ONE AS name,
            COUNT( a.ID ) AS value
        FROM
            gy_data AS a ,
	        gy_unit AS b
        WHERE
             b.UNIT_CITY = #{city}
             AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
        GROUP BY
            a.DATA_TYPE_ONE
        ORDER BY
            value DESC
    </select>


</mapper>