<?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.resource.dal.mysql.route.ResourceRouteMapper">

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

    <select id="selectAppRoutePage" resultType="com.yc.ship.module.resource.controller.app.route.vo.AppResourceRouteRespVO">
        select * from (
        SELECT
            rr.direction,
            rr.id route_id,
           rr.NAME route_name,
           rr.on_dock_id dock_id,
           rp.id port_id,
           rr.short_name route_short_name,
           rr.duration,
           rs.id ship_id,
           rs.NAME ship_name,
           rr.route_img routeImg,
           rs.img ship_img,
           rr.route_video routeVideo,
           t2.price
       FROM
           resource_route rr
               LEFT JOIN resource_dock rd on rr.on_dock_id = rd.id
               LEFT JOIN resource_port rp on rd.port_id = rp.id
               LEFT JOIN product_voyage pv ON rr.id = pv.route_id
                                                  <if test="reqVo.startDay != null and reqVo.startDay != ''">
                                                      and pv.start_time BETWEEN CONCAT(#{reqVo.startDay},' 00:00:00') and CONCAT(#{reqVo.startDay},' 23:59:59')
                                                  </if>
               LEFT JOIN resource_ship rs ON pv.ship_id = rs.id
               LEFT JOIN (
               SELECT
                   *
               FROM
                   (
                       SELECT
                           ppv.voyage_id,
                           pprm.price
                       FROM
                           product_price_voyage ppv
                               LEFT JOIN product_price_room_model pprm ON pprm.object_id = ppv.id
                       where ppv.deleted = 0 and pprm.deleted = 0
                       ORDER BY
                           pprm.price ASC
                   ) t1
               GROUP BY
                   t1.voyage_id
           ) t2 ON pv.id = t2.voyage_id
        where rr.deleted = 0 and rd.deleted = 0 and rp.deleted = 0 and pv.deleted = 0 and rs.deleted = 0 and pv.start_time > now()
        and pv.channel like '%2%'
       GROUP BY
        rr.id, rs.id
      ) t3 where 1=1 and t3.price is not null
     <if test="reqVo.shipId != null">
      and t3.ship_id = #{reqVo.shipId}
      </if>
     <if test="reqVo.dockId != null">
      and t3.dock_id = #{reqVo.dockId}
      </if>
      <if test="reqVo.portId != null">
          and t3.port_id = #{reqVo.portId}
      </if>
        <if test="reqVo.routeId != null">
                  and t3.routeId = #{reqVo.routeId}
        </if>
      <if test="reqVo.direction != null">
                  and t3.direction = #{reqVo.direction}
        </if>
        order by t3.price asc
    </select>


</mapper>