<?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.evobms.project.battery.mapper.ChargeDischargeSummaryMapper">

    <resultMap type="ChargeDischargeSummary" id="ChargeDischargeSummaryResult">
        <result property="id" column="id" />
        <result property="deviceId" column="device_id" />
        <result property="timestamp" column="timestamp" />
        <result property="dischargeKwh" column="discharge_kwh" />
        <result property="chargeKwh" column="charge_kwh" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />
        <result property="createBy" column="create_by" />
        <result property="updateBy" column="update_by" />
    </resultMap>

    <sql id="selectChargeDischargeSummaryVo">
        select id, device_id, timestamp, discharge_kwh, charge_kwh, create_time, update_time, create_by, update_by from charge_discharge_summary
    </sql>

    <select id="selectChargeDischargeSummaryList" parameterType="ChargeDischargeSummary" resultMap="ChargeDischargeSummaryResult">
        <include refid="selectChargeDischargeSummaryVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="timestamp != null "> and timestamp = #{timestamp}</if>
            <if test="dischargeKwh != null "> and discharge_kwh = #{dischargeKwh}</if>
            <if test="chargeKwh != null "> and charge_kwh = #{chargeKwh}</if>
        </where>
    </select>

    <select id="selectChargeDischargeSummaryById" parameterType="Long" resultMap="ChargeDischargeSummaryResult">
        <include refid="selectChargeDischargeSummaryVo"/>
        where id = #{id}
    </select>

    <insert id="insertChargeDischargeSummary" parameterType="ChargeDischargeSummary" useGeneratedKeys="true" keyProperty="id">
        insert into charge_discharge_summary
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="dischargeKwh != null">discharge_kwh,</if>
            <if test="chargeKwh != null">charge_kwh,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="timestamp != null">#{timestamp},</if>
            <if test="dischargeKwh != null">#{dischargeKwh},</if>
            <if test="chargeKwh != null">#{chargeKwh},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateChargeDischargeSummary" parameterType="ChargeDischargeSummary">
        update charge_discharge_summary
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="dischargeKwh != null">discharge_kwh = #{dischargeKwh},</if>
            <if test="chargeKwh != null">charge_kwh = #{chargeKwh},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteChargeDischargeSummaryById" parameterType="Long">
        delete from charge_discharge_summary where id = #{id}
    </delete>

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