<?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="org.king.modules.ad.mapper.AdChargingRecordMapper">

    <select id="listMonth" resultType="org.king.modules.ad.entity.ComChargingMonth">
        select t2.COM_NAME, shipNum, consumption, chargeLength
        from (
                 select com_no,
                        count(1)                                                              as shipNum,
                        sum(consumption)                                                      as consumption,
                        ifnull(sum(timestampdiff(minute, real_start_time, real_end_time)), 0) as chargeLength
                 from ad_charging_record
                 where date_format(create_time, '%Y-%m') = #{month}
                 <if test="comNos != null and comNos.size()>0">
                     and com_no in
                     <foreach item="key" collection="comNos" open="(" separator="," close=")">
                         #{key}
                     </foreach>
                 </if>
                 and real_start_time is not null
                 group by com_no) t1
                 left join web_company t2 on t1.com_no = t2.COM_NO
        <where>
            <if test="comNo != null and comNo != ''">
                COM_NAME like concat ('%',#{comNo},'%')
            </if>
        </where>
    </select>

    <select id="listDevice" resultType="org.king.modules.ad.entity.ComChargingDevice">
        select t2.service_name,
               t2.berth_id,
               t3.area_name,
               t4.COM_NAME,
               t2.type,
               t2.status,
               t2.voltage,
               ifnull(consumption, 0) as consumption,
               ifnull(shipNum, 0)     as shipNum
        from (
                 select ad_device_id,
                        count(1)         as shipNum,
                        sum(consumption) as consumption
                 from ad_charging_record
                 where real_start_time is not null
                 group by ad_device_id) t1
                 right join ad_device t2 on t1.ad_device_id = t2.id
                 left join hhad.ent_company_geom t3 on t2.berth_id = t3.ecg_no
                 left join web_company t4 on t3.com_no = t4.COM_NO
        <where>
            <if test="comNos != null and comNos.size()>0">
                t4.com_no in
                <foreach item="key" collection="comNos" open="(" separator="," close=")">
                    #{key}
                </foreach>
            </if>
            <if test="type != null and type != ''">
                and t2.type = #{type}
            </if>
            <if test="status != null and status != ''">
                and t2.status = #{status}
            </if>
        </where>

    </select>

    <select id ="getChargeData">
        SELECT
        count( consumption ) count,
        sum( consumption ) sum,
        sum( TIMESTAMPDIFF( SECOND, real_start_time, real_end_time )/ 3600 ) timeTotal
        FROM
        ad_charging_record
    </select>

    <select id="getChargeByFive">
        SELECT
        DATE_FORMAT( ad.real_start_time, '%Y-%m-%d' ) startTime,
        Sum(
        TIMESTAMPDIFF( SECOND, ad.real_start_time, ad.real_end_time ))/3600 timeTotal,
        sum( consumption ) sum,
        count( 1 ) count
        FROM
        (
        SELECT
        *
        FROM
        ad_charging_record
        WHERE
        DATE_FORMAT( real_start_time, '%Y-%m-%d' )> DATE_FORMAT( date_sub( now(), INTERVAL 5 DAY ), '%Y-%m-%d' )) ad
        GROUP BY
        DATE_FORMAT(
        ad.real_start_time,
        '%Y-%m-%d'
        )
    </select>

    <select id="queryChargingStatistics" resultType="org.king.modules.ad.entity.AdChargingStatistics">
        select gemo_id,
               sum(deviceNum)    as deviceNum,
               sum(entNum)       as entNum,
               sum(shipNum)      as shipNum,
               sum(consumption)  as consumption,
               sum(chargeLength) as chargeLength
        from (
                 select t1.berth_id      as gemo_id,
                        count(t1.id)     as deviceNum,
                        count(t2.COM_NO) as entNum,
                        0                as shipNum,
                        0                as consumption,
                        0                as chargeLength
                 from ad_device t1
                          left join web_company t2 on t1.sys_office_id = t2.COM_NO
                 where t1.is_del = 0
                 group by t1.berth_id
                 union all
                 select gemo_id,
                        0                                                                     as deviceNum,
                        0                                                                     as entNum,
                        count(1)                                                              as shipNum,
                        sum(consumption)                                                      as consumption,
                        ifnull(sum(timestampdiff(minute, real_start_time, real_end_time)), 0) as chargeLength
                 from ad_charging_record
                 where real_start_time is not null
                 group by gemo_id) a
        group by gemo_id
    </select>
</mapper>