<?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="cn.cecep.talroad.mapper.analysis.SRaFactoryTransferRateAnalysisMapper">

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaFactoryTransferRateAnalysis" id="SRaFactoryTransferRateAnalysisResult">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
        <result column="factory_name" jdbcType="VARCHAR" property="factoryName" />
        <result column="district_name" jdbcType="VARCHAR" property="districtName" />
        <result column="district_code" jdbcType="VARCHAR" property="districtCode" />
        <result column="industry_category_name" jdbcType="VARCHAR" property="industryCategoryName" />
        <result column="industry_category_code" jdbcType="VARCHAR" property="industryCategoryCode" />
        <result column="outlet_code" jdbcType="VARCHAR" property="outletCode" />
        <result column="outlet_name" jdbcType="VARCHAR" property="outletName" />
        <result column="count_type" jdbcType="VARCHAR" property="countType" />
        <result column="statistics_time" jdbcType="VARCHAR" property="statisticsTime" />
        <result column="elec_transmitt_num" jdbcType="INTEGER" property="elecTransmittNum" />
        <result column="elec_actual_transmitt_num" jdbcType="INTEGER" property="elecActualTransmittNum" />
        <result column="elec_transmission_rate" jdbcType="NUMERIC" property="elecTransmissionRate" />
        <result column="unorganized_transmitt_minute_num" jdbcType="INTEGER" property="unorganizedTransmittMinuteNum" />
        <result column="unorganized_actual_transmitt_minute_num" jdbcType="INTEGER" property="unorganizedActualTransmittMinuteNum" />
        <result column="unorganized_transmission_minute_rate" jdbcType="NUMERIC" property="unorganizedTransmissionMinuteRate" />
        <result column="unorganized_transmitt_hour_num" jdbcType="INTEGER" property="unorganizedTransmittHourNum" />
        <result column="unorganized_actual_transmitt_hour_num" jdbcType="INTEGER" property="unorganizedActualTransmittHourNum" />
        <result column="unorganized_transmission_hour_rate" jdbcType="NUMERIC" property="unorganizedTransmissionHourRate" />
        <result column="unorganized_transmitt_day_num" jdbcType="INTEGER" property="unorganizedTransmittDayNum" />
        <result column="unorganized_actual_transmitt_day_num" jdbcType="INTEGER" property="unorganizedActualTransmittDayNum" />
        <result column="unorganized_transmission_day_rate" jdbcType="NUMERIC" property="unorganizedTransmissionDayRate" />
        <result column="dcs_transmitt_minute_num" jdbcType="INTEGER" property="dcsTransmittMinuteNum" />
        <result column="dcs_actual_transmitt_num" jdbcType="INTEGER" property="dcsActualTransmittNum" />
        <result column="dcs_transmission_rate" jdbcType="NUMERIC" property="dcsTransmissionRate" />
        <result column="gridded_transmitt_minute_num" jdbcType="INTEGER" property="griddedTransmittMinuteNum" />
        <result column="gridded_actual_transmitt_minute_num" jdbcType="INTEGER" property="griddedActualTransmittMinuteNum" />
        <result column="gridded_transmission_minute_rate" jdbcType="NUMERIC" property="griddedTransmissionMinuteRate" />
        <result column="gridded_transmitt_hour_num" jdbcType="INTEGER" property="griddedTransmittHourNum" />
        <result column="gridded_actual_transmitt_hour_num" jdbcType="INTEGER" property="griddedActualTransmittHourNum" />
        <result column="gridded_transmission_hour_rate" jdbcType="NUMERIC" property="griddedTransmissionHourRate" />
        <result column="gridded_transmitt_day_num" jdbcType="INTEGER" property="griddedTransmittDayNum" />
        <result column="gridded_actual_transmitt_gridded_transmitt_day_num" jdbcType="INTEGER" property="griddedActualTransmittGriddedTransmittDayNum" />
        <result column="gridded_transmission_day_rate" jdbcType="NUMERIC" property="griddedTransmissionDayRate" />
        <result column="online_monitoring_minute_num" jdbcType="INTEGER" property="onlineMonitoringMinuteNum" />
        <result column="online_monitoring_actual_minute_num" jdbcType="INTEGER" property="onlineMonitoringActualMinuteNum" />
        <result column="online_monitoring_minute_rate" jdbcType="NUMERIC" property="onlineMonitoringMinuteRate" />
        <result column="online_monitoring_hour_num" jdbcType="INTEGER" property="onlineMonitoringHourNum" />
        <result column="online_monitoring_actual_hour_num" jdbcType="INTEGER" property="onlineMonitoringActualHourNum" />
        <result column="online_monitoring_hour_rate" jdbcType="NUMERIC" property="onlineMonitoringHourRate" />
        <result column="online_monitoring_day_num" jdbcType="INTEGER" property="onlineMonitoringDayNum" />
        <result column="online_monitoring_actual_day_num" jdbcType="INTEGER" property="onlineMonitoringActualDayNum" />
        <result column="online_monitoring_day_rate" jdbcType="NUMERIC" property="onlineMonitoringDayRate" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="data_time" jdbcType="TIMESTAMP" property="dataTime" />
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, factory_id, factory_name, district_name, district_code, industry_category_name,
        industry_category_code, outlet_code, outlet_name, count_type, statistics_time, elec_transmitt_num,
        elec_actual_transmitt_num, elec_transmission_rate, unorganized_transmitt_minute_num,
        unorganized_actual_transmitt_minute_num, unorganized_transmission_minute_rate, unorganized_transmitt_hour_num,
        unorganized_actual_transmitt_hour_num, unorganized_transmission_hour_rate, unorganized_transmitt_day_num,
        unorganized_actual_transmitt_day_num, unorganized_transmission_day_rate, dcs_transmitt_minute_num,
        dcs_actual_transmitt_num, dcs_transmission_rate, gridded_transmitt_minute_num, gridded_actual_transmitt_minute_num,
        gridded_transmission_minute_rate, gridded_transmitt_hour_num, gridded_actual_transmitt_hour_num,
        gridded_transmission_hour_rate, gridded_transmitt_day_num, gridded_actual_transmitt_gridded_transmitt_day_num,
        gridded_transmission_day_rate, online_monitoring_minute_num, online_monitoring_actual_minute_num,
        online_monitoring_minute_rate, online_monitoring_hour_num, online_monitoring_actual_hour_num,
        online_monitoring_hour_rate, online_monitoring_day_num, online_monitoring_actual_day_num,
        online_monitoring_day_rate, create_by, create_time, update_by, update_time,data_time
    </sql>


    <sql id="selectSRaFactoryTransferRateAnalysisVo">
        select
        <include refid="Base_Column_List"/>
        FROM s_ra_factory_transfer_rate_analysis
    </sql>

    <select id="selectSRaFactoryTransferRateAnalysisList" parameterType="cn.cecep.talroad.domain.analysis.SRaFactoryTransferRateAnalysis" resultMap="SRaFactoryTransferRateAnalysisResult">
        <include refid="selectSRaFactoryTransferRateAnalysisVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="factoryName != null and factoryName != ''"> and data_source = #{factoryName}</if>
            <if test="outletName != null  and outletName != ''"> and outlet_name like concat('%', #{outletName}, '%')</if>
            <if test="outletCode != null  and outletCode != ''"> and outlet_code = #{outletCode}</if>
            <if test="statisticsTime != null "> and statistics_time = #{statisticsTime}</if>
            <if test="districtName != null "> and district_name = #{districtName}</if>
            <if test="industryCategoryName != null "> and industry_category_name = #{industryCategoryName}</if>
            <if test="outletName != null "> and outlet_name = #{outletName}</if>
            <if test="countType != null "> and count_type = #{countType}</if>
        </where>
    </select>

    <select id="selectFactoryTransferRateParamVoList" resultType="cn.cecep.talroad.vo.analysis.FactoryTransferRateParamVo">
        select  a.id,b.factory_id as factoryId,
               a.device_id as deviceId, a.industry as industry,
               a.device_type as deviceType,b.name as deviceName ,
               a.param_id as paramId,c.param_code_svc as paramCodeSvc
        from bas_device_param a
            inner JOIN  bas_device b on a.device_id = b.device_id
        inner join  bas_param c
                    on  a.param_id = c.param_id
                        and a.device_type = c.device_type
                        and a.facility_type = c.facility_type
                        and a.industry = c.industry
        where a.device_id = #{deviceId} and a.is_online=1 and b.is_status ='1'
    </select>

    <select id="selectAvgRateByDateTimeGroupFactoryId"
            resultType="cn.cecep.talroad.data.analyse.task.entity.SimpleSRaFactoryTransferRateAnalysis">
        select factory_id,
               avg(online_monitoring_day_rate) as onlineMonitoringRate ,
               avg(dcs_transmission_rate) as dcsTransmissionRate ,
               avg(unorganized_transmission_day_rate) as unorganizedTransmissionRate ,
               avg(elec_transmission_rate) as elecTransmissionRate
        from s_ra_factory_transfer_rate_analysis
        where count_type = #{countType} and statistics_time <![CDATA[ >= ]]> #{dataTime}
        group by factory_id
    </select>

    <select id="selectDayRateOutletLt50List" resultMap="SRaFactoryTransferRateAnalysisResult">
        <include refid="selectSRaFactoryTransferRateAnalysisVo"/>
        where
        online_monitoring_day_rate &lt;50 and
        factory_id in
        <foreach collection="factoryIds" open="(" separator="," close=")" item="factoryId" index="i">
            #{factoryId}
        </foreach>
        and outlet_code in
        <foreach collection="outletCodes" open="(" separator="," close=")" item="outletCode" index="i">
            #{outletCode}
        </foreach>
        and  create_time>=#{startTime} and create_time &lt;=#{endTime}
    </select>
    <select id="selectListByArea" resultMap="SRaFactoryTransferRateAnalysisResult">
        select
            district_code ,
            district_name ,
            statistics_time ,
            data_time,
            sum(online_monitoring_minute_num) as online_monitoring_minute_num ,
            sum(online_monitoring_actual_minute_num) as online_monitoring_actual_minute_num ,
            round(avg(online_monitoring_minute_rate),2) as online_monitoring_minute_rate ,
            sum(online_monitoring_hour_num) as online_monitoring_hour_num ,
            sum(online_monitoring_actual_hour_num) as online_monitoring_actual_hour_num ,
            round(avg(online_monitoring_hour_rate),2) as online_monitoring_hour_rate ,
            sum(online_monitoring_day_num) as online_monitoring_day_num ,
            sum(online_monitoring_actual_day_num) as online_monitoring_actual_day_num ,
            round(avg(online_monitoring_day_rate),2) as online_monitoring_day_rate ,
            sum(elec_transmitt_num) as elec_transmitt_num ,
            sum(elec_actual_transmitt_num) as elec_actual_transmitt_num ,
            round(avg(elec_transmission_rate),2) as elec_transmission_rate ,
            sum(unorganized_transmitt_hour_num) as unorganized_transmitt_hour_num ,
            sum(unorganized_actual_transmitt_hour_num) as unorganized_actual_transmitt_hour_num ,
            round(avg(unorganized_transmission_hour_rate),2) as unorganized_transmission_hour_rate ,
            sum(dcs_transmitt_minute_num) as dcs_transmitt_minute_num ,
            sum(dcs_actual_transmitt_num)as dcs_actual_transmitt_num ,
            round(avg(dcs_transmission_rate),2) as dcs_transmission_rate ,
            sum(gridded_transmitt_hour_num) as gridded_transmitt_hour_num ,
            sum(gridded_actual_transmitt_hour_num) as gridded_actual_transmitt_hour_num ,
            round(avg(gridded_transmission_hour_rate),2) as gridded_transmission_hour_rate
        from
            s_ra_factory_transfer_rate_analysis srftra
        where statistics_time = #{lastDay} and count_type = #{countType}
        group by
            district_code ,
            district_name ,
            statistics_time ,
            data_time

    </select>
    <select id="selectStatisticsLastMonthData" resultMap="SRaFactoryTransferRateAnalysisResult">
        select
            factory_id,
            factory_name,
            district_code ,
            district_name ,
            industry_category_name,
            industry_category_code,
            outlet_code,
            outlet_name
            sum(online_monitoring_minute_num) as online_monitoring_minute_num ,
            sum(online_monitoring_actual_minute_num) as online_monitoring_actual_minute_num ,
            round(avg(online_monitoring_minute_rate),2) as online_monitoring_minute_rate ,
            sum(online_monitoring_hour_num) as online_monitoring_hour_num ,
            sum(online_monitoring_actual_hour_num) as online_monitoring_actual_hour_num ,
            round(avg(online_monitoring_hour_rate),2) as online_monitoring_hour_rate ,
            sum(online_monitoring_day_num) as online_monitoring_day_num ,
            sum(online_monitoring_actual_day_num) as online_monitoring_actual_day_num ,
            round(avg(online_monitoring_day_rate),2) as online_monitoring_day_rate ,
            sum(elec_transmitt_num) as elec_transmitt_num ,
            sum(elec_actual_transmitt_num) as elec_actual_transmitt_num ,
            round(avg(elec_transmission_rate),2) as elec_transmission_rate ,
            sum(unorganized_transmitt_hour_num) as unorganized_transmitt_hour_num ,
            sum(unorganized_actual_transmitt_hour_num) as unorganized_actual_transmitt_hour_num ,
            round(avg(unorganized_transmission_hour_rate),2) as unorganized_transmission_hour_rate ,
            sum(dcs_transmitt_minute_num) as dcs_transmitt_minute_num ,
            sum(dcs_actual_transmitt_num)as dcs_actual_transmitt_num ,
            round(avg(dcs_transmission_rate),2) as dcs_transmission_rate ,
            sum(gridded_transmitt_hour_num) as gridded_transmitt_hour_num ,
            sum(gridded_actual_transmitt_hour_num) as gridded_actual_transmitt_hour_num ,
            round(avg(gridded_transmission_hour_rate),2) as gridded_transmission_hour_rate
        from
            s_ra_factory_transfer_rate_analysis srftra
        where statistics_time =#{lastMonth} and count_type = #{countType}
        group by factory_id,
            factory_name,
            district_code ,
            district_name ,
            industry_category_name,
            industry_category_code,
            outlet_code,
            outlet_name
    </select>
    <select id="selectStatisticsSeasonData" resultMap="SRaFactoryTransferRateAnalysisResult">
        select
            factory_id,
            factory_name,
            district_code ,
            district_name ,
            industry_category_name,
            industry_category_code,
            outlet_code,
            outlet_name
            sum(online_monitoring_minute_num) as online_monitoring_minute_num ,
            sum(online_monitoring_actual_minute_num) as online_monitoring_actual_minute_num ,
            round(avg(online_monitoring_minute_rate),2) as online_monitoring_minute_rate ,
            sum(online_monitoring_hour_num) as online_monitoring_hour_num ,
            sum(online_monitoring_actual_hour_num) as online_monitoring_actual_hour_num ,
            round(avg(online_monitoring_hour_rate),2) as online_monitoring_hour_rate ,
            sum(online_monitoring_day_num) as online_monitoring_day_num ,
            sum(online_monitoring_actual_day_num) as online_monitoring_actual_day_num ,
            round(avg(online_monitoring_day_rate),2) as online_monitoring_day_rate ,
            sum(elec_transmitt_num) as elec_transmitt_num ,
            sum(elec_actual_transmitt_num) as elec_actual_transmitt_num ,
            round(avg(elec_transmission_rate),2) as elec_transmission_rate ,
            sum(unorganized_transmitt_hour_num) as unorganized_transmitt_hour_num ,
            sum(unorganized_actual_transmitt_hour_num) as unorganized_actual_transmitt_hour_num ,
            round(avg(unorganized_transmission_hour_rate),2) as unorganized_transmission_hour_rate ,
            sum(dcs_transmitt_minute_num) as dcs_transmitt_minute_num ,
            sum(dcs_actual_transmitt_num)as dcs_actual_transmitt_num ,
            round(avg(dcs_transmission_rate),2) as dcs_transmission_rate ,
            sum(gridded_transmitt_hour_num) as gridded_transmitt_hour_num ,
            sum(gridded_actual_transmitt_hour_num) as gridded_actual_transmitt_hour_num ,
            round(avg(gridded_transmission_hour_rate),2) as gridded_transmission_hour_rate
        from
            s_ra_factory_transfer_rate_analysis srftra
        where count_type = 'M' and statistics_time in
        <foreach collection="monthList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by factory_id,
            factory_name,
            district_code ,
            district_name ,
            industry_category_name,
            industry_category_code,
            outlet_code,
            outlet_name
    </select>
</mapper>
