package com.yc.ship.module.product.dal.mysql.voyage;

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.product.controller.admin.voyage.vo.VoyageCalendarReqVO;
import com.yc.ship.module.product.controller.admin.voyage.vo.VoyagePageReqVO;
import com.yc.ship.module.product.controller.admin.voyage.vo.VoyageReqVO;
import com.yc.ship.module.product.controller.app.voyage.vo.AppVoyageDayRespVO;
import com.yc.ship.module.product.dal.dataobject.voyage.VoyageDO;
import com.yc.ship.module.product.enums.VoyageShelfStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 航次管理 Mapper
 *
 * @author lfy
 */
@Mapper
public interface VoyageMapper extends BaseMapperX<VoyageDO> {

    default PageResult<VoyageDO> selectPage(VoyagePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoyageDO>()
                .likeIfPresent(VoyageDO::getName, reqVO.getName())
                .eqIfPresent(VoyageDO::getCode, reqVO.getCode())
                .eqIfPresent(VoyageDO::getShipId, reqVO.getShipId())
                .eqIfPresent(VoyageDO::getRouteId, reqVO.getRouteId())
                .betweenIfPresent(VoyageDO::getBoardingTime, reqVO.getBoardingTime())
                .betweenIfPresent(VoyageDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(VoyageDO::getLeaveTime, reqVO.getLeaveTime())
                .eqIfPresent(VoyageDO::getType, reqVO.getType())
                .eqIfPresent(VoyageDO::getTransfer, reqVO.getTransfer())
                .eqIfPresent(VoyageDO::getShelfStatus, reqVO.getShelfStatus())
                .eqIfPresent(VoyageDO::getIsLock, reqVO.getIsLock())
                .betweenIfPresent(VoyageDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(VoyageDO::getId, reqVO.getVoyageIds())
                .orderByDesc(VoyageDO::getId));
    }

    default List<VoyageDO> selectListSimple(VoyageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoyageDO>()
                .eqIfPresent(VoyageDO::getShipId, reqVO.getShipId())
                .eqIfPresent(VoyageDO::getRouteId, reqVO.getRouteId())
                .eq(VoyageDO::getShelfStatus, VoyageShelfStatusEnum.ON_SHELF.getValue())
                .likeIfPresent(VoyageDO::getChannel, reqVO.getChannel())
                .orderByAsc(VoyageDO::getStartTime));
    }

    default List<VoyageDO> selectOnShelfListByShipId(Long shipId) {
        return selectList(new LambdaQueryWrapperX<VoyageDO>()
                .eq(VoyageDO::getShipId, shipId)
                .gt(VoyageDO::getStartTime, LocalDate.now())
                .eq(VoyageDO::getShelfStatus, VoyageShelfStatusEnum.ON_SHELF.getValue())
                .like(VoyageDO::getChannel, "1")
                .orderByAsc(VoyageDO::getStartTime));
    }

    List<VoyageDO> selectVovageList(@Param("shipId") Long shipId);

    /**
     * 删除该日期内当前游轮当前航线的所有航次
     * @param startDate
     * @param endDate
     * @param shipId
     * @param routeId
     */
    default void deleteByShipIdAndRouteId(LocalDate startDate, LocalDateTime endDate, Long shipId, Long routeId) {
        delete(new LambdaQueryWrapperX<VoyageDO>()
                .between(VoyageDO::getStartTime, startDate, endDate)
                .eq(VoyageDO::getShipId, shipId)
                .eq(VoyageDO::getRouteId, routeId));
    }

    /**
     * 获取资源管理-航次列表-日历
     * @param reqVO
     * @return
     */
    default List<VoyageDO> selectListCalendar(VoyageCalendarReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoyageDO>()
                .inIfPresent(VoyageDO::getShipId, reqVO.getShipIds())
                .betweenIfPresent(VoyageDO::getStartTime, reqVO.getStartDate(), reqVO.getEndDate())
                .eq(VoyageDO::getShelfStatus, VoyageShelfStatusEnum.ON_SHELF.getValue())
                .likeIfPresent(VoyageDO::getChannel, reqVO.getChannel())
                .orderByAsc(VoyageDO::getStartTime));
    }

    List<VoyageDO> selectListCalendar2(@Param("vo") VoyageCalendarReqVO reqVO);

    default List<VoyageDO> selectListByShipId(Long shipId) {
        return selectList(new LambdaQueryWrapperX<VoyageDO>()
                .eq(VoyageDO::getShipId, shipId)
                .orderByAsc(VoyageDO::getStartTime));
    }
    /**
     * 根据船Id和日期查询航次
     * @param shipId
     * @param date
     * @return
     */
    List<AppVoyageDayRespVO> selectVoyageListByShipIdAndDate(@Param("shipId") Long shipId, @Param("date") String date);

    /**
     * 批量统计航次的预定人数
     * @param voyageIds
     * @return Map<voyageId, count>
     */
    List<Map<String, Object>> countBatchByVoyageIds(@Param("voyageIds") List<Long> voyageIds);
}