package com.yc.ship.module.resource.dal.mysql.room;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.resource.api.ship.dto.RoomModelFloorNumDTO;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomPageReqVO;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomRespVO;
import com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源管理-游轮房间管理 Mapper
 *
 * @author lfy
 */
@Mapper
public interface ResourceRoomMapper extends BaseMapperX<ResourceRoomDO> {

    default PageResult<ResourceRoomDO> selectPage(ResourceRoomPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ResourceRoomDO>()
                .eqIfPresent(ResourceRoomDO::getRoomNum, reqVO.getRoomNum())
                .eqIfPresent(ResourceRoomDO::getRoomModelId, reqVO.getRoomModelId())
                .eqIfPresent(ResourceRoomDO::getShipId, reqVO.getShipId())
                .eqIfPresent(ResourceRoomDO::getFloors, reqVO.getFloors())
                .eqIfPresent(ResourceRoomDO::getStatus, reqVO.getStatus())
                .orderByDesc(ResourceRoomDO::getId));
    }

    default List<ResourceRoomDO> selectByShipId(String shipId) {
        return selectList(new LambdaQueryWrapperX<ResourceRoomDO>()
                .eqIfPresent(ResourceRoomDO::getShipId, shipId));
    }

    default ResourceRoomDO selectByShipIdAndRoomNums(Integer roomNum, Long shipId) {
        return selectOne(new LambdaQueryWrapperX<ResourceRoomDO>()
                .eqIfPresent(ResourceRoomDO::getRoomNum, roomNum)
                .eqIfPresent(ResourceRoomDO::getShipId, shipId));
    }

    default List<ResourceRoomDO> selectByShipIdAndFloors(String shipId, List<Integer> floorList) {
        return selectList(new LambdaQueryWrapperX<ResourceRoomDO>()
                .eqIfPresent(ResourceRoomDO::getShipId, shipId)
                .inIfPresent(ResourceRoomDO::getFloors, floorList));
    }

    default List<String> selectRoomNumListByShipId(Long shipId, List<String> roomNums) {
        List<ResourceRoomDO> list = selectList(new LambdaQueryWrapperX<ResourceRoomDO>()
                .eq(ResourceRoomDO::getShipId, shipId)
                .inIfPresent(ResourceRoomDO::getRoomNum, roomNums));
        return list.stream().map(ResourceRoomDO::getRoomNum).map(String::valueOf).collect(Collectors.toList());
    }

    @Select("select t1.room_model_id as roomModelId, t1.floors as floor, count(t1.id) as num, t2.name as room_model_name,t2.short_name as  shortName from resource_room t1 left JOIN resource_room_model t2 on t1.room_model_id = t2.id where t1.ship_id = #{shipId} and t2.can_sale = 1 and t1.deleted = 0 and t2.deleted = 0 group by t1.room_model_id, t1.floors ORDER BY t2.id ASC,t1.floors ASC")
    List<RoomModelFloorNumDTO> selectRoomModelFloorToNum(Long shipId);

    List<ResourceRoomDO> selectRoomListByRoomModelIdAndFloor(@Param("shipId") Long shipId, @Param("roomModelIdFloorList") List<String> roomModelIdFloorList);

    /**
     * 查询所有空闲客房
     * @param voyageId
     * @param roomId
     * @return
     */
    List<ResourceRoomDO> selectFreeRoomList(@Param("voyageId") String voyageId, @Param("roomId") String roomId);

    List<ResourceRoomRespVO> selectAllByShipIdAndFloors(@Param("shipId") String shipId, @Param("voyageId") String voyageId, @Param("floors") String floors);

}