<?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.TradeVisitorMapper">

    <select id="selectVisitorListBySupplierOrder"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">

        select v.*,d.voucher_code
        FROM trade_detail_base db
        INNER JOIN trade_detail d on d.id = db.detail_id
        INNER JOIN trade_visitor v on v.detail_id = d.id
        <where>
            db.plat_supplier_order_id = #{platSupplierOrderId}
        </where>
        group by v.id
        order by v.detail_id

    </select>

    <select id="selectVisitorListBySpecType"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">

        select v.*
        FROM trade_detail_base db
        INNER JOIN trade_detail d on d.id = db.detail_id
        INNER JOIN trade_visitor v on v.detail_id = d.id
        <where>
            db.plat_supplier_order_id = #{platSupplierOrderId}
            and db.spec_type = #{specType}
            and db.product_base_id in
            <foreach collection="baseList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
        group by v.id
        order by v.detail_id

    </select>

    <select id="selectVisitorVoucherListByOrderId"
            resultType="com.yc.ship.module.trade.service.order.bo.TradeVisitorBO">

        select v.*,d.voucher_code,d.quantity_of_one
        FROM trade_detail d
        LEFT JOIN trade_visitor v on v.detail_id = d.id
        <where>
            d.order_id = #{orderId}
        </where>
        order by d.id
    </select>

    <select id="selectInsureVisitor"
            resultType="com.yc.ship.module.trade.service.order.bo.TradeVisitorBO">

        select tv.*,td.use_date
        from trade_detail td
        inner join trade_visitor tv on tv.detail_id = td.id
        <where>
            td.order_id =#{orderId} and td.voucher_status not in (4,5) and td.voucher_status>=1 and tv.is_insure =1
        and td.product_id=#{productId}
        </where>
    </select>

    <select id="selectContractVisitor"
            resultType="com.yc.ship.module.trade.service.order.bo.TradeVisitorBO">

        select tv.*,td.use_date
        from trade_detail td
        inner join trade_visitor tv on tv.detail_id = td.id
        <where>
            td.order_id =#{orderId} and td.voucher_status not in (4,5) and td.voucher_status>=1
            and td.product_id=#{productId}
        </where>
    </select>

    <select id="selectSupplierVisitor"
            resultType="com.yc.ship.module.trade.service.order.bo.TradeVisitorBO">

        select tdb.supplier_voucher_code, v.*
        from trade_detail_base tdb
        inner join trade_visitor v on v.detail_id = tdb.detail_id
        <where>
            tdb.plat_supplier_order_id = #{platSupplierOrderId}
            <if test="visitorIdList!=null">
                and  v.id  in
                <foreach collection="visitorIdList" item="item" separator=","  open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectShipVisitor"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        select t.product_type productType,v.*,t3.id_card jzCard,t3.arrive_time jzTime,t3.phone jzPhone,t3.address jzAddress
        , a.name nationalityName,t5.room_model_name roomModelName
        from trade_detail t inner join trade_visitor v on v.detail_id = t.id
        left join trade_order_jz_detail t3 on v.credential_no = t3.id_card and t3.deleted =0
        left join area a on v.nationality = a.id
        left join trade_order_room_model t5 on v.room_index_id = t5.room_index_id
        where t.order_id = #{orderId} and v.deleted =0 and t.product_type =0 and t.deleted =0
        group by v.id
    </select>
    <select id="selectRoomShipVisitor"
            resultType="com.yc.ship.module.product.api.dto.OrderRoomUseDTO">
        select room_model_id,floor,room_id,count(1) num from  trade_visitor
        where order_id = #{orderId} and deleted =0
        group by room_model_id,floor,room_id
    </select>
    <select id="selectRefundRoomShipVisitor"
            resultType="com.yc.ship.module.product.api.dto.OrderRoomUseDTO">
        select t1.room_model_id,t1.floor,t1.room_id,count(distinct t1.id) num from  trade_visitor t1
        inner join trade_detail t2 on t1.detail_id = t2.id
        inner join trade_refund_detail t3 on t2.id = t3.trade_detail_id
        where t3.refund_id = #{refundId} and deleted =0
        group by t1.room_model_id,t1.floor,t1.room_id
    </select>
    <select id="selectRoomShipVisitor2"
            resultType="com.yc.ship.module.product.api.dto.OrderRoomUseDTO">
        select room_model_id,floor,'' room_id,count(1) num from  trade_order_room_model
        where order_id = #{orderId}
        group by room_model_id,floor
    </select>
    <select id="queryRelatedVisitor"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        SELECT v1.*, v2.id associatedVisitorId
        FROM trade_visitor v1
            INNER JOIN trade_visitor v2 ON v2.ota_detail_id = v1.detail_id
        WHERE v2.id in
        <foreach collection="visitorIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        UNION ALL
        SELECT v1.*, v2.id associatedVisitorId
        FROM trade_visitor v1
        INNER JOIN trade_visitor v2 ON v2.detail_id = v1.ota_detail_id
        WHERE v2.id in
        <foreach collection="visitorIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="queryVisitorByOrderId" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeVisitorRespVO">
        SELECT v1.*,t3.name nationalityName FROM trade_visitor v1 INNER JOIN trade_detail v2 ON v1.id = v2.visitor_id
        left join area t3 on  v1.nationality = t3.id
        WHERE v2.order_id = #{orderId} and  v1.deleted = 0  and v2.deleted = 0 and v2.product_type = 0
    </select>

    <select id="selectVisitorListByDate" resultType="com.yc.ship.module.trade.service.order.bo.TradeVisitorBO">
        select tv.*,td.product_name,td.spec_type,td.use_date,prp.name planName
        from trade_detail td
        inner join trade_order tt on td.order_id = tt.id
        left join product_route_plan prp on tt.route_plan_id = prp.id
        inner join trade_visitor tv on td.id = tv.detail_id
        inner join trade_detail_base tdb on td.id = tdb.detail_id
        where td.use_date =#{useDate} and tv.credential_no is not null
        and tdb.project_code='boat'
        and tv.is_push is null
        and td.voucher_status in (1,2,4,8,9)
        <if test="orderId!=null">
            and td.order_id = #{orderId}
        </if>
        <if test="tenantId!=null">
            and td.tenant_id = #{tenantId}
        </if>
        GROUP BY td.id,tv.id
        order by tv.create_time asc
    </select>


    <select id="selectBoatVisitorListByRefund" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        select tv.*
        from trade_refund tr
         INNER JOIN trade_refund_detail_base trdb on trdb.refund_id = tr.id
         INNER JOIN trade_detail_base tdb on tdb.id = trdb.trade_detail_base_id
         INNER JOIN trade_visitor tv on tv.detail_id = trdb.trade_detail_id
        where tr.id = #{refundId} and tdb.project_code = 'boat'
        <if test="useDate!=null">
            and tdb.use_date = #{useDate}
        </if>
        GROUP BY tv.id
    </select>

    <select id="selectOrderVisitorCount" resultType="java.lang.Integer">
        select count(*) totalCount
        from trade_detail td
        INNER JOIN trade_visitor tv on tv.detail_id = td.id
        where td.order_id = #{orderId}
        and td.voucher_status !=5
    </select>
    <select id="selectShipByOrderIds"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        select * from trade_visitor where detail_id in (
            select id from trade_detail where order_id in
            <foreach collection="orderIds" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            )
    </select>

    <select id="queryVisitorByVovageId" resultType="java.lang.Integer">
        select count(*) from trade_visitor t1 inner join trade_order t2 on t1.order_id=t2.id
        where t1.credential_no=#{credentialNo} and t2.voyage_id=#{voyageId} and t2.order_no!=#{orderNo}
        and t2.deleted = 0 and t1.deleted = 0 and t2.order_status in (15, 14, 13, 10,  9, 8, 7, 6, 5, 4, 3, 2, 1)
    </select>

    <select id="selectListByVoyageId" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        select tv.*,if(pprmt.total_num &gt;=2 and pprmt.enable_share=0,1,0) as isHaveTogethers,if(pprmt.total_num =1 and pprmt.enable_share=0,1,0) as isAlone
        from trade_visitor tv
                 join trade_order to1 on tv.order_id=to1.id
                 join trade_order_room_model torm on tv.room_index_id=torm.room_index_id
                 join product_price_room_model_type pprmt on torm.room_model_type_id = pprmt.id
        where to1.voyage_id=#{voyageId} and to1.order_status=6
            join trade_order to1 on tv.order_id=to1.id and to1.deleted=0
            join trade_order_room_model torm on tv.room_index_id=torm.room_index_id and torm.deleted=0
            join product_price_room_model_type pprmt on torm.room_model_type_id = pprmt.id and pprmt.deleted=0
        where to1.voyage_id=#{voyageId} and to1.order_status=6 and tv.deleted=0
    </select>

    <select id="selectListByVoyageIdAndRoomId"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        select tv.*
        from trade_visitor tv
                 join trade_order b on tv.order_id=b.id
                 join trade_order b on tv.order_id=b.id and b.deleted=0
        where b.order_status=6 and b.voyage_id=#{voyageId} and if(tv.final_room_id is null,tv.init_room_id,tv.final_room_id)=#{roomId}
    </select>

    <select id="getExportVisitorList" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespExcelVO">
        select t1.order_no orderNo,t1.group_no groupNo,t1.order_status orderStatus,t1.source_name sourceName,t3.name name,
        t3.gender gender,t3.type visitorType,t3.credential_type credentialType,t3.credential_no credentialNo,
        t7.name nationality,t3.birthday birthday,t3.mobile mobile,t3.age age,t3.mobile mobile,t5.short_name roomModelName,t3.floor floor,t6.room_num roomName,t2.price price,t3.remark remark
        from trade_order t1 inner join trade_detail t2 on t1.id=t2.order_id and t2.product_type = 0 and t2.deleted = 0
        inner join trade_visitor t3 on t2.visitor_id=t3.id and t3.deleted =0
        left join trade_order_room_model t4 on t3.room_index_id=t4.room_index_id and t4.deleted=0
        left join resource_room_model t5 on t4.room_model_id = t5.id and t5.deleted=0
        left join resource_room t6 on t3.room_id = t6.id and t6.deleted=0
        left join area t7 on t3.nationality = t7.id
        where t1.order_status in (15, 14, 13, 11, 10, 12, 9, 8, 7, 6, 5, 4, 3, 2, 1)
        <if test="vo.orderNo != null and vo.orderNo != ''">
            AND t1.order_no = #{vo.orderNo}
        </if>
        <if test="vo.voyageId != null and vo.voyageId != ''">
            AND t1.voyage_id = #{vo.voyageId}
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND t1.source_id = #{vo.otaId}
        </if>
        <if test="vo.orderStatus!=null and vo.orderStatus.size()>0">
            and  t1.order_status in
            <foreach collection="vo.orderStatus" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>


    <!-- 查询游客名单导出游客列表 -->
    <select id="selectTouristExportVisitor" resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.TouristExportVisitorVO">
        SELECT
            od.name AS sourceName,
            tor.order_no AS orderNo,
            tor.group_no AS groupNo,
            CASE WHEN rr.direction = 1 THEN '上水' ELSE '下水' END AS direction,
            CASE WHEN tjz.is_jz = 1 THEN '是' ELSE '否' END AS jz,
            DATE_FORMAT(pv.start_time, '%Y.%m.%d') AS travelDate,
            tor.pay_amount AS amount,
            top.pay_amount AS payAmount,
            tor.deposi,
            tv.room_index_id AS roomIndexId,
            torm.room_model_name AS roomType,
            tv.name,
            tv.gender,
            tv.birthday,
            tv.mobile,
            tv.order_id as orderId,
            tv.credential_type AS credentialType,
            tv.credential_no AS credentialNo,
            tv.type AS visitorType,
            tv.floor as floor,
            a.name AS nationalityName,
            GROUP_CONCAT(ps.product_name) AS valueAddedService,
            tor.remark,
            tor.order_status as orderStatus
        FROM trade_visitor tv
        INNER JOIN trade_order tor ON tv.order_id = tor.id AND tor.deleted = 0
        INNER JOIN trade_detail td ON tv.id = td.visitor_id AND td.deleted = 0
        LEFT JOIN area a ON tv.nationality = a.id
        LEFT JOIN product_voyage pv ON tor.voyage_id = pv.id
        LEFT JOIN resource_route rr ON pv.route_id = rr.id
        LEFT JOIN ota_distributor od ON tor.source_id = od.id
        LEFT JOIN trade_order_room_model torm ON tv.room_index_id = torm.room_index_id AND torm.deleted = 0
        left JOIN product_spu ps on ps.id = td.product_id
        left join trade_order_pay top ON top.order_id = tor.id and top.deleted = 0 and top.pay_status = 1
        left Join trade_order_jz tjz on tjz.order_id = tor.id

        WHERE tv.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.userName != null and vo.userName != ''">
            AND tv.name LIKE CONCAT('%', #{vo.userName}, '%')
        </if>
        <if test="vo.credentialNo != null and vo.credentialNo != ''">
            AND tv.credential_no = #{vo.credentialNo}
        </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 tv.floor = #{vo.floor}
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            AND tor.source_id = #{vo.otaId}
        </if>
        <if test="vo.distributorId != null and vo.distributorId != ''">
            AND tor.source_id = #{vo.distributorId}
        </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>
        group by tv.id
        ORDER BY od.id ASC,tor.order_no ASC, tv.room_index_id ASC
    </select>
    <select id="selectPersonDesc" resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppPersonDescVO">
        select order_id,name, count(1) num
        from trade_visitor
        where deleted = 0 and order_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")">
            #{item}
        </foreach>
        <if test="keyword != null and keyword != ''">
            and (mobile = #{keyword} or credential_no = #{keyword})
        </if>
        GROUP BY order_id
    </select>
    <select id="selectWithLeaderStatsByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderWithLeaderStatVO">
        select
        type,
        if(gender=0,'女','男') gender,
        count(1) num
        from trade_visitor
        where deleted = 0 and (type = 'with' or type = 'leader') and order_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY type, gender
    </select>
    <select id="selectOrderCountryStatsByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalCountryVO">
        SELECT
            d1.country_id,
            d2.country_name,
            d1.adultNum,
            d1.childNum,
            d1.babyNum,
            d1.leaderNum,
            d1.withNum,
            d2.twoNum,
            d2.threeNum,
            d2.fourNum,
            d2.fiveNum,
            d2.sixNum
        FROM
            (
                SELECT
                    a1.country_id,
                    a1.country_name,
                    sum( CASE WHEN a1.type = 'adultTake' OR a1.type = 'adultPlus' THEN a1.num ELSE 0 END ) AS adultNum,
                    sum( CASE WHEN a1.type = 'childTake' OR a1.type = 'childNonTake' or a1.type = 'childPlus' THEN a1.num ELSE 0 END ) AS childNum,
                    sum( CASE WHEN a1.type = 'babyTake' OR a1.type = 'babyNonTake' or a1.type = 'babyPlus' THEN a1.num ELSE 0 END ) AS babyNum,
                    sum( CASE WHEN a1.type = 'leader' THEN a1.num ELSE 0 END ) AS leaderNum,
                    sum( CASE WHEN a1.type = 'with' THEN a1.num ELSE 0 END ) AS withNum
                FROM
                    (
                        SELECT
                            t1.nationality country_id,
                            t2.`name` country_name,
                            t1.type,
                            count( t1.id ) num
                        FROM
                            trade_visitor t1
                                LEFT JOIN area t2 ON t1.nationality = t2.id
                        WHERE
                            t1.deleted = 0
                          AND t1.nationality IS NOT NULL
                          AND t1.nationality != ''
                        and t1.order_id in
                        <foreach item="item" collection="orderIds" separator="," open="(" close=")">
                            #{item}
                        </foreach>
                        GROUP BY
                            t1.nationality,
                            t1.type
                    ) a1
                GROUP BY
                    a1.country_id
                ORDER BY
                    a1.country_id ASC
            ) d1
                LEFT JOIN (
                SELECT
                    a1.country_id,
                    a1.country_name,
                    sum( CASE WHEN a1.floor = 2 THEN a1.num ELSE 0 END ) AS twoNum,
                    sum( CASE WHEN a1.floor = 3 THEN a1.num ELSE 0 END ) AS threeNum,
                    sum( CASE WHEN a1.floor = 4 THEN a1.num ELSE 0 END ) AS fourNum,
                    sum( CASE WHEN a1.floor = 5 THEN a1.num ELSE 0 END ) AS fiveNum,
                    sum( CASE WHEN a1.floor = 6 THEN a1.num ELSE 0 END ) AS sixNum
                FROM
                    (
                        SELECT
                            t1.nationality country_id,
                            t2.`name` country_name,
                            t3.floor,
                            count( t1.id ) num
                        FROM
                            trade_visitor t1
                                LEFT JOIN area t2 ON t1.nationality = t2.id
                                LEFT JOIN trade_order_room_model t3 ON t1.room_index_id = t3.room_index_id
                        WHERE
                            t1.deleted = 0
                          AND t3.deleted = 0
                          AND t1.nationality IS NOT NULL
                          AND t1.nationality != ''
                        and t1.order_id in
                        <foreach item="item" collection="orderIds" separator="," open="(" close=")">
                            #{item}
                        </foreach>
                        GROUP BY
                            t1.nationality,
                            t3.floor
                    ) a1
                GROUP BY
                    a1.country_id
                ORDER BY
                    a1.country_id ASC
            ) d2 ON d1.country_id = d2.country_id
        ORDER BY
            d1.country_id ASC
    </select>
    <select id="selectPersonListByOrderId"
            resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppItineraryPersonVO">
        SELECT
            t1.id,
            t1.NAME,
            t1.mobile,
            t1.final_room_id,
            t2.room_num final_room_name,
            t1.init_room_id,
            t3.room_num init_room_name,
            t1.room_id,
            t4.room_num room_name
        FROM
            trade_visitor t1
                LEFT JOIN resource_room t2 ON t1.final_room_id = t2.id
                LEFT JOIN resource_room t3 ON t1.init_room_id = t3.id
                LEFT JOIN resource_room t4 ON t1.room_id = t4.id
        WHERE
            t1.deleted = 0
          AND t1.order_id = #{orderId}
        <if test="keyword != null and keyword != ''">
            AND (t1.mobile = #{keyword} or t1.credential_no = #{keyword})
        </if>
    </select>
</mapper>
