<?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.yc.ship.module.trade.dal.mysql.bill.BillMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="getOrderList"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*,6 paymentType
        FROM trade_order td
        INNER JOIN trade_order_pay top on top.order_id = td.id
        where
        td.order_status not in (-1,-2,0,1,6)  and (td.is_change_order is null or td.is_change_order =1)
        and not exists(select 1 from trade_detail t where t.order_id = td.id and t.voucher_status not in (3,5,6,7,8,9))
        and top.pay_status =1
        AND (top.payment_type = 6 and top.pay_amount>top.refund_amount or td.is_change_order = 1
        and exists(select 1 from trade_order_pay top
        inner join trade_order_bind tob on tob.order_id  = top.order_id and tob.type =2
        where tob.bind_id = td.id and top.payment_type = 6 and top.pay_amount>top.refund_amount and top.pay_status =1))
        and td.is_bill = 0 and td.source_id = #{vo.agencyId}

          <if test="vo.orderStratTime != null">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.orderStratTime}
        </if>
        <if test="vo.orderEndTime != null">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.orderEndTime}
        </if>
        GROUP BY td.id
        ORDER BY td.create_time DESC
    </select>

    <select id="getOrderPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*,6 paymentType,tou.contact_name contactName,tou.mobile mobile,tdl.product_name productName,tdl.product_id productId
        FROM trade_order td
        INNER JOIN trade_order_user tou on tou.order_id = td.id
        INNER JOIN trade_detail tdl on tdl.order_id = td.id
        INNER JOIN trade_order_pay top on top.order_id = td.id
        where 1= 1
        and td.order_status not in (-1,-2,0,1,6)
        and not exists(select 1 from trade_detail t where t.order_id = td.id and t.voucher_status not in (3,5,6,7,8,9))
        and top.pay_status =1
        AND top.payment_type = 6 and top.pay_amount>top.refund_amount
        <if test="vo.type != null">
            AND td.is_bill =  #{vo.type}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName},'%')
        </if>
        <if test="vo.distributorId != null">
            AND td.source_id = #{vo.distributorId}
        </if>
        <if test="vo.storeId != null">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.travelDateStart != null">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        GROUP BY td.id
        ORDER BY td.create_time DESC
    </select>

    <select id="getOrderById"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*,t.*
        FROM trade_order td
                 INNER JOIN
             (
                 select top.order_id,top.payment_type paymentType,sum(top.pay_amount) payTypeAmount,sum(top.refund_amount) payTypeRefundAmount,
                        (sum(top.pay_amount) - sum(top.refund_amount)) actualPayAmount
                 from (
                    select top.payment_type, top.pay_amount,top.refund_amount,top.order_id
                    from  trade_order_pay top
                    where top.order_id =#{id}  and top.payment_type = 6 and top.pay_status =1
                    union all
                    select top.payment_type, top.pay_amount,top.refund_amount,tob.bind_id order_id
                    from trade_order_pay top
                             INNER JOIN trade_order_bind tob ON tob.order_id = top.order_id
                        AND tob.type = 2
                    where  tob.bind_id = #{id}  and top.payment_type = 6 and top.pay_status =1
                ) top
             ) t on t.order_id = td.id
        WHERE
            td.id = #{id}
    </select>

    <select id="getBillAgencyById"
            resultType="com.yc.ship.module.trade.dal.dataobject.bill.BillDO">
        SELECT ob.*
        FROM ota_bill ob
        where  ob.agency_id = #{agencyId} and ob.bill_status != 1  and ob.prohibit_type != 0
    </select>

    <update id="updateOrderIsBill">
        UPDATE trade_order SET is_bill = #{isBill} WHERE id = #{orderId}
    </update>

    <select id="getBillPage"
            resultType="com.yc.ship.module.trade.controller.admin.bill.vo.BillRespVO">
        SELECT ob.*
        FROM ota_bill ob
        where 1=1
        <if test="vo.billStatus != null">
            AND ob.bill_status =  #{vo.billStatus}
        </if>
        <if test="vo.operatorName != null and vo.operatorName != ''">
            AND ob.operator_name like concat('%',#{vo.operatorName},'%')
        </if>
        <if test="vo.orderStratTime != null">
            AND ob.create_time <![CDATA[ >= ]]> #{vo.orderStratTime}
        </if>
        <if test="vo.orderEndTime != null">
            AND ob.create_time <![CDATA[ <= ]]> #{vo.orderEndTime}
        </if>
        <if test="vo.agencyId != null">
            AND ob.agency_id = #{vo.agencyId}
        </if>
        <if test="vo.billId != null">
            AND ob.id = #{vo.billId}
        </if>
        order by ob.create_time desc
    </select>

    <select id="getPage"
            resultType="com.yc.ship.module.trade.controller.admin.bill.vo.BillRespVO">
        SELECT ob.*
        FROM ota_bill ob
        where 1 = 1
        <if test="ids!=null and ids.size()>0">
            and  ob.agency_id in
            <foreach collection="ids" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.billStatus != null">
            AND ob.bill_status =  #{vo.billStatus}
        </if>
        <if test="vo.operatorName != null and vo.operatorName != ''">
            AND ob.operator_name like concat('%',#{vo.operatorName},'%')
        </if>
        <if test="vo.orderStratTime != null">
            AND ob.create_time <![CDATA[ >= ]]> #{vo.orderStratTime}
        </if>
        <if test="vo.orderEndTime != null">
            AND ob.create_time <![CDATA[ <= ]]> #{vo.orderEndTime}
        </if>
        <if test="vo.agencyId != null">
            AND ob.agency_id = #{vo.agencyId}
        </if>
        <if test="vo.billId != null">
            AND ob.id = #{vo.billId}
        </if>
        order by ob.create_time desc
    </select>

    <select id="getBillStorePage"
            resultType="com.yc.ship.module.trade.controller.admin.bill.vo.BillRespVO">
        SELECT obs.*
        FROM ota_bill_store obs
        where obs.ota_tenant_id = #{vo.otaTenantId}
        <if test="vo.billStatus != null">
            AND obs.bill_status =  #{vo.billStatus}
        </if>
        <if test="vo.operatorName != null and vo.operatorName != ''">
            AND obs.operator_name like concat('%',#{vo.operatorName},'%')
        </if>
        <if test="vo.orderStratTime != null">
            AND obs.create_time <![CDATA[ >= ]]> #{vo.orderStratTime}
        </if>
        <if test="vo.orderEndTime != null">
            AND obs.create_time <![CDATA[ <= ]]> #{vo.orderEndTime}
        </if>
        <if test="vo.billId != null">
            AND obs.bill_id = #{vo.billId}
        </if>
        order by obs.create_time desc
    </select>

    <select id="getOrderIdDetail" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">
        SELECT td.*
        FROM trade_detail td
        where 1= 1
        <if test="orderId != null and orderId != ''">
            AND td.order_id = #{orderId}
        </if>
    </select>

    <select id="getBillOver"
            resultType="com.yc.ship.module.trade.dal.dataobject.bill.BillDO">
        SELECT ob.*
        FROM ota_bill ob
        where  ob.bill_status = 0 and ob.prohibit_type = 2
        and NOW() > ob.pay_deadline
    </select>

    <select id="getBillStoreOver"
            resultType="com.yc.ship.module.trade.dal.dataobject.bill.BillStoreDO">
        SELECT obs.*
            FROM ota_bill_store obs
        where  obs.bill_status = 0
        and NOW() > obs.pay_deadline
    </select>

    <select id="getPayOrderData"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT top.order_id id,top.payment_type paymentType,sum(top.pay_amount) payTypeAmount,sum(top.refund_amount) payTypeRefundAmount,
               (sum(top.pay_amount) - sum(top.refund_amount)) actualPayAmount
        FROM trade_order_pay top
        WHERE
            top.order_id = #{id} and top.payment_type = 6 and top.pay_status =1
        GROUP BY top.order_id

    </select>

    <select id="getPayOrderWithChangeData"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT #{id} id,top.payment_type paymentType,sum(top.pay_amount) payTypeAmount,sum(top.refund_amount) payTypeRefundAmount,
               (sum(top.pay_amount) - sum(top.refund_amount)) actualPayAmount
        from (
                 select top.*
                 FROM trade_order_pay top
                 WHERE
                     top.order_id = #{id} and top.payment_type = 6 and top.pay_status =1
                 union all
                 select top.*
                 FROM trade_order_pay top
                   inner join trade_order_bind tob on tob.order_id  = top.order_id and tob.type =2
                 where tob.bind_id = #{id}  and top.payment_type = 6 and top.pay_status =1

             ) top
    </select>

    <update id="updateOrderByBillId">
        update trade_order tod ,ota_bill_order obo
        set tod.is_bill =0
        where tod.id = obo.order_id and obo.bill_id= #{id}
    </update>
    <delete id="deleteStoreBillByBillId">
        delete from ota_bill_store where bill_id =  #{id}
    </delete>
    <delete id="deleteBillByBillId">
        delete from ota_bill where id =  #{id}
    </delete>
    <delete id="deleteBillOrderByBillId">
        delete from ota_bill_order where bill_id = #{id}
    </delete>
</mapper>