<?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.order.TradeOrderMapper">

    <resultMap id="MiddleWareDtoResultMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="voyageId" column="voyage_id"/>
        <result property="bindId" column="bindId"/>
        <result property="isRead" column="isRead"/>
        <result property="sourceName" column="source_name"/>
        <result property="externalOrderNo" column="external_order_no"/>
        <result property="routePlanId" column="route_plan_id"/>
        <result property="routePlanName" column="route_plan_name"/>
        <result property="visitorType" column="visitor_type"/>
        <result property="travelDate" column="travel_date"/>
        <result property="sellMethod" column="sell_method"/>
        <result property="sourceName" column="source_name"/>
        <result property="sellerId" column="seller_id"/>
        <result property="memberId" column="member_id"/>
        <result property="amount" column="amount"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="paymentStatus" column="pay_status"/>
        <result property="paymentType" column="payment_type"/>
        <result property="orderStatus" column="order_status"/>
        <result property="paymentDate" column="payment_date"/>
        <result property="areaManagerId" column="area_manager_id"/>
        <result property="storeName" column="store_name"/>
        <result property="contactName" column="contactName"/>
        <result property="shareName" column="share_name"/>
        <result property="isBind" column="is_bind"/>
        <result property="credentialNo" column="credentialNo"/>
        <result property="city" column="city"/>
        <result property="province" column="province"/>
        <result property="county" column="county"/>
        <result property="createTime" column="create_time"/>
        <result property="mobile" column="mobile"/>
        <result property="auditState" column="audit_state"/>
        <result property="storeId" column="store_id"/>
        <result property="sourceId" column="source_id"/>
        <result property="isMarketing" column="is_marketing"/>
        <result property="isMarketingUse" column="is_marketing_use"/>
        <result property="remark" column="remark"/>
        <result property="dispatchStatus" column="dispatch_status"/>
        <result property="isInvoice" column="is_invoice"/>
        <result property="isChangeOrder" column="is_change_order"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="confirmType" column="confirm_type"/>
        <result property="travelStatus" column="travel_status"/>
        <result property="finishStatus" column="finish_status"/>
        <result property="agencyGroupId" column="agency_group_id"/>
        <collection property="orderPayList" resultMap="orderPayList"/>
        <collection property="insuranceList" resultMap="insuranceResultMap"/>
        <collection property="contractList" resultMap="contractResultMap"/>
    </resultMap>
    <resultMap id="MiddleWareDtoResultMap2" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderPdaRespVO">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="bindId" column="bindId"/>
        <result property="isRead" column="isRead"/>
        <result property="sourceName" column="source_name"/>
        <result property="externalOrderNo" column="external_order_no"/>
        <result property="routePlanId" column="route_plan_id"/>
        <result property="routePlanName" column="route_plan_name"/>
        <result property="visitorType" column="visitor_type"/>
        <result property="travelDate" column="travel_date"/>
        <result property="sellMethod" column="sell_method"/>
        <result property="sourceName" column="source_name"/>
        <result property="sellerId" column="seller_id"/>
        <result property="memberId" column="member_id"/>
        <result property="amount" column="amount"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="paymentStatus" column="pay_status"/>
        <result property="paymentType" column="payment_type"/>
        <result property="orderStatus" column="order_status"/>
        <result property="paymentDate" column="payment_date"/>
        <result property="areaManagerId" column="area_manager_id"/>
        <result property="storeName" column="store_name"/>
        <result property="contactName" column="contactName"/>
        <result property="shareName" column="share_name"/>
        <result property="isBind" column="is_bind"/>
        <result property="credentialNo" column="credentialNo"/>
        <result property="city" column="city"/>
        <result property="province" column="province"/>
        <result property="county" column="county"/>
        <result property="createTime" column="create_time"/>
        <result property="mobile" column="mobile"/>
        <result property="auditState" column="audit_state"/>
        <result property="storeId" column="store_id"/>
        <result property="sourceId" column="source_id"/>
        <result property="isMarketing" column="is_marketing"/>
        <result property="isMarketingUse" column="is_marketing_use"/>
        <result property="remark" column="remark"/>
        <result property="dispatchStatus" column="dispatch_status"/>
        <result property="isInvoice" column="is_invoice"/>
        <result property="isChangeOrder" column="is_change_order"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="confirmType" column="confirm_type"/>
        <result property="travelStatus" column="travel_status"/>
        <result property="finishStatus" column="finish_status"/>
        <result property="agencyGroupId" column="agency_group_id"/>
        <collection property="orderPayList" resultMap="orderPayList"/>
        <collection property="insuranceList" resultMap="insuranceResultMap"/>
        <collection property="contractList" resultMap="contractResultMap"/>
    </resultMap>
    <resultMap id="orderPayList" type="com.yc.ship.module.trade.api.dto.TradeOrderPayDTO">
        <id property="id" column="payId"/>
        <result property="orderId" column="id"/>
        <result property="payAmount" column="payAmount"/>
        <result property="paymentNo" column="payment_no"/>
        <result property="payStatus" column="pay_status"/>
        <result property="paymentType" column="payment_type"/>
        <result property="paymentDate" column="payment_date"/>
    </resultMap>
    <resultMap id="insuranceResultMap" type="com.yc.ship.module.trade.dal.dataobject.insurance.InsuranceDO">
        <id property="id" column="insuranceId"/>
        <result property="orderId" column="id"/>
        <result property="policyNo" column="policy_no"/>
        <result property="proposalNo" column="proposal_no"/>
        <result property="insuranceNo" column="insurance_no"/>
        <result property="insuranceStatus" column="insurance_status"/>
        <result property="insuredNum" column="insured_num"/>
        <result property="insuranceEffectDate" column="insurance_effect_date"/>
        <result property="electronicPolicy" column="electronic_policy"/>
    </resultMap>
    <resultMap id="contractResultMap" type="com.yc.ship.module.trade.dal.dataobject.contract.ContractDO">
        <id property="id" column="contractId"/>
        <result property="orderId" column="id"/>
        <result property="contractNo" column="contract_no"/>
        <result property="status" column="status"/>
        <result property="touristsCount" column="tourists_count"/>
        <result property="contractUrl" column="contract_url"/>
        <result property="operateName" column="operate_name"/>
        <result property="signerName" column="signer_name"/>
        <result property="signerMobile" column="signer_mobile"/>
        <result property="signingTime" column="signing_time"/>
        <result property="signingUrl" column="signing_url"/>
        <result property="viewUrl" column="view_url"/>
    </resultMap>



    <resultMap id="RefundResultMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="sourceName" column="source_name"/>
        <result property="externalOrderNo" column="external_order_no"/>
        <result property="routePlanId" column="route_plan_id"/>
        <result property="visitorType" column="visitor_type"/>
        <result property="travelDate" column="travel_date"/>
        <result property="sellMethod" column="sell_method"/>
        <result property="sourceName" column="source_name"/>
        <result property="sellerId" column="seller_id"/>
        <result property="memberId" column="member_id"/>
        <result property="amount" column="amount"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="orderStatus" column="order_status"/>
        <result property="paymentDate" column="payment_date"/>
        <result property="contactName" column="contact_name"/>
        <result property="credentialNo" column="credential_no"/>
        <result property="mobile" column="mobile"/>
        <result property="remark" column="remark"/>
        <collection property="tradeDetailList" resultMap="tradeDetailResultMap"/>
    </resultMap>
    <resultMap id="tradeDetailResultMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">
        <id property="orderId" column="id"/>
        <result property="id" column="detailId"/>
        <result property="voucherCode" column="voucher_code"/>
        <result property="voucherStatus" column="voucher_status"/>
        <result property="productName" column="product_name"/>
        <result property="specType" column="spec_type"/>
        <collection property="visitors" resultMap="visitorResultMap"/>
    </resultMap>
    <resultMap id="visitorResultMap" type="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        <id property="detailId" column="detailId"/>
        <result property="id" column="visitorId"/>
        <result property="credentialNo" column="credential_no"/>
        <result property="credentialType" column="credential_type"/>
        <result property="name" column="name"/>
    </resultMap>

    <select id="getCategoryTreeList" resultType="com.yc.ship.module.product.api.dto.CategoryRespDTO">
        SELECT id,parent_id parentId,cate_name cateName,sort_num sortNum from product_category WHERE type = 1 AND use_status = 1 AND deleted = 0 AND tenant_id = #{tenantId} order by sort_num
    </select>

    <select id="getCategoryOTATreeList" resultType="com.yc.ship.module.product.api.dto.CategoryRespDTO">
        SELECT id,parent_id parentId,cate_name cateName,sort_num sortNum from product_category WHERE type = 1 AND use_status = 1 AND deleted = 0
    </select>

    <select id="selectTouristExportHead"
            resultType="java.util.Map">
        SELECT
            SUM(a.num) AS totalOrders,
            SUM(a.num1) AS adultNum,
            SUM(a.num2) AS childBabyNum,
            SUM(a.num3) AS withNum,
            SUM(a.num4) AS leaderNum,
            SUM(a.payAmount) AS totalPayAmount,
            SUM(a.actual_amount) AS totalActualAmount,
            SUM(a.deposi) as deposi,
            IFNULL((
                SELECT GROUP_CONCAT(CONCAT( nationalityName, '(', num, ')') SEPARATOR ' ')
                FROM (
                SELECT
                IFNULL(NULLIF(tv.nationality, ''), 1) AS nationalityCode,
                IFNULL(NULLIF(ar.name, ''), '中国') AS nationalityName,
                COUNT(1) AS num
                FROM trade_order td
                INNER JOIN trade_order_user tou ON td.id = tou.order_id AND tou.deleted = 0
                LEFT JOIN trade_detail tdl ON td.id = tdl.order_id AND tdl.deleted = 0
                LEFT JOIN trade_visitor tv ON tdl.id = tv.detail_id AND tv.deleted = 0
                LEFT JOIN trade_order_room_model torm ON tv.room_index_id = torm.room_index_id AND torm.deleted = 0 AND td.id = torm.order_id
                LEFT JOIN resource_room_model rrm ON torm.room_model_id = rrm.id
                LEFT JOIN product_spu ps ON ps.id = tdl.product_id
                LEFT JOIN ota_distributor od ON od.id = td.source_id
                LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
                LEFT JOIN area ar ON tv.nationality = ar.id
                WHERE td.deleted = 0
                    AND tou.deleted = 0
                    AND tv.type IS NOT NULL
                    <include refid="commonWhereConditions"/>
                    GROUP BY nationalityCode
                ) nationality_stats
            ), '') AS nationalityStats,
            IFNULL((
                SELECT GROUP_CONCAT(CONCAT(roomName, '(', roomNum, ')') SEPARATOR ',')
                FROM (
                SELECT
                CONCAT(rrm.name, torm.floor, 'F') AS roomName,
                torm.floor AS floor,
                COUNT(DISTINCT torm.room_index_id) AS roomNum
                FROM trade_order td
                INNER JOIN trade_order_user tou ON td.id = tou.order_id AND tou.deleted = 0
                LEFT JOIN trade_detail tdl ON td.id = tdl.order_id AND tdl.deleted = 0
                LEFT JOIN trade_visitor tv ON tdl.id = tv.detail_id AND tv.deleted = 0
                LEFT JOIN trade_order_room_model torm ON tv.room_index_id = torm.room_index_id AND torm.deleted = 0 AND td.id = torm.order_id
                LEFT JOIN resource_room_model rrm ON torm.room_model_id = rrm.id
                LEFT JOIN product_spu ps ON ps.id = tdl.product_id
                LEFT JOIN ota_distributor od ON od.id = td.source_id
                LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
                WHERE td.deleted = 0
                    AND tou.deleted = 0
                    AND tv.type IS NOT NULL
                <include refid="commonWhereConditions"/>
                GROUP BY rrm.name, torm.floor
                ORDER BY torm.floor
            ) room_stats
            ), '') AS roomStats,
        (
        SELECT COUNT(DISTINCT torm.room_index_id)
            FROM trade_order td
            INNER JOIN trade_order_user tou ON td.id = tou.order_id AND tou.deleted = 0
            LEFT JOIN trade_detail tdl ON td.id = tdl.order_id AND tdl.deleted = 0
            LEFT JOIN trade_visitor tv ON tdl.id = tv.detail_id AND tv.deleted = 0
            LEFT JOIN trade_order_room_model torm ON tv.room_index_id = torm.room_index_id AND torm.deleted = 0 AND td.id = torm.order_id
            LEFT JOIN resource_room_model rrm ON torm.room_model_id = rrm.id
            LEFT JOIN product_spu ps ON ps.id = tdl.product_id
            LEFT JOIN ota_distributor od ON od.id = td.source_id
            LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
        WHERE td.deleted = 0
            AND tou.deleted = 0
            AND tv.type IS NOT NULL
            <include refid="commonWhereConditions"/>
        ) AS totalRooms
        FROM (
            SELECT
            COUNT(1) AS num,
            SUM(CASE WHEN tv.type IN ('adultPlus', 'adultTake') THEN 1 ELSE 0 END) AS num1,
            SUM(CASE WHEN tv.type IN ('babyTake','babyPlus','babyNonTake','childTake','childPlus','childNonTake') THEN 1 ELSE 0 END) AS num2,
            SUM(CASE WHEN tv.type = 'with' THEN 1 ELSE 0 END) AS num3,
            SUM(CASE WHEN tv.type = 'leader' THEN 1 ELSE 0 END) AS num4,
            td.pay_amount AS payAmount,
            td.deposi,
            IFNULL(topay.actual_amount, 0) AS actual_amount
            FROM trade_order td
            INNER JOIN trade_order_user tou ON td.id = tou.order_id AND tou.deleted = 0
            LEFT JOIN trade_detail tdl ON td.id = tdl.order_id AND tdl.deleted = 0
            LEFT JOIN trade_visitor tv ON tdl.id = tv.detail_id AND tv.deleted = 0
            LEFT JOIN product_spu ps ON ps.id = tdl.product_id
            LEFT JOIN ota_distributor od ON od.id = td.source_id
            LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
            left join trade_order_room_model torm on tv.room_index_id = torm.room_index_id and torm.deleted =0 AND td.id = torm.order_id
            LEFT JOIN (
            SELECT order_id, SUM(pay_amount) AS actual_amount
            FROM trade_order_pay
            WHERE pay_status = 1
            GROUP BY order_id
            ) topay ON td.id = topay.order_id
        WHERE
            td.deleted = 0
            AND tv.type IS NOT NULL
            <include refid="commonWhereConditions"/>
        GROUP BY td.id, td.pay_amount) a

    </select>

    <!-- 公共查询条件 -->
    <sql id="commonWhereConditions">
        <if test="vo.orderStatus!=null and vo.orderStatus.size()>0">
            AND td.order_status IN
            <foreach collection="vo.orderStatus" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.tenantId!=null">
            AND td.tenant_id = #{vo.tenantId}
        </if>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no LIKE CONCAT('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>

        <if test="vo.memberId != null and vo.memberId != ''">
            AND td.member_id = #{vo.memberId}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.shipId != null and vo.shipId != ''">
            AND td.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND EXISTS (SELECT 1 FROM product_voyage top WHERE top.id = td.voyage_id AND top.route_id = #{vo.routeId})
        </if>
        <if test="vo.dispatchStatus != null">
            AND td.dispatch_status = #{vo.dispatchStatus}
        </if>
        <if test="vo.payType != null">
            AND EXISTS (SELECT 1 FROM trade_order_pay top WHERE top.order_id = td.id AND top.payment_type = #{vo.payType})
        </if>
        <if test="vo.otaCateId != null and vo.otaCateId != ''">
            AND od.ota_category_id = #{vo.otaCateId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            AND od.ota_category_id IN
            <foreach collection="vo.otaCateIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.productTag != null and vo.productTag != ''">
            AND ps.product_tag = #{vo.productTag}
        </if>
        <if test="vo.productTag2 != null and vo.productTag2 != ''">
            AND ps.product_tag2 = #{vo.productTag2}
        </if>
        <if test="vo.productTag3 != null and vo.productTag3 != ''">
            AND ps.product_tag3 = #{vo.productTag3}
        </if>
        <if test="vo.productTag4 != null and vo.productTag4 != ''">
            AND ps.product_tag4 = #{vo.productTag4}
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and (vo.planIds==null or vo.planIds.size()==0)">
            AND tdl.product_id IN
            <foreach collection="vo.cateIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="vo.externalOrderNo != null and vo.externalOrderNo != ''">
            AND td.external_order_no = #{vo.externalOrderNo}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND EXISTS (SELECT tv.id FROM trade_visitor tv WHERE tv.detail_id = tdl.id AND tv.credential_no = #{vo.credentialNo})
        </if>
        <if test="vo.userName != null and vo.userName != ''">
            AND EXISTS (SELECT tv.id FROM trade_visitor tv WHERE tv.detail_id = tdl.id AND tv.name = #{vo.userName})
        </if>
        <choose>
            <when test="vo.mobile != null and vo.mobile.length() == 4">
                AND tou.mobile LIKE CONCAT('%',#{vo.mobile})
            </when>
            <when test="vo.mobile != null and vo.mobile != ''">
                AND tou.mobile = #{vo.mobile}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND td.create_time <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND td.create_time <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.orderDate != null and vo.orderDate != ''">
            AND td.create_time = #{vo.orderDate}
        </if>
        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND td.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.shareName != null and vo.shareName != ''">
            AND td.share_name LIKE CONCAT('%',#{vo.shareName},'%')
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name LIKE CONCAT('%',#{vo.productName}, '%')
        </if>
        <if test="vo.isFullPay == 0">
            AND td.is_full_pay = #{vo.isFullPay}
        </if>
        <if test="vo.isComment != null">
            AND td.is_comment = #{vo.isComment}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.distributorId != null and vo.distributorId != ''">
            AND td.source_id = #{vo.distributorId}
        </if>
        <if test="vo.sourceName != null and vo.sourceName != ''">
            AND td.source_name LIKE CONCAT('%',#{vo.sourceName}, '%')
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.roomModelId != null and vo.roomModelId != ''">
            AND torm.room_model_id = #{vo.roomModelId}
        </if>
        <if test="vo.floor != null and vo.floor != ''">
            AND torm.floor = #{vo.floor}
        </if>
        <if test="vo.orderRefundDateStart != null and vo.orderRefundDateEnd != ''">
            AND EXISTS (SELECT 1 FROM trade_refund tr WHERE tr.order_id=td.id AND tr.refund_status = 6 AND tr.refund_time BETWEEN #{vo.orderRefundDateStart} and #{vo.orderRefundDateEnd})
        </if>
    </sql>

    <select id="getDailyBusinessStatisticsPage"
            resultType="com.yc.ship.module.trade.controller.admin.business.vo.DailyBusinessStatisticsRespVO">
        SELECT
            td.source_id AS sourceId,
            td.source_name as sourceName,
            IFNULL(tot.free_num, 0) AS freeNum,
            0 AS freeCount,
            IFNULL(topay.actual_amount, 0) AS actualAmount,
            COUNT(1) AS totalCount,
            SUM(CASE WHEN tv.type IN ('adultPlus', 'adultTake') THEN 1 ELSE 0 END) AS adultCount,
            SUM(CASE WHEN tv.type IN ('childTake','childPlus','childNonTake') THEN 1 ELSE 0 END) AS childCount,
            SUM(CASE WHEN tv.type = 'with' THEN 1 ELSE 0 END) AS withCount,
            SUM(CASE WHEN tv.type = 'leader' THEN 1 ELSE 0 END) AS leaderCount,
            SUM(CASE WHEN tv.type IN ('babyTake','babyPlus','babyNonTake') THEN 1 ELSE 0 END) AS infantCount,
            COUNT(1) - IFNULL(tot.free_num, 0) AS validCount
        FROM trade_order td
                 INNER JOIN trade_order_user tou ON td.id = tou.order_id AND tou.deleted = 0
                 LEFT JOIN trade_detail tdl ON td.id = tdl.order_id AND tdl.deleted = 0
                 LEFT JOIN trade_visitor tv ON tdl.id = tv.detail_id AND tv.deleted = 0
                 left join trade_order_total tot on td.id = tot.old_order_id and tot.deleted = 0
                 LEFT JOIN (SELECT order_id, SUM(pay_amount) AS actual_amount
                            FROM trade_order_pay
                            WHERE pay_status = 1 GROUP BY order_id) topay ON td.id = topay.order_id
        WHERE
            td.deleted = 0
            AND tv.type IS NOT NULL
            AND td.order_status > 0
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND exists (select 1 from product_voyage top where top.id = td.voyage_id and top.route_id = #{vo.routeId})
        </if>
        GROUP BY td.source_id;

    </select>

    <select id="getTradeOrderUserPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*,toa.audit_time auditTime,tou.contact_name,tou.credential_no,tou.mobile,count(tdl.id) num,sum(CASE WHEN tdl.voucher_status in (1,2,3,4,6,8,9) THEN	1 ELSE 0 END) realnum,(tr.origin_amount-tr.refund_amount) feeAmount,tdl.product_name,tdl.spec_type,ifnull(tr.origin_amount-tr.refund_amount,0) realAmount
             ,tr.refund_amount refundAmount,
             ps.product_tag productTag,ps.product_tag2 productTag2,ps.product_tag3 productTag3,ps.product_tag4 productTag4
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        left JOIN trade_detail tdl on td.id = tdl.order_id and tdl.deleted = 0
        left JOIN product_spu ps on ps.id = tdl.product_id
        LEFT JOIN ota_distributor od on od.id = td.source_id
        LEFT JOIN trade_order_audit toa on toa.order_id = td.id and toa.audit_status = 1
        LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
        LEFT JOIN trade_order_room_model torm on torm.order_id = td.id
        where td.deleted = 0 and tou.deleted = 0
        <if test="vo.orderStatus!=null and vo.orderStatus.size()>0">
            and  td.order_status in
            <foreach collection="vo.orderStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.tenantId!=null">
            AND td.tenant_id = #{vo.tenantId}
        </if>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>

        <if test="vo.memberId != null and vo.memberId != ''">
            AND td.member_id = #{vo.memberId}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.shipId != null and vo.shipId != ''">
            AND td.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND exists (select 1 from product_voyage top where top.id = td.voyage_id and top.route_id = #{vo.routeId})
        </if>
        <if test="vo.dispatchStatus != null">
            AND td.dispatch_status = #{vo.dispatchStatus}
        </if>
        <if test="vo.payType != null">
            AND exists (select 1 from trade_order_pay top where top.order_id = td.id and top.payment_type = #{vo.payType})
        </if>
        <if test="vo.otaCateId != null and vo.otaCateId != ''">
            AND od.ota_category_id = #{vo.otaCateId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            and od.ota_category_id in
            <foreach collection="vo.otaCateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.productTag != null and vo.productTag != ''">
            AND ps.product_tag = #{vo.productTag}
        </if>
        <if test="vo.productTag2 != null and vo.productTag2 != ''">
            AND ps.product_tag2 = #{vo.productTag2}
        </if>
        <if test="vo.productTag3 != null and vo.productTag3 != ''">
            AND ps.product_tag3 = #{vo.productTag3}
        </if>
        <if test="vo.productTag4 != null and vo.productTag4 != ''">
            AND ps.product_tag4 = #{vo.productTag4}
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and (vo.planIds==null or vo.planIds.size()==0)">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="vo.externalOrderNo != null and vo.externalOrderNo != ''">
            AND td.external_order_no = #{vo.externalOrderNo}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.credential_no = #{vo.credentialNo})
        </if>
        <if test="vo.userName != null and vo.userName != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.name = #{vo.userName})
        </if>
        <choose>
            <when test="vo.mobile != null and vo.mobile.length() == 4">
                AND tou.mobile like concat('%',#{vo.mobile})
            </when>
            <when test="vo.mobile != null and vo.mobile != ''">
                AND tou.mobile = #{vo.mobile}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND td.create_time <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND td.create_time <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.orderDate != null and vo.orderDate != ''">
            AND td.create_time = #{vo.orderDate}
        </if>
        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND td.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.shareName != null and vo.shareName != ''">
            AND td.share_name like concat('%',#{vo.shareName},'%')
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.isFullPay == 0">
            AND td.is_full_pay = #{vo.isFullPay}
        </if>
        <if test="vo.isComment != null">
            AND td.is_comment = #{vo.isComment}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.paymentStatus != null">
            AND td.pay_status = #{vo.paymentStatus}
        </if>

        <if test="vo.distributorId != null and vo.distributorId != ''">
            AND td.source_id = #{vo.distributorId}
        </if>
        <if test="vo.sourceName != null and vo.sourceName != ''">
            AND td.source_name like concat('%',#{vo.sourceName}, '%')
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.roomModelId != null and vo.roomModelId != ''">
            AND torm.room_model_id = #{vo.roomModelId}
        </if>
        <if test="vo.floor != null and vo.floor != ''">
            AND torm.floor = #{vo.floor}
        </if>
        <if test="vo.orderRefundDateStart != null and vo.orderRefundDateEnd != ''">
            AND exists (select 1 from trade_refund tr  where tr.order_id=td.id and tr.refund_status = 6 and tr.refund_time BETWEEN #{vo.orderRefundDateStart} and #{vo.orderRefundDateEnd} )
        </if>
        GROUP BY td.id
        ORDER BY td.create_time DESC

    </select>


    <select id="getTradeOrderUserCount" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderCountRespVO">
        SELECT count(DISTINCT td.id) orderNum,sum(if(tdl.voucher_status in (1,2,3,4,6,8,9), quantity_of_one,0)) totalNum,
        sum(if(tdl.spec_type='full' and tdl.voucher_status in (1,2,3,4,6,8,9), quantity_of_one,0)) fullNum,
        sum(if(tdl.spec_type='half' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) halfNum,
        sum(if(tdl.spec_type='free' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) freeNum,
        sum(if(tdl.spec_type='common' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) commonNum,
        sum(if(tdl.spec_type='car_seat' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) carSeatNum,
        sum(if(tdl.spec_type='car_yd' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) carYdNum,
        sum(if(tdl.spec_type='other' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) otherNum,
        sum(if(tdl.spec_type='carrying_children' and tdl.voucher_status in (1,2,3,4,6,8,9),quantity_of_one,0)) carryingChildrenNum,
        sum(tdl.actual_price) totalPayAmount
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id
        INNER JOIN product_spu ps on ps.id = tdl.product_id
        LEFT JOIN ota_distributor od on od.id = td.source_id
        where 1= 1
        <if test="vo.orderStatus!=null and vo.orderStatus.size()>0">
            and  td.order_status in
            <foreach collection="vo.orderStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.tenantId!=null">
            AND td.tenant_id = #{vo.tenantId}
        </if>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>

        <if test="vo.memberId != null and vo.memberId != ''">
            AND td.member_id = #{vo.memberId}
        </if>
        <if test="vo.productTag != null and vo.productTag != ''">
            AND ps.product_tag = #{vo.productTag}
        </if>
        <if test="vo.productTag2 != null and vo.productTag2 != ''">
            AND ps.product_tag2 = #{vo.productTag2}
        </if>
        <if test="vo.productTag3 != null and vo.productTag3 != ''">
            AND ps.product_tag3 = #{vo.productTag3}
        </if>
        <if test="vo.productTag4 != null and vo.productTag4 != ''">
            AND ps.product_tag4 = #{vo.productTag4}
        </if>
        <if test="vo.dispatchStatus != null">
            AND td.dispatch_status = #{vo.dispatchStatus}
        </if>
        <if test="vo.payType != null">
            AND exists (select 1 from trade_order_pay top where top.order_id = td.id and top.payment_type = #{vo.payType})
        </if>
        <if test="vo.otaCateId != null and vo.otaCateId != ''">
            AND od.ota_category_id = #{vo.otaCateId}
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and vo.cateIds!=null and vo.cateIds.size()>0">
            AND (td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            or tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and (vo.planIds==null or vo.planIds.size()==0)">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and (vo.cateIds==null or vo.cateIds.size()==0)">
            and td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            and od.ota_category_id in
            <foreach collection="vo.otaCateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.externalOrderNo != null and vo.externalOrderNo != ''">
            AND td.external_order_no = #{vo.externalOrderNo}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.credential_no = #{vo.credentialNo})
        </if>
        <if test="vo.userName != null and vo.userName != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.name = #{vo.userName})
        </if>
        <choose>
            <when test="vo.mobile != null and vo.mobile.length() == 4">
                AND tou.mobile like concat('%',#{vo.mobile})
            </when>
            <when test="vo.mobile != null and vo.mobile != ''">
                AND tou.mobile = #{vo.mobile}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND td.create_time <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND td.create_time <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.orderDate != null and vo.orderDate != ''">
            AND td.create_time = #{vo.orderDate}
        </if>
        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND td.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.shareName != null and vo.shareName != ''">
            AND td.share_name like concat('%',#{vo.shareName},'%')
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.isFullPay == 0">
            AND td.is_full_pay = #{vo.isFullPay}
        </if>
        <if test="vo.isComment != null">
            AND td.is_comment = #{vo.isComment}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.sourceName != null and vo.sourceName != ''">
            AND td.source_name like concat('%',#{vo.sourceName}, '%')
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.orderRefundDateStart != null and vo.orderRefundDateEnd != ''">
            AND exists (select 1 from trade_refund tr  where tr.order_id=td.id and tr.refund_status = 6 and tr.refund_time BETWEEN #{vo.orderRefundDateStart} and #{vo.orderRefundDateEnd} )
        </if>

    </select>


    <select id="getPaidOrderPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*, op.pay_amount payAmount, op.refund_amount refundAmount, op.pay_status paymentStatus, op.payment_type paymentType, op.payment_date paymentDate
        FROM trade_order td
        INNER JOIN trade_order_pay op on op.order_id = td.id
        where td.order_status <![CDATA[ >= ]]> 6 and td.order_status <![CDATA[ <= ]]> 11
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.payType != null">
            AND op.payment_type = #{vo.payType}
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND op.payment_date <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND op.payment_date <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.orderDate != null and vo.orderDate != ''">
            AND td.create_time = #{vo.orderDate}
        </if>
        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND td.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        ORDER BY td.create_time DESC
    </select>


    <select id="getRefundDetailPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">
        SELECT td.*,tv.credential_no,tv.`name`,tv.mobile,tor.order_no
        FROM trade_detail td
        INNER JOIN  trade_visitor tv on tv.detail_id = td.id
        LEFT JOIN trade_order tor on tor.id = td.order_id
        where 1= 1
        <if test="vo.name != null and vo.name != ''">
            AND tv.name = #{vo.name}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND tor.order_no = #{vo.orderNo}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND tv.credential_no = #{vo.credentialNo}
        </if>
        <if test="vo.mobile != null and vo.mobile != ''">
            AND tv.mobile = #{vo.mobile}
        </if>
        <if test="vo.orderStatus != null and vo.orderStatus != ''">
            AND tor.order_status = #{vo.orderStatus}
        </if>

    </select>


    <select id="getRefundRecordPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundRespVO">
        SELECT tr.*,td.order_no, ou.mobile,td.travel_date, ou.contact_name ,tdl.product_name
        FROM trade_refund tr
        INNER JOIN  trade_order td on tr.order_id = td.id
        inner join product_voyage pv on td.voyage_id = pv.id
        inner join trade_order_user ou on ou.order_id = td.id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id  and tdl.deleted = 0
        LEFT JOIN ota_distributor od on od.id = td.source_id
        where 1= 1
        <if test="vo.shipId != null and vo.shipId != ''">
            AND td.ship_id = #{vo.shipId}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND pv.route_id = #{vo.routeId}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.refundStatus!=null and vo.refundStatus.size()>0">
            and  tr.refund_status in
            <foreach collection="vo.refundStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.refundMethod != null and vo.refundMethod != ''">
            AND tr.refund_method = #{vo.refundMethod}
        </if>
        <if test="vo.refundUserId!=null and vo.refundUserId.size()>0">
            and  tr.refund_user in
            <foreach collection="vo.refundUserId" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.refundChannel != null">
            AND tr.refund_channel = #{vo.refundChannel}
        </if>
        <if test="vo.mobile != null and vo.mobile != ''">
            AND ou.mobile = #{vo.mobile}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.refundTime != null and vo.refundTime != ''">
            AND tr.refund_time = #{vo.refundTime}
        </if>
        <if test="vo.refundTimeStart != null and vo.refundTimeStart != ''">
            AND tr.refund_time <![CDATA[ >= ]]> #{vo.refundTimeStart}
        </if>
        <if test="vo.refundTimeEnd != null and vo.refundTimeEnd != ''">
            AND tr.refund_time <![CDATA[ <= ]]> #{vo.refundTimeEnd}
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            and od.ota_category_id in
            <foreach collection="vo.otaCateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and vo.planIds==null">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and (vo.cateIds==null or vo.cateIds.size()==0)">
            and td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by tr.id
        ORDER BY tr.create_time DESC
    </select>


    <select id="getOrderInfo"  resultMap="MiddleWareDtoResultMap">
        SELECT td.id,td.voyage_id,td.is_read isRead,td.pay_amount,td.order_no,td.external_order_no,td.visitor_type,td.travel_date,td.sell_method,td.source_id,td.source_name,td.store_id,td.seller_id,td.member_id,td.amount,td.order_status,td.remark,td.share_name,td.create_time,td.is_marketing,td.is_marketing_use,
        tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,tou.county,tou.province,tou.city,td.agency_group_id,
        top.id payId,top.pay_amount payAmount,top.pay_status,top.payment_type,top.payment_date,top.payment_no,
        td.store_name,td.is_invoice, td.confirm_type, td.travel_status, tr.refund_amount,td.finish_status,
        ti.id insuranceId,ti.policy_no,ti.proposal_no,ti.insurance_no,ti.insurance_status,ti.insured_num,ti.insurance_effect_date,ti.electronic_policy,
        tc.id contractId,tc.contract_no,tc.`status`,tc.tourists_count,tc.contract_url,tc.operate_name,tc.signer_name,tc.signer_mobile,tc.signing_time
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        left JOIN trade_order_pay top on td.id = top.order_id AND top.pay_status = 1 and top.deleted = 0
        left JOIN trade_insurance ti on td.id = ti.order_id
        left JOIN trade_contract tc on td.id = tc.order_id
        LEFT JOIN (select m2.order_id,sum(m1.refund_amount) refund_amount from trade_refund_detail m1
            inner join trade_refund m2 on m1.refund_id = m2.id where m2.order_id = #{id} and m1.refund_status = 6 GROUP BY m2.order_id) tr on td.id = tr.order_id
        where 1= 1
        AND td.id = #{id}


    </select>

    <select id="getPdaOrderInfo"  resultMap="MiddleWareDtoResultMap2">
        SELECT td.id,td.is_read isRead,td.pay_amount,td.order_no,td.external_order_no,td.route_plan_id,rt.name route_plan_name,td.visitor_type,td.travel_date,td.sell_method,td.source_id,td.source_name,td.store_id,td.seller_id,td.member_id,td.amount,td.order_status,td.remark,td.share_name,td.create_time,td.is_bind,td.area_manager_id,td.is_marketing,td.is_marketing_use,
        tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,tou.county,tou.province,tou.city,td.is_change_order,td.agency_group_id,
        top.id payId,top.pay_amount payAmount,top.pay_status,top.payment_type,top.payment_date,top.payment_no,
        td.store_name,td.dispatch_status,td.is_invoice, td.confirm_type, td.travel_status, tr.refund_amount,td.finish_status,
        ti.id insuranceId,ti.policy_no,ti.proposal_no,ti.insurance_no,ti.insurance_status,ti.insured_num,ti.insurance_effect_date,ti.electronic_policy,
        tc.id contractId,tc.contract_no,tc.`status`,tc.tourists_count,tc.contract_url,tc.operate_name,tc.signer_name,tc.signer_mobile,tc.signing_time,tc.signing_url,tc.view_url
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        left JOIN trade_order_pay top on td.id = top.order_id AND top.pay_status = 1 and top.deleted = 0
        left JOIN trade_insurance ti on td.id = ti.order_id
        left JOIN trade_contract tc on td.id = tc.order_id
        left JOIN product_route_plan rt on rt.id = td.route_plan_id
        LEFT JOIN (select m2.order_id,sum(m1.refund_amount) refund_amount from trade_refund_detail m1
        inner join trade_refund m2 on m1.refund_id = m2.id where m2.order_id = #{id} and m1.refund_status = 6 GROUP BY m2.order_id) tr on td.id = tr.order_id
        where 1= 1
        AND td.id = #{id}


    </select>


    <select id="getMarketingApplyOrderPage"  resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.id,td.pay_amount,td.order_no,td.external_order_no,td.route_plan_id,td.visitor_type,td.travel_date,td.sell_method,td.source_name,td.source_id,td.seller_id,td.member_id,td.amount,td.order_status,td.remark,td.share_name,td.create_time,td.is_bind,td.area_manager_id,td.agency_group_id,
               tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,
               td.store_name,td.store_id,pma.audit_state,pma.use_audit_state,pma.type auditType,pma.id marketingAuditId,pma.is_execute,pma.error_text,pmk.name marketingName,pmk.policy_type policyType,pma.remark auditRemark,pma.file_url fileUrl
        FROM
            product_marketing_audit pma
                INNER JOIN trade_order td on pma.order_id = td.id
                inner join trade_detail ttd on td.id = ttd.order_id
                INNER JOIN trade_order_user tou ON td.id = tou.order_id
                LEFT JOIN product_marketing pmk ON pmk.id = pma.marketing_id
        WHERE 1 = 1 AND td.order_status != 11 and
        pma.travel_id in (SELECT DISTINCT travel_id FROM product_marketing_pm WHERE user_id = #{vo.userId} and deleted = 0)
        <if test="vo.ids.size()>0">
            and pma.travel_id in
            <foreach collection="vo.ids" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.policyType != null and vo.policyType != ''">
            AND pmk.policy_type = #{vo.policyType}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND ttd.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.marketingName != null and vo.marketingName != ''">
            AND pmk.name like concat('%',#{vo.marketingName}, '%')
        </if>
          <if test="vo.isExecute != null and vo.isExecute != ''">
            AND pma.is_execute  = #{vo.isExecute}
        </if>
        <if test="vo.distributorId != null and vo.distributorId != ''">
            AND pma.agency_id = #{vo.distributorId}
        </if>
          <if test="vo.storeId != null and vo.storeId != ''">
            AND pma.store_id = #{vo.storeId}
        </if>
        <if test="vo.orderStartTime != null and vo.orderStartTime != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.orderStartTime}
        </if>
        <if test="vo.orderEndTime != null and vo.orderEndTime != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.orderEndTime}
        </if>
        <if test="vo.auditState != null ">
            AND pma.audit_state = #{vo.auditState}
        </if>
        GROUP BY pma.id,td.id
        order by pma.create_time desc
    </select>

    <select id="getStoresMarketingOrderPage"  resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeMarketOrderRespVO">
        SELECT td.id,td.pay_amount,td.order_no,td.external_order_no,td.route_plan_id,td.visitor_type,td.travel_date,td.sell_method,td.source_name,td.source_id,td.seller_id,td.member_id,td.amount,td.order_status,td.remark,td.share_name,td.create_time,td.is_bind,td.area_manager_id,td.agency_group_id,
        tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,
        td.store_name,td.store_id,pma.audit_state,pma.use_audit_state,pma.type auditType,pma.id marketingAuditId,pma.remark auditRemark,pma.is_execute,pma.error_text,pma.file_url fileUrl,if(pms.id is null,null, pmk.`name`) marketingName,if(pms.id is null,null, pmk.policy_type)   policyType
        FROM
        product_marketing_audit pma
        INNER JOIN trade_order td on pma.order_id = td.id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id
        INNER JOIN trade_order_user tou ON td.id = tou.order_id
        LEFT JOIN product_marketing_pool_spu pms on td.id = pms.order_id
        LEFT JOIN product_marketing pmk ON pmk.id = pma.marketing_id
        WHERE 1 = 1
        <if test="vo.storeId != null">
            and pma.store_id = #{vo.storeId}
        </if>
        <if test="vo.distributorId != null">
            and td.source_id = #{vo.distributorId}
        </if>
        <if test="vo.ids.size()>0">
            and pma.travel_id in
            <foreach collection="vo.ids" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.isExecute != null">
            and pma.is_execute = #{vo.isExecute}
        </if>
        <if test="vo.policyType != null and vo.policyType != ''">
            AND pmk.policy_type = #{vo.policyType}
        </if>
        <if test="vo.marketingName != null and vo.marketingName != ''">
            AND pmk.name like concat('%',#{vo.marketingName}, '%')
        </if>
        <if test="vo.auditState != null">
            and pma.audit_state = #{vo.auditState}
        </if>
        <if test="vo.useAuditState != null">
            and pma.use_audit_state = #{vo.useAuditState}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        GROUP BY pma.id
        order by pma.create_time desc
    </select>

    <select id="getStoresMarketingOrderDesc"  resultType="java.util.Map">
        SELECT td.id,td.pay_amount,td.order_no,td.external_order_no,td.route_plan_id,td.visitor_type,td.travel_date,td.sell_method,td.source_name,td.source_id,td.seller_id,td.member_id,td.amount,td.order_status,td.remark,td.share_name,td.create_time,td.is_bind,td.area_manager_id,td.agency_group_id,
        tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,
        td.store_name,td.store_id,pma.audit_state,pma.use_audit_state,pma.type auditType,pma.id marketingAuditId,pma.remark auditRemark,pma.is_execute,pma.error_text,pma.file_url fileUrl
        FROM
        product_marketing_audit pma
        INNER JOIN trade_order td on pma.order_id = td.id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id
        INNER JOIN trade_order_user tou ON td.id = tou.order_id
        LEFT JOIN product_marketing pmk ON pmk.id = pma.marketing_id
        WHERE 1 = 1
        <if test="vo.storeId != null">
            and pma.store_id = #{vo.storeId}
        </if>
        <if test="vo.ids.size()>0">
            and pma.travel_id in
            <foreach collection="vo.ids" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.isExecute != null">
            and pma.is_execute = #{vo.isExecute}
        </if>
        <if test="vo.policyType != null and vo.policyType != ''">
            AND pmk.policy_type = #{vo.policyType}
        </if>
        <if test="vo.marketingName != null and vo.marketingName != ''">
            AND pmk.name like concat('%',#{vo.marketingName}, '%')
        </if>
        <if test="vo.auditState != null">
            and pma.audit_state = #{vo.auditState}
        </if>
        <if test="vo.useAuditState != null">
            and pma.use_audit_state = #{vo.useAuditState}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        GROUP BY pma.id
        order by pma.create_time desc
    </select>

    <select id="selectBindMarketing"  resultType="java.lang.Long">
        SELECT tob.bind_id
        FROM
            trade_order_bind tob
        WHERE
            tob.type = 7 AND tob.deleted = 0 AND tob.order_id = #{orderId}
    </select>

    <select id="selectBindUseMarketing"  resultType="java.lang.Long">
        SELECT tob.bind_id
        FROM
            trade_order_bind tob
        WHERE
            tob.type = 8 AND tob.deleted = 0 AND tob.order_id = #{orderId}
    </select>
    <select id="selectOrderIdByOrderNo"  resultType="java.lang.Long">
        SELECT id FROM trade_order WHERE order_no = #{orderNo}
    </select>

    <select id="getRefundOrderInfo"  resultMap="RefundResultMap">
        SELECT td.id,td.order_no,td.external_order_no,td.route_plan_id,td.visitor_type,td.travel_date,td.sell_method,td.source_name,td.seller_id,td.member_id,td.amount,td.pay_amount,td.order_status,td.payment_date,td.remark,
        tou.contact_name contactName,tou.credential_no credentialNo,tou.mobile,
        tdl.id detailId,tdl.voucher_code,tdl.voucher_status,tdl.product_name,tdl.spec_type,
        tv.id visitorId,tv.credential_no,tv.credential_type,tv.`name`
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id
        INNER JOIN trade_visitor tv on tdl.id = tv.detail_id
        where 1= 1
        <if test="id != null and id != ''">
            AND td.id = #{id}
        </if>

    </select>
    <select id="getTradeOrderByExpire"  resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        select * from trade_order where order_status =14 and travel_date <![CDATA[ <= ]]> #{expiretime}
    </select>

    <select id="getPageRefundRecord"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundRecordRespVO">
        SELECT trr.*,td.order_no, td.id orderId, td.travel_date
        FROM trade_refund_record trr
        left join trade_order_pay top on trr.order_pay_id = top.id
        left join trade_refund tr on trr.refund_id = tr.id
        left join trade_order td on td.id = top.order_id
        left join product_voyage pv on td.voyage_id = pv.id
        LEFT JOIN ota_distributor od on od.id = td.source_id
        where 1= 1
        <if test="vo.shipId != null and vo.shipId != ''">
            AND td.ship_id = #{vo.shipId}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND pv.route_id = #{vo.routeId}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.refundNo != null and vo.refundNo != ''">
            AND trr.refund_no = #{vo.refundNo}
        </if>
        <if test="vo.paymentNo != null and vo.paymentNo != ''">
            AND trr.payment_no = #{vo.paymentNo}
        </if>
        <if test="vo.refundStatus != null and vo.refundStatus != ''">
            AND trr.refund_status = #{vo.refundStatus}
        </if>
        <if test="vo.refundId != null and vo.refundId != ''">
            AND trr.refund_id = #{vo.refundId}
        </if>
        <if test="vo.openId != null and vo.openId != ''">
            AND trr.openId = #{vo.openId}
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            and od.ota_category_id in
            <foreach collection="vo.otaCateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.refundTime != null and vo.refundTime != ''">
            AND trr.refund_time = #{vo.refundTime}
        </if>
        <if test="vo.refundTimeStart != null and vo.refundTimeStart != ''">
            AND trr.refund_time <![CDATA[ >= ]]> #{vo.refundTimeStart}
        </if>
        <if test="vo.refundTimeEnd != null and vo.refundTimeEnd != ''">
            AND trr.refund_time <![CDATA[ <= ]]> #{vo.refundTimeEnd}
        </if>
        ORDER BY trr.create_time DESC
    </select>

    <select id="getPrintTemp"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.otc.PrintRespVO">
        SELECT pt.* FROM print_temp pt where pt.template_code = #{printTemp}
    </select>

    <select id="queryCanInvoicedOrderPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT
        o.order_no,
        o.travel_date,
        o.order_status,
        o.amount,
        o.create_time,
        o.external_order_no,
        o.id,
        o.is_invoice,
        o.item_id,
        o.payment_date,
        o.remark,
        o.seller_id,
        o.sell_method,
        o.share_name,
        o.source_id,
        o.source_name,
        o.store_id,
        o.store_name,
        o.visitor_type,
        tou.contact_name,
        tou.credential_no,
        tou.mobile,
        td.product_name,
        count(
        DISTINCT
        CASE
        WHEN (
        td.voucher_status in (3,6,7,8)
        OR ( td.voucher_status = 9 AND td.check_status = 1 )
        ) THEN
        td.id ELSE null
        END
        ) AS num,
        o.pay_amount - SUM( ifnull(trd.refund_amount,0) ) - SUM(
        CASE
        WHEN ( td.voucher_status in (1,2) )
        AND DATE_FORMAT( td.validity_end_date, '%Y-%c-%d' ) >= DATE_FORMAT( NOW( ), '%Y-%c-%d' ) THEN
        td.actual_price ELSE 0
        END
        ) AS pay_amount
        FROM
        trade_order o
        INNER JOIN trade_detail td ON td.order_id = o.id
        LEFT JOIN trade_refund_detail trd ON trd.trade_detail_id = td.id
        AND trd.refund_status = 6
        INNER JOIN trade_order_user tou ON o.id = tou.order_id
        LEFT JOIN trade_invoice_detail i ON i.order_no = o.order_no
        LEFT JOIN trade_invoice ii ON i.invoice_id = ii.id AND ii.invoice_status IN (1,3)
        LEFT JOIN (select order_id,payment_type from trade_order_pay where pay_status=1 GROUP BY order_id) op ON op.order_id = o.id
        LEFT JOIN ota_bill_order obo ON obo.order_id = o.id
        LEFT JOIN ota_bill_store obs ON obs.id = obo.bill_store_id AND obs.bill_status=1
        where td.use_date >= DATE_SUB(NOW(), INTERVAL #{canInvoicedDay} DAY)
        and o.is_invoice in (0,2)
        and o.order_status in (6,7,8,9,11,12)
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND o.order_no = #{vo.orderNo}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND o.source_id = #{vo.sourceId}
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND o.store_id = #{vo.storeId}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND tou.credential_no = #{vo.credentialNo}
        </if>
        <if test="vo.mobile != null and vo.mobile != ''">
            AND tou.mobile = #{vo.mobile}
        </if>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND o.travel_date = #{vo.travelDate}
        </if>

        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND o.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND td.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND o.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.use_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.use_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND o.payment_date <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND o.payment_date <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND td.product_name like concat('%',#{vo.productName}, '%')
        </if>
        AND ii.id IS NULL
        AND (op.payment_type != 6 OR (o.is_bill=2 AND obs.id IS NOT NULL))
        AND date_format(NOW(), '%Y-%m-%d') > date_format(o.travel_date, '%Y-%m-%d')
        GROUP BY
        o.order_no
        HAVING
        SUM(CASE WHEN td.voucher_status IN (1,2) AND td.validity_end_date >= CURDATE() THEN 1 ELSE 0 END) = 0
        AND pay_amount > 0
        ORDER BY
        o.travel_date DESC
    </select>

    <select id="getOrderByVoucherCode"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT o.* FROM trade_order o
            inner join trade_detail d on d.order_id = o.id
        where (o.order_status = 6 or o.order_status = 7) and d.voucher_code = #{voucherCode} limit 1
    </select>

    <select id="getOrderByExternalOrderNo" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        select t1.* from trade_order t1 inner join trade_supplier_order t2 on t1.id = t2.order_id where t2.trade_no=#{tradeNo} limit 1
    </select>

    <select id="getOtcOrderByExternalOrderNo"  resultType="java.util.Map">
        select t2.* from trade_order t1 inner join trade_supplier_order t2 on t1.id = t2.order_id where t1.order_no=#{tradeNo} limit 1
    </select>

    <select id="getTradeOrderByVoucherCode" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT o.* FROM trade_order o  inner join trade_detail d on d.order_id = o.id where d.voucher_code = #{voucherCode} limit 1
    </select>

    <select id="getOrderByCardNo"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT
            o.*
        FROM
            trade_order o
                INNER JOIN trade_detail d ON d.order_id = o.id
                INNER JOIN trade_visitor v ON v.detail_id = d.id
        WHERE
            o.travel_date >= CURDATE()
          and d.voucher_status in (1,2,3,4,8,9)
          and v.credential_no = #{cardNo}
    </select>

    <select id="getOrderByDetailBase"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT o.* FROM trade_order o
                            inner join trade_detail d on d.order_id = o.id
                            inner join trade_detail_base tdb on tdb.detail_id = d.id
        where (o.order_status = 6 or o.order_status = 7) and tdb.id = #{baseId} limit 1
    </select>


    <select id="getAccountOrderPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.*,tou.contact_name,tou.credential_no,tou.mobile,COUNT(tdl.id) num,tdl.product_name,tdl.spec_type
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        INNER JOIN trade_detail tdl on td.id = tdl.order_id
        where 1= 1 AND td.order_status NOT IN (-2, -1, 0, 1, 2, 12)  AND td.is_account = 0
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND td.order_no = #{vo.orderNo}
        </if>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        GROUP BY td.id
        ORDER BY td.create_time DESC

    </select>

    <update id="updateOrderStatus">
        UPDATE trade_order SET order_status = #{orderStatus} WHERE id = #{orderId} and order_status != #{orderStatus}
    </update>

    <update id="updateOrderIsAccount">
        UPDATE trade_order SET is_account = #{isAccount} WHERE id = #{orderId}
    </update>

    <update id="updateOrderDeleted">
        UPDATE trade_order SET deleted = #{deleted},update_time = now() WHERE id = #{orderId}
    </update>

    <resultMap id="OrderAndDetailResultMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderPdaRespVO">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="sourceName" column="source_name"/>
        <result property="externalOrderNo" column="external_order_no"/>
        <result property="routePlanId" column="route_plan_id"/>
        <result property="visitorType" column="visitor_type"/>
        <result property="travelDate" column="travel_date"/>
        <result property="sellMethod" column="sell_method"/>
        <result property="sourceName" column="source_name"/>
        <result property="sellerId" column="seller_id"/>
        <result property="memberId" column="member_id"/>
        <result property="amount" column="amount"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="orderStatus" column="order_status"/>
        <result property="paymentDate" column="payment_date"/>
        <result property="contactName" column="contact_name"/>
        <result property="credentialNo" column="credential_no"/>
        <result property="mobile" column="mobile"/>
        <result property="remark" column="remark"/>
        <collection property="tradeDetailList" resultMap="tradeDetailMap"/>
    </resultMap>


    <resultMap id="tradeDetailMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">

        <result property="id" column="detailId"/>
        <result property="orderId" column="order_id"/>
        <result property="voucherCode" column="voucher_code"/>
        <result property="voucherStatus" column="voucher_status"/>
        <result property="useDate" column="use_date"/>
        <result property="validityStartDate" column="validity_start_date"/>
        <result property="validityEndDate" column="validity_end_date"/>
        <result property="usageDays" column="usage_days"/>
        <result property="checkStatus" column="check_status"/>
        <result property="takeStatus" column="take_status"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="verifyTime" column="verify_time"/>
        <result property="activateStatus" column="activate_status"/>
        <result property="activateDays" column="activate_days"/>
        <result property="activateTime" column="activate_time"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="specType" column="spec_type"/>
        <collection property="detailBaseList" resultMap="tradeDetailBaseResMap"/>
    </resultMap>

    <resultMap id="tradeDetailBaseResMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.DetailBaseRespVO">

        <result property="id" column="detail_base_id"/>
        <result property="detailId" column="detailId"/>
        <result property="productBaseId" column="product_base_id"/>
        <result property="productBaseName" column="product_base_name"/>
        <result property="productBaseType" column="product_base_type"/>
        <result property="productResourceId" column="product_resource_id"/>
        <result property="productResourceName" column="product_resource_name"/>
        <result property="supplierProductId" column="supplier_product_id"/>
        <result property="supplierId" column="supplier_id"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="isSystemDirect" column="is_system_direct"/>
        <result property="useStatus" column="use_status"/>
        <result property="checkStatus" column="base_check_status"/>
        <result property="verifyTime" column="base_verify_time"/>
        <result property="price" column="base_price"/>
        <result property="usageCount" column="usage_count"/>
        <result property="productResourcePrice" column="product_resource_price"/>
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="specificationId" column="specification_id"/>
        <result property="specificationName" column="specification_name"/>
        <result property="specType" column="base_spec_type"/>
        <result property="supplierVoucherCode" column="supplier_voucher_code"/>
        <result property="platSupplierOrderId" column="plat_supplier_order_id"/>
    </resultMap>


    <select id="getOrderAndDetailInfo"  resultMap="OrderAndDetailResultMap">
        SELECT distinct td.id,td.order_no,td.external_order_no,td.route_plan_id,td.visitor_type,td.travel_date,td.sell_method,td.source_name,td.seller_id,td.member_id,td.amount,td.pay_amount,td.order_status,td.payment_date,td.remark,
        tou.contact_name,tou.credential_no,tou.mobile,
        tdl.id detailId,tdl.voucher_code,tdl.voucher_status,tdl.product_name,tdl.spec_type,tdl.validity_start_date,tdl.validity_end_date,
        tdb.id detail_base_id,tdb.detail_id,tdb.product_base_id,tdb.product_base_name,tdb.product_base_type,tdb.product_resource_id,tdb.product_resource_name,tdb.supplier_product_id,tdb.supplier_id,
        tdb.supplier_name,tdb.is_system_direct,tdb.use_status,tdb.check_status base_check_status,tdb.verify_time base_verify_time,tdb.price base_price,tdb.usage_count,tdb.product_resource_price,tdb.project_id,tdb.project_name,
        tdb.specification_id,tdb.specification_name,tdb.spec_type base_spec_type,tdb.supplier_voucher_code,tdb.plat_supplier_order_id
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id  and td.deleted = 0
        INNER JOIN trade_detail tdl on td.id = tdl.order_id  and tdl.deleted = 0
        INNER JOIN trade_visitor tv on tv.detail_id = tdl.id  and tv.deleted = 0
        inner join trade_detail_base tdb on tdl.id = tdb.detail_id
        where 1= 1 and td.order_status >= 6 and td.order_status <![CDATA[ <= ]]> 9
          and td.sell_method != 3  and td.sell_method != 4
        and tdl.validity_start_date   <![CDATA[ <= ]]>  NOW()
        and tdl.validity_end_date   <![CDATA[ >= ]]>  NOW()
        <if test="channelId != null and channelId != ''">
            and tdb.product_base_id in (select product_id from product_check_channel where channel_id=#{channelId} and deleted = 0)
        </if>
        <if test="orderNo != null and orderNo != ''">
            AND td.order_no = #{orderNo}
        </if>
        <choose>
            <when test="phone != null and phone.length() == 4">
                AND tou.mobile like concat('%',#{phone})
            </when>
            <when test="phone != null and phone != ''">
                AND tou.mobile = #{phone}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="cardNo != null and cardNo.length() == 4">
                AND tv.credential_no like concat('%',#{cardNo})
            </when>
            <when test="cardNo != null and cardNo != ''">
                AND tv.credential_no = #{cardNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        ORDER BY td.travel_date DESC
    </select>

    <select id="getPlatOrderNo"  resultType="java.lang.String">
        select t3.order_no from trade_order t1
            inner join trade_supplier_order t2 on t1.id = t2.order_id
            inner join trade_order t3 on t2.trade_no = t3.external_order_no
            where t1.order_no = #{orderNo}
    </select>

    <select id="getTotalPayByOrderNo"  resultType="java.math.BigDecimal">
        select sum(t2.pay_amount) from trade_order t1 inner join trade_order_pay t2 on t1.id = t2.order_id
        where t1.order_no = #{orderNo} and t2.pay_status = 1
    </select>

    <select id="getCheckOrder" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.id,td.order_no,td.external_order_no,td.route_plan_id,td.travel_status,plan.name as route_plan_name,td.visitor_type,td.travel_date,
               td.sell_method,td.source_name,td.seller_id,td.member_id,td.amount,td.pay_amount,td.order_status,
               td.payment_date,td.remark, tou.contact_name,tou.credential_no,tou.mobile,td.is_add_order
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id  and td.deleted = 0
        INNER JOIN trade_detail tdl on td.id = tdl.order_id  and tdl.deleted = 0
        left join product_route_plan plan on td.route_plan_id = plan.id
        <if test="vo.type != null and vo.dispatchId == null and vo.type == 'boat'">
            left join dispatch_operate_plan_boat_order_detail_base dopbodb on td.id = dopbodb.order_id and dopbodb.deleted = 0
        </if>
        <if test="vo.type != null and vo.dispatchId == null and vo.type == 'car'">
            left join dispatch_car_dispatch_order_detail_base dcdodb on td.id = dcdodb.order_id and dcdodb.deleted = 0
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            INNER JOIN trade_visitor tv on tv.detail_id = tdl.id and tv.deleted = 0
        </if>
        where 1=1
        <choose>
            <when test="vo.orderStatus != null">
                AND td.order_status = #{vo.orderStatus}
            </when>
            <otherwise>
                and td.order_status in (6,7,8,9,10,13)
            </otherwise>
        </choose>
        <choose>
            <when test="vo.travelDate != null and vo.travelDate != ''">
                AND DATE_FORMAT(tdl.validity_start_date, '%Y-%m-%d') <![CDATA[ <= ]]> #{vo.travelDate}
                AND DATE_FORMAT(tdl.validity_end_date, '%Y-%m-%d') <![CDATA[ >= ]]> #{vo.travelDate}
            </when>
            <otherwise>
                and tdl.validity_start_date <![CDATA[ <= ]]> NOW()
                and tdl.validity_end_date <![CDATA[ >= ]]> NOW()
            </otherwise>
        </choose>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.phone != null and vo.phone.length() == 4">
                AND tou.mobile like concat('%',#{vo.phone})
            </when>
            <when test="vo.phone != null and vo.phone != ''">
                AND tou.mobile = #{vo.phone}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.credentialNo != null and vo.credentialNo.length() == 4">
                AND tv.credential_no like concat('%',#{vo.credentialNo})
            </when>
            <when test="vo.credentialNo != null and vo.credentialNo != ''">
                AND tv.credential_no = #{vo.credentialNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'boat'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_operate_plan_boat_order_detail_base
                                                   WHERE plan_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'car'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_car_dispatch_order_detail_base
                                                    WHERE car_dispatch_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.sellMethod != null">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and vo.cateIds!=null and vo.cateIds.size()>0">
            AND (td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            or tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and vo.planIds==null">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and (vo.cateIds==null or vo.cateIds.size()==0)">
            and td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by td.id
        ORDER BY td.create_time DESC
    </select>

    <select id="countCheckOrder" resultType="java.lang.Integer">
        SELECT count(DISTINCT td.id) num FROM trade_order td
        INNER JOIN trade_detail tdl ON td.id = tdl.order_id
        INNER JOIN trade_order_user tou ON td.id = tou.order_id
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            INNER JOIN trade_visitor tv on tv.detail_id = tdl.id and tv.deleted = 0
        </if>
        where 1=1
        <choose>
            <when test="vo.orderStatus != null">
                AND td.order_status = #{vo.orderStatus}
            </when>
            <otherwise>
                and td.order_status in (6,7,8,9,10,13)
            </otherwise>
        </choose>
        <choose>
            <when test="vo.travelDate != null and vo.travelDate != ''">
                AND DATE_FORMAT(tdl.validity_start_date, '%Y-%m-%d') <![CDATA[ <= ]]> #{vo.travelDate}
                AND DATE_FORMAT(tdl.validity_end_date, '%Y-%m-%d') <![CDATA[ >= ]]> #{vo.travelDate}
            </when>
            <otherwise>
                and tdl.validity_start_date <![CDATA[ <= ]]> NOW()
                and tdl.validity_end_date <![CDATA[ >= ]]> NOW()
            </otherwise>
        </choose>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.phone != null and vo.phone.length() == 4">
                AND tou.mobile like concat('%',#{vo.phone})
            </when>
            <when test="vo.phone != null and vo.phone != ''">
                AND tou.mobile = #{vo.phone}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.credentialNo != null and vo.credentialNo.length() == 4">
                AND tv.credential_no like concat('%',#{vo.credentialNo})
            </when>
            <when test="vo.credentialNo != null and vo.credentialNo != ''">
                AND tv.credential_no = #{vo.credentialNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'boat'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_operate_plan_boat_order_detail_base
            WHERE plan_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'car'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_car_dispatch_order_detail_base
            WHERE car_dispatch_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.sellMethod != null">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and vo.cateIds!=null and vo.cateIds.size()>0">
            AND (td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            or tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and vo.planIds==null">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and (vo.cateIds==null or vo.cateIds.size()==0)">
            and td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectStoreMachineProductList"
            resultType="java.lang.Long">
        SELECT t1.product_id FROM otc_agency_store_machine_product t1
        where 1=1
        <if test="vo.storeId != null and vo.storeId != ''">
            AND t1.store_id = #{vo.storeId}
        </if>
        <if test="vo.machineId != null and vo.machineId != ''">
            AND t1.machine_id = #{vo.machineId}
        </if>
    </select>

    <select id="orderListByOta" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT
            t1.*
        FROM
            trade_order t1
        inner join trade_detail t3 on t1.id = t3.order_id
        inner join trade_visitor t4 on t3.id = t4.detail_id
            INNER JOIN trade_order_user t2 ON t2.order_id = t1.id
        WHERE
            t1.sell_method = 0
          AND t1.order_status in (6,7,8,9,10,13)
          AND t1.tenant_id = #{tenantId}
          AND t1.travel_date = #{travelDate}
          <if test="credentialNo != null and credentialNo != ''">
              AND  t4.credential_no = #{credentialNo}
          </if>
          <if test="mobile != null and mobile != ''">
              AND t2.mobile = #{mobile}
          </if>
        ORDER BY
            t1.payment_date DESC
    </select>

    <select id="orderList" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT
        t1.*
        FROM
        trade_order t1
        INNER JOIN trade_order_user t2 ON t2.order_id = t1.id
        INNER JOIN trade_detail td on t1.id = td.order_id
        INNER JOIN trade_visitor v on v.detail_id = td.id
        WHERE
        1 = 1
        <if test="orderStatus!=null and orderStatus.size()>0">
            AND t1.order_status in
            <foreach collection="orderStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        AND t1.tenant_id = #{tenantId}
        AND t1.travel_date = #{travelDate}
        <if test="credentialNo != null and credentialNo != ''">
            AND v.credential_no = #{credentialNo}
        </if>
        <if test="mobile != null and mobile != ''">
            AND t2.mobile = #{mobile}
        </if>
        group by t1.id
        ORDER BY
        t1.payment_date DESC

    </select>


    <select id="queryOneByOrderNo" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT DISTINCT o2.*
        FROM trade_order o
                 INNER JOIN trade_detail d on d.order_id = o.id
                 INNER JOIN trade_detail_base db ON db.detail_id = d.id
                 INNER JOIN trade_detail d2 ON db.supplier_voucher_code = d2.voucher_code
                 INNER JOIN trade_order o2 ON d2.order_id = o2.id
        WHERE o.order_no = #{orderNo}
        union all
        SELECT o.* FROM trade_order o WHERE o.sell_method in (0,1,4,6,7) and o.order_no = #{orderNo}
    </select>

    <select id="updateAppletProductSellNum">
        update applet_product t1 inner JOIN applet_product_group t2 ON t1.id = t2.applet_product_id
        and t2.deleted =0
        set t1.sell_num = t1.sell_num+#{sellNum} where t2.product_id = #{productId}
    </select>

    <select id="selectCanBindOrderList" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT o.id,o.is_bind
        FROM trade_order o
         INNER JOIN trade_detail d ON d.order_id = o.id
         INNER JOIN trade_order_user u on o.id = u.order_id
        WHERE
            o.order_status > 5 AND o.deleted = 0
          AND (o.is_bind IS NULL or o.is_bind = 0 ) and o.id != #{vo.orderId}
          AND o.travel_date = #{vo.travelDate}
          AND d.product_id = #{vo.productId}
          <if test="vo.routePlanId !=null">
              And o.route_plan_id = #{vo.routePlanId}
          </if>
          AND u.mobile = #{vo.mobile}
        GROUP BY o.id
    </select>

    <update id="deleteOrderById">
        delete from trade_order where id = #{orderId}
    </update>

    <update id="deleteOrderByOrderNo">
        update from trade_order where order_no = #{orderNo}
    </update>

    <select id="getInvoiceOrderCount" resultType="java.lang.Integer">
        select count(1) num from trade_invoice t1 inner join trade_invoice_detail t2 on t1.id = t2.invoice_id
        and t1.invoice_status in (1,3)
        where t2.order_no = #{orderNo} and t1.deleted = 0
    </select>

    <select id="selectPlatPayAmountByOriginOrderNo" resultType="java.util.Map">
        select sum(tod.pay_amount) totalPayAmount
        from trade_order tod
        where tod.external_origin_order_no =#{originOrderNo} and tod.order_status>=6
    </select>


    <select id="selectChangeCountByOrderId" resultType="java.lang.Integer">
        select count(*) totalCount
        from trade_order tod
                 inner join trade_order_bind tb on tod.id = tb.order_id and tb.type = 2 and tb.deleted =0
                 inner join trade_order tod1 on tod1.id = tb.bind_id
        where tod.id = #{orderId} and tod1.order_status>=6
    </select>

    <update id="updateOrderFinishStatus">
        update trade_order tod,
            (select tod.id
             from trade_order tod
                      inner join trade_detail td on tod.id = td.order_id
             where tod.sell_method in
                <foreach collection="sellMethodList" item="item" separator=","  open="(" close=")">
                    #{item}
                </foreach>
               and tod.order_status >=6
               and td.validity_end_date = #{endDate}
               and not EXISTS (
                 select 1
                 from trade_detail td1
                 where td1.validity_end_date > #{endDate} and td1.order_id = tod.id and td1.refund_status != 1
             )
            ) t
        set tod.finish_status = 1
        where tod.id = t.id
    </update>

    <select id="getSupplierCheckOrder" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO">
        SELECT td.id,td.order_no,td.external_order_no,td.route_plan_id,plan.name as route_plan_name,td.visitor_type,td.travel_date,
        td.sell_method,td.source_name,td.seller_id,td.member_id,td.amount,td.pay_amount,td.order_status,
        td.payment_date,td.remark, tou.contact_name,tou.credential_no,tou.mobile
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id  and td.deleted = 0
        INNER JOIN trade_detail tdl on td.id = tdl.order_id  and tdl.deleted = 0
        INNER JOIN trade_detail_base tdb on tdb.detail_id = tdl.id
        left join product_route_plan plan on td.route_plan_id = plan.id
        <if test="vo.type != null and vo.dispatchId == null and vo.type == 'boat'">
            inner join dispatch_operate_plan_boat_order_detail_base dopbodb on td.id = dopbodb.order_id and dopbodb.deleted = 0
        </if>
        <if test="vo.type != null and vo.dispatchId == null and vo.type == 'car'">
            inner join dispatch_car_dispatch_order_detail_base dcdodb on td.id = dcdodb.order_id and dcdodb.deleted = 0
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            INNER JOIN trade_visitor tv on tv.detail_id = tdl.id and tv.deleted = 0
        </if>
        where  supplier_id in (
        select t2.id from product_supplier_user t1 inner join product_supplier t2 on t1.supplier_id = t2.id
        where t1.user_id=#{vo.userId} and t1.deleted =0
        )
        <choose>
            <when test="vo.orderStatus != null">
                AND td.order_status = #{vo.orderStatus}
            </when>
            <otherwise>
                and td.order_status in (6,7,8,9,10,13)
            </otherwise>
        </choose>
        <choose>
            <when test="vo.travelDate != null and vo.travelDate != ''">
                AND DATE_FORMAT(tdl.validity_start_date, '%Y-%m-%d') <![CDATA[ <= ]]> #{vo.travelDate}
                AND DATE_FORMAT(tdl.validity_end_date, '%Y-%m-%d') <![CDATA[ >= ]]> #{vo.travelDate}
            </when>
            <otherwise>
                and tdl.validity_start_date <![CDATA[ <= ]]> NOW()
                and tdl.validity_end_date <![CDATA[ >= ]]> NOW()
            </otherwise>
        </choose>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.phone != null and vo.phone.length() == 4">
                AND tou.mobile like concat('%',#{vo.phone})
            </when>
            <when test="vo.phone != null and vo.phone != ''">
                AND tou.mobile = #{vo.phone}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <choose>
            <when test="vo.credentialNo != null and vo.credentialNo.length() == 4">
                AND tv.credential_no like concat('%',#{vo.credentialNo})
            </when>
            <when test="vo.credentialNo != null and vo.credentialNo != ''">
                AND tv.credential_no = #{vo.credentialNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'boat'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_operate_plan_boat_order_detail_base
            WHERE plan_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.type != null and vo.dispatchId != null and vo.type == 'car'">
            AND td.id in (SELECT DISTINCT order_id FROM dispatch_car_dispatch_order_detail_base
            WHERE car_dispatch_id = #{vo.dispatchId} and deleted = 0)
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.checkType != null and vo.checkType == 1">
            AND tdb.use_status in (1,2)
        </if>
        <if test="vo.checkType != null and vo.checkType == 2">
            AND tdb.use_status in (3,6,8)
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.sellMethod != null">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and vo.cateIds!=null and vo.cateIds.size()>0">
            AND (td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            or tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and vo.planIds==null">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.planIds!=null and vo.planIds.size()>0 and (vo.cateIds==null or vo.cateIds.size()==0)">
            and td.route_plan_id in
            <foreach collection="vo.planIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by td.id
        ORDER BY td.create_time DESC
    </select>
    <select id="getOtaTradeOrderPage"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">
        SELECT td.*,tor.order_no,pbr.check_start_date,pbr.check_end_date
        FROM trade_detail td
        LEFT JOIN product_basic_rule pbr on td.product_id = pbr.id
        LEFT JOIN trade_order tor on tor.id = td.order_id
        where 1= 1 AND
        EXISTS (select * from product_sale_channel sc where sc.product_id = td.product_id and sc.channel_id = 0)
        <if test="vo.productName != null and vo.productName != ''">
            AND td.product_name = #{vo.productName}
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND tor.order_no = #{vo.orderNo}
        </if>
        <if test="vo.orderStatus != null and vo.orderStatus != ''">
            AND tor.order_status = #{vo.orderStatus}
        </if>
        <if test="vo.paymentDateStart != null">
            AND tor.payment_date <![CDATA[ >= ]]> #{vo.paymentDateStart}
        </if>
        <if test="vo.paymentDateEnd != null">
            AND tor.payment_date <![CDATA[ <= ]]> #{vo.paymentDateEnd}
        </if>
        <if test="vo.type == 1">
            AND tor.source_id = #{distributorId}
        </if>
        GROUP BY td.voucher_code
        ORDER BY td.create_time DESC
    </select>


    <select id="regionStatic"  resultType="java.util.Map">
        select t4.id,t4.`name`,t6.province,t6.city,count(DISTINCT t3.id) num from trade_order t1 inner join trade_detail t2 on t1.id = t2.order_id
        inner join trade_visitor t3 on t2.visitor_id = t3.id
        inner join ota_distributor t4 on t1.source_id = t4.id
        inner join ota_distributor_region t5 on t4.id = t5.distributor_id
        and (left(t3.credential_no,2) = t5.distributor_region_id or left(t3.credential_no,4) = t5.distributor_region_id)
        inner join ota_region t6 on ((t5.distributor_region_id = province_code and LEVEL=1) or t5.distributor_region_id = left(t6.id,4) )
        where t2.voucher_status in (1,2,3,4,6,8,9)
        <if test="vo.startDate != null and vo.startDate!=''">
            AND t2.use_date <![CDATA[ >= ]]> #{vo.startDate}
        </if>
        <if test="vo.endDate != null and vo.endDate!=''">
            AND t2.use_date <![CDATA[ <= ]]> #{vo.endDate}
        </if>
        GROUP BY t4.id
        order by num desc
    </select>
    <select id="queryOrderByLast" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT  * FROM trade_order WHERE order_no = #{orderNo} and deleted = 1  ORDER BY update_time DESC limit 1
    </select>

    <select id="queryOrderById" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT  * FROM trade_order WHERE id = #{orderId}   ORDER BY update_time DESC limit 1
    </select>
    <select id="selectConfirmationCountry"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderConfirmationCountryRespVO">
        SELECT
            t1.real_money actual_price,
            t1.room_model_id,
            t2.short_name room_model_short_name,
            t2.NAME room_model_name,
            t3.type,
            ifnull( t3.nationality, 1 ) nationality,
            t3.floor,
            IFNULL( t4.`name`, '中国' ) nationality_name,
            count( t3.id ) num
        FROM
            trade_order_room_model t1
                LEFT JOIN resource_room_model t2 ON t1.room_model_id = t2.id
                LEFT JOIN trade_visitor t3 ON t1.room_index_id = t3.room_index_id
                LEFT JOIN area t4 ON t3.nationality = t4.id
        WHERE
            t1.order_id = #{orderId}
          AND t1.deleted = 0
        GROUP BY
            t1.room_model_id,
            t1.floor,
            t3.type
        ORDER BY
            floor DESC
    </select>

    <select id="getOrderCount" resultType="java.util.Map">
        select GROUP_CONCAT(num,'*',name) name,sum(num1) num1,sum(num2) num2,sum(num3) num3,sum(num4) num4 from (
        SELECT  sum(CASE WHEN tv.type='adultPlus' or tv.type='adultTake' THEN 1 ELSE 0 END) num1,
        sum(CASE WHEN tv.type='babyTake' or tv.type='babyPlus' or tv.type='babyNonTake' or tv.type='childTake' or tv.type='childPlus' or tv.type='childNonTake'  THEN 1 ELSE 0 END) num2,
        sum(CASE WHEN tv.type='with'  THEN 1 ELSE 0 END) num3,
        sum(CASE WHEN tv.type='leader'  THEN 1 ELSE 0 END) num4,
        CONCAT(rrm.short_name,'(',tv.floor,'F)') name ,count(1) num
        FROM trade_order td
        INNER JOIN trade_order_user tou on td.id = tou.order_id
        left JOIN trade_detail tdl on td.id = tdl.order_id and tdl.deleted = 0
        left join trade_visitor tv on tdl.id = tv.detail_id
        left JOIN product_spu ps on ps.id = tdl.product_id
        LEFT JOIN ota_distributor od on od.id = td.source_id
        left join trade_order_room_model tm on tv.room_index_id = tm.room_index_id and tm.deleted =0 AND td.id = tm.order_id
        left join resource_room_model rrm on tm.room_model_id = rrm.id
        LEFT JOIN (select m2.order_id,sum(m2.refund_amount) refund_amount,sum(m2.origin_amount) origin_amount from trade_refund m2  where m2.refund_status = 6 and m2.refund_method !=2 GROUP BY m2.order_id) tr on td.id = tr.order_id
        where td.deleted = 0 and tou.deleted = 0 and td.order_status >0 and tv.type is not null
        <if test="vo.orderStatus!=null and vo.orderStatus.size()>0">
            and  td.order_status in
            <foreach collection="vo.orderStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.tenantId!=null">
            AND td.tenant_id = #{vo.tenantId}
        </if>
        <choose>
            <when test="vo.orderNo != null and vo.orderNo.length() == 4">
                AND td.order_no like concat('%',#{vo.orderNo})
            </when>
            <when test="vo.orderNo != null and vo.orderNo != ''">
                AND td.order_no = #{vo.orderNo}
            </when>
            <otherwise>
            </otherwise>
        </choose>

        <if test="vo.externalOriginOrderNo != null and vo.externalOriginOrderNo != ''">
            AND td.external_origin_order_no = #{vo.externalOriginOrderNo}
        </if>

        <if test="vo.memberId != null and vo.memberId != ''">
            AND td.member_id = #{vo.memberId}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND td.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.shipId != null and vo.shipId != ''">
            AND td.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND exists (select 1 from product_voyage top where top.id = td.voyage_id and top.route_id = #{vo.routeId})
        </if>
        <if test="vo.dispatchStatus != null">
            AND td.dispatch_status = #{vo.dispatchStatus}
        </if>
        <if test="vo.payType != null">
            AND exists (select 1 from trade_order_pay top where top.order_id = td.id and top.payment_type = #{vo.payType})
        </if>
        <if test="vo.otaCateId != null and vo.otaCateId != ''">
            AND od.ota_category_id = #{vo.otaCateId}
        </if>
        <if test="vo.otaCateIds!=null and vo.otaCateIds.size()>0">
            and od.ota_category_id in
            <foreach collection="vo.otaCateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND td.source_id = #{vo.otaId}
        </if>
        <if test="vo.storeId != null and vo.storeId != ''">
            AND td.store_id = #{vo.storeId}
        </if>
        <if test="vo.productTag != null and vo.productTag != ''">
            AND ps.product_tag = #{vo.productTag}
        </if>
        <if test="vo.productTag2 != null and vo.productTag2 != ''">
            AND ps.product_tag2 = #{vo.productTag2}
        </if>
        <if test="vo.productTag3 != null and vo.productTag3 != ''">
            AND ps.product_tag3 = #{vo.productTag3}
        </if>
        <if test="vo.productTag4 != null and vo.productTag4 != ''">
            AND ps.product_tag4 = #{vo.productTag4}
        </if>
        <if test="vo.cateIds!=null and vo.cateIds.size()>0 and (vo.planIds==null or vo.planIds.size()==0)">
            AND tdl.product_id in
            <foreach collection="vo.cateIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="vo.externalOrderNo != null and vo.externalOrderNo != ''">
            AND td.external_order_no = #{vo.externalOrderNo}
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.credential_no = #{vo.credentialNo})
        </if>
        <if test="vo.userName != null and vo.userName != ''">
            AND exists (select tv.id from trade_visitor tv where tv.detail_id = tdl.id and tv.name = #{vo.userName})
        </if>
        <choose>
            <when test="vo.mobile != null and vo.mobile.length() == 4">
                AND tou.mobile like concat('%',#{vo.mobile})
            </when>
            <when test="vo.mobile != null and vo.mobile != ''">
                AND tou.mobile = #{vo.mobile}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tou.contact_name = #{vo.contactName}
        </if>
        <if test="vo.travelDate != null and vo.travelDate != ''">
            AND td.travel_date = #{vo.travelDate}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND td.travel_date <![CDATA[ >= ]]> #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND td.travel_date <![CDATA[ <= ]]> #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND td.create_time <![CDATA[ >= ]]> #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND td.create_time <![CDATA[ <= ]]> #{vo.orderDateEnd}
        </if>
        <if test="vo.orderDate != null and vo.orderDate != ''">
            AND td.create_time = #{vo.orderDate}
        </if>
        <if test="vo.sellerId != null and vo.sellerId != ''">
            AND td.seller_id = #{vo.sellerId}
        </if>
        <if test="vo.shareName != null and vo.shareName != ''">
            AND td.share_name like concat('%',#{vo.shareName},'%')
        </if>
        <if test="vo.voucherCode != null and vo.voucherCode != ''">
            AND tdl.voucher_code = #{vo.voucherCode}
        </if>
        <if test="vo.sellMethod != null and vo.sellMethod != ''">
            AND td.sell_method = #{vo.sellMethod}
        </if>
        <if test="vo.productName != null and vo.productName != ''">
            AND tdl.product_name like concat('%',#{vo.productName}, '%')
        </if>
        <if test="vo.isFullPay == 0">
            AND td.is_full_pay = #{vo.isFullPay}
        </if>
        <if test="vo.isComment != null">
            AND td.is_comment = #{vo.isComment}
        </if>
        <if test="vo.sourceId != null and vo.sourceId != ''">
            AND td.source_id = #{vo.sourceId}
        </if>
        <if test="vo.distributorId != null and vo.distributorId != ''">
            AND td.source_id = #{vo.distributorId}
        </if>
        <if test="vo.sourceName != null and vo.sourceName != ''">
            AND td.source_name like concat('%',#{vo.sourceName}, '%')
        </if>
        <if test="vo.travelStatus != null">
            AND td.travel_status = #{vo.travelStatus}
        </if>
        <if test="vo.orderRefundDateStart != null and vo.orderRefundDateEnd != ''">
            AND exists (select 1 from trade_refund tr  where tr.order_id=td.id and tr.refund_status = 6 and tr.refund_time BETWEEN #{vo.orderRefundDateStart} and #{vo.orderRefundDateEnd} )
        </if>
        GROUP BY rrm.short_name,tv.floor
        ) p
    </select>



    <select id="selectTotalByOrderIds" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalRespVO">
        select sum(adult_total_num) adultTotalNum, sum(child_total_num) childTotalNum, sum(baby_total_num) babyTotalNum,
        sum(with_total_num) withTotalNum, sum(leader_total_num) leaderTotalNum, sum(free_num) freeNum,
        sum(order_money) orderMoney, sum(real_money) realMoney,  sum(other_money) otherMoney,
        sum(adjust_total_money) adjustTotalMoney,  sum(with_amount) withTotalNumMoney,
        sum(leader_amount) leaderTotalMoney, sum(free_amount) freeAmount, sum(service_money) serviceMoney,
        sum(up_floor_total_num) upFloorTotalNum, sum(up_floor_total_money) upFloorTotalMoney,
        sum(plus_total_num) plusTotalNum, sum(collection_service_money) collectionServiceMoney,
        sum(use_room_total_num) totalRoomNum,sum(free_amount) freeAmount   from trade_order_total
        where 1=1
        AND old_order_id in
        <foreach collection="orderIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
    </select>


    <!-- 查询游客名单导出基础信息（船名、航期、航向） -->
    <select id="selectTouristExportBase" resultType="java.util.Map">
        SELECT
        rs.name as shipName,
        pv.start_time as travelDate,
        rr.direction as direction
        FROM trade_order tor
        LEFT JOIN product_voyage pv ON tor.voyage_id = pv.id
        LEFT JOIN resource_ship rs ON pv.ship_id = rs.id
        LEFT JOIN resource_route rr ON pv.route_id = rr.id
        LEFT JOIN ota_distributor od on od.id = tor.source_id
        WHERE tor.deleted = 0
        <if test="vo.orderNo != null and vo.orderNo != ''">
                AND tor.order_no = #{vo.orderNo}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND tor.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.shipId != null and vo.shipId != ''">
            AND pv.ship_id = #{vo.shipId}
        </if>
        <if test="vo.routeId != null and vo.routeId != ''">
            AND pv.route_id = #{vo.routeId}
        </if>
        <if test="vo.travelDateStart != null and vo.travelDateStart != ''">
            AND pv.start_time &gt;= #{vo.travelDateStart}
        </if>
        <if test="vo.travelDateEnd != null and vo.travelDateEnd != ''">
            AND pv.start_time &lt;= #{vo.travelDateEnd}
        </if>
        <if test="vo.orderDateStart != null and vo.orderDateStart != ''">
            AND tor.create_time &gt;= #{vo.orderDateStart}
        </if>
        <if test="vo.orderDateEnd != null and vo.orderDateEnd != ''">
            AND tor.create_time &lt;= #{vo.orderDateEnd}
        </if>
        <if test="vo.orderStatus != null and vo.orderStatus.size() > 0">
            AND tor.order_status IN
            <foreach collection="vo.orderStatus" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND tor.source_id = #{vo.otaId}
        </if>
        <if test="vo.otaCateId != null and vo.otaCateId != ''">
            AND od.ota_category_id = #{vo.otaCateId}
        </if>
        <if test="vo.otaCateIds != null and vo.otaCateIds.size() > 0">
            AND od.ota_category_id IN
            <foreach collection="vo.otaCateIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="vo.contactName != null and vo.contactName != ''">
            AND tor.link_man LIKE CONCAT('%', #{vo.contactName}, '%')
        </if>
        <if test="vo.mobile != null and vo.mobile != ''">
            AND tor.link_mobile = #{vo.mobile}
        </if>
        LIMIT 1
    </select>
    <select id="selectAppPage" resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppTradeOrderRespNewVO">
        select t1.id, t1.order_no, t3.img, t1.ship_id, t3.`name` ship_name, t3.short_name ship_short_name,t4.`name` route_name, t4.direction, t1.voyage_id, t2.`name` voyage_name,t2.boarding_time,t2.start_time,t2.leave_time, t1.order_status, t1.amount price from trade_order t1 LEFT JOIN product_voyage t2 on t1.voyage_id = t2.id
        LEFT JOIN resource_ship t3 on t1.ship_id = t3.id
        LEFT JOIN resource_route t4 on t2.route_id = t4.id
        where t1.deleted = 0
        and member_id = #{pageReqVO.memberId}
        <if test="pageReqVO.orderStatus != null and pageReqVO.orderStatus != ''">
            and order_status = #{pageReqVO.orderStatus}
        </if>
        order by t1.create_time desc
    </select>
    <select id="selectMyItinerary"
            resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppMyItineraryVO">
        SELECT
            t1.id order_id,
            t1.order_no,
            t1.ship_id,
            t4.name ship_name,
            t1.voyage_id,
            t2.`name` voyage_name,
            t2.route_id,
            t3.`name` route_name,
            t2.boarding_time,
            t2.start_time,
            t2.leave_time,
            t3.direction
        FROM
            trade_order t1
                LEFT JOIN product_voyage t2 ON t1.voyage_id = t2.id
                LEFT JOIN resource_route t3 ON t2.route_id = t3.id
                LEFT JOIN resource_ship t4 ON t2.ship_id = t4.id
                left join trade_visitor t5 on t1.id = t5.order_id
        WHERE
            t1.deleted = 0
          and t1.order_status in (3,6,7,8,9)
          <choose>
              <when test="keyword != null and keyword != ''">
                  and (t5.mobile = #{keyword} or t5.credential_no = #{keyword})
              </when>
              <otherwise>
                  and member_id = #{memberId}
              </otherwise>
          </choose>
          <if test="status != null and status != ''">
              <if test="status == '1' or status == 1">
                  and t2.boarding_time > now()
              </if>
              <if test="status == '2' or status == 2">
                  and t2.boarding_time &lt;= now() and t2.leave_time > now()
              </if>
              <if test="status == '3' or status == 3">
                  and t2.leave_time &lt; now()
              </if>
          </if>
        order by t2.boarding_time asc
    </select>
    <select id="selectTotal1ByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalRespVO">
        select sum(deposi) deposi, sum(damaged) damaged, sum(supplement_amount) supplement_amount from trade_order
        where 1=1 and id in
        <foreach collection="orderIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectItineraryDetail"
            resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppItineraryDetailVO">
        SELECT
            t1.id order_id,
            t1.order_no,
            t2.boarding_time,
            t2.start_time,
            t2.leave_time,
            t2.route_id,
            t3.`name` route_name,
            t3.on_dock_id,
            t4.`name` on_dock_name
        FROM
            trade_order t1
                LEFT JOIN product_voyage t2 ON t1.voyage_id = t2.id
                LEFT JOIN resource_route t3 ON t2.route_id = t3.id
                LEFT JOIN resource_dock t4 ON t3.on_dock_id = t4.id
        WHERE
            1=1 and
            t1.id = #{orderId}
    </select>
    <select id="selectTotal2ByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalRespVO">
        SELECT
        ifnull( sum( CASE WHEN type = 'adultTake' OR type = 'adultPlus' THEN num ELSE 0 END ), 0 ) AS adultTotalNum,
        ifnull( sum( CASE WHEN type = 'childTake' OR type = 'childNonTake' OR type = 'childPlus' THEN num ELSE 0 END ), 0 ) AS childTotalNum,
        ifnull( sum( CASE WHEN type = 'babyTake' OR type = 'babyNonTake' OR type = 'babyPlus' THEN num ELSE 0 END ), 0 ) AS babyTotalNum,
        ifnull( sum( CASE WHEN type = 'leader' THEN num ELSE 0 END ), 0 ) AS leaderTotalNum,
        ifnull( sum( CASE WHEN type = 'with' THEN num ELSE 0 END ), 0 ) AS withTotalNum
        FROM
            ( SELECT type, count( 1 ) num FROM trade_visitor WHERE deleted = 0
                                                             and order_id in
                                                             <foreach collection="orderIds" item="item" separator=","  open="(" close=")">
                                                                 #{item}
                                                             </foreach>
                                                             GROUP BY type ) a
    </select>
    <select id="selectCanBuyInsuranceOrder" resultType="java.lang.Long">
        select id
        from trade_order
        where deleted = 0
          and order_status in (1,6)
          and (is_insure in (2,3) or is_insure is null)
          and travel_date BETWEEN now() and CONCAT(DATE_ADD(CURDATE(),INTERVAL 2 DAY), ' 23:59:59')
    </select>
</mapper>

