<?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.jsowell.pile.mapper.OrderBasicInfoMapper">
    <resultMap type="com.jsowell.pile.domain.OrderBasicInfo" id="OrderBasicInfoResult">
        <result property="id" column="id"/>
        <result property="orderCode" column="order_code"/>
        <result property="transactionCode" column="transaction_code"/>
        <result property="orderStatus" column="order_status"/>
        <result property="memberId" column="member_id"/>
        <result property="stationId" column="station_id"/>
        <result property="pileSn" column="pile_sn"/>
        <result property="connectorCode" column="connector_code"/>
        <result property="pileConnectorCode" column="pile_connector_code"/>
        <result property="logicCard" column="logic_card"/>
        <result property="startMode" column="start_mode"/>
        <result property="payMode" column="pay_mode"/>
        <result property="payStatus" column="pay_status"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="payTime" column="pay_time"/>
        <result property="orderAmount" column="order_amount"/>
        <result property="chargeStartTime" column="charge_start_time"/>
        <result property="chargeEndTime" column="charge_end_time"/>
        <result property="startType" column="start_type"/>
        <result property="appointmentTime" column="appointment_time"/>
        <result property="startSOC" column="start_soc"/>
        <result property="endSOC" column="end_soc"/>
        <result property="reason" column="reason"/>
        <result property="settlementTime" column="settlement_time"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <resultMap id="OrderBasicInfoOrderDetailResult" type="com.jsowell.pile.domain.OrderBasicInfo" extends="OrderBasicInfoResult">
        <collection property="orderDetailList" notNullColumn="sub_id" javaType="java.util.List"
                    resultMap="OrderDetailResult"/>
    </resultMap>

    <resultMap type="com.jsowell.pile.domain.OrderDetail" id="OrderDetailResult">
        <result property="id" column="id"/>
        <result property="orderCode" column="order_code"/>
        <result property="totalUsedElectricity" column="total_used_electricity"/>
        <result property="totalOrderAmount" column="total_order_amount"/>
        <result property="totalElectricityAmount" column="total_electricity_amount"/>
        <result property="totalServiceAmount" column="total_service_amount"/>
        <result property="sharpPrice" column="sharp_price"/>
        <result property="sharpUsedElectricity" column="sharp_used_electricity"/>
        <result property="sharpElectricityPrice" column="sharp_electricity_price"/>
        <result property="sharpServicePrice" column="sharp_service_price"/>
        <result property="sharpAmount" column="sharp_amount"/>
        <result property="peakPrice" column="peak_price"/>
        <result property="peakUsedElectricity" column="peak_used_electricity"/>
        <result property="peakElectricityPrice" column="peak_electricity_price"/>
        <result property="peakServicePrice" column="peak_service_price"/>
        <result property="peakAmount" column="peak_amount"/>
        <result property="flatPrice" column="flat_price"/>
        <result property="flatUsedElectricity" column="flat_used_electricity"/>
        <result property="flatElectricityPrice" column="flat_electricity_price"/>
        <result property="flatServicePrice" column="flat_service_price"/>
        <result property="flatAmount" column="flat_amount"/>
        <result property="valleyPrice" column="valley_price"/>
        <result property="valleyUsedElectricity" column="valley_used_electricity"/>
        <result property="valleyElectricityPrice" column="valley_electricity_price"/>
        <result property="valleyServicePrice" column="valley_service_price"/>
        <result property="valleyAmount" column="valley_amount"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="selectOrderBasicInfoVo">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
    </sql>

    <sql id="Base_Column_List">
        id,
        order_code,
        transaction_code,
        order_status,
        member_id,
        station_id,
        pile_sn,
        connector_code,
        pile_connector_code,
        logic_card,
        start_mode,
        pay_mode,
        pay_status,
        pay_amount,
        pay_time,
        order_amount,
        charge_start_time,
        charge_end_time,
        start_type,
        appointment_time,
        start_soc,
        end_soc,
        reason,
        settlement_time,
        refund_amount,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>

    <sql id="Detail_Base_Column_List">
        id,
        order_code,
        total_used_electricity,
        total_order_amount,
        total_electricity_amount,
        total_service_amount,
        sharp_price,
        sharp_used_electricity,
        sharp_electricity_price,
        sharp_service_price,
        sharp_amount,
        peak_price,
        peak_used_electricity,
        peak_electricity_price,
        peak_service_price,
        peak_amount,
        flat_price,
        flat_used_electricity,
        flat_electricity_price,
        flat_service_price,
        flat_amount,
        valley_price,
        valley_used_electricity,
        valley_electricity_price,
        valley_service_price,
        valley_amount,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>

    <select id="selectOrderBasicInfoList" parameterType="com.jsowell.pile.dto.QueryOrderDTO" resultType="com.jsowell.pile.vo.web.OrderListVO">
        SELECT
            t1.id as id,
            t1.order_code as orderCode,
            t1.transaction_code as transactionCode,
            t1.order_status as orderStatus,
            t1.member_id as memberId,
            t2.nick_name as nickName,
            t2.mobile_number as mobileNumber,
            t1.station_id as stationId,
            t3.station_name as stationName,
            t1.pile_sn as pileSn,
            t1.connector_code as connectorCode,
            t1.logic_card as logicCard,
            t1.start_mode as startMode,
            t1.pay_mode as payMode,
            t1.pay_status as payStatus,
            t1.pay_amount as payAmount,
            t1.order_amount as orderAmount,
            t1.start_soc as startSoc,
            t1.end_soc as endSoc,
            t1.charge_start_time as chargeStartTime,
            t1.charge_end_time as chargeEndTime,
            t1.create_time as createTime,
            t4.total_used_electricity as chargingDegree
        from order_basic_info t1
        left join member_basic_info t2 on t1.member_id = t2.member_id
        join pile_station_info t3 on t1.station_id = t3.id
        join order_detail t4 on t4.order_code = t1.order_code
        where t1.del_flag = '0'
        <if test="pileSn != null and pileSn != ''">
            and t1.pile_sn =  #{pileSn,jdbcType=VARCHAR}
        </if>
        <if test="connectorCode != null and connectorCode != ''">
            and t1.connector_code =  #{connectorCode,jdbcType=VARCHAR}
        </if>
        <if test="memberId != null and memberId != ''">
            and t1.member_id = #{memberId,jdbcType=VARCHAR}
        </if>
        <if test="orderStatus != null and orderStatus != ''">
            and t1.order_status = #{orderStatus,jdbcType=VARCHAR}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and t1.order_code = #{orderCode,jdbcType=VARCHAR}
        </if>
        <if test="transactionCode != null and transactionCode != ''">
            and t1.transaction_code = #{transactionCode,jdbcType=VARCHAR}
        </if>
        <if test="mobileNumber != null and mobileNumber != ''">
            and t2.mobile_number = #{mobileNumber,jdbcType=VARCHAR}
        </if>
        <if test="stationId != null and stationId != ''">
            and t1.station_id = #{stationId,jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != ''">
            and t1.create_time <![CDATA[ >= ]]> #{startTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null and endTime != ''">
            and t1.create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        </if>
        <!-- 数据范围过滤 -->
        <!--<if test="merchantDeptIds != null and merchantDeptIds.size() != 0">
            and t3.dept_id in
            <foreach collection="merchantDeptIds" item="merchantDeptId" open="(" separator="," close=")">
                #{merchantDeptId}
            </foreach>
        </if>-->
        <if test="stationDeptIds != null and stationDeptIds.size() != 0">
            and t3.dept_id in
            <foreach collection="stationDeptIds" item="stationDeptId" open="(" separator="," close=")">
                #{stationDeptId}
            </foreach>
        </if>
        order by t1.create_time desc
    </select>

    <select id="selectOrderBasicInfoById" parameterType="Long" resultMap="OrderBasicInfoOrderDetailResult">
        select a.id,
               a.order_code,
               a.order_status,
               a.member_id,
               a.station_id,
               a.pile_sn,
               a.connector_code,
               a.start_mode,
               a.pay_mode,
               a.pay_status,
               a.pay_amount,
               a.pay_time,
               a.order_amount,
               a.reason,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.del_flag,
               b.id                       as sub_id,
               b.order_code               as sub_order_code,
               b.total_used_electricity   as sub_total_used_electricity,
               b.total_order_amount       as sub_total_order_amount,
               b.total_electricity_amount as sub_total_electricity_amount,
               b.total_service_amount     as sub_total_service_amount,
               b.sharp_used_electricity   as sub_sharp_used_electricity,
               b.sharp_electricity_price  as sub_sharp_electricity_price,
               b.sharp_service_price      as sub_sharp_service_price,
               b.peak_used_electricity    as sub_peak_used_electricity,
               b.peak_electricity_price   as sub_peak_electricity_price,
               b.peak_service_price       as sub_peak_service_price,
               b.flat_used_electricity    as sub_flat_used_electricity,
               b.flat_electricity_price   as sub_flat_electricity_price,
               b.flat_service_price       as sub_flat_service_price,
               b.valley_used_electricity  as sub_valley_used_electricity,
               b.valley_electricity_price as sub_valley_electricity_price,
               b.valley_service_price     as sub_valley_service_price,
               b.create_by                as sub_create_by,
               b.create_time              as sub_create_time,
               b.update_by                as sub_update_by,
               b.update_time              as sub_update_time,
               b.del_flag                 as sub_del_flag
        from order_basic_info a
                 left join order_detail b on b.order_code = a.id
        where a.id = #{id}
    </select>

    <insert id="insertOrderBasicInfo" parameterType="com.jsowell.pile.domain.OrderBasicInfo" useGeneratedKeys="true" keyProperty="id">
        insert into order_basic_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderCode != null">
                order_code,
            </if>
            <if test="transactionCode != null">
                transaction_code,
            </if>
            <if test="orderStatus != null">
                order_status,
            </if>
            <if test="memberId != null">
                member_id,
            </if>
            <if test="stationId != null">
                station_id,
            </if>
            <if test="pileSn != null">
                pile_sn,
            </if>
            <if test="connectorCode != null">
                connector_code,
            </if>
            <if test="pileConnectorCode != null">
                pile_connector_code,
            </if>
            <if test="logicCard != null">
                logic_card,
            </if>
            <if test="startMode != null">
                start_mode,
            </if>
            <if test="payMode != null">
                pay_mode,
            </if>
            <if test="payStatus != null">
                pay_status,
            </if>
            <if test="payAmount != null">
                pay_amount,
            </if>
            <if test="payTime != null">
                pay_time,
            </if>
            <if test="orderAmount != null">
                order_amount,
            </if>
            <if test="chargeStartTime != null">
                charge_start_time,
            </if>
            <if test="chargeEndTime != null">
                charge_end_time,
            </if>
            <if test="startType != null">
                start_type,
            </if>
            <if test="appointmentTime != null">
                appointment_time,
            </if>
            <if test="startSOC != null">
                start_soc,
            </if>
            <if test="endSOC != null">
                end_soc,
            </if>
            <if test="reason != null">
                reason,
            </if>
            <if test="settlementTime != null">
                settlement_time,
            </if>
            <if test="refundAmount != null">
                refund_amount,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderCode != null">
                #{orderCode},
            </if>
            <if test="transactionCode != null">
                #{transactionCode},
            </if>
            <if test="orderStatus != null">
                #{orderStatus},
            </if>
            <if test="memberId != null">
                #{memberId},
            </if>
            <if test="stationId != null">
                #{stationId},
            </if>
            <if test="pileSn != null">
                #{pileSn,jdbcType=VARCHAR},
            </if>
            <if test="connectorCode != null">
                #{connectorCode},
            </if>
            <if test="pileConnectorCode != null">
                #{pileConnectorCode},
            </if>
            <if test="logicCard != null">
                #{logicCard},
            </if>
            <if test="startMode != null">
                #{startMode},
            </if>
            <if test="payMode != null">
                #{payMode},
            </if>
            <if test="payStatus != null">
                #{payStatus},
            </if>
            <if test="payAmount != null">
                #{payAmount},
            </if>
            <if test="payTime != null">
                #{payTime},
            </if>
            <if test="orderAmount != null">
                #{orderAmount},
            </if>
            <if test="chargeStartTime != null">
                #{chargeStartTime},
            </if>
            <if test="chargeEndTime != null">
                #{chargeEndTime},
            </if>
            <if test="startType != null">
                #{startType},
            </if>
            <if test="appointmentTime != null">
                #{appointmentTime},
            </if>
            <if test="startSOC != null">
                #{startSOC},
            </if>
            <if test="endSOC != null">
                #{endSOC},
            </if>
            <if test="reason != null">
                #{reason},
            </if>
            <if test="settlementTime != null">
                #{settlementTime},
            </if>
            <if test="refundAmount != null">
                #{refundAmount},
            </if>
            <if test="createBy != null">
                #{createBy},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateBy != null">
                #{updateBy},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="delFlag != null">
                #{delFlag},
            </if>
        </trim>
    </insert>

    <update id="updateOrderBasicInfo" parameterType="com.jsowell.pile.domain.OrderBasicInfo">
        update order_basic_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">
                order_code = #{orderCode},
            </if>
            <if test="orderStatus != null">
                order_status = #{orderStatus},
            </if>
            <if test="memberId != null">
                member_id = #{memberId},
            </if>
            <if test="stationId != null">
                station_id = #{stationId},
            </if>
            <if test="pileSn != null">
                pile_sn = #{pileSn},
            </if>
            <if test="connectorCode != null">
                connector_code = #{connectorCode},
            </if>
            <if test="pileConnectorCode != null">
                pile_connector_code = #{pileConnectorCode},
            </if>
            <if test="logicCard != null">
                logic_card = #{logicCard},
            </if>
            <if test="startMode != null">
                start_mode = #{startMode},
            </if>
            <if test="payMode != null">
                pay_mode = #{payMode},
            </if>
            <if test="payStatus != null">
                pay_status = #{payStatus},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            <if test="payTime != null">
                pay_time = #{payTime},
            </if>
            <if test="orderAmount != null">
                order_amount = #{orderAmount},
            </if>
            <if test="chargeStartTime != null">
                charge_start_time = #{chargeStartTime},
            </if>
            <if test="chargeEndTime != null">
                charge_end_time = #{chargeEndTime},
            </if>
            <if test="startType != null">
                start_type = #{startType},
            </if>
            <if test="appointmentTime != null">
                appointment_time = #{appointmentTime},
            </if>
            <if test="startSOC != null">
                start_soc = #{startSOC},
            </if>
            <if test="endSOC != null">
                end_soc = #{endSOC},
            </if>
            <if test="reason != null">
                reason = #{reason},
            </if>
            <if test="settlementTime != null">
                settlement_time = #{settlementTime},
            </if>
            <if test="refundAmount != null">
                refund_amount = #{refundAmount},
            </if>
            <if test="createBy != null">
                create_by = #{createBy},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
        </trim>
        where id = #{id}
    </update>

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

    <delete id="deleteOrderDetailByOrderCodes" parameterType="String">
        delete
        from order_detail where order_code in
        <foreach item="orderCode" collection="array" open="(" separator="," close=")">
            #{orderCode}
        </foreach>
    </delete>

    <delete id="deleteOrderDetailByOrderCode" parameterType="Long">
        delete
        from order_detail
        where order_code = #{orderCode}
    </delete>

    <insert id="batchOrderDetail">
        insert into order_detail(id, order_code, total_used_electricity, total_order_amount, total_electricity_amount,total_service_amount,
                    sharp_price, sharp_used_electricity, sharp_electricity_price, sharp_service_price, sharp_amount,
                    peak_price, peak_used_electricity, peak_electricity_price, peak_service_price, peak_amount,
                    flat_price, flat_used_electricity, flat_electricity_price, flat_service_price, flat_amount,
                    valley_price, valley_used_electricity, valley_electricity_price, valley_service_price, valley_amount,
                    create_by,update_by, create_time)
        values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.id}, #{item.orderCode}, #{item.totalUsedElectricity}, #{item.totalOrderAmount}, #{item.totalElectricityAmount}, #{item.totalServiceAmount},
            #{item.sharpPrice}, #{item.sharpUsedElectricity}, #{item.sharpElectricityPrice}, #{item.sharpServicePrice}, #{item.sharpAmount},
            #{item.peakPrice}, #{item.peakUsedElectricity}, #{item.peakElectricityPrice}, #{item.peakServicePrice}, #{item.peakAmount},
            #{item.flatPrice}, #{item.flatUsedElectricity}, #{item.flatElectricityPrice}, #{item.flatServicePrice}, #{item.flatAmount},
            #{item.valleyPrice}, #{item.valleyUsedElectricity}, #{item.valleyElectricityPrice}, #{item.valleyServicePrice}, #{item.valleyAmount},
            #{item.createBy}, #{item.updateBy}, #{item.createTime,jdbcType=TIMESTAMP})
        </foreach>
    </insert>

    <update id="updateOrderDetail" parameterType="com.jsowell.pile.domain.OrderDetail">
        update order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">
                order_code = #{orderCode},
            </if>
            <if test="totalUsedElectricity != null">
                total_used_electricity = #{totalUsedElectricity},
            </if>
            <if test="totalOrderAmount != null">
                total_order_amount = #{totalOrderAmount},
            </if>
            <if test="totalElectricityAmount != null">
                total_electricity_amount = #{totalElectricityAmount},
            </if>
            <if test="totalServiceAmount != null">
                total_service_amount = #{totalServiceAmount},
            </if>
            <if test="sharpPrice != null">
                sharp_price = #{sharpPrice},
            </if>
            <if test="sharpUsedElectricity != null">
                sharp_used_electricity = #{sharpUsedElectricity},
            </if>
            <if test="sharpElectricityPrice != null">
                sharp_electricity_price = #{sharpElectricityPrice},
            </if>
            <if test="sharpServicePrice != null">
                sharp_service_price = #{sharpServicePrice},
            </if>
            <if test="sharpAmount != null">
                sharp_amount = #{sharpAmount},
            </if>
            <if test="peakPrice != null">
                peak_price = #{peakPrice},
            </if>
            <if test="peakUsedElectricity != null">
                peak_used_electricity = #{peakUsedElectricity},
            </if>
            <if test="peakElectricityPrice != null">
                peak_electricity_price = #{peakElectricityPrice},
            </if>
            <if test="peakServicePrice != null">
                peak_service_price = #{peakServicePrice},
            </if>
            <if test="peakAmount != null">
                peak_amount = #{peakAmount},
            </if>
            <if test="flatPrice != null">
                flat_price = #{flatPrice},
            </if>
            <if test="flatUsedElectricity != null">
                flat_used_electricity = #{flatUsedElectricity},
            </if>
            <if test="flatElectricityPrice != null">
                flat_electricity_price = #{flatElectricityPrice},
            </if>
            <if test="flatServicePrice != null">
                flat_service_price = #{flatServicePrice},
            </if>
            <if test="flatAmount != null">
                flat_amount = #{flatAmount},
            </if>
            <if test="valleyPrice != null">
                valley_price = #{valleyPrice},
            </if>
            <if test="valleyUsedElectricity != null">
                valley_used_electricity = #{valleyUsedElectricity},
            </if>
            <if test="valleyElectricityPrice != null">
                valley_electricity_price = #{valleyElectricityPrice},
            </if>
            <if test="valleyServicePrice != null">
                valley_service_price = #{valleyServicePrice},
            </if>
            <if test="valleyAmount != null">
                valley_amount = #{valleyAmount},
            </if>
            <if test="createBy != null">
                create_by = #{createBy},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
        </trim>
        where id = #{id}
    </update>


    <select id="getOrderInfoByOrderCode" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        <where>
            order_code = #{orderCode,jdbcType=VARCHAR}
        </where>
        and del_flag = '0'
    </select>

    <select id="getOrderInfoByTransactionCode" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        and transaction_code = #{transactionCode,jdbcType=VARCHAR}
    </select>

    <select id="queryOrderBasicInfo" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        and pile_sn = #{pileSn,jdbcType=VARCHAR}
        and connector_code = #{connectorCode,jdbcType=VARCHAR}
        and order_status = #{orderStatus,jdbcType=VARCHAR}
        order by create_time DESC
        limit 1
    </select>

    <select id="getOrderDetailByOrderCode" resultMap="OrderDetailResult">
        select
        <include refid="Detail_Base_Column_List"/>
        from order_detail
        where del_flag = '0'
        and order_code = #{orderCode,jdbcType=VARCHAR}
    </select>

    <select id="getListByMemberIdAndOrderStatus" resultType="com.jsowell.pile.vo.uniapp.OrderVO">
        SELECT
            t1.order_code as orderCode,
            t1.order_status as orderStatus,
            t1.reason,
            t1.station_id,
            t3.station_name as stationName,
            t1.pile_sn as pileSn,
            t1.connector_code as connectorCode,
            t1.pay_status as payStatus,
            t1.order_amount as orderAmount,
            t1.pay_amount as payAmount,
            t2.total_used_electricity as chargingDegree,
            t1.charge_start_time as startTime,
            t1.charge_end_time as endTime,
            t2.total_electricity_amount as totalElectricityAmount,
            t2.total_service_amount as totalServiceAmount
        from order_basic_info t1
            join order_detail t2 on t1.order_code = t2.order_code
            join pile_station_info t3 on t1.station_id = t3.id
        where t1.del_flag = '0'
          and t1.member_id = #{memberId,jdbcType=VARCHAR}
        <if test="orderStatusList != null and orderStatusList.size() != 0">
            and t1.order_status in
            <foreach collection="orderStatusList" item="orderStatus" open="(" separator="," close=")">
                #{orderStatus,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="dateTime != null">
            and t1.create_time <![CDATA[ >= ]]> #{dateTime,jdbcType=VARCHAR}
        </if>
        order by t1.create_time desc

    </select>

    <update id="updateOrderStatusByOrderCode">
        update order_basic_info set order_status = #{orderStatus,jdbcType=VARCHAR}
        where del_flag = '0'
        and order_code = #{orderCode,jdbcType=VARCHAR}
    </update>

    <select id="getIndexOrderInfo" resultType="com.jsowell.pile.vo.web.IndexOrderInfoVO">
        SELECT
            DATE_FORMAT(t1.create_time,'%Y-%m-%d') as date,
            sum( t2.total_used_electricity ) AS totalElectricity,
            sum( t2.total_order_amount ) AS totalOrderAmount,
            sum( t2.sharp_used_electricity ) AS totalSharpUsedElectricity,
            sum( t2.peak_used_electricity ) AS totalPeakUsedElectricity,
            sum( t2.flat_used_electricity ) AS totalFlatUsedElectricity,
            sum( t2.valley_used_electricity ) AS totalValleyUsedElectricity
        FROM
            order_basic_info t1
                JOIN order_detail t2 ON t1.order_code = t2.order_code
                AND t1.del_flag = '0'
        WHERE
            date(t1.create_time) >= DATE_SUB(CURDATE(),INTERVAL 30 day)
          and t1.order_status = '6'
            <if test="dto.stationId != null and dto.stationId != ''">
                and t1.station_id = #{dto.stationId,jdbcType=VARCHAR}
            </if>
            <if test="dto.stationIdList != null and dto.stationIdList.size() != 0">
                and t1.station_id in
                <foreach collection="dto.stationIdList" item="item" open="(" separator="," close=")">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        group by DATE_FORMAT(t1.create_time,'%Y-%m-%d')
    </select>

    <select id="getUnpaidOrderListOver15Min" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        and order_status = '0'
        and pay_status = '0'
        and create_time <![CDATA[ <= ]]> #{createTime,jdbcType=VARCHAR}
    </select>

    <select id="selectOrderListByTimeRangeAndStatus" resultMap="OrderBasicInfoResult">
        select
            <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        <if test="startTime != null and startTime != ''">
            and create_time <![CDATA[ >= ]]> #{startTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null and endTime != ''">
            and create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        </if>
        <if test="orderStatus != null and orderStatus != ''">
            and order_status = #{orderStatus,jdbcType=VARCHAR}
        </if>
        <if test="payStatus != null and payStatus != ''">
            and pay_status = #{payStatus,jdbcType=VARCHAR}
        </if>
    </select>

    <update id="updateOrderStatusById">
        update order_basic_info set order_status = #{orderStatus,jdbcType=VARCHAR}
        where del_flag = '0'
        and id in
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")" >
            #{orderId,jdbcType=VARCHAR}
        </foreach>
    </update>

    <select id="selectOrderInfoByOrderCode" resultType="com.jsowell.pile.vo.uniapp.SendMessageVO">
        SELECT
            t1.order_code as orderCode,
            t1.pile_sn as pileSn,
            t1.connector_code as connectorCode,
            t1.charge_start_time as chargeStartTime,
            t1.charge_end_time as chargeStopTime,
            t1.reason as stopReason,
            t1.station_id as stationId,
            t2.station_name as stationName,
            t3.total_order_amount as orderAmount,
            t3.total_used_electricity as chargingDegree,
            t4.open_id as openId
        FROM
            order_basic_info t1
            JOIN pile_station_info t2 ON t1.station_id = t2.id
            join order_detail t3 on t1.order_code = t3.order_code
            join member_basic_info t4 on t1.member_id = t4.member_id
        where t1.order_code = #{orderCode,jdbcType=VARCHAR}
    </select>

    <select id="getOrderBasicInfo" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        <if test="memberId != null and memberId != ''">
            AND member_id = #{memberId,jdbcType=VARCHAR}
        </if>
        <if test="pileConnectorCode != null and pileConnectorCode != ''">
            AND pile_connector_code = #{pileConnectorCode,jdbcType=VARCHAR}
        </if>
        <if test="orderStatus != null and orderStatus != ''">
            AND order_status = #{orderStatus,jdbcType=VARCHAR}
        </if>
        <if test="startMode != null and startMode != ''">
            AND start_mode = #{startMode,jdbcType=VARCHAR}
        </if>
        order by create_time desc
        limit 1
    </select>


    <select id="getAccumulativeInfo" resultType="com.jsowell.pile.vo.uniapp.PersonPileConnectorSumInfoVO">
        SELECT
            t1.member_id as memberId,
            t1.pile_connector_code as pileConnectorCode,
            t1.pile_sn as pileSn,
            t1.charge_start_time as chargeStartTime,
            t1.charge_end_time as chargeEndTime,
            t2.total_used_electricity as sumChargingDegree
        FROM
            order_basic_info t1
                JOIN order_detail t2 ON t1.order_code = t2.order_code
        WHERE
            t1.pile_connector_code = #{pileConnectorCode,jdbcType=VARCHAR}
          AND t1.member_id = #{memberId,jdbcType=VARCHAR}
          AND t1.create_time <![CDATA[ >= ]]> #{startTime,jdbcType=VARCHAR}
          AND t1.create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        AND t1.order_status = '6'
        AND t1.del_flag = '0'
    </select>

    <select id="getAppointmentOrder" resultMap="OrderBasicInfoResult">
        select
        <include refid="Base_Column_List"/>
        from order_basic_info
        where del_flag = '0'
        and pay_status = '1'
        and start_type = 'appointment'
        and order_status = '0'
        and appointment_time <![CDATA[ <= ]]> #{dateTime,jdbcType=TIMESTAMP}
    </select>

    <select id="getListByOrderCodes" resultType="com.jsowell.pile.vo.uniapp.OrderVO">
        SELECT
            t1.order_code as orderCode,
            t1.order_status as orderStatus,
            t1.reason,
            t1.station_id,
            t3.station_name as stationName,
            t1.pile_sn as pileSn,
            t1.connector_code as connectorCode,
            t1.pay_status as payStatus,
            t1.order_amount as orderAmount,
            t1.pay_amount as payAmount,
            t2.total_used_electricity as chargingDegree,
            t1.charge_start_time as startTime,
            t1.charge_end_time as endTime,
            t2.total_electricity_amount as totalElectricityAmount,
            t2.total_service_amount as totalServiceAmount
        from order_basic_info t1
        join order_detail t2 on t1.order_code = t2.order_code
        join pile_station_info t3 on t1.station_id = t3.id
        where t1.del_flag = '0'
        and t1.order_code in
        <foreach collection="orderCodes" item="orderCode" open="(" separator="," close=")">
            #{orderCode,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="getAccumulativeInfoForLianLian" resultType="com.jsowell.pile.vo.lianlian.AccumulativeInfoVO">
        SELECT
            t1.order_code as orderCode,
            t1.pile_connector_code as pileConnectorCode,
            t1.pile_sn as pileSn,
            t1.station_id as stationId,
            IFNULL(t2.total_used_electricity,0.00) as connectorElectricity
        FROM
            order_basic_info t1
                JOIN order_detail t2 ON t1.order_code = t2.order_code
        WHERE
            t1.del_flag = '0'
          AND t1.station_id = #{stationID,jdbcType=VARCHAR}
          AND t1.create_time <![CDATA[ >= ]]> #{startTime,jdbcType=VARCHAR}
          AND t1.create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}

    </select>

    <select id="queryElecAmountNullList" resultMap="OrderDetailResult">
        select
            <include refid="Detail_Base_Column_List"/>
        from order_detail
        where del_flag = '0'
        and total_order_amount is not null
        AND total_order_amount > '0.00'
        and total_electricity_amount is null
    </select>

    <update id="batchUpdateOrderDetail">
        update order_detail
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="total_electricity_amount =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.totalElectricityAmount!=null">
                        when id=#{item.id} then #{item.totalElectricityAmount}
                    </if>
                </foreach>
            </trim>
            <trim prefix="total_service_amount =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.totalServiceAmount!=null">
                        when id=#{item.id} then #{item.totalServiceAmount}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index" >
            id=#{i.id}
        </foreach>
    </update>

    <select id="getOrderDetailList" resultMap="OrderDetailResult">
        select
        <include refid="Detail_Base_Column_List"/>
        from order_detail
        where del_flag = '0'
        and order_code in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>
</mapper>