<?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.system.mapper.EtOrderMapper">

    <resultMap type="EtOrder" id="EtOrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="outTradeNo"    column="out_trade_no"    />
        <result property="userId"    column="user_id"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="deviceMac"    column="device_mac"    />
        <result property="sn"    column="sn"    />
        <result property="vehicleNum"    column="vehicle_num"    />
        <result property="payTime"    column="pay_time"    />
        <result property="paid"    column="paid"    />
        <result property="payType"    column="pay_type"    />
        <result property="type"    column="type"    />
        <result property="totalFee"    column="total_fee"    />
        <result property="payFee"    column="pay_fee"    />
        <result property="dispatchFee"    column="dispatch_fee"    />
        <result property="manageFee"    column="manage_fee"    />
        <result property="ridingFee"    column="riding_fee"    />
        <result property="appointmentFee"    column="appointment_fee"    />
        <result property="mark"    column="mark"    />
        <result property="duration"    column="duration"    />
        <result property="distance"    column="distance"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="appointmentStartTime"    column="appointment_start_time"    />
        <result property="appointmentEndTime"    column="appointment_end_time"    />
        <result property="appointmentTimeout"    column="appointment_timeout"    />
        <result property="unlockTime"    column="unlock_time"    />
        <result property="returnTime"    column="return_time"    />
        <result property="ruleEndTime"    column="rule_end_time"    />
        <result property="returnType"    column="return_type"    />
        <result property="tripRoute"    column="trip_route"    />
        <result property="tripRouteStr"    column="trip_route_str"    />
        <result property="cycle"    column="cycle"    />
        <result property="depositDeduction"    column="deposit_deduction"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="deductionAmount"    column="deduction_amount"    />
        <result property="audioFiles"    column="audio_files"    />
        <result property="usedSn"    column="used_sn"    />
        <result property="changeReason"    column="change_reason"    />
    </resultMap>

    <sql id="selectEtOrderVo">
        select order_id, area_id, order_no, out_trade_no, user_id, rule_id,
               device_mac, sn, pay_time, paid, pay_type, type, total_fee, pay_fee, dispatch_fee,
               manage_fee, riding_fee, appointment_fee, mark, duration, distance, status,
               create_time, appointment_start_time, appointment_end_time,appointment_timeout, unlock_time,return_time,
               rule_end_time, return_type, AsText(trip_route) trip_route,trip_route_str,cycle,deposit_deduction,video_url,deduction_amount,audio_files,used_sn,change_reason from et_order
    </sql>

    <select id="selectEtOrderList" parameterType="EtOrder" resultMap="EtOrderResult">
        SELECT
        o.order_id,
        o.area_id,
        oa.area_name AS area,
        oa.is_deposit_deduction isDepositDeduction,
        u.user_name AS userName,
        u.phonenumber AS phonenumber,
        u.real_name AS realName,
        o.order_no,
        o.out_trade_no,
        o.user_id,
        o.rule_id,
        o.device_mac,
        o.sn,
        de.vehicle_num,
        o.pay_time,
        o.paid,
        o.pay_type,
        o.type,
        COALESCE(o.total_fee, 0) AS total_fee,
        COALESCE(o.pay_fee, 0) AS pay_fee,
        COALESCE(o.dispatch_fee, 0) AS dispatch_fee,
        COALESCE(o.manage_fee, 0) AS manage_fee,
        COALESCE(o.riding_fee, 0) AS riding_fee,
        COALESCE(o.appointment_fee, 0) AS appointment_fee,
        o.mark,
        o.duration,
        o.distance,
        o.status,
        o.create_time,
        o.appointment_start_time,
        o.appointment_end_time,
        o.appointment_timeout,
        o.unlock_time,
        o.return_time,
        o.rule_end_time,
        o.return_type,
        AsText(o.trip_route),
        o.trip_route_str,
        o.video_url,
        o.deduction_amount,
        o.audio_files,
        o.used_sn,
        o.change_reason
        FROM
        et_order o
        LEFT JOIN
        et_operating_area oa ON o.area_id = oa.area_id
        LEFT JOIN
        et_device de ON de.sn = o.sn
        LEFT JOIN
        et_user u ON u.user_id = o.user_id
        LEFT join et_area_dept ad on ad.area_id = oa.area_id
        LEFT join sys_dept d on d.dept_id = ad.dept_id
        where 1 = 1
        <if test="orderNo != null  and orderNo != ''"> and o.order_no like concat('%', #{orderNo}, '%')</if>
        <if test="area != null  and area != ''"> and oa.area_name like concat('%', #{area}, '%')</if>
        <if test="areaId != null"> and o.area_id = #{areaId}</if>
        <if test="userName != null  and userName != ''"> and u.user_name like concat('%', #{userName}, '%')</if>
        <if test="phonenumber != null  and phonenumber != ''"> and u.phonenumber like concat('%', #{phonenumber}, '%')</if>
        <if test="userId != null  and userId != ''"> and o.user_id = #{userId}</if>
        <if test="deviceMac != null  and deviceMac != ''"> and o.device_mac like concat('%', #{deviceMac}, '%')</if>
        <if test="vehicleNum  != null  and vehicleNum  != ''"> and de.vehicle_num  like concat('%', #{vehicleNum }, '%')</if>
        <if test="sn != null  and sn != ''"> and o.sn like concat('%', #{sn}, '%')</if>
        <if test="type != null  and type != ''"> and o.type = #{type}</if>
        <if test="status != null  and status != ''"> and o.status = #{status}</if>
        <if test="paid != null  and paid != ''"> and o.paid = #{paid}</if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
        </if>
        <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
            AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
        <if test="startTime != null and startTime != ''"><!-- 开始时间检索 -->
            AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
            AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        <if test="statusList != null">
            AND o.status IN
            <foreach item="item" index="index" collection="statusList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by o.create_time desc
    </select>

    <select id="selectEtOrderByOrderId" parameterType="Long" resultMap="EtOrderResult">
        select o.order_id,
               o.area_id,
               o.order_no,
               o.out_trade_no,
               oa.area_name AS area,
               u.user_name AS userName,
               u.phonenumber AS phonenumber,
               o.rule_id,
               o.device_mac,
               o.sn,
               o.pay_time,
               o.paid,
               o.pay_type,
               o.video_url,
               o.deduction_amount,
               o.audio_files,
               o.type, COALESCE(o.total_fee, 0) AS total_fee,
               COALESCE(o.pay_fee, 0) AS pay_fee,
               COALESCE(o.dispatch_fee, 0) AS dispatch_fee,
               COALESCE(o.manage_fee, 0) AS manage_fee,
               COALESCE(o.riding_fee, 0) AS riding_fee,
               COALESCE(o.appointment_fee, 0) AS appointment_fee,
               o.mark,
               o.duration,
               o.distance,
               o.status,
               o.create_time,
               o.appointment_start_time,
               o.appointment_end_time,
               o.appointment_timeout,
               o.unlock_time,
               o.return_time,
               o.rule_end_time,
               o.return_type,
               AsText(o.trip_route) trip_route,
               o.trip_route_str from et_order o
        LEFT JOIN et_user u ON u.user_id = o.user_id
        LEFT JOIN et_operating_area oa ON o.area_id = oa.area_id
        where order_id = #{orderId}
    </select>

    <select id="selectEtOrderByOrderNo" parameterType="String" resultMap="EtOrderResult">
        <include refid="selectEtOrderVo"/>
        where order_no = #{orderNo}
    </select>

    <select id="selectEtOrderByOutTradeNo" parameterType="String" resultMap="EtOrderResult">
        <include refid="selectEtOrderVo"/>
        where out_trade_no = #{outTradeNo}
    </select>

    <select id="isInOrder" resultMap="EtOrderResult" parameterType="Long">
        <include refid="selectEtOrderVo"/>
        <where>
            <if test="orderNo != null  and orderNo != ''"> and order_no != #{orderNo}</if>
             and user_id = #{userId} and status !=4 and status !=5 and status !=6 and status !=7 and type = 1
        </where>
    </select>

    <select id="isInOrderBySn" resultMap="EtOrderResult" parameterType="String">
        <include refid="selectEtOrderVo"/>
            where sn = #{sn} and status =2  and type = 1
    </select>

    <select id="checkIsUnique" resultType="Integer" parameterType="Long">
        select count(1) from et_order
        where user_id = #{userId} and status = 0 and type=1
    </select>

    <select id="getTotalIncome" resultType="java.lang.String">
        select COALESCE(SUM(total_fee), 0)  from et_order where status = 4 and type = 1
        AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalUnpaid" resultType="java.lang.String" parameterType="String">
        select  COALESCE(SUM(total_fee), 0)  from et_order where status = 3 and type = 1 and paid = 0
        <if test="timeStart != null  and timeStart != ''">
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        </if>
        <if test="timeEnd != null  and timeEnd != ''">
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        </if>
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalPaid" resultType="java.lang.String">
        select  COALESCE(SUM(total_fee), 0)  from et_order where status = 4 and type = 1 and paid = 1
        AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalRefund" resultType="java.lang.String">
        select  COALESCE(SUM(r.amount), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>

    <select id="getTotalRidingFee" resultType="java.lang.String">
        select  COALESCE(SUM(riding_fee), 0) from et_order where status = 4 and type = 1 and paid = 1
         AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
         AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalRidingRefund" resultType="java.lang.String">
        select  COALESCE(SUM(r.riding_fee), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>

    <select id="getTotalDispatchFee" resultType="java.lang.String">
        select COALESCE(SUM(dispatch_fee), 0) from et_order where status = 4 and type = 1 and paid = 1
           AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
           AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
           <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalDispatchRefund" resultType="java.lang.String">
        select  COALESCE(SUM(r.dispatch_fee), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>

    <select id="getTotalAppointmentFee" resultType="java.lang.String">
        select COALESCE(SUM(appointment_fee), 0) from et_order where status = 4 and type = 1 and paid = 1
              AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
              AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalAppointmentRefund" resultType="java.lang.String">
        select  COALESCE(SUM(r.appointment_fee), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>

    <select id="getTotalManageFee" resultType="java.lang.String">
        select COALESCE(SUM(manage_fee), 0) from et_order where status = 4 and type = 1 and paid = 1
         AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
         AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getTotalManageRefund" resultType="java.lang.String">
        select  COALESCE(SUM(r.manage_fee), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>
    <select id="getPaidOrder" resultType="java.lang.String">
        select COALESCE(count(1), 0) from et_order where status = 4 and type = 1 and paid = 1
          AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
          AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
          <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>
    <select id="getRidingOrder" resultType="java.lang.String">
        select COALESCE(count(1), 0) from et_order where status = 2 and type = 1
          AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
          AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getRefundOrder" resultType="java.lang.String">
        select  COALESCE(count(1), 0)  from et_refund r left join et_order o on o.order_no = r.order_no where r.type = 1 AND refund_result = 'SUCCESS'
        AND date_format(r.create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(r.create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and o.area_id = #{areaId}</if>
    </select>

    <select id="getUnpaidOrder" resultType="java.lang.String">
        select COALESCE(count(1), 0) from et_order where status = 3 and type = 1 and paid = 0
        <if test="timeStart != null  and timeStart != ''">
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        </if>
        <if test="timeEnd != null  and timeEnd != ''">
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        </if>
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <!--还车待审核订单数-->
    <select id="getAuditOrderNum" resultType="java.lang.Integer">
        select COALESCE(count(1), 0) from et_order where status = 5 and type = 1
        <if test="timeStart != null  and timeStart != ''">
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        </if>
        <if test="timeEnd != null  and timeEnd != ''">
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        </if>
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <!--待审核还车押金扣款 -->
    <select id="getReturnOrderDeductFee" resultType="java.math.BigDecimal">
        select COALESCE(SUM(total_fee), 0) from et_order where status > 5 and type = 1
        <if test="timeStart != null  and timeStart != ''">
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        </if>
        <if test="timeEnd != null  and timeEnd != ''">
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        </if>
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getOrderNum" resultType="java.lang.Integer">
        select COALESCE(count(1), 0) from et_order where type = 1
        <if test="timeStart != null  and timeStart != ''">
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        </if>
        <if test="timeEnd != null  and timeEnd != ''">
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        </if>
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getServiceFee" resultType="java.math.BigDecimal">
        select COALESCE(SUM(total_fee), 0) from et_order where status = 4 and type = 1 and paid = 1
        AND date_format(create_time,'%y%m%d') &gt;= date_format(#{timeStart},'%y%m%d')
        AND date_format(create_time,'%y%m%d') &lt;= date_format(#{timeEnd},'%y%m%d')
        <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
    </select>

    <select id="getPayFee" resultType="java.math.BigDecimal">
        select COALESCE(SUM(pay_fee), 0) from et_order
        <where>
            <if test="sn != null  and sn != ''"> and sn = #{sn}</if>
            <if test="areaId != null"> and area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
              AND date_format(create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND status = 4 and type = 1 and paid = 1
        </where>
    </select>

    <select id="getOrderFee" resultType="java.math.BigDecimal">
        select COALESCE(SUM(pay_fee), 0) from et_order
        <where>
            <if test="areaId != null"> and area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
                AND date_format(create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND status = 4 and type = 1
        </where>
    </select>

    <select id="getPartnerFee" resultType="java.math.BigDecimal">
        SELECT COALESCE
                   ( SUM( dd.dividend_amount ), 0 )
        FROM
            et_dividend_detail dd
                LEFT JOIN et_order o ON o.order_no = dd.order_no
        <where>
            <if test="areaId != null"> and o.area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND o.status = 4 and o.type = 1 and o.paid = 1 and dd.partner_id is not null
        </where>
    </select>

    <select id="getPlatformServiceFee" resultType="java.math.BigDecimal">
        SELECT COALESCE
        ( SUM( dd.dividend_amount ), 0 )
        FROM
        et_dividend_detail dd
        LEFT JOIN et_order o ON o.order_no = dd.order_no
        <where>
            <if test="areaId != null"> and o.area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND o.status = 4 and o.type = 1 and o.paid = 1 and dd.partner_id is null
        </where>
    </select>

    <select id="getIncome" resultType="java.math.BigDecimal">
        select COALESCE(SUM(pay_fee), 0) from et_order
        <where>
            <if test="sn != null  and sn != ''"> and sn = #{sn}</if>
            <if test="areaId != null"> and area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
                AND date_format(create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND status = 4 and type = 1
        </where>
   </select>

    <!--todo 待修改-->
    <select id="getRefundFee" resultType="java.math.BigDecimal">
        select  COALESCE(SUM(amount), 0)  from et_refund ref
        left join et_order o on o.order_no = ref.order_no
        <where>
            <if test="sn != null  and sn != ''"> and o.sn = #{sn}</if>
            <if test="areaId != null"> and o.area_id = #{areaId}</if>
            <if test="startDateStr != null  and startDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{startDateStr},'%y%m%d')
            </if>
            <if test="endDateStr != null  and endDateStr != ''">
                AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{endDateStr},'%y%m%d')
            </if>
            AND ref.type = 1 AND ref.refund_result = 'SUCCESS'
        </where>
    </select>
    <select id="selectLatestOrder" resultMap="EtOrderResult">
        SELECT *
        FROM et_order
        <where>
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="sn != null"> and sn = #{sn}</if>
            and type = 1
        </where>
        ORDER BY create_time DESC
            LIMIT 1
    </select>

    <select id="selectLatestOrderList" resultMap="EtOrderResult">
        SELECT o.*,u.phonenumber,u.real_name realName
        FROM et_order o
        left join et_user u on u.user_id = o.user_id
        <where>
            <if test="userId != null"> and o.user_id = #{userId}</if>
            <if test="sn != null"> and o.sn = #{sn}</if>
            and o.type = 1
        </where>
        ORDER BY o.create_time DESC
        LIMIT 1
    </select>
    <select id="selectAppointmentUnfinished" resultType="com.ruoyi.system.domain.EtOrder">
        select * from et_order where appointment_start_time is not null and appointment_start_time != '' AND appointment_end_time IS NULL
    </select>

    <select id="selectUnrefundableDepositOrder" resultMap="EtOrderResult">
        select * from   et_order o
                            left JOIN et_refund r on r.order_no = o.order_no
        where o.status ='4' and o.paid = '1' and o.type = 2 and r.refund_result  IS NULL
    </select>

    <select id="selectUserListFinishOrder" resultMap="EtOrderResult">
        select * from   et_order o
        where o.status ='4' and o.paid = '1' and o.type = 1 and o.is_test = '0'
        GROUP BY o.user_id
    </select>
    <select id="selectNeedDividendOrder" resultType="com.ruoyi.system.domain.EtOrder">
        SELECT
            *
        FROM
            et_order o
        WHERE
            o.STATUS = '4'
          AND o.paid = '1'
          AND o.type = 1
          AND o.pay_type != 'sys'
	      AND o.total_fee != 0
          AND o.is_test = '0'
    </select>

    <insert id="insertEtOrder" parameterType="EtOrder" useGeneratedKeys="true" keyProperty="orderId">
        insert into et_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="orderNo != null and orderNo != ''">order_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="ruleId != null">rule_id,</if>
            <if test="deviceMac != null">device_mac,</if>
            <if test="sn != null">sn,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="paid != null">paid,</if>
            <if test="payType != null and payType != ''">pay_type,</if>
            <if test="type != null">type,</if>
            <if test="totalFee != null">total_fee,</if>
            <if test="payFee != null">pay_fee,</if>
            <if test="dispatchFee != null">dispatch_fee,</if>
            <if test="manageFee != null">manage_fee,</if>
            <if test="ridingFee != null">riding_fee,</if>
            <if test="appointmentFee != null">appointment_fee,</if>
            <if test="mark != null">mark,</if>
            <if test="duration != null">duration,</if>
            <if test="distance != null">distance,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="appointmentStartTime != null">appointment_start_time,</if>
            <if test="appointmentEndTime != null">appointment_end_time,</if>
            <if test="appointmentTimeout != null">appointment_timeout,</if>
            <if test="unlockTime != null">unlock_time,</if>
            <if test="returnTime != null">return_time,</if>
            <if test="ruleEndTime != null">rule_end_time,</if>
            <if test="returnType != null">return_type,</if>
            <if test="tripRoute != null">trip_route,</if>
            <if test="tripRouteStr != null">trip_route_str,</if>
            <if test="cycle != null">cycle,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="orderNo != null and orderNo != ''">#{orderNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="ruleId != null">#{ruleId},</if>
            <if test="deviceMac != null">#{deviceMac},</if>
            <if test="sn != null">#{sn},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="paid != null">#{paid},</if>
            <if test="payType != null and payType != ''">#{payType},</if>
            <if test="type != null">#{type},</if>
            <if test="totalFee != null">#{totalFee},</if>
            <if test="payFee != null">#{payFee},</if>
            <if test="dispatchFee != null">#{dispatchFee},</if>
            <if test="manageFee != null">#{manageFee},</if>
            <if test="ridingFee != null">#{ridingFee},</if>
            <if test="appointmentFee != null">#{appointmentFee},</if>
            <if test="mark != null">#{mark},</if>
            <if test="duration != null">#{duration},</if>
            <if test="distance != null">#{distance},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="appointmentStartTime != null">#{appointmentStartTime},</if>
            <if test="appointmentEndTime != null">#{appointmentEndTime},</if>
            <if test="appointmentTimeout != null">#{appointmentTimeout},</if>
            <if test="unlockTime != null">#{unlockTime},</if>
            <if test="returnTime != null">#{returnTime},</if>
            <if test="ruleEndTime != null">#{ruleEndTime},</if>
            <if test="returnType != null">#{returnType},</if>
            <if test="tripRoute != null">GeomFromText(#{tripRoute}),</if>
            <if test="tripRouteStr != null">#{tripRouteStr},</if>
            <if test="cycle != null">#{cycle},</if>
         </trim>
    </insert>

    <update id="updateEtOrder" parameterType="EtOrder">
        update et_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="outTradeNo != null and outTradeNo != ''">out_trade_no = #{outTradeNo},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="deviceMac != null">device_mac = #{deviceMac},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="paid != null">paid = #{paid},</if>
            <if test="payType != null and payType != ''">pay_type = #{payType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="totalFee != null">total_fee = #{totalFee},</if>
            <if test="payFee != null">pay_fee = #{payFee},</if>
            <if test="dispatchFee != null">dispatch_fee = #{dispatchFee},</if>
            <if test="manageFee != null">manage_fee = #{manageFee},</if>
            <if test="ridingFee != null">riding_fee = #{ridingFee},</if>
            <if test="appointmentFee != null">appointment_fee = #{appointmentFee},</if>
            <if test="mark != null">mark = #{mark},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="distance != null">distance = #{distance},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="appointmentStartTime != null">appointment_start_time = #{appointmentStartTime},</if>
            <if test="appointmentEndTime != null">appointment_end_time = #{appointmentEndTime},</if>
            <if test="appointmentTimeout != null">appointment_timeout = #{appointmentTimeout},</if>
            <if test="unlockTime != null">unlock_time = #{unlockTime},</if>
            <if test="returnTime != null">return_time = #{returnTime},</if>
            <if test="returnType != null">return_type = #{returnType},</if>
            <if test="tripRoute != null">trip_route = GeomFromText(#{tripRoute}),</if>
            <if test="tripRouteStr != null">trip_route_str = #{tripRouteStr},</if>
            <if test="cycle != null">cycle = #{cycle},</if>
            <if test="depositDeduction != null">deposit_deduction = #{depositDeduction},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="deductionAmount != null">deduction_amount = #{deductionAmount},</if>
            <if test="audioFiles != null">audio_files = #{audioFiles},</if>
            <if test="usedSn != null">used_sn = #{usedSn},</if>
            <if test="changeReason != null">change_reason = #{changeReason},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <update id="updateEtOrderByOrderNo" parameterType="EtOrder">
        update et_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="outTradeNo != null and outTradeNo != ''">out_trade_no = #{outTradeNo},</if>
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="deviceMac != null">device_mac = #{deviceMac},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="paid != null">paid = #{paid},</if>
            <if test="payType != null and payType != ''">pay_type = #{payType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="totalFee != null">total_fee = #{totalFee},</if>
            <if test="payFee != null">pay_fee = #{payFee},</if>
            <if test="dispatchFee != null">dispatch_fee = #{dispatchFee},</if>
            <if test="manageFee != null">manage_fee = #{manageFee},</if>
            <if test="ridingFee != null">riding_fee = #{ridingFee},</if>
            <if test="appointmentFee != null">appointment_fee = #{appointmentFee},</if>
            <if test="mark != null">mark = #{mark},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="distance != null">distance = #{distance},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="appointmentStartTime != null">appointment_start_time = #{appointmentStartTime},</if>
            <if test="appointmentEndTime != null">appointment_end_time = #{appointmentEndTime},</if>
            <if test="appointmentTimeout != null">appointment_timeout = #{appointmentTimeout},</if>
            <if test="unlockTime != null">unlock_time = #{unlockTime},</if>
            <if test="returnTime != null">return_time = #{returnTime},</if>
            <if test="returnType != null">return_type = #{returnType},</if>
            <if test="tripRoute != null">trip_route = GeomFromText(#{tripRoute}),</if>
            <if test="tripRouteStr != null">trip_route_str = #{tripRouteStr},</if>
            <if test="cycle != null">cycle = #{cycle},</if>
            <if test="depositDeduction != null">deposit_deduction = #{depositDeduction},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="deductionAmount != null">deduction_amount = #{deductionAmount},</if>
            <if test="audioFiles != null">audio_files = #{audioFiles},</if>
            <if test="usedSn != null">used_sn = #{usedSn},</if>
            <if test="changeReason != null">change_reason = #{changeReason},</if>
        </trim>
        where order_no = #{orderNo}
    </update>

    <delete id="deleteEtOrderByOrderId" parameterType="Long">
        delete from et_order where order_id = #{orderId}
    </delete>

    <delete id="deleteEtOrderByOrderIds" parameterType="String">
        delete from et_order where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>
    <delete id="deleteEtOrderByOrderNo" parameterType="String">
        delete from et_order where order_no = #{orderNo}
    </delete>


</mapper>