<?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.pwt.web.mapper.PwtChargeRecordMapper">
    
    <resultMap type="PwtChargeRecordVo" id="PwtChargeRecordResult">
        <result property="recordId"    column="record_id"    />
        <result property="vehicleNo"    column="vehicle_no"    />
        <result property="chargerNo"    column="charger_no"    />
        <result property="batterySn"    column="battery_sn"    />
        <result property="chargeQuantity"    column="charge_quantity"    />
        <result property="chargeBeginTime"    column="charge_begin_time"    />
        <result property="chargeEndTime"    column="charge_end_time"    />
        <result property="chargeDuration"    column="charge_duration"    />
        <result property="startSoc"    column="start_soc"    />
        <result property="endSoc"    column="end_soc"    />
        <result property="stopReason"    column="stop_reason"    />
        <result property="chargeMode"    column="charge_mode"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectPwtChargeRecordVo">
        select record_id,
               vehicle_no,
               charger_no,
               battery_sn,
               charge_quantity,
               charge_begin_time,
               charge_end_time,
               charge_duration,
               start_soc,
               end_soc,
               sr.description as `stop_reason`,
               CASE
                   WHEN charge_mode = 1 THEN '室内充电'
                   WHEN charge_mode = 2 THEN '应急充电'
                   END AS `charge_mode`,
               create_time
        from pwt_charge_record r
                 LEFT JOIN pwt_charge_stop_reason sr
                           ON r.stop_reason = sr.id
    </sql>

    <select id="selectPwtChargeRecordList" parameterType="PwtChargeRecordVo" resultMap="PwtChargeRecordResult">
        <include refid="selectPwtChargeRecordVo"/>
        <where>
            <if test="chargerNo != null  and chargerNo != ''"> and charger_no = #{chargerNo}</if>
            <if test="chargerMode != null  and chargerMode != ''"> and charge_mode = #{chargerMode}</if>
            <if test="chargeBeginTime != null "> and date_format(charge_begin_time, '%Y-%m-%d') &gt;= date_format(#{chargeBeginTime},'%Y-%m-%d')</if>
            <if test="chargeEndTime != null "> and date_format(charge_end_time,'%Y-%m-%d') &lt;= date_format(#{chargeEndTime},'%Y-%m-%d')</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectPwtChargeRecordById" parameterType="Long" resultMap="PwtChargeRecordResult">
        <include refid="selectPwtChargeRecordVo"/>
        where record_id = #{recordId}
    </select>

    <select id="getChargerNoDict" resultType="java.lang.String">
        select distinct(charger_no) from pwt_charge_record where charger_no is not null
    </select>

    <select id="getStatisticsRecord" parameterType="com.pwt.web.domain.StatisticsRecord" resultType="com.pwt.web.domain.StatisticsRecord">
        SELECT r.charger_no as 'chargerNo', SUM(r.charge_quantity) AS 'chargeQuantity', COUNT(1) AS 'chargeCount',
        (SELECT COUNT( 1 ) FROM pwt_change_battery_record cbr
        LEFT JOIN pwt_holder_charge hc on hc.holder_no = cbr.take_battery_compartment_no
        WHERE hc.charge_no = r.charger_no) AS 'changeCount'
        FROM pwt_charge_record r
        <where>
            <if test="chargerNo != null"> and charger_no = #{chargerNo}</if>
            <if test="beginTime != null "> and date_format(charge_begin_time, '%Y-%m-%d') &gt;= date_format(#{beginTime},'%Y-%m-%d')</if>
            <if test="endTime != null "> and date_format(charge_end_time,'%Y-%m-%d') &lt;= date_format(#{endTime},'%Y-%m-%d')</if>
        </where>
        GROUP BY r.charger_no ORDER BY charger_no
    </select>

    <insert id="insertPwtChargeRecord" parameterType="PwtChargeRecordDto" useGeneratedKeys="true" keyProperty="recordId">
        insert into pwt_charge_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="vehicleNo != null">vehicle_no,</if>
            <if test="chargerNo != null">charger_no,</if>
            <if test="batterySn != null">battery_sn,</if>
            <if test="chargeQuantity != null">charge_quantity,</if>
            <if test="chargeBeginTime != null">charge_begin_time,</if>
            <if test="chargeEndTime != null">charge_end_time,</if>
            <if test="chargeDuration != null">charge_duration,</if>
            <if test="startSoc != null">start_soc,</if>
            <if test="endSoc != null">end_soc,</if>
            <if test="stopReason != null">stop_reason,</if>
            <if test="chargeMode != null">charge_mode,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="vehicleNo != null">#{vehicleNo},</if>
            <if test="chargerNo != null">#{chargerNo},</if>
            <if test="batterySn != null">#{batterySn},</if>
            <if test="chargeQuantity != null">#{chargeQuantity},</if>
            <if test="chargeBeginTime != null">#{chargeBeginTime},</if>
            <if test="chargeEndTime != null">#{chargeEndTime},</if>
            <if test="chargeDuration != null">#{chargeDuration},</if>
            <if test="startSoc != null">#{startSoc},</if>
            <if test="endSoc != null">#{endSoc},</if>
            <if test="stopReason != null">#{stopReason},</if>
            <if test="chargeMode != null">#{chargeMode},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updatePwtChargeRecord" parameterType="PwtChargeRecordVo">
        update pwt_charge_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="vehicleNo != null">vehicle_no = #{vehicleNo},</if>
            <if test="chargerNo != null">charger_no = #{chargerNo},</if>
            <if test="batterySn != null">battery_sn = #{batterySn},</if>
            <if test="chargeQuantity != null">charge_quantity = #{chargeQuantity},</if>
            <if test="chargeBeginTime != null">charge_begin_time = #{chargeBeginTime},</if>
            <if test="chargeEndTime != null">charge_end_time = #{chargeEndTime},</if>
            <if test="chargeDuration != null">charge_duration = #{chargeDuration},</if>
            <if test="startSoc != null">start_soc = #{startSoc},</if>
            <if test="endSoc != null">end_soc = #{endSoc},</if>
            <if test="stopReason != null">stop_reason = #{stopReason},</if>
            <if test="chargeMode != null">charge_mode = #{chargeMode},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deletePwtChargeRecordById" parameterType="Long">
        delete from pwt_charge_record where record_id = #{recordId}
    </delete>

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