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



    <select id="getTotal" resultType="java.lang.Integer">
        select  COUNT( DISTINCT enterpriseCode) from (
        SELECT a.enterpriseCode  FROM electric_enterprise_info a
        where 1=1
        <if test="area !=null" >
            and a.regionCode LIKE concat(#{area},"%")
        </if>
        <if test="inde1!=null">
            and a.industry_code = #{inde1}
        </if>

        union all
        SELECT t.enterprise_code  FROM t_enterprise_old t
        where 1=1
        and state =0
        <if test="area !=null" >
            and t.region_code like concat(#{area},"%")
        </if>
        <if test="inde1!=null">
            and t.industryCode = #{inde1}
        </if>
        union all
        SELECT d.enterpriseCode  FROM enterprise_info d
        where 1=1
        <if test="area !=null" >
            and d.regionCode like concat(#{area},"%")
        </if>
        <if test="inde1!=null">
            and d.tid = #{inde1}
        </if>
        ) t
    </select>

    <select id="getInfo" resultType="java.lang.Integer">
       SELECT count(enterpriseCode)  FROM electric_enterprise_info
    </select>

    <select id="selectElectricityData2019" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
        SELECT
        a.record_circle as statDate,
        a.enterprise_id as enterpriseCode,
        sum(
        CASE a.quota_unit
        WHEN '万千瓦·时' THEN  a.guideline_val*10000
        WHEN '千瓦·时' THEN  a.guideline_val
        ELSE a.guideline_val END
        ) as dataValue
        FROM
        t_unitenergy2019 a LEFT JOIN t_enterprise_old b on a.entId = b.entId
        WHERE
        a.entid = a.unitId
        <if test="area!=null">
           and  b.region_code like #{area}
        </if>
        <if test="industry!=null">
            and b.industryCode = #{industry}
        </if>

        AND a.record_circle BETWEEN #{preTime} and #{endTime}
        AND a.CODE LIKE 'JZNH%'
        AND a.energyName IN ( "电", "电力" )
        AND a.guideline_val > 0
        AND a.guideline_val &lt; 100000000
        GROUP BY
        a.record_circle
    </select>

    <select id="selectElectricityData2020" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
        select DISTINCT a.dataValue,a.statDate,a.enterpriseCode from t_electricity_data_value as a join electric_enterprise_info as b on a.enterpriseCode
      =b.enterpriseCode
      <where>
          <if test="area!=null">
              b.regionCode like #{area}
          </if>
          <if test="industry!=null">
              and b.industry_code = #{industry}
          </if>
           and a.statDate between #{preTime} and #{endTime}
          AND a.dataValue between 0 and 1000000000
      </where>
    </select>

    <select id="selectEnterpriseData2020" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
         select DISTINCT dataValue,statDate,enterpriseCode from t_electricity_data_value where enterpriseCode = #{enterpriseCode} and statDate between #{preTime} and #{endTime}
         order by statDate
    </select>
    <select id="selectEnterpriseData2019" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
        SELECT a.record_circle as statDate, a.enterprise_id as enterpriseCode, sum(
            CASE a.quota_unit WHEN '万千瓦·时' THEN  a.guideline_val*10000
            WHEN '千瓦·时' THEN  a.guideline_val ELSE a.guideline_val END
        ) as dataValue FROM t_unitenergy2019 a
        LEFT JOIN t_enterprise_old b on a.entId = b.entId
        LEFT JOIN ( SELECT unitId, IFNULL(id,0) processCode FROM history_energy_accountunit group by unitId ) c on a.entId = c.unitId
        where 1 = 1 and a.unitId = c.processCode
        and a.enterprise_id = #{enterpriseCode} AND a.record_circle BETWEEN #{preTime} and #{endTime}
        AND a.CODE LIKE 'JZNH%' AND a.energyName IN ( "电", "电力" )
        AND a.guideline_val > 0 AND a.guideline_val &lt; 1000000000
        GROUP BY a.record_circle
    </select>

     <select id="countCmpNum2019" resultType="int">
         select count(distinct enterprise_code) from  t_enterprise_old
         <where>
             <if test="area!=null">
                 region_code like #{area}
             </if>
             <if test="industry!=null">
                 and industryCode = #{industry}
             </if>
         </where>
     </select>

    <select id="countCmpNum2020" resultType="int">
        select count(distinct enterpriseCode) from electric_enterprise_info
        <where>
            <if test="area!=null">
                regionCode like #{area}
            </if>
            <if test="industry!=null">
                and industry_code = #{industry}
            </if>
        </where>
    </select>


    <select id="selectByDay" resultType="com.nantian.cloud.dbcapp.dto.ElectricityDataDto">
        select Sum(`data`) as `value`,`time` as `month` from company_data_detail where enterpriseCode = #{param1} and `time` like #{param2}
        group by `time` order by `time`
    </select>

    <select id="selectCollectEleByDay" resultType="com.nantian.cloud.dbcapp.dto.ElectricityDataDto">
        select sum(dataValue)/10000 as `uploadValue`, left(statDate,10) as `month`, GROUP_CONCAT(distinct dataCode) dataCode from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and #{dataCode} like concat('%',dataCode,'%') and left(statDate,10) like #{time}
        group by `month` order by `month`
    </select>

    <select id="selectCollectEleByMonth" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
        select left(statDate,7) statDate,  round(sum(dataValue),4) dataValue, GROUP_CONCAT(distinct dataCode) dataCode from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and #{dataCode} like concat('%',dataCode,'%') and left(statDate,7) between left(#{startTime},7) and left(#{endTime},7)
        group by left(statDate,7) order by left(statDate,7)
    </select>

    <select id="selectCollectByMonth" resultType="com.nantian.cloud.dbcapp.dto.ElectricityDataDto">
        select consume_sum as `value` , record_circle as `month` from t_energy_structure
        where enterprise_id = #{enterpriseCode} and record_circle between #{preTime} and #{endTime}
        and dataCode = '0000000002330011'
    </select>

    <select id="selectCollectByMonthEnt" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
           select sum(StandardValue)*10000 as `dataValue`,record_circle as `statDate` from t_unitenergy2020 where enterprise_id = #{enterpriseCode}
            and energyTypeCode ='3300' and `code` ='00000000' and record_circle between #{preTime} and #{endTime} group by record_circle order by record_circle
    </select>

    <select id="selectDataCode" resultType="float">
            select SUM(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode = '0000000002330011' and record_circle = #{time}
            union all
                    select SUM(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode = '0000000002330020' and record_circle = #{time}
            union all
                    select SUM(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode = '0000000002330021' and record_circle = #{time}
            union all
             select SUM(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode = '0000000002330010' and record_circle = #{time}

    </select>

    <select id="selectOutput" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
        select consume_sum as dataValue,record_circle as statDate from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode = '0000000002330060' ORDER BY record_circle
    </select>


    <select id="selectCollectData" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(StandardValue) as `value`, record_circle as `month` from t_unitenergy2020 a left join enterprise_info b
        on a.enterprise_id = b.enterpriseCode
        <where>
            a.code = '00000000' and energyTypeCode = '3300' and StandardValue between 0 and 100000
            <if test="areaCode!=null">
              and b.regionCode like concat(#{areaCode},'%')
            </if>
            <if test="industryCode!=null">
              and b.tid = #{industryCode}
            </if>
            and record_circle between #{preTime} and #{endTime}
        </where>
        group by record_circle order by record_circle
    </select>
    <select id="selectCollectData2019" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
         SELECT
        a.record_circle as `month`,
        sum(
        CASE a.quota_unit
        WHEN '万千瓦·时' THEN  a.guideline_val
        WHEN '千瓦·时' THEN  a.guideline_val/10000
        ELSE a.guideline_val END
        ) as 'value'
        FROM
        t_unitenergy2019 a LEFT JOIN t_enterprise_old b on a.entId = b.entId
        <where>
            a.entid = a.unitId
            AND a.record_circle BETWEEN #{preTime} and #{endTime}
            AND a.CODE LIKE 'JZNH%'
            AND a.energyName IN ( "电", "电力" )
            AND a.guideline_val > 0
            AND a.guideline_val &lt; 100000000
            <if test="areaCode!=null">
                and b.region_code like concat(#{areaCode},'%')
            </if>
            <if test="industryCode!=null">
                and b.industryCode = #{industryCode}
            </if>
        </where>
        GROUP BY
        a.record_circle
    </select>
    <select id="getMaterialElectricEnt" resultType="com.nantian.cloud.dbcapp.vo.ElectricityDataVo">
		select left(t.statDate,7) statDate,  round(sum(dataValue),4) dataValue, t.dataCode from enterprise_energy_data t, (
            select dataCode, case when SUBSTRING(dataCode,15,2) = '11' then 1 when SUBSTRING(dataCode,15,2) = '20' then 2 when SUBSTRING(dataCode,15,2) = '21' then 3 when SUBSTRING(dataCode,15,2) = '10' then 4 else 5 end num
            from configure_data_collect
            where enterpriseCode = #{enterpriseCode} and SUBSTRING(dataCode,1,8)  = '00000000'
            and SUBSTRING(dataCode,11,4) = '3300' and SUBSTRING(dataCode,15,2) in ("11","20","21","10") order by num asc limit 0,1
        ) c where t.enterpriseCode = #{enterpriseCode} and left(t.statDate,7) between left(#{startTime},7) and left(#{endTime},7) and t.dataCode = c.dataCode
		group by left(t.statDate,7) order by left(t.statDate,7)
    </select>
    <select id="getEleDataCode" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
        select dataCodeOld, GROUP_CONCAT(dataCode) key1, case when SUBSTRING(dataCodeOld,15,2) = '11' then 1 when SUBSTRING(dataCodeOld,15,2) = '20' then 2 when SUBSTRING(dataCodeOld,15,2) = '21' then 3 when SUBSTRING(dataCodeOld,15,2) = '10' then 4 else 5 end value
        from configure_data_collect_new
        where enterpriseCode = #{enterpriseCode} and SUBSTRING(dataCodeOld,1,8)  = '00000000'
        and SUBSTRING(dataCodeOld,11,4) = '3300' and SUBSTRING(dataCodeOld,15,2) in ("11","20","21","10")
        group by dataCodeOld order by value asc limit 0,1
    </select>
</mapper>
