<?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.ss.billEleRecord.mapper.BillEleRecordMapper">

    <resultMap type="BillEleRecordVO" id="BillEleRecordResult" autoMapping="true"/>

    <sql id="selectBillEleRecordVo">
        select
            sber.id,
            sber.bill_id,
            sber.total_ele,
            sber.used_ele,
            sber.price,
            sber.total_price,
            sber.create_time,
            sber.time_start,
            sber.time_end,
            sber.record_time,
            stb.bill_no as bill_no
        from ss_bill_ele_record sber
            left join sm_transaction_bill stb on stb.bill_id = sber.bill_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and sber.id = #{query.id}</if>
        <if test="query.billId != null "> and sber.bill_id = #{query.billId}</if>
         <if test="query.billNo != null "> and stb.bill_no like concat('%',#{query.billNo},'%')</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectBillEleRecordList" parameterType="BillEleRecordQuery" resultMap="BillEleRecordResult">
        <include refid="selectBillEleRecordVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectBillEleRecordById" parameterType="Long" resultMap="BillEleRecordResult">
        <include refid="selectBillEleRecordVo"/>
        where sber.id = #{id}
    </select>

    <select id="selectMaxEle" resultType="java.math.BigDecimal">
        select max(sber.total_ele)
        from ss_bill_ele_record sber
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSumOfTotalPrice" resultType="java.math.BigDecimal">
        select sum(sber.total_price)
        from ss_bill_ele_record sber
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertBillEleRecord" parameterType="BillEleRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ss_bill_ele_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billId != null">bill_id,</if>
            <if test="totalEle != null">total_ele,</if>
            <if test="usedEle != null">used_ele,</if>
            <if test="price != null">price,</if>
            <if test="totalPrice != null">total_price,</if>
            <if test="createTime != null">create_time,</if>
            <if test="timeStart != null">time_start,</if>
            <if test="timeEnd != null">time_end,</if>
            <if test="recordTime != null">record_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billId != null">#{billId},</if>
            <if test="totalEle != null">#{totalEle},</if>
            <if test="usedEle != null">#{usedEle},</if>
            <if test="price != null">#{price},</if>
            <if test="totalPrice != null">#{totalPrice},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="timeStart != null">#{timeStart},</if>
            <if test="timeEnd != null">#{timeEnd},</if>
            <if test="recordTime != null">#{recordTime},</if>
         </trim>
    </insert>


    <insert id="batchInsert" parameterType="BillEleRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ss_bill_ele_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            bill_id,
            total_ele,
            used_ele,
            price,
            total_price,
            create_time,
            time_start,
            time_end,
            record_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.billId != null ">#{i.billId},</if>
                <if test="i.billId == null ">default,</if>
                <if test="i.totalEle != null ">#{i.totalEle},</if>
                <if test="i.totalEle == null ">default,</if>
                <if test="i.usedEle != null ">#{i.usedEle},</if>
                <if test="i.usedEle == null ">default,</if>
                <if test="i.price != null ">#{i.price},</if>
                <if test="i.price == null ">default,</if>
                <if test="i.totalPrice != null ">#{i.totalPrice},</if>
                <if test="i.totalPrice == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.timeStart != null ">#{i.timeStart},</if>
                <if test="i.timeStart == null ">default,</if>
                <if test="i.timeEnd != null ">#{i.timeEnd},</if>
                <if test="i.timeEnd == null ">default,</if>
                <if test="i.recordTime != null ">#{i.recordTime},</if>
                <if test="i.recordTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateBillEleRecord" parameterType="BillEleRecord">
        update ss_bill_ele_record
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.billId != null">bill_id = #{data.billId},</if>
        <if test="data.totalEle != null">total_ele = #{data.totalEle},</if>
        <if test="data.usedEle != null">used_ele = #{data.usedEle},</if>
        <if test="data.price != null">price = #{data.price},</if>
        <if test="data.totalPrice != null">total_price = #{data.totalPrice},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.timeStart != null">time_start = #{data.timeStart},</if>
        <if test="data.timeEnd != null">time_end = #{data.timeEnd},</if>
        <if test="data.recordTime != null">record_time = #{data.recordTime},</if>
    </sql>

    <delete id="deleteBillEleRecordById" parameterType="Long">
        delete from ss_bill_ele_record where id = #{id}
    </delete>

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