<?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.zbkj.service.dao.StoreOrderDao">

    <update id="refundRefuseUpdateById" parameterType="com.zbkj.common.model.order.StoreOrder">
        update eb_store_order
        <set>
            refund_status = 0,
            refund_reason = #{refundReason},
            refund_reason_wap_img = null,
            refund_reason_wap_explain = null,
            refund_reason_wap = null,
            refund_reason_time = null,
            refund_price = 0
        </set>
        where id = #{ id, jdbcType=INTEGER}
    </update>

    <select id="canApplyOrderList" resultType="com.zbkj.common.response.CanApplyOrderResponse">
        SELECT o.id, o.order_id orderId, o.create_time createTime, o.pay_price payPrice
        FROM eb_store_order o
        INNER JOIN eb_store_order_status os on o.id = os.oid and os.change_type = 'user_take_delivery' and DATE_ADD(os.create_time, INTERVAL 2 DAY) > now()
        WHERE o.paid = 1
        AND o.`status` = 2
        AND o.refund_status = 0
        AND o.is_del = 0
        AND o.is_system_del = 0
        AND o.uid = #{id}
        ORDER BY o.id DESC
    </select>

    <select id="selectOrderReplyList" resultType="com.zbkj.common.response.OrderReplyListResponse">
        SELECT
            o.id, o.order_id orderNo, o.create_time createTime,
            oi.product_id productId,
            oi.product_name productName,
            oi.unique uni, oi.image,
            r.pics, r.comment,
            r.product_score productScore,
            r.service_score serviceScore
        FROM eb_store_order o
        INNER JOIN eb_store_order_info oi ON o.id = oi.order_id
        LEFT JOIN eb_store_product_reply r ON o.id = r.oid AND oi.product_id = r.product_id
        where o.`status` in (2, 3) and o.uid = #{uid}
        <if test="type == 0"> and r.id is null </if>
        <if test="type == 1"> and r.id is not null </if>
    </select>

    <select id="getTotalPrice" resultType="java.math.BigDecimal">
        select sum(pay_price) from eb_store_order where ${where} and refund_status = 0
    </select>
    <select id="getRefundPrice" resultType="java.math.BigDecimal">
        select sum(refund_price) from eb_store_order where ${where} and refund_status = 2
    </select>
    <select id="getRefundTotal" resultType="java.lang.Integer">
        select count(id) from eb_store_order where ${where} and refund_status = 2
    </select>
    <select id="getOrderVerificationDetail" parameterType="com.zbkj.common.request.PageParamRequest"
            resultType="com.zbkj.common.response.StoreStaffDetail">
        select sum(o.`pay_price`) as price, count(o.`id`) as count, DATE_FORMAT(o.`create_time`, '%Y-%m-%d') as time
        from `eb_store_order` o
        where o.`is_del` = 0 and o.`paid` = 1 and o.`refund_status` = 0
        <if test="null != startTime and startTime != ''">
            and o.create_time >= #{ startTime }
        </if>
        <if test="null != endTime and endTime != ''">
            and o.create_time &lt; #{ endTime }
        </if>
        GROUP by DATE_FORMAT(o.`create_time`, '%Y-%m-%d') order by o.`create_time` desc limit #{ page },#{ limit };
    </select>
    <select id="getOrderStatisticsPriceDetail" parameterType="com.zbkj.common.request.StoreDateRangeSqlPram"
            resultType="com.zbkj.common.response.StoreOrderStatisticsChartItemResponse">
        select sum(o.pay_price) as num,date_format(o.create_time, '%Y-%m-%d') as time
        from eb_store_order o
        where o.is_del >= 0 and o.paid >= 1 and o.refund_status >= 0
        and o.create_time >= #{ startTime }
        and o.create_time &lt; #{ endTime }
        group by date_format(o.create_time, '%Y-%m-%d')
        order by o.create_time desc;
    </select>
    <select id="getOrderStatisticsOrderCountDetail" parameterType="com.zbkj.common.request.StoreDateRangeSqlPram"
            resultType="com.zbkj.common.response.StoreOrderStatisticsChartItemResponse">
        select count(id) as num, date_format(o.create_time, '%Y-%m-%d') as time
        from eb_store_order o
        where o.is_del >= 0 and o.paid >= 1 and o.refund_status >= 0
        and o.create_time >= #{ startTime }
        and o.create_time &lt; #{ endTime }
        group by date_format(o.create_time, '%Y-%m-%d')
        order by o.create_time asc;
    </select>

    <select id="getBrokerageData" resultType="com.zbkj.common.response.OrderBrokerageData">
        select 0 as num, 0 as price
    </select>
</mapper>
