<?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.refund.RefundDetailMapper">

    <update id="recoveryStatusByRefundId">
        update trade_detail td,trade_refund_detail rd
        set td.voucher_status = rd.voucher_status
        where td.id = rd.trade_detail_id and rd.refund_id=#{refundId}
    </update>

    <update id="updateStatusByRefundId">
        update trade_detail td,trade_refund_detail rd
        set td.voucher_status = #{refundStatus}
        where td.id = rd.trade_detail_id and rd.refund_id=#{refundId}
    </update>

    <select id="selectRefundOrderProduct" resultType="java.util.Map">
        select td.product_id,td.route_plan_id,td.route_plan_group_type,td.product_name,td.use_date,td.quantity_of_one,count(*) refundCount
        FROM  trade_refund_detail trd
        inner join trade_detail td on trd.trade_detail_id = td.id
        where trd.refund_id=#{refundId}
        group by td.product_id
    </select>

    <select id="selectRefundOrderProductV2" resultType="java.util.Map">
        select td.product_id,td.route_plan_id,td.route_plan_group_type,td.product_name,td.use_date,td.quantity_of_one,
        count(if(FIND_IN_SET(td.spec_type,
        <foreach collection="noContainSpecList" item="item" separator=","  open="" close="">
            #{item}
        </foreach> )<![CDATA[<= ]]> 0,1,null)) refundCount
        FROM  trade_refund_detail trd
                  inner join trade_detail td on trd.trade_detail_id = td.id
        where trd.refund_id=#{refundId}
        group by td.product_id
    </select>

    <select id="selectOrderRefundAmount" resultType="java.util.Map">
        select sum(refund_amount) totalRefundAmount
        FROM  trade_refund tr
        where tr.order_id=#{orderId} and tr.refund_status=6
        group by tr.order_id
    </select>

    <select id="selectOrderRefundFee" resultType="java.util.Map">
        select sum(fee) totalFee
        FROM  trade_refund tr
        where tr.order_id=#{orderId} and tr.refund_status=6
        group by tr.order_id
    </select>

    <select id="selectProductBaseByRefund" resultType="com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBaseBO">
        select td.product_id,tdb.product_base_id,tdb.product_resource_id,tdb.product_base_type,tdb.use_date,count(*) quantity
        from trade_refund_detail_base  rb
        INNER JOIN trade_detail_base tdb on rb.trade_detail_base_id = tdb.id
        INNER JOIN trade_detail td on td.id = tdb.detail_id
        where rb.refund_id = #{refundId}
        GROUP BY td.product_id,tdb.product_base_id
    </select>

    <select id="selectProjectCountByRefund" resultType="com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBaseBO">
        SELECT
            tdb.project_code,
            count( DISTINCT tdb.detail_id ) quantity,
            ord.order_no,
            ord.order_status,
            dt.voucher_code,
            dis.dispatch_id,
            dt.product_name,
            tdb.detail_id,
            dt.use_date duseDate
        FROM
            trade_refund_detail_base rb
                INNER JOIN trade_detail_base tdb ON rb.trade_detail_base_id = tdb.id
                INNER JOIN trade_detail dt ON dt.id = tdb.detail_id
                INNER JOIN trade_order ord ON ord.id = dt.order_id
                inner join (select t1.* from trade_order_dispatch t1 inner join dispatch_operate_plan_boat t2 on t1.dispatch_id = t2.id
                            where order_id=#{orderId} ) dis on  dis.order_id = ord.id
        WHERE
            rb.refund_id = #{refundId}
          and tdb.project_code='boat'
        GROUP BY
            tdb.project_code,
            dis.dispatch_id,
            ord.order_no

        UNION ALL
        SELECT
            tdb.project_code,
            count( DISTINCT tdb.detail_id ) quantity,
            ord.order_no,
            ord.order_status,
            dt.voucher_code,
            dis.dispatch_id,
            dt.product_name,
            tdb.detail_id,
            dt.use_date duseDate
        FROM
            trade_refund_detail_base rb
                INNER JOIN trade_detail_base tdb ON rb.trade_detail_base_id = tdb.id
                INNER JOIN trade_detail dt ON dt.id = tdb.detail_id
                INNER JOIN trade_order ord ON ord.id = dt.order_id
                inner join (select t1.* from trade_order_dispatch t1 inner join dispatch_car_dispatch t2 on t1.dispatch_id = t2.id
                            where order_id=#{orderId} ) dis on  (dis.order_id = ord.id and (dis.detail_base_id is null or dis.detail_id is not null and dis.detail_id = dt.id))
        WHERE
            rb.refund_id = #{refundId}
          and tdb.project_code!='boat'
        GROUP BY
            tdb.project_code,
            dis.dispatch_id,
            ord.order_no


    </select>

    <select id="selectOrderApplyRefundAmount" resultType="java.util.Map">
        select sum(refund_amount) totalRefundAmount
        FROM  trade_refund tr
        where tr.order_id=#{orderId} and tr.refund_status not in(2,99)
        group by tr.order_id
    </select>

    <select id="selectTradeDetailStatusByRefund" resultType="java.util.Map">
        select tdb.detail_id ,count(tdb.detail_id) totalCount,sum(if(tdb.use_status=5,1,0)) refundCount
        from trade_refund_detail trd
        inner join trade_detail_base tdb on trd.trade_detail_id = tdb.detail_id
        where trd.refund_id = #{refundId}
        GROUP BY tdb.detail_id
    </select>

    <select id="selectOrderOriginRefundAmount" resultType="java.util.Map">
        select sum(origin_amount) totalOriginRefundAmount
        FROM  trade_refund tr
        where tr.order_id=#{orderId} and tr.refund_status=6
        group by tr.order_id
    </select>

    <select id="selectRefundAmountByOriginOrderNo" resultType="java.util.Map">
        select sum(refund_amount) totalRefundAmount
        FROM  trade_order tod
        inner join trade_refund tr on tod.id = tr.order_id
        where tod.external_origin_order_no =#{originOrderNo}  and tr.refund_status=6
    </select>

    <select id="selectRefundAmountFromOrderDetail" resultType="java.util.Map">
        select sum(tr.actual_price) totalRefundAmount
        FROM  trade_detail tr
        where tr.order_id=#{orderId} and tr.refund_status in (1,2)
    </select>
</mapper>