<?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.orderjzdetail.OrderJzDetailMapper">
    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectPage2"
            resultType="com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO">
        select distinct d.* , r.direction , rm.name as room_model_name,
        SUBSTRING_INDEX(o.order_no, '-', -1) as orderNo, dj.dispatch_no as dispatchNo, dj.group_no as groupNo
        from trade_order_jz_detail d
        inner join trade_order_jz oz on d.jz_id = oz.id
        inner join trade_order o on o.deleted = 0 and o.id = oz.order_id
        inner join product_voyage v on o.voyage_id = v.id
        inner join resource_route r on v.route_id = r.id
        inner join trade_visitor u on u.credential_no = d.id_card and u.order_id = oz.order_id
        left join resource_room_model rm on u.room_model_id = rm.id
        left join trade_order_jz_dispatch dj on d.dispatch_id = dj.id
        where d.deleted = 0 and o.voyage_id = #{vo.voyageId}
        <if test="vo.idCard != null">
            and d.id_card like concat('%', #{vo.idCard}, '%')
        </if>
        <if test="vo.name != null">
            and d.name like concat('%', #{vo.name}, '%')
        </if>
        <if test="vo.address != null">
            and d.address = #{vo.address}
        </if>
        <if test="vo.arriveTime != null">
            and d.arrive_time = #{vo.arriveTime}
        </if>
        <if test="vo.dispatchNo != null and vo.dispatchNo != ''">
            and dj.dispatch_no like concat('%', #{vo.dispatchNo}, '%')
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 1">
            and d.sign_time is not null
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 0">
            and d.sign_time is null
        </if>
        order by CAST(SUBSTRING_INDEX(o.order_no, '-', -1) AS UNSIGNED) asc,
                 d.address asc, d.arrive_time asc, d.batch_no asc
    </select>

    <!-- selectVisitorExportList: 导出游客接站明细，条件与selectPage2一致，不分页 -->
    <select id="selectVisitorExportList"
            resultType="com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO">
        select distinct d.* , r.direction , rm.name as room_model_name,
        SUBSTRING_INDEX(o.order_no, '-', -1) as orderNo, dj.dispatch_no as dispatchNo, dj.group_no as groupNo
        from trade_order_jz_detail d
        inner join trade_order_jz oz on d.jz_id = oz.id
        inner join trade_order o on o.deleted = 0 and o.id = oz.order_id
        inner join product_voyage v on o.voyage_id = v.id
        inner join resource_route r on v.route_id = r.id
        inner join trade_visitor u on u.credential_no = d.id_card and u.order_id = oz.order_id
        left join resource_room_model rm on u.room_model_id = rm.id
        left join trade_order_jz_dispatch dj on d.dispatch_id = dj.id
        inner join trade_detail td on td.order_id = o.id and td.product_id = 2034458675435925505
        where d.deleted = 0 and o.voyage_id = #{vo.voyageId}
        <if test="vo.idCard != null and vo.idCard != ''">
            and d.id_card like concat('%', #{vo.idCard}, '%')
        </if>
        <if test="vo.name != null and vo.name != ''">
            and d.name like concat('%', #{vo.name}, '%')
        </if>
        <if test="vo.address != null and vo.address != ''">
            and d.address = #{vo.address}
        </if>
        <if test="vo.arriveTime != null and vo.arriveTime != ''">
            and d.arrive_time = #{vo.arriveTime}
        </if>
        <if test="vo.dispatchNo != null and vo.dispatchNo != ''">
            and dj.dispatch_no like concat('%', #{vo.dispatchNo}, '%')
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 1">
            and d.sign_time is not null
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 0">
            and d.sign_time is null
        </if>
        order by CAST(SUBSTRING_INDEX(o.order_no, '-', -1) AS UNSIGNED) asc,
                 d.address asc, d.arrive_time asc, d.batch_no asc
    </select>

    <select id="selectPage3" resultType="map">
        select distinct o.id, o.order_no, r.direction , v.name, v.credential_no, v.mobile, rm.name AS room_model_name
        from trade_order o
        inner join product_voyage p on o.voyage_id = p.id
        inner join resource_route r on p.route_id = r.id
        inner join trade_visitor v on o.id = v.order_id
        inner join resource_room_model rm ON v.room_model_id = rm.id
        where o.deleted = 0 and p.deleted = 0 and r.deleted = 0 and v.deleted = 0
        and  o.voyage_id =  #{vo.voyageId}
        <if test="vo.queryCode != null">
          and (v.credential_no like concat('%', #{vo.queryCode}, '%') or v.mobile like concat('%', #{vo.queryCode}, '%')  or o.order_no like concat('%', #{vo.queryCode}, '%'))
        </if>
    </select>

    <update id="cleanSign">
        update trade_order_jz_detail set sign_time = null, sign_man = null, sign_remark = null, sign_image = null
        where id = #{id}
    </update>

    <!-- ==================== 赠送行程接站相关 ==================== -->

    <!-- 赠送行程分页查询：基于trade_order_jz_detail，关联trade_detail过滤product_id -->
    <select id="selectGiftPage"
            resultType="com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO">
        select distinct d.* , r.direction , rm.name as room_model_name,
        SUBSTRING_INDEX(o.order_no, '-', -1) as orderNo, dj.dispatch_no as dispatchNo, dj.group_no as groupNo
        from trade_order_jz_detail d
        inner join trade_order_jz oz on d.jz_id = oz.id
        inner join trade_order o on o.deleted = 0 and o.id = oz.order_id
        inner join product_voyage v on o.voyage_id = v.id
        inner join resource_route r on v.route_id = r.id
        inner join trade_visitor u on u.credential_no = d.id_card and u.order_id = oz.order_id
        left join resource_room_model rm on u.room_model_id = rm.id
        left join trade_order_jz_dispatch dj on d.dispatch_id = dj.id
        inner join trade_detail td on td.order_id = o.id and td.product_id = 2034458675435925505
        where d.deleted = 0 and o.voyage_id = #{vo.voyageId}
        <if test="vo.idCard != null">
            and d.id_card like concat('%', #{vo.idCard}, '%')
        </if>
        <if test="vo.name != null">
            and d.name like concat('%', #{vo.name}, '%')
        </if>
        <if test="vo.address != null">
            and d.address = #{vo.address}
        </if>
        <if test="vo.arriveTime != null">
            and d.arrive_time = #{vo.arriveTime}
        </if>
        <if test="vo.dispatchNo != null and vo.dispatchNo != ''">
            and dj.dispatch_no like concat('%', #{vo.dispatchNo}, '%')
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 1">
            and d.sign_time is not null
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 0">
            and d.sign_time is null
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            and o.order_no like concat('%', #{vo.orderNo}, '%')
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            and o.source_id = #{vo.otaId}
        </if>
        order by CAST(SUBSTRING_INDEX(o.order_no, '-', -1) AS UNSIGNED) asc,
                 d.address asc, d.arrive_time asc, d.batch_no asc
    </select>

    <!-- 赠送行程导出查询：条件与selectGiftPage一致，不分页 -->
    <select id="selectGiftExportList"
            resultType="com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO">
        select distinct d.* , r.direction , rm.name as room_model_name,
        SUBSTRING_INDEX(o.order_no, '-', -1) as orderNo, dj.dispatch_no as dispatchNo, dj.group_no as groupNo
        from trade_order_jz_detail d
        inner join trade_order_jz oz on d.jz_id = oz.id
        inner join trade_order o on o.deleted = 0 and o.id = oz.order_id
        inner join product_voyage v on o.voyage_id = v.id
        inner join resource_route r on v.route_id = r.id
        inner join trade_visitor u on u.credential_no = d.id_card and u.order_id = oz.order_id
        left join resource_room_model rm on u.room_model_id = rm.id
        left join trade_order_jz_dispatch dj on d.dispatch_id = dj.id
        inner join trade_detail td on td.order_id = o.id and td.product_id = 2034458675435925505
        where d.deleted = 0 and o.voyage_id = #{vo.voyageId}
        <if test="vo.idCard != null and vo.idCard != ''">
            and d.id_card like concat('%', #{vo.idCard}, '%')
        </if>
        <if test="vo.name != null and vo.name != ''">
            and d.name like concat('%', #{vo.name}, '%')
        </if>
        <if test="vo.address != null and vo.address != ''">
            and d.address = #{vo.address}
        </if>
        <if test="vo.arriveTime != null and vo.arriveTime != ''">
            and d.arrive_time = #{vo.arriveTime}
        </if>
        <if test="vo.dispatchNo != null and vo.dispatchNo != ''">
            and dj.dispatch_no like concat('%', #{vo.dispatchNo}, '%')
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 1">
            and d.sign_time is not null
        </if>
        <if test="vo.signStatus != null and vo.signStatus == 0">
            and d.sign_time is null
        </if>
        <if test="vo.orderNo != null and vo.orderNo != ''">
            and o.order_no like concat('%', #{vo.orderNo}, '%')
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            and o.source_id = #{vo.otaId}
        </if>
        order by CAST(SUBSTRING_INDEX(o.order_no, '-', -1) AS UNSIGNED) asc,
                 d.address asc, d.arrive_time asc, d.batch_no asc
    </select>

    <!-- 赠送行程补登订单查询：根据订单号/证件号后4位搜索，关联trade_detail过滤product_id -->
    <select id="selectGiftOrderPage" resultType="map">
        select distinct o.id, o.order_no, r.direction , v.name, v.credential_no, v.mobile, rm.name AS room_model_name
        from trade_order o
        inner join product_voyage p on o.voyage_id = p.id
        inner join resource_route r on p.route_id = r.id
        inner join trade_visitor v on o.id = v.order_id
        inner join resource_room_model rm ON v.room_model_id = rm.id
        inner join trade_detail td on td.order_id = o.id and td.product_id = 2034458675435925505
        where o.deleted = 0 and p.deleted = 0 and r.deleted = 0 and v.deleted = 0
        and o.voyage_id = #{vo.voyageId}
        <if test="vo.queryCode != null">
            and (v.credential_no like concat('%', #{vo.queryCode}, '%') or v.mobile like concat('%', #{vo.queryCode}, '%') or o.order_no like concat('%', #{vo.queryCode}, '%'))
        </if>
        <if test="vo.otaId != null and vo.otaId != ''">
            and o.source_id = #{vo.otaId}
        </if>
    </select>
</mapper>