<?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.nantian.cloud.dbcapp.mapper.record.DataHomeMapper">


    <select id="getData" resultType="com.nantian.cloud.dbcapp.vo.DataHomeDataVo">
        SELECT dr.tname region,i.cmpName enterpriseName,i.enterpriseCode enterpriseCode,ROUND(SUM(case when tu.guideline_val >= 0 then tu.guideline_val else 0 end)/10000,4) AS value, <![CDATA[ IF(MIN(IFNULL(tu.guideline_val,0)) >= 0 and MAX(tu.guideline_val) < 1000000,'0','1') ]]> exception,
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end trade1,
        case when #{tradeStr} like concat('%',i.productTrade,'%') then productTrade else '其他' end trade2
        FROM
        energy_record.enterprise_info i
        LEFT JOIN dict_industry_code dic ON i.industryCode = dic.id
        LEFT JOIN dict_region dr ON dr.CODE = CONCAT( LEFT ( i.regionCode, 4 ), '00')
        LEFT JOIN t_unitenergy2020 tu ON tu.enterprise_id = i.enterpriseCode  AND tu.code = '00000000' AND tu.energy_type_code in ('01','02') AND tu.energyTypeCode = '0000' AND LEFT(tu.record_circle,7) BETWEEN #{startTime} AND #{endTime}
        WHERE
        JSON_EXTRACT( i.reportNum, '$.accomplish' ) = '已完成'
        <if test="regionCode != null and regionCode != ''">
            AND CONCAT( LEFT ( i.regionCode, 4 ), '00' ) = #{regionCode}
        </if>
        <if test="tradeType != null and tradeType != ''">
            <choose>
                <when test="tradeType == '1'.toString()"> and SUBSTR(dic.code,2,2) = #{tradeCode} </when>
                <when test="tradeType == '2'.toString()"> and i.productTrade = #{tradeCode} </when>
            </choose>
        </if>
        GROUP BY i.enterpriseCode
        ORDER BY value DESC
    </select>

    <sql id="base_trade1_ranking">
        select i.enterpriseCode, tu.record_circle, tu.guideline_val, dic.meaning, dic.tname, SUBSTR(dic.code,2,2) code,
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end name
        from enterprise_info i
        left join dict_industry_code dic on dic.id = i.industryCode
        left join t_unitenergy2020 tu on tu.enterprise_id = i.enterpriseCode and tu.record_circle between #{startTime} and #{endTime}
        and tu.energyTypeCode = '0000' and tu.code='00000000' and tu.isRight = "1" and tu.guideline_val > 0 and tu.guideline_val &lt; 1000000
        where JSON_EXTRACT( i.reportNum, '$.accomplish' ) = '已完成' and SUBSTR(dic.code,2,2) in (25,26,30,31,32,44)
    </sql>

    <sql id="base_trade2_ranking">
        select i.enterpriseCode, tu.record_circle, tu.guideline_val, i.productTrade code, i.productTrade name
        from enterprise_info i
        left join t_unitenergy2020 tu on tu.enterprise_id = i.enterpriseCode and tu.record_circle between #{startTime} and #{endTime}
        and tu.energyTypeCode = '0000' and tu.code='00000000' and tu.isRight = "1" and tu.guideline_val > 0 and tu.guideline_val &lt; 1000000
        where JSON_EXTRACT( i.reportNum, '$.accomplish' ) = '已完成' and #{tradeStr} like concat('%',i.productTrade,'%')
    </sql>

    <select id="getTradeRanking" resultType="java.util.Map">
        select code, name, count(distinct enterpriseCode) epcNum, ROUND(SUM(guideline_val)/10000,4) tce from (
            <choose>
                <when test="tradeType == '1'.toString()"> <include refid="base_trade1_ranking"></include> </when>
                <when test="tradeType == '2'.toString()"> <include refid="base_trade2_ranking"></include> </when>
            </choose>
        ) tmp group by code order by tce desc
    </select>

    <select id="getTradeTotal" resultType="java.util.Map">
        select '行业合计值' code, '行业合计值' name, count(distinct enterpriseCode) epcNum, ROUND(SUM(guideline_val)/10000,4) tce from (
            <choose>
                <when test="tradeType == '1'.toString()"> <include refid="base_trade1_ranking"></include> </when>
                <when test="tradeType == '2'.toString()"> <include refid="base_trade2_ranking"></include> </when>
            </choose>
        ) tmp
    </select>

    <sql id="base_trend">
        select dr.code regionCode, dr.tname regionName, i.enterpriseCode, i.cmpName enterpriseName, SUBSTR(dic.code,2,2) trade1Code,
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end trade1,
        case when #{tradeStr} like concat('%',i.productTrade,'%') then productTrade else '其他' end trade2
        <foreach collection="yearArr" index="i" item="date">
            , ( select sum(guideline_val)
            <choose>
                <when test="date >= '2020'.toString()">
                    from t_unitenergy2020 where 1 = 1 and code='00000000' and isRight = "1"
                </when>
                <otherwise>
                    from t_unitenergy2019 where 1 = 1
                </otherwise>
            </choose>
             and enterprise_id = i.enterpriseCode and record_circle like concat(#{date},"%") and energyTypeCode = '0000' and guideline_val > 0 and guideline_val &lt; 1000000) value${i+1}
        </foreach>
        from enterprise_info i
        left join dict_region dr on dr.code = concat(left(i.regionCode,4),'00')
        left join dict_industry_code dic on dic.id = i.industryCode
        where JSON_EXTRACT( i.reportNum, '$.accomplish' ) = '已完成' having value1 != 0 and value2 != 0 and value3 != 0
    </sql>

    <select id="getTradeTrends" resultType="java.util.Map">
        select code, name, count(distinct enterpriseCode) epcNum, ROUND((SUM(value1)+SUM(value2)+SUM(value3))/10000,4) value, ROUND(SUM(value1)/10000,4) value1, ROUND(SUM(value2)/10000,4) value2, ROUND(SUM(value3)/10000,4) value3 from (
            select enterpriseCode, SUM(value1) as value1, SUM(value2) as value2, SUM(value3) as value3,
            <choose>
                <when test="tradeType == '1'.toString()"> trade1Code code, trade1 name </when>
                <when test="tradeType == '2'.toString()"> trade2 code, trade2 name </when>
            </choose>
            from (
                <include refid="base_trend"></include>
            ) tmp where 1 = 1
            <choose>
                <when test="tradeType == '1'.toString()"> and trade1Code in (25,26,30,31,32,44) </when>
                <when test="tradeType == '2'.toString()"> and #{tradeStr} like concat('%',trade2,'%') </when>
            </choose>
            group by enterpriseCode
        ) tmp group by code order by value3 desc
    </select>

    <select id="getRegionTrends" resultType="java.util.Map">
        select regionCode, regionName, count(distinct enterpriseCode) epcNum,
        ROUND((SUM(value1)+SUM(value2)+SUM(value3))/10000,4) value, ROUND(SUM(value1)/10000,4) as value1, ROUND(SUM(value2)/10000,4) as value2, ROUND(SUM(value3)/10000,4) as value3
        from (
            <include refid="base_trend"></include>
        ) tmp
        group by regionCode order by value3 desc
    </select>


    <sql id="Base_Electricity_Data">
        SELECT dr.code regionCode, dr.tname region, i.cmpName enterpriseName,i.enterpriseCode enterpriseCode,tes.record_circle, SUBSTR(dic.code,2,2) trade1Code,
        ROUND(SUM(tes.consume_sum)/10000,4) zxElectricityValue,
        ROUND(SUM(tedv.dataValue)/10000,4) AS dwElectricityValue,
        ROUND(ABS(SUM(IF(tedv.dataValue IS NULL,0,tedv.dataValue)) - SUM(IF(tes.consume_sum IS NULL,0,tes.consume_sum)))/10000,4) margin,
        <![CDATA[ IF(MIN(tes.consume_sum) > 0 and MAX(tes.consume_sum) < 1000000,"0","1") exception,]]>
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end trade1,
        case when #{tradeStr} like concat('%',i.productTrade,'%') then productTrade else '其他' end trade2
        FROM
        energy_record.enterprise_info i
        LEFT JOIN dict_industry_code dic ON i.industryCode = dic.id
        LEFT JOIN dict_region dr ON dr.CODE = CONCAT( LEFT ( i.regionCode, 4 ), "00" )
        LEFT JOIN (
        SELECT enterpriseCode, CASE WHEN GROUP_CONCAT(dataCode) LIKE '%0000000002330011%' THEN '0000000002330011' WHEN GROUP_CONCAT(dataCode) LIKE '%0000000002330020%' THEN '0000000002330020' WHEN GROUP_CONCAT(dataCode) LIKE '%0000000002330021%' THEN '0000000002330021' ELSE '0000000002330010' END dataCode
        FROM configure_data_collect
        WHERE dataCode in ("0000000002330011", "0000000002330020", "0000000002330021", "0000000002330010")
        GROUP BY enterpriseCode
        ) cd ON i.enterpriseCode = cd.enterpriseCode
        LEFT JOIN t_energy_structure tes ON i.enterpriseCode = tes.enterprise_id AND cd.dataCode = tes.dataCode AND LEFT(tes.record_circle,7) BETWEEN #{startTime} AND #{endTime}
        LEFT JOIN t_electricity_data_value tedv ON i.enterpriseCode = tedv.enterpriseCode AND tedv.statDate = tes.record_circle
        WHERE JSON_EXTRACT( i.reportNum, "$.accomplish" ) = "已完成"
        <if test="regionCode != null and regionCode != ''">
            AND CONCAT( LEFT ( i.regionCode, 4 ), '00' ) = #{regionCode}
        </if>
        <if test="tradeType != null and tradeType != ''">
            <choose>
                <when test="tradeType == '1'.toString()">
                    and SUBSTR(dic.code,2,2) in (25,26,30,31,32,44)
                    <if test="tradeCode != null and tradeCode != ''">
                        and SUBSTR(dic.code,2,2) = #{tradeCode}
                    </if>
                </when>
                <when test="tradeType == '2'.toString()">
                    and #{tradeStr} like concat('%',i.productTrade,'%')
                    <if test="tradeCode != null and tradeCode != ''">
                        and i.productTrade = #{tradeCode}
                    </if>
                </when>
            </choose>
        </if>
    </sql>

    <sql id="Base_Electricity_Column">
        ROUND(SUM(zxElectricityValue),4) zxElectricityValue, ROUND(SUM(dwElectricityValue),4) dwElectricityValue, ABS(IF(SUM(zxElectricityValue) IS NULL, 0, SUM(zxElectricityValue)) - IF(SUM(dwElectricityValue) IS NULL , 0, SUM(dwElectricityValue))) margin
    </sql>

    <select id="getElectricityData" resultType="com.nantian.cloud.dbcapp.vo.DataHomeDataVo">
        <include refid="Base_Electricity_Data"></include>
        GROUP BY i.enterpriseCode
        ORDER BY margin DESC
    </select>

    <select id="getElectricityDatas" resultType="java.util.Map">
        <choose>
            <when test="type == '1'.toString()"> SELECT record_circle statDate, <include refid="Base_Electricity_Column"></include></when>
            <when test="type == '2'.toString()"> SELECT regionCode, region regionName, <include refid="Base_Electricity_Column"></include> </when>
            <when test="type == '3'.toString() and tradeType == '1'.toString()"> SELECT trade1Code tradeCode, trade1 tradeName, <include refid="Base_Electricity_Column"></include> </when>
            <when test="type == '3'.toString() and tradeType == '2'.toString()"> SELECT trade2 tradeCode, trade2 tradeName, <include refid="Base_Electricity_Column"></include> </when>
        </choose>
        FROM
        (<include refid="Base_Electricity_Data"></include>
        GROUP BY i.enterpriseCode,tes.record_circle ORDER BY margin DESC ) result
        WHERE record_circle IS NOT NULL
        <choose>
            <when test="type == '1'.toString()"> GROUP BY record_circle ORDER BY record_circle </when>
            <when test="type == '2'.toString()"> GROUP BY region ORDER BY zxElectricityValue DESC </when>
            <when test="type == '3'.toString() and tradeType == '1'.toString()"> GROUP BY trade1 ORDER BY zxElectricityValue DESC </when>
            <when test="type == '3'.toString() and tradeType == '2'.toString()"> GROUP BY trade2 ORDER BY zxElectricityValue DESC </when>
        </choose>
    </select>

    <select id="getTrendsDetail" resultType="com.nantian.cloud.dbcapp.vo.DataHomeDataVo">
        select regionName region, enterpriseCode, enterpriseName, ROUND((SUM(value1)+SUM(value2)+SUM(value3))/10000,4) value, trade1, trade2,
        ROUND(SUM(value1)/10000,4) as value1, ROUND(SUM(value2)/10000,4) as value2, ROUND(SUM(value3)/10000,4) as value3
        from (
            <include refid="base_trend"></include>
            <if test="regionCode != null and regionCode != ''">
                and concat( left(regionCode,4), '00' ) = #{regionCode}
            </if>
            <if test="tradeType != null and tradeType != ''">
                <choose>
                    <when test="tradeType == '1'.toString()"> and trade1Code = #{tradeCode} </when>
                    <when test="tradeType == '2'.toString()"> and trade2 = #{tradeCode} </when>
                </choose>
            </if>
        ) tmp group by enterpriseCode order by value3 desc
    </select>

    <sql id="base_energy_consumption">
        select dr.tname region, i.enterpriseCode, i.cmpName enterpriseName, te.record_circle, te.energy_code, te.energy_name, te.energy_consumption, SUBSTR(dic.code,2,2) trade1Code,
        case when #{energyStr} like concat('%',te.energy_category,'%') then te.energy_category else '其他' end energy_category,
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end trade1,
        case when #{tradeStr} like concat('%',i.productTrade,'%') then productTrade else '其他' end trade2
        from enterprise_info i
        left join dict_industry_code dic on i.industryCode = dic.id
        left join dict_region dr on dr.code = concat(left(i.regionCode,4),'00')
        left join t_energy_consumption te on i.enterpriseCode = te.enterprise_code and te.record_circle between #{startTime} and #{endTime}
        where JSON_EXTRACT( i.reportNum, '$.accomplish' ) = '已完成'
    </sql>

    <select id="getEnergyConsumptionRatio" resultType="java.util.Map">
        select energy_category energyName, GROUP_CONCAT(distinct energy_code) energyCode, round(sum(energy_consumption)/10000,4) energyConsumption
        from(
            <include refid="base_energy_consumption"></include>
            and energy_category != '其他'  and energy_consumption > 0 and energy_consumption &lt; 1000000
        ) tmp group by energy_category order by energyConsumption desc
    </select>

    <select id="getEnergyConsumptionTrends" resultType="java.util.Map">
        select
        <choose>
            <when test="type == '2'.toString()"> record_circle name, record_circle code</when>
            <when test="type == '3'.toString() and tradeType == '1'.toString()"> trade1 name, trade1Code code </when>
            <when test="type == '3'.toString() and tradeType == '2'.toString()"> trade2 name, trade2 code </when>
        </choose>
        <foreach collection="list" item="item" index="i">
            , round(sum(case when energy_category = #{item} then energy_consumption else 0 end)/10000,4) energyConsumption${i+1}
        </foreach>
        from (
            <include refid="base_energy_consumption"></include>
            and energy_consumption > 0 and energy_consumption &lt; 1000000
            <choose>
                <when test="type == '3'.toString() and tradeType == '1'.toString()"> and SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) </when>
                <when test="type == '3'.toString() and tradeType == '2'.toString()"> and #{tradeStr} like concat('%',i.productTrade,'%') </when>
            </choose>
        ) tmp
        <choose>
            <when test="type == '2'.toString()"> group by record_circle </when>
            <when test="type == '3'.toString() and tradeType == '1'.toString()"> group by trade1 </when>
            <when test="type == '3'.toString() and tradeType == '2'.toString()"> group by trade2 </when>
        </choose>
        having code is not null order by code asc
    </select>

    <select id="getEnergyConsumptionDetail" resultType="com.nantian.cloud.dbcapp.vo.DataHomeDataVo">
        select region, enterpriseCode, enterpriseName, trade1, trade2, round(sum(energy_consumption)/10000,4) value, IF(MIN(IFNULL(energy_consumption,0)) >= 0 and MAX(energy_consumption) &lt; 1000000,'0','1') exception
        from(
            <include refid="base_energy_consumption"></include>
            and te.energy_category = #{energyName}
            <if test="tradeType != null and tradeType != ''">
                <choose>
                    <when test="tradeType == '1'.toString()"> and SUBSTR(dic.code,2,2) = #{tradeCode} </when>
                    <when test="tradeType == '2'.toString()"> and i.productTrade = #{tradeCode} </when>
                </choose>
            </if>
        ) tmp group by enterpriseCode order by value desc
    </select>
    <sql id="Base_Quota_Value">
        SELECT
            dr.tname region,CONCAT( LEFT ( i.regionCode, 4 ), "00" ) regionCode,i.cmpName enterpriseName,i.enterpriseCode enterpriseCode,tu.record_circle AS statDate,tu.quota_name AS quotaName,tu.guideline_val AS quotaValue,ts.limit_value AS limitValue,CASE WHEN IFNULL( tu.guideline_val, 0 ) > ts.limit_value THEN "1" ELSE "0" END exception,i.productTrade,i.industryCode
        FROM
            energy_record.enterprise_info i
            LEFT JOIN dict_region dr ON dr.CODE = CONCAT( LEFT ( i.regionCode, 4 ), "00" )
            LEFT JOIN t_unitvalue tu ON tu.enterprise_id= i.enterpriseCode AND tu.record_circle BETWEEN #{startTime} AND #{endTime}
            LEFT JOIN data_quality_status d ON i.enterpriseCode = d.enterprise_code
            LEFT JOIN t_standard ts ON tu.quota_name = ts.quota_name AND d.quality_industry = ts.code
            LEFT JOIN formul f ON f.enterprise_code = i.enterpriseCode AND f.formul_name = tu.quota_name AND f.is_use = "1" AND length(f.product_data_code)>=16
        WHERE JSON_EXTRACT( i.reportNum, "$.accomplish" ) = "已完成"
    </sql>

    <select id="getOverLimit" resultType="java.util.Map">
        SELECT DISTINCT region,regionCode,COUNT(DISTINCT (CASE WHEN exception = "1" THEN enterpriseCode END)) exceptionNum,COUNT(DISTINCT enterpriseCode) totalNum, COUNT(DISTINCT enterpriseCode) - COUNT(DISTINCT (CASE WHEN exception = "1" THEN enterpriseCode END)) normalNum
        FROM (<include refid="Base_Quota_Value"></include>) r1
        GROUP BY regionCode
        ORDER BY totalNum DESC
    </select>

    <select id="getOverLimitDetail" resultType="java.util.Map">
        SELECT region,enterpriseName,enterpriseCode,IFNULL(SUM(CASE WHEN exception = "1" THEN 1 END ),0) exceptionNum, IFNULL(SUM(CASE WHEN exception = "0" THEN 1 END ),0) normalNum,COUNT(1) Num,
        case when SUBSTR(dic.code,2,2) in (25,26,30,31,32,44) then ( select meaning from dict_industry_code where LENGTH(code)=3 and code like concat('%',SUBSTR(dic.code,2,2)) ) else '其他' end trade1,
        case when #{tradeStr} like concat('%',productTrade,'%') then productTrade else '其他' end trade2
        FROM (<include refid="Base_Quota_Value"></include>) r1
        LEFT JOIN dict_industry_code dic ON r1.industryCode = dic.id
        WHERE regionCode = #{regionCode}
        <if test="type != null and type != ''">
            AND exception = #{type}
        </if>
        GROUP BY enterpriseCode
        ORDER BY Num DESC
    </select>
</mapper>
