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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRoutePageReqVO;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRoutePriceVO;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRouteRespVO;
import com.yc.ship.module.resource.dal.dataobject.route.ResourceRouteDO;
import com.yc.ship.module.resource.enums.CommonStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import com.yc.ship.module.resource.controller.admin.route.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 航线管理 Mapper
 *
 * @author lfy
 */
@Mapper
public interface ResourceRouteMapper extends BaseMapperX<ResourceRouteDO> {

    default PageResult<ResourceRouteDO> selectPage(ResourceRoutePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ResourceRouteDO>()
                .likeIfPresent(ResourceRouteDO::getName, reqVO.getName())
                .eqIfPresent(ResourceRouteDO::getDirection, reqVO.getDirection())
                .eqIfPresent(ResourceRouteDO::getOnDockId, reqVO.getOnDockId())
                .eqIfPresent(ResourceRouteDO::getLeaveDockId, reqVO.getLeaveDockId())
                .eqIfPresent(ResourceRouteDO::getMidwayDockIds, reqVO.getMidwayDockIds())
                .eqIfPresent(ResourceRouteDO::getSort, reqVO.getSort())
                .eqIfPresent(ResourceRouteDO::getStatus, reqVO.getStatus())
                .orderByAsc(ResourceRouteDO::getSort)
                .orderByDesc(ResourceRouteDO::getId));
    }
    IPage<AppResourceRouteRespVO> selectAppRoutePage(IPage<AppResourceRouteRespVO> ipage, @Param("reqVo") AppResourceRoutePageReqVO reqVo);

    default List<ResourceRouteDO> selectEnableList() {
        return selectList(new LambdaQueryWrapperX<ResourceRouteDO>()
                .eq(ResourceRouteDO::getStatus, CommonStatusEnum.ENABLE.getStatus()));
    }

    @Select("select min(price) min_price, max(price) max_price from product_price_room_model where deleted = 0 and object_id in (select id from product_price_voyage where route_id = #{routeId} and ship_id = #{shipId})")
    AppResourceRoutePriceVO selectMinAndMaxPrice(@Param("routeId") Long routeId, @Param("shipId") Long shipId);

    default PageResult<ResourceRouteDO> selectAppPageNew(AppResourceRoutePageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<ResourceRouteDO>()
                .likeIfPresent(ResourceRouteDO::getName, pageReqVO.getKeyword())
                .eq(ResourceRouteDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .orderByAsc(ResourceRouteDO::getSort)
        );
    }
}