<?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.orderjzdispatch.OrderJzDispatchMapper">

    <select id="selectPage" resultType="com.yc.ship.module.trade.dal.dataobject.orderjzdispatch.OrderJzDispatchDO">
        SELECT id, order_id, dispatch_time, voyage_id, dispatch_no, bus_number, driver_name, driver_phone,
               receiver_name, receiver_phone, source_type, passenger_count, remark, tenant_id, creator,
               create_time, updater, update_time, deleted
        FROM trade_order_jz_dispatch
        <where>
            deleted = 0
            <if test="pageReqVO.orderId != null">
                AND order_id = #{pageReqVO.orderId}
            </if>
            <if test="pageReqVO.voyageId != null">
                AND voyage_id = #{pageReqVO.voyageId}
            </if>
            <if test="pageReqVO.dispatchNo != null and pageReqVO.dispatchNo != ''">
                AND dispatch_no LIKE CONCAT('%', #{pageReqVO.dispatchNo}, '%')
            </if>
            <if test="pageReqVO.busNumber != null and pageReqVO.busNumber != ''">
                AND bus_number LIKE CONCAT('%', #{pageReqVO.busNumber}, '%')
            </if>
            <if test="pageReqVO.sourceType != null">
                AND source_type = #{pageReqVO.sourceType}
            </if>
        </where>
        ORDER BY id DESC
        LIMIT #{pageReqVO.pageSize} OFFSET #{pageReqVO.offset}
    </select>

    <select id="selectCount" resultType="java.lang.Long">
        SELECT COUNT(1)
        FROM trade_order_jz_dispatch
        <where>
            deleted = 0
            <if test="pageReqVO.orderId != null">
                AND order_id = #{pageReqVO.orderId}
            </if>
            <if test="pageReqVO.voyageId != null">
                AND voyage_id = #{pageReqVO.voyageId}
            </if>
            <if test="pageReqVO.dispatchNo != null and pageReqVO.dispatchNo != ''">
                AND dispatch_no LIKE CONCAT('%', #{pageReqVO.dispatchNo}, '%')
            </if>
            <if test="pageReqVO.busNumber != null and pageReqVO.busNumber != ''">
                AND bus_number LIKE CONCAT('%', #{pageReqVO.busNumber}, '%')
            </if>
            <if test="pageReqVO.sourceType != null">
                AND source_type = #{pageReqVO.sourceType}
            </if>
        </where>
    </select>

    <!-- 分页查询分车记录（按分车号分组） -->
    <select id="selectDispatchRecordPage"
            resultType="com.yc.ship.module.trade.controller.admin.orderjzdetail.vo.OrderJzDispatchRecordRespVO">
        SELECT
            MAX(dispatch_no) AS dispatchNo,
            MAX(group_no) AS groupNo,
            MAX(bus_number) AS busNumber,
            SUM(passenger_count) AS passengerCount,
            MAX(driver_name) AS driverName,
            MAX(driver_phone) AS driverPhone,
            MAX(receiver_name) AS receiverName,
            MAX(receiver_phone) AS receiverPhone,
            MAX(remark) AS remark
        FROM trade_order_jz_dispatch
        <where>
            deleted = 0
            AND dispatch_no IS NOT NULL
            AND dispatch_no != ''
            <if test="reqVO.voyageId != null">
                AND voyage_id = #{reqVO.voyageId}
            </if>
            <if test="reqVO.dispatchNo != null and reqVO.dispatchNo != ''">
                AND dispatch_no LIKE CONCAT('%', #{reqVO.dispatchNo}, '%')
            </if>
        </where>
        GROUP BY dispatch_no,group_no
        ORDER BY MAX(id) DESC
        LIMIT #{limit} OFFSET #{offset}
    </select>

    <!-- 查询分车记录总数 -->
    <select id="selectDispatchRecordCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT dispatch_no)
        FROM trade_order_jz_dispatch
        <where>
            deleted = 0
            AND dispatch_no IS NOT NULL
            AND dispatch_no != ''
            <if test="reqVO.voyageId != null">
                AND voyage_id = #{reqVO.voyageId}
            </if>
            <if test="reqVO.dispatchNo != null and reqVO.dispatchNo != ''">
                AND dispatch_no LIKE CONCAT('%', #{reqVO.dispatchNo}, '%')
            </if>
        </where>
    </select>

</mapper>
