<?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.report.IncomeOrderLedgerMapper">

    <!-- 分页查询订单收入台账 -->
    <select id="selectIncomeOrderLedgerPage"
            resultType="com.yc.ship.module.trade.controller.admin.report.vo.IncomeOrderLedgerRespVO">
        SELECT
            o.id,
            o.order_no AS orderNo,
            o.order_status AS orderStatus,
            o.pay_status AS paymentStatus,
            DATE_FORMAT(v.start_time, '%Y/%m/%d') AS sailTime,
            o.source_name AS travelAgency,
            v.name AS voyage,
            r.name AS routeName,
            rm.room_model_name AS roomTypeName,
            IFNULL(tot.use_room_total_num, 0) AS roomCount,
            IFNULL(tot.adult_total_num, 0) AS adultCount,
            IFNULL(tot.child_total_num, 0) AS childCount,
            IFNULL(tot.baby_total_num, 0) AS infantCount,
            IFNULL(tot.with_total_num, 0) AS companionCount,
            IFNULL(tot.free_num, 0) AS freeCount,
            (IFNULL(tot.adult_total_num, 0) + IFNULL(tot.child_total_num, 0) +
             IFNULL(tot.baby_total_num, 0) + IFNULL(tot.with_total_num, 0) + IFNULL(tot.free_num, 0)) AS totalCount,
            o.amount AS marketingPrice,
            o.free_amount AS discountAmount,
            COALESCE(refund_fee_sum.refundFee, 0) AS refundFee,
            (o.amount - IFNULL(o.free_amount, 0) - COALESCE(refund_fee_sum.refundFee, 0)) AS actualSettlementPrice
        FROM trade_order o
        INNER JOIN product_voyage v ON o.voyage_id = v.id AND v.deleted = 0
        INNER JOIN resource_route r ON v.route_id = r.id AND r.deleted = 0
        LEFT JOIN trade_order_total tot ON o.id = tot.old_order_id AND tot.deleted = 0
        LEFT JOIN (
            SELECT rm.order_id,
                   GROUP_CONCAT(DISTINCT rm.room_model_name SEPARATOR ',') AS room_model_name
            FROM trade_order_room_model rm
            WHERE rm.deleted = 0
            GROUP BY rm.order_id
        ) rm ON o.id = rm.order_id
        LEFT JOIN (
            SELECT order_id, SUM(fee) AS refundFee
            FROM trade_refund
            WHERE deleted = 0 AND refund_status IN (3)
            GROUP BY order_id
        ) refund_fee_sum ON o.id = refund_fee_sum.order_id
        WHERE o.deleted = 0
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND o.order_no LIKE CONCAT('%', #{vo.orderNo}, '%')
        </if>
        <if test="vo.orderStatus != null">
            AND o.order_status = #{vo.orderStatus}
        </if>
        <if test="vo.paymentStatus != null">
            AND o.pay_status = #{vo.paymentStatus}
        </if>
        <if test="vo.sailTime != null">
            AND DATE(v.start_time) = #{vo.sailTime}
        </if>
        <if test="vo.travelAgency != null and vo.travelAgency != ''">
            AND o.source_name LIKE CONCAT('%', #{vo.travelAgency}, '%')
        </if>
        <if test="vo.shipId != null">
            AND v.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeName != null and vo.routeName != ''">
            AND r.name LIKE CONCAT('%', #{vo.routeName}, '%')
        </if>
        <if test="vo.roomTypeName != null and vo.roomTypeName != ''">
            AND EXISTS (
                SELECT 1 FROM trade_order_room_model orm2
                WHERE orm2.order_id = o.id AND orm2.deleted = 0
                AND orm2.room_model_name LIKE CONCAT('%', #{vo.roomTypeName}, '%')
            )
        </if>
        ORDER BY o.create_time DESC
    </select>

    <!-- 导出查询（与分页查询条件一致，不分页） -->
    <select id="selectIncomeOrderLedgerExportList"
            resultType="com.yc.ship.module.trade.controller.admin.report.vo.IncomeOrderLedgerRespVO">
        SELECT
            o.id,
            o.order_no AS orderNo,
            o.order_status AS orderStatus,
            o.pay_status AS paymentStatus,
            DATE_FORMAT(v.start_time, '%Y/%m/%d') AS sailTime,
            o.source_name AS travelAgency,
            v.name AS voyage,
            r.name AS routeName,
            rm.room_model_name AS roomTypeName,
            IFNULL(tot.use_room_total_num, 0) AS roomCount,
            IFNULL(tot.adult_total_num, 0) AS adultCount,
            IFNULL(tot.child_total_num, 0) AS childCount,
            IFNULL(tot.baby_total_num, 0) AS infantCount,
            IFNULL(tot.with_total_num, 0) AS companionCount,
            IFNULL(tot.free_num, 0) AS freeCount,
            (IFNULL(tot.adult_total_num, 0) + IFNULL(tot.child_total_num, 0) +
             IFNULL(tot.baby_total_num, 0) + IFNULL(tot.with_total_num, 0) + IFNULL(tot.free_num, 0)) AS totalCount,
            o.amount AS marketingPrice,
            o.free_amount AS discountAmount,
            COALESCE(refund_fee_sum.refundFee, 0) AS refundFee,
            (o.amount - IFNULL(o.free_amount, 0) - COALESCE(refund_fee_sum.refundFee, 0)) AS actualSettlementPrice
        FROM trade_order o
        INNER JOIN product_voyage v ON o.voyage_id = v.id AND v.deleted = 0
        INNER JOIN resource_route r ON v.route_id = r.id AND r.deleted = 0
        LEFT JOIN trade_order_total tot ON o.id = tot.old_order_id AND tot.deleted = 0
        LEFT JOIN (
            SELECT rm.order_id,
                   GROUP_CONCAT(DISTINCT rm.room_model_name SEPARATOR ',') AS room_model_name
            FROM trade_order_room_model rm
            WHERE rm.deleted = 0
            GROUP BY rm.order_id
        ) rm ON o.id = rm.order_id
        LEFT JOIN (
            SELECT order_id, SUM(fee) AS refundFee
            FROM trade_refund
            WHERE deleted = 0 AND refund_status IN (3)
            GROUP BY order_id
        ) refund_fee_sum ON o.id = refund_fee_sum.order_id
        WHERE o.deleted = 0
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND o.order_no LIKE CONCAT('%', #{vo.orderNo}, '%')
        </if>
        <if test="vo.orderStatus != null">
            AND o.order_status = #{vo.orderStatus}
        </if>
        <if test="vo.paymentStatus != null">
            AND o.pay_status = #{vo.paymentStatus}
        </if>
        <if test="vo.sailTime != null">
            AND DATE(v.start_time) = #{vo.sailTime}
        </if>
        <if test="vo.travelAgency != null and vo.travelAgency != ''">
            AND o.source_name LIKE CONCAT('%', #{vo.travelAgency}, '%')
        </if>
        <if test="vo.shipId != null">
            AND v.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeName != null and vo.routeName != ''">
            AND r.name LIKE CONCAT('%', #{vo.routeName}, '%')
        </if>
        <if test="vo.roomTypeName != null and vo.roomTypeName != ''">
            AND EXISTS (
                SELECT 1 FROM trade_order_room_model orm2
                WHERE orm2.order_id = o.id AND orm2.deleted = 0
                AND orm2.room_model_name LIKE CONCAT('%', #{vo.roomTypeName}, '%')
            )
        </if>
        ORDER BY o.create_time DESC
    </select>

</mapper>
