<?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.ruoyi.ems.mapper.ChargingMeterHMapper">

    <resultMap id="BaseResultMap" type="com.ruoyi.ems.domain.ChargingMeterH">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="transaction_no" property="transactionNo" jdbcType="VARCHAR"/>
        <result column="area_code" property="areaCode" jdbcType="VARCHAR"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="record_time" property="recordTime" jdbcType="TIMESTAMP"/>
        <result column="date" property="date" jdbcType="DATE"/>
        <result column="time" property="time" jdbcType="TIME"/>
        <result column="elec_quantity" property="elecQuantity" jdbcType="DOUBLE"/>
        <result column="elec_quantity_loss" property="elecQuantityLoss" jdbcType="DOUBLE"/>
        <result column="meter_type" property="meterType" jdbcType="INTEGER"/>
        <result column="meter_unit_price" property="meterUnitPrice" jdbcType="DOUBLE"/>
        <result column="use_elec_cost" property="useElecCost" jdbcType="DOUBLE"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, transaction_no, area_code, device_code, record_time, date, time,
        elec_quantity, elec_quantity_loss, meter_type, meter_unit_price, use_elec_cost,
        create_time
    </sql>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.ChargingMeterH"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO adm_charging_meter_h (
            transaction_no, area_code, device_code, record_time, date, time,
            elec_quantity, elec_quantity_loss, meter_type, meter_unit_price, use_elec_cost
        ) VALUES (
                     #{transactionNo}, #{areaCode}, #{deviceCode}, #{recordTime}, #{date}, #{time},
                     #{elecQuantity}, #{elecQuantityLoss}, #{meterType}, #{meterUnitPrice}, #{useElecCost}
                 )
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO adm_charging_meter_h (
        transaction_no, area_code, device_code, record_time, date, time,
        elec_quantity, elec_quantity_loss, meter_type, meter_unit_price, use_elec_cost
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.transactionNo}, #{item.areaCode}, #{item.deviceCode}, #{item.recordTime},
            #{item.date}, #{item.time}, #{item.elecQuantity}, #{item.elecQuantityLoss},
            #{item.meterType}, #{item.meterUnitPrice}, #{item.useElecCost})
        </foreach>
    </insert>

    <select id="selectByTransactionNo" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_meter_h
        WHERE transaction_no = #{transactionNo}
        ORDER BY meter_type DESC
    </select>

    <select id="selectByDeviceAndDateRange" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_meter_h
        WHERE device_code = #{deviceCode}
        AND date BETWEEN #{startDate} AND #{endDate}
        ORDER BY record_time DESC, meter_type DESC
    </select>

    <select id="selectByAreaAndDate" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_meter_h
        WHERE area_code = #{areaCode}
        AND date = #{date}
        ORDER BY record_time DESC, meter_type DESC
    </select>

    <select id="selectDailyStatByMeterType" resultMap="BaseResultMap">
        SELECT
            area_code,
            date,
            meter_type,
            SUM(elec_quantity) as elec_quantity,
            SUM(elec_quantity_loss) as elec_quantity_loss,
            SUM(use_elec_cost) as use_elec_cost
        FROM adm_charging_meter_h
        WHERE area_code = #{areaCode}
          AND date = #{date}
        GROUP BY area_code, date, meter_type
        ORDER BY meter_type DESC
    </select>

    <select id="selectLatestByDevice" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_meter_h
        WHERE device_code = #{deviceCode}
        ORDER BY record_time DESC
        LIMIT 1
    </select>

</mapper>