<?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="getByCondition" resultType="com.bootdo.datas.domain.DataDO">
        SELECT
        a.ID as id,a.DATA_SOURCE as dataSource,a.DATA_NAME as dataName,a.DATA_LEVEL as dataLevel,a.DATA_TYPE as
        dataType,a.DATA_SUB as dataSub,
        a.DATA_ADJ_SUB as dataAdjSub,a.DATA_SCOPE as dataScope,a.DATA_RESULT as dataResult,a.DATA_LOCATION as
        dataLocation,a.DATA_RANGE as dataRange,
        a.DATA_RULER as dataRuler,a.SHARE_RANGE as shareRange,a.CROSS_RANGE as crossRange,a.DATA_SAFETY as
        dataSafety,a.DATA_DESC as dataDesc,
        a.DATA_STATUS as dataStatus,a.UPLOAD_USER as uploadUser,a.UPLOAD_USERID as uploadUserid,a.UPLOAD_TIME as
        uploadTime,
        a.UPDATE_TIME as updateTime,a.LIAISE_MAN as liaiseMan,a.LIAISE_WAY as liaiseWay,a.REPORTER_UNIT as reporterUnit,
        IFNULL(a.DATA_MANAGER,'/') AS dataManager,IFNULL(a.DATA_PRECISION,'/') AS dataPrecision,
        b.`DATA_INDUSTRY` as dataIndustry,b.`UNIT_PROVINCE` as unitProvince,b.`UNIT_CITY` as unitCity ,b.`UNIT_OTHER` as
        unitOther,b.`INDUSTRY_TYPE` as industryType
        FROM tf_data a,tf_unit b
        <where>
            a.REPORTER_UNIT = b.REPORTER_UNIT
            AND a.UNIT_PROVINCE = b.UNIT_PROVINCE
            AND a.UNIT_CITY = b.UNIT_CITY
            AND a.UNIT_OTHER = b.UNIT_OTHER
            AND a.DATA_INDUSTRY = b.DATA_INDUSTRY
            AND a.INDUSTRY_TYPE = b.INDUSTRY_TYPE
            <!--正常未删除状态-->
            AND a.DELETE_STATUS = '0'
            <if test="dataSource != null and dataSource != ''">and a.DATA_SOURCE = #{dataSource}</if>
            <if test="dataName != null and dataName != ''"> and DATA_NAME = #{dataName} </if>
<!--            <if test="dataName != null and dataName != ''">and a.DATA_NAME like CONCAT('%',#{dataName},'%')</if>-->
            <if test="reporterUnit != null and reporterUnit != ''">and a.REPORTER_UNIT like
                CONCAT('%',#{reporterUnit},'%')
            </if>
            <if test="unitProvince != null and unitProvince != ''">and b.UNIT_PROVINCE = #{unitProvince}</if>
            <if test="unitCity != null and unitCity != ''">and (b.UNIT_CITY = #{unitCity} or b.UNIT_OTHER = #{unitCity})</if>
            <if test="dataIndustry != null and dataIndustry != ''">and b.DATA_INDUSTRY = #{dataIndustry}</if>
            <if test="industryType != null and industryType != ''">and a.INDUSTRY_TYPE  = #{industryType}</if>
       </where>
   </select>

    <!--统计数据 全国地图 根据数据级别-->
    <select id="mapDataList" resultType="com.bootdo.datas.dto.MapDataDTO" parameterType="java.lang.String">
        SELECT
         b.UNIT_PROVINCE  as provinceName,
         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>
             and a.DATA_STATUS = '3'
            AND a.DELETE_STATUS != '1'
            AND b.STATUS = '0'
        </where>
        GROUP BY
            b.UNIT_PROVINCE
        ORDER BY
            amount DESC
    </select>

<!--        根据条件获取 备案 审批通过数据-->
    <select id="portalDataList" resultType="com.bootdo.datas.domain.DataDO">
        SELECT
        a.ID as id,a.DATA_SOURCE as dataSource,a.DATA_NAME as dataName,a.DATA_LEVEL as dataLevel,a.DATA_TYPE as
        dataType,a.DATA_SUB as dataSub,
        a.DATA_ADJ_SUB as dataAdjSub,a.DATA_SCOPE as dataScope,a.DATA_RESULT as dataResult,a.DATA_LOCATION as
        dataLocation,a.DATA_RANGE as dataRange,
        a.DATA_RULER as dataRuler,a.SHARE_RANGE as shareRange,a.CROSS_RANGE as crossRange,a.DATA_SAFETY as
        dataSafety,a.DATA_DESC as dataDesc,
        a.DATA_STATUS as dataStatus,a.UPLOAD_USER as uploadUser,a.UPLOAD_USERID as uploadUserid,a.UPLOAD_TIME as
        uploadTime,
        a.UPDATE_TIME as updateTime,a.LIAISE_MAN as liaiseMan,a.LIAISE_WAY as liaiseWay,a.REPORTER_UNIT as reporterUnit,
        IFNULL(a.DATA_MANAGER,'/') AS dataManager,IFNULL(a.DATA_PRECISION,'/') AS dataPrecision,
        b.`DATA_INDUSTRY` as dataIndustry,b.`UNIT_PROVINCE` as unitProvince,b.`UNIT_CITY` as unitCity ,b.`UNIT_OTHER` as
        unitOther,b.`INDUSTRY_TYPE` as industryType
        FROM tf_data a,tf_unit b
        <where>
            a.REPORTER_UNIT = b.REPORTER_UNIT
            AND a.UNIT_PROVINCE = b.UNIT_PROVINCE
            AND a.UNIT_CITY = b.UNIT_CITY
            AND a.UNIT_OTHER = b.UNIT_OTHER
            AND a.DATA_INDUSTRY = b.DATA_INDUSTRY
            AND a.INDUSTRY_TYPE = b.INDUSTRY_TYPE
            AND a.UPLOAD_USERID = b.UPLOAD_USERID


            <if test="dataName != null and dataName != ''"> and a.DATA_NAME = #{dataName} </if>
            <if test="reporterUnit != null and reporterUnit != ''"> and a.REPORTER_UNIT = #{reporterUnit} </if>
            <if test="industryType != null and industryType != ''">and a.INDUSTRY_TYPE  = #{industryType}</if>
<!--            <if test="dataName != null and dataName != ''">and a.DATA_NAME like CONCAT('%',#{dataName},'%')</if>-->
            and a.DATA_STATUS = '3'
            and a.DELETE_STATUS = '0'
        </where>
         order by ID desc
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

<!--    根据条件获取 备案 审批通过数据 总数量-->
    <select id="portalDataListCount" resultType="java.lang.Long">
        SELECT
        COUNT(DISTINCT a.ID)
        FROM tf_data a,tf_unit b
        <where>
            a.`REPORTER_UNIT` = b.`REPORTER_UNIT`
            AND a.`UNIT_PROVINCE` = b.`UNIT_PROVINCE`
            AND a.`UNIT_CITY` = b.`UNIT_CITY`
            AND a.`UNIT_OTHER` = b.`UNIT_OTHER`
            AND a.`DATA_INDUSTRY` = b.`DATA_INDUSTRY`
            AND a.`INDUSTRY_TYPE` = b.`INDUSTRY_TYPE`
            AND a.`UPLOAD_USERID` = b.`UPLOAD_USERID`

            <if test="dataName != null and dataName != ''"> and a.DATA_NAME = #{dataName} </if>
            <if test="reporterUnit != null and reporterUnit != ''"> and a.REPORTER_UNIT = #{reporterUnit} </if>
            <if test="industryType != null and industryType != ''">and a.INDUSTRY_TYPE  = #{industryType}</if>
<!--            <if test="dataName != null and dataName != ''">and a.DATA_NAME like CONCAT('%',#{dataName},'%')</if>-->
            and a.DATA_STATUS = '3'
            and a.DELETE_STATUS = '0'

        </where>
    </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="provinceName == null or provinceName == ''">  and a.DATA_STATUS = '3' </if>
            <if test="provinceName != null and provinceName != ''">  AND b.UNIT_PROVINCE = #{provinceName} </if>
            AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
            AND b.STATUS = '0'
        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="provinceName == null or provinceName == ''">  and a.DATA_STATUS = '3' </if>
            <if test="provinceName != null and provinceName != ''">  AND b.UNIT_PROVINCE = #{provinceName} </if>
            AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
            AND b.STATUS = '0'
        </where>
        GROUP BY
            b.DATA_INDUSTRY_TWO
        ORDER BY
            value DESC
    </select>

    <!--统计数据所属行业-->
    <select id="industryTypeData" resultType="com.bootdo.datas.dto.NameAndValueDTO">
        SELECT
            b.INDUSTRY_TYPE_ONE AS name,
            COUNT( a.ID ) AS value
        FROM
            gy_data AS a ,
            gy_unit AS b
        <where>
            <if test="provinceName == null or provinceName == ''">  and a.DATA_STATUS = '3' </if>
            <if test="provinceName != null and provinceName != ''">  AND b.UNIT_PROVINCE = #{provinceName} </if>
            AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
            AND b.STATUS = '0'
        </where>
        GROUP BY
            b.INDUSTRY_TYPE_ONE
        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="provinceName != null and provinceName != ''">  AND b.UNIT_PROVINCE = #{provinceName} </if>
             <if test="provinceName == null or provinceName == ''">  and a.DATA_STATUS = '3' </if>
             AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
            AND b.STATUS = '0'
        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_PROVINCE = #{provinceName}
                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_PROVINCE = #{provinceName}
                AND b.CREDIT_CODE = a.GY_NUIT_ID
                AND a.DATA_STATUS = '3'
                AND a.DELETE_STATUS != '1'
                AND b.STATUS = '0') 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_PROVINCE = #{provinceName}
             AND b.CREDIT_CODE = a.GY_NUIT_ID
            AND a.DELETE_STATUS != '1'
            AND a.DATA_STATUS != '4'
            AND b.STATUS = '0'
        GROUP BY
            a.DATA_TYPE_ONE
        ORDER BY
            value DESC
    </select>

    <!--查询备案信息和企业信息-->
    <select id="query" resultType="com.bootdo.datas.domain.DataDO">
        SELECT
        a.ID as id,a.DATA_SOURCE as dataSource,a.DATA_NAME as dataName,a.DATA_LEVEL as dataLevel,a.DATA_TYPE as
        dataType,a.DATA_SUB as dataSub,
        a.DATA_ADJ_SUB as dataAdjSub,a.DATA_SCOPE as dataScope,a.DATA_RESULT as dataResult,a.DATA_LOCATION as
        dataLocation,a.DATA_RANGE as dataRange,
        a.DATA_RULER as dataRuler,a.SHARE_RANGE as shareRange,a.CROSS_RANGE as crossRange,a.DATA_SAFETY as
        dataSafety,a.DATA_DESC as dataDesc,
        a.DATA_STATUS as dataStatus,a.UPLOAD_USER as uploadUser,a.UPLOAD_USERID as uploadUserid,a.UPLOAD_TIME as
        uploadTime,
        a.UPDATE_TIME as updateTime,a.LIAISE_MAN as liaiseMan,a.LIAISE_WAY as liaiseWay,a.REPORTER_UNIT as reporterUnit,
        a.UNIONS_TATUS as unionStatus,a.UNION_DESC as unionDesc,
        IFNULL(a.DATA_MANAGER,'/') AS dataManager,IFNULL(a.DATA_PRECISION,'/') AS dataPrecision,
        b.`DATA_INDUSTRY` as dataIndustry,b.`UNIT_PROVINCE` as unitProvince,b.`UNIT_CITY` as unitCity ,b.`UNIT_OTHER` as
        unitOther,b.`INDUSTRY_TYPE` as industryType
        FROM tf_data a,tf_unit b
        <where>
            a.`REPORTER_UNIT` = b.`REPORTER_UNIT`
            AND a.`UNIT_PROVINCE` = b.`UNIT_PROVINCE`
            AND a.`UNIT_CITY` = b.`UNIT_CITY`
            AND a.`UNIT_OTHER` = b.`UNIT_OTHER`
            AND a.`DATA_INDUSTRY` = b.`DATA_INDUSTRY`
            AND a.`INDUSTRY_TYPE` = b.`INDUSTRY_TYPE`
            AND a.`UPLOAD_USERID` = b.`UPLOAD_USERID`
            AND a.`DELETE_STATUS` != 1
            and a.ID = #{value}
        </where>

    </select>

</mapper>