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

    <resultMap type="cn.cecep.talroad.domain.MonElecReal" id="MonElecRealResult">
        <result property="dataId"    column="data_id"    />
        <result property="deviceSiteId"    column="device_site_id"    />
        <result property="deviceSiteName"    column="device_site_name"    />
        <result property="timeType"    column="time_type"    />
        <result property="dataTime"    column="data_time"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectMonElecRealVo">
        select data_id, device_site_id, device_site_name, time_type, data_time, create_time from mon_elec_real
    </sql>
    <select id="selectElecRealPage" resultType="cn.cecep.talroad.vo.MonElecRealVo">
        select
           t1.*,
           t3.name AS "deviceName"
        from mon_elec_real t1
        LEFT JOIN bas_device_site t2 ON t1.device_site_id = t2.id
        LEFT JOIN bas_device t3 ON t2.device_id = t3.device_id
        <where>
            <if test="query.dataTimeStart != null and query.dataTimeStart != ''">
                AND t1.data_time >= to_timestamp(#{query.dataTimeStart},'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.dataTimeEnd != null and query.dataTimeEnd != ''">
                AND t1.data_time &lt;= to_timestamp(#{query.dataTimeEnd},'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.deviceSiteId != null and query.deviceSiteId != ''">
                AND t1.device_site_id = #{query.deviceSiteId}
            </if>
            <if test="query.deviceSiteIds != null and query.deviceSiteIds.size() > 0">
                AND t1.device_site_id IN
                <foreach collection="query.deviceSiteIds" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        <choose>
            <when test='query.timeSortType != null and query.timeSortType == "asc"'>
                ORDER BY t1.data_time, t1.data_id
            </when>
            <otherwise>
                ORDER BY t1.data_time DESC, t1.data_id
            </otherwise>
        </choose>
    </select>

    <select id="selectLatestMonElecRealByParams" resultType="cn.cecep.talroad.vo.MonElecRealSiteVo">
        SELECT
            b.device_site_id as deviceSiteId,
            bd.factory_id as factoryId,
            bd.device_id as deviceId,
            b.time_type as timeType,
            b.data_time as dataTime,
            b.active_power as activePower,
            bds.min_value as minValue
        FROM
            (
                SELECT
                    r.device_site_id,
                    MAX ( r.data_time ) AS data_time
                FROM
                    mon_elec_real r
                WHERE
                    r.time_type = 'R'
                  AND r.data_time between #{lastDate} and #{currDate}
                GROUP BY
                    device_site_id
            )
                A LEFT JOIN mon_elec_real b ON A.device_site_id = b.device_site_id
                AND A.data_time = b.data_time
                  LEFT JOIN bas_device_site bds on b.device_site_id = bds."id"
                  LEFT JOIN bas_device bd on bd.device_id =bds.device_id and bd.factory_id=bds.factory_id
        WHERE
            b.data_time between #{lastDate} and #{currDate}
          AND time_type = 'R'
          AND bd.industry = #{industry} AND bd."type" = #{deviceType} and bds.is_stat ='1'
    </select>
    <select id="selectKwhChartData" resultType="java.util.Map">
        SELECT
            to_char(t.data_time, 'yyyy-mm-dd hh24:MI') as "dataTime",
            sum(t.active_power_quantity::NUMERIC) as "activePowerQuantity"
        FROM mon_elec_real t
        INNER JOIN bas_device_site t1 ON t.device_site_id = t1.id
        INNER JOIN bas_device t2 ON t1.device_id = t2.device_id
        INNER JOIN pc_factory t3 ON t2.factory_id = t3.id
        WHERE t.data_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
        AND t.data_time &lt;= to_timestamp(CONCAT(#{endTime}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
        AND t.active_power_quantity is not NULL
        AND t3.country_code IN
        <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        GROUP BY t.data_time
        ORDER BY t.data_time
    </select>
    <select id="selectKwhMonitor" resultType="cn.cecep.talroad.vo.MonElecRealVo$KwhMonitorVo">
        <if test='type == "1"'>
            SELECT
                t3.country_name as name,
                sum(t.active_power_quantity::NUMERIC) as kwh
            FROM mon_elec_real t
            INNER JOIN bas_device_site t1 ON t.device_site_id = t1.id
            INNER JOIN pc_factory t3 ON t1.factory_id = t3.id
            WHERE t.data_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
              AND t.data_time &lt;= to_timestamp(CONCAT(#{endTime}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
              AND t.active_power_quantity is not NULL
              AND t3.country_code IN
                <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            GROUP BY t3.country_code, t3.country_name
            ORDER BY t3.country_code
        </if>
        <if test='type == "2"'>
            SELECT
                t4.indus_name as name,
                sum(t.active_power_quantity::NUMERIC) as kwh
            FROM mon_elec_real t
            INNER JOIN bas_device_site t1 ON t.device_site_id = t1.id
            INNER JOIN pc_factory t3 ON t1.factory_id = t3.id
            INNER JOIN pc_industry t4 ON t4.indus_code = t3.industry_category
            WHERE t.data_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd hh24:MI:SS')
            AND t.data_time &lt;= to_timestamp(CONCAT(#{endTime}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
            AND t.active_power_quantity is not NULL
            AND t3.country_code IN
            <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
            GROUP BY t4.indus_code, t4.indus_name
            ORDER BY t4.indus_code
        </if>
    </select>
    <select id="selectLastDayData" resultType="cn.cecep.talroad.vo.analysis.AvgValueVo">
        select a.* from (
            SELECT
                bs.device_id as deviceId ,count(1) as avgValue
            from
                bas_device_site bs
                    left join mon_elec_real r on bs.id = r.device_site_id
            WHERE
                r.time_type = 'R'
              AND r.data_time between #{lastDayBegin} and #{lastDayEnd}
            group by bs.device_id
        )a order by a.deviceId limit #{pageSize} offset #{startCount}

    </select>

</mapper>
