<?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.room.ResourceRoomMapper">

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


    <select id="selectRoomListByRoomModelIdAndFloor"
            resultType="com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO">
        select * from resource_room
        where deleted = 0
        and ship_id = #{shipId}
        and concat(room_model_id, '_', floors) in
        <foreach item="item" collection="roomModelIdFloorList" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>
    </select>


    <select id="selectAllByShipIdAndFloors"
            resultType="com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomRespVO">
        select t1.id            as id,
               t1.room_num      as roomNum,
               t1.ship_id       as shipId,
               t1.room_model_id as roomModelId,
               t2.name          as roomModelName,
               t1.floors        as floors,
               CASE
                   WHEN t5.status IS NULL THEN
                       CASE
                           WHEN t3.name IS NOT NULL THEN 3
                           ELSE 0
                           END
                   ELSE t5.status
                   END          as status,
               group_concat(concat(t3.name,'-',coalesce(t3.nationality,''),'-',coalesce(t3.mobile,'')) separator ',') as visitorName

        from resource_room t1
                 left join resource_room_model t2 on t1.room_model_id = t2.id
                 left join (select tv.*
                            from trade_visitor tv
                                     join trade_order to1 on tv.order_id = to1.id
                            where to1.voyage_id = #{voyageId}) t3
                           on if(t3.final_room_id is null,t3.init_room_id,t3.final_room_id)= t1.id
                 left join buss_room_status t5 on t1.id = t5.room_id and t5.voyage_id= #{voyageId}
        where t1.ship_id = #{shipId}
          and t1.deleted = 0
          and t1.floors = #{floors}
        group by t1.id, t1.room_num, t1.ship_id, t1.room_model_id, t2.name, t1.floors ,CASE
                                                                                           WHEN t5.status IS NULL THEN
                                                                                               CASE
                                                                                                   WHEN t3.name IS NOT NULL THEN 3
                                                                                                   ELSE 0
                                                                                                   END
                                                                                           ELSE t5.status
            END
        order by t1.room_num
    </select>

    <select id="selectFreeRoomList" resultType="com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO">
        select t1.id            as id,
        t1.room_num      as roomNum,
        t1.room_model_id as roomModelId,
        t1.floors as floors,
        group_concat(t3.gender separator ',') as genders,
        count(t3.id) as count
        from resource_room t1
        left join (select tv.* from trade_visitor tv
        join trade_order to1 on tv.order_id = to1.id where to1.voyage_id = #{voyageId}) t3
        on if(t3.final_room_id is null,t3.init_room_id,t3.final_room_id)= t1.id
        where t1.ship_id = (select ship_id from product_voyage where id=#{voyageId})
        <if test="roomId!=null" >and t1.id=#{roomId}</if>

        group by t1.id, t1.room_num, t1.room_model_id, t1.floors having count(t3.id)&lt;2
        ORDER BY t1.room_num
    </select>
</mapper>