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

    <resultMap id="BaseResultMap" type="com.ruoyi.ems.domain.ChargingTransaction">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="transaction_no" property="transactionNo" jdbcType="VARCHAR"/>
        <result column="pile_code" property="pileCode" jdbcType="VARCHAR"/>
        <result column="gun_no" property="gunNo" jdbcType="VARCHAR"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="area_code" property="areaCode" jdbcType="VARCHAR"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="charging_duration" property="chargingDuration" jdbcType="INTEGER"/>
        <result column="record_date" property="recordDate" jdbcType="DATE"/>
        <result column="meter_start" property="meterStart" jdbcType="DOUBLE"/>
        <result column="meter_end" property="meterEnd" jdbcType="DOUBLE"/>
        <result column="total_energy" property="totalEnergy" jdbcType="DOUBLE"/>
        <result column="total_energy_loss" property="totalEnergyLoss" jdbcType="DOUBLE"/>
        <result column="total_amount" property="totalAmount" jdbcType="DOUBLE"/>
        <result column="vin" property="vin" jdbcType="VARCHAR"/>
        <result column="transaction_type" property="transactionType" jdbcType="TINYINT"/>
        <result column="stop_reason" property="stopReason" jdbcType="VARCHAR"/>
        <result column="stop_reason_desc" property="stopReasonDesc" jdbcType="VARCHAR"/>
        <result column="is_normal_stop" property="isNormalStop" jdbcType="TINYINT"/>
        <result column="physical_card_no" property="physicalCardNo" jdbcType="VARCHAR"/>
        <result column="include_in_ems" property="includeInEms" jdbcType="TINYINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, transaction_no, pile_code, gun_no, device_code, area_code,
        start_time, end_time, charging_duration, record_date,
        meter_start, meter_end, total_energy, total_energy_loss, total_amount,
        vin, transaction_type, stop_reason, stop_reason_desc, is_normal_stop,
        physical_card_no, include_in_ems, create_time, update_time
    </sql>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.ChargingTransaction"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO adm_charging_transaction (
            transaction_no, pile_code, gun_no, device_code, area_code,
            start_time, end_time, charging_duration, record_date,
            meter_start, meter_end, total_energy, total_energy_loss, total_amount,
            vin, transaction_type, stop_reason, stop_reason_desc, is_normal_stop,
            physical_card_no, include_in_ems
        ) VALUES (
            #{transactionNo}, #{pileCode}, #{gunNo}, #{deviceCode}, #{areaCode},
            #{startTime}, #{endTime}, #{chargingDuration}, #{recordDate},
            #{meterStart}, #{meterEnd}, #{totalEnergy}, #{totalEnergyLoss}, #{totalAmount},
            #{vin}, #{transactionType}, #{stopReason}, #{stopReasonDesc}, #{isNormalStop},
            #{physicalCardNo}, #{includeInEms}
        )
    </insert>

    <select id="selectByTransactionNo" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_transaction
        WHERE transaction_no = #{transactionNo}
    </select>

    <select id="selectByDeviceAndDateRange" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_transaction
        WHERE device_code = #{deviceCode}
          AND record_date BETWEEN #{startDate} AND #{endDate}
        ORDER BY end_time DESC
    </select>

    <select id="selectByAreaAndDate" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_transaction
        WHERE area_code = #{areaCode}
          AND record_date = #{date}
        ORDER BY end_time DESC
    </select>

    <select id="selectIncludedInEms" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM adm_charging_transaction
        WHERE include_in_ems = 1
          AND record_date = #{date}
        ORDER BY end_time
    </select>

    <update id="updateIncludeInEms">
        UPDATE adm_charging_transaction
        SET include_in_ems = #{includeInEms}
        WHERE transaction_no = #{transactionNo}
    </update>

    <select id="selectDailyStat" resultMap="BaseResultMap">
        SELECT
            #{areaCode} as area_code,
            #{date} as record_date,
            COUNT(*) as charging_duration,
            SUM(total_energy) as total_energy,
            SUM(total_energy_loss) as total_energy_loss,
            SUM(total_amount) as total_amount
        FROM adm_charging_transaction
        WHERE area_code = #{areaCode}
          AND record_date = #{date}
    </select>

</mapper>
