<?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.xzzn.ems.mapper.EmsDailyChargeDataMapper">

    <resultMap type="EmsDailyChargeData" id="EmsDailyChargeDataResult">
        <result property="id"    column="id"    />
        <result property="siteId"    column="site_id"    />
        <result property="dateTime"    column="date_time"    />
        <result property="totalChargeData"    column="total_charge_Data"    />
        <result property="totalDischargeData"    column="total_discharge_Data"    />
        <result property="chargeData"    column="charge_data"    />
        <result property="dischargeData"    column="discharge_data"    />
        <result property="totalRevenue"    column="total_revenue"    />
        <result property="dayRevenue"    column="day_revenue"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectEmsDailyChargeDataVo">
        select id, site_id, date_time, total_charge_Data, total_discharge_Data, charge_data, discharge_data, total_revenue, day_revenue, create_by, create_time, update_by, update_time, remark from ems_daily_charge_data
    </sql>

    <select id="selectEmsDailyChargeDataList" parameterType="EmsDailyChargeData" resultMap="EmsDailyChargeDataResult">
        <include refid="selectEmsDailyChargeDataVo"/>
        <where>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="dateTime != null "> and date_time = #{dateTime}</if>
            <if test="totalChargeData != null "> and total_charge_Data = #{totalChargeData}</if>
            <if test="totalDischargeData != null "> and total_discharge_Data = #{totalDischargeData}</if>
            <if test="chargeData != null "> and charge_data = #{chargeData}</if>
            <if test="dischargeData != null "> and discharge_data = #{dischargeData}</if>
            <if test="totalRevenue != null "> and total_revenue = #{totalRevenue}</if>
            <if test="dayRevenue != null "> and day_revenue = #{dayRevenue}</if>
        </where>
        order by date_time desc
    </select>

    <select id="selectEmsDailyChargeDataById" parameterType="Long" resultMap="EmsDailyChargeDataResult">
        <include refid="selectEmsDailyChargeDataVo"/>
        where id = #{id}
    </select>

    <select id="selectBySiteIdAndDateTime" resultMap="EmsDailyChargeDataResult">
        <include refid="selectEmsDailyChargeDataVo"/>
        where site_id = #{siteId}
          and date(date_time) = date(#{dateTime})
        order by date_time desc
        limit 1
    </select>

    <insert id="insertEmsDailyChargeData" parameterType="EmsDailyChargeData" useGeneratedKeys="true" keyProperty="id">
        insert into ems_daily_charge_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="siteId != null">site_id,</if>
            <if test="dateTime != null">date_time,</if>
            <if test="totalChargeData != null">total_charge_Data,</if>
            <if test="totalDischargeData != null">total_discharge_Data,</if>
            <if test="chargeData != null">charge_data,</if>
            <if test="dischargeData != null">discharge_data,</if>
            <if test="totalRevenue != null">total_revenue,</if>
            <if test="dayRevenue != null">day_revenue,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="siteId != null">#{siteId},</if>
            <if test="dateTime != null">#{dateTime},</if>
            <if test="totalChargeData != null">#{totalChargeData},</if>
            <if test="totalDischargeData != null">#{totalDischargeData},</if>
            <if test="chargeData != null">#{chargeData},</if>
            <if test="dischargeData != null">#{dischargeData},</if>
            <if test="totalRevenue != null">#{totalRevenue},</if>
            <if test="dayRevenue != null">#{dayRevenue},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateEmsDailyChargeData" parameterType="EmsDailyChargeData">
        update ems_daily_charge_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="dateTime != null">date_time = #{dateTime},</if>
            <if test="totalChargeData != null">total_charge_Data = #{totalChargeData},</if>
            <if test="totalDischargeData != null">total_discharge_Data = #{totalDischargeData},</if>
            <if test="chargeData != null">charge_data = #{chargeData},</if>
            <if test="dischargeData != null">discharge_data = #{dischargeData},</if>
            <if test="totalRevenue != null">total_revenue = #{totalRevenue},</if>
            <if test="dayRevenue != null">day_revenue = #{dayRevenue},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsDailyChargeDataById" parameterType="Long">
        delete from ems_daily_charge_data where id = #{id}
    </delete>

    <delete id="deleteEmsDailyChargeDataByIds" parameterType="String">
        delete from ems_daily_charge_data where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertOrUpdateData" parameterType="EmsDailyChargeData">
        INSERT into ems_daily_charge_data (
            id,
            site_id,
            date_time,
            total_charge_Data,
            total_discharge_Data,
            charge_data,
            discharge_data,
            total_revenue,
            day_revenue,
            create_by,
            create_time,
            update_by,
            update_time,
            remark
                 ) values (
                     #{id},
                     #{siteId},
                     #{dateTime},
                     #{totalChargeData},
                     #{totalDischargeData},
                     #{chargeData},
                     #{dischargeData},
                     #{totalRevenue},
                     #{dayRevenue},
                     #{createBy},
                     #{createTime},
                     #{updateBy},
                     #{updateTime},
                     #{remark}
                 )
            on duplicate key update
             total_charge_Data = #{totalChargeData},
             total_discharge_Data = #{totalDischargeData},
             charge_data = #{chargeData},
             discharge_data = #{dischargeData},
             total_revenue = IFNULL(#{totalRevenue}, total_revenue),
             day_revenue = IFNULL(#{dayRevenue}, day_revenue),
             update_time = NOW()
    </insert>

    <update id="updateRevenueBySiteAndDate">
        update ems_daily_charge_data
        set total_revenue = #{totalRevenue},
            day_revenue = #{dayRevenue},
            update_by = #{updateBy},
            update_time = NOW()
        where site_id = #{siteId}
          and date(date_time) = date(#{dateTime})
    </update>

    <select id="getAllSiteChargeData" resultType="map">
        SELECT
            SUM(t.total_charge_data) AS totalChargedCap,
            SUM(t.total_discharge_data) AS totalDischargedCap
        FROM ems_daily_charge_data t
        LEFT JOIN ems_site_setting ss ON t.site_id = ss.site_id
        INNER JOIN (
            SELECT site_id,MAX(date_time) AS latest_date  FROM ems_daily_charge_data
            WHERE  date_time &lt;= #{nowData}
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            AND total_charge_data IS NOT NULL
            AND total_discharge_data IS NOT NULL
            GROUP BY site_id
        ) AS latest ON t.date_time = latest.latest_date
            and t.site_id = latest.site_id
        <if test="siteId != null">
            where t.site_id = #{siteId}
        </if>
    </select>

    <select id="getSingleSiteChargeData" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        SELECT SUM(t.charge_data) AS chargedCap,
               SUM(t.discharge_data) AS disChargedCap,
               t.date_time AS ammeterDate
        FROM ems_daily_charge_data t
        WHERE t.site_id = #{siteId}
          AND t.date_time BETWEEN #{startDate} AND #{endDate}
        group by ammeterDate
        order by ammeterDate
    </select>

    <select id="getTotalChargeDataByDay" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        SELECT  t.date_time AS ammeterDate,
                SUM(t.charge_data) as chargedCap,
                SUM(t.discharge_data) as disChargedCap
        FROM ems_daily_charge_data t
        WHERE t.site_id = #{siteId}
          AND t.date_time BETWEEN #{startDate} AND #{endDate}
        group BY ammeterDate
        order by ammeterDate
    </select>

    <select id="getTotalChargeDataByMonth" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        SELECT
            DATE_FORMAT(t.date_time, '%Y-%m') AS ammeterMonth,
            SUM(t.charge_data) AS chargedCap,
            SUM(t.discharge_data) AS disChargedCap
        FROM ems_daily_charge_data t
        WHERE t.site_id = #{siteId}
          AND t.date_time BETWEEN #{startDate} AND #{endDate}
        GROUP BY ammeterMonth
        ORDER BY ammeterMonth
    </select>

    <select id="getAllSiteChargeDataByMonth" resultType="com.xzzn.ems.domain.vo.ElectricIndexList">
        SELECT
            DATE_FORMAT(t.date_time, '%Y-%m') AS dateMonth,
            SUM(t.charge_data) AS chargeEnergy,
            SUM(t.discharge_data) AS disChargeEnergy
        FROM ems_daily_charge_data t
        GROUP BY dateMonth
        ORDER BY dateMonth
    </select>
</mapper>
