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

import java.util.*;

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.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.product.dal.dataobject.productBase.ProductBaseDO;
import com.yc.ship.module.product.dal.dataobject.voyage.VoyageDO;
import com.yc.ship.module.product.dal.dataobject.voyagestock.VoyageStockDO;
import org.apache.ibatis.annotations.Mapper;
import com.yc.ship.module.product.controller.admin.voyagestock.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 航次库存 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface VoyageStockMapper extends BaseMapperX<VoyageStockDO> {

    @TenantIgnore
    default PageResult<VoyageStockDO> selectPage(VoyageStockPageReqVO reqVO) {
        /*return selectPage(reqVO, new LambdaQueryWrapperX<VoyageStockDO>()
                .likeIfPresent(VoyageStockDO::getVoyageName, reqVO.getVoyageName())
                .eqIfPresent(VoyageStockDO::getShipId, reqVO.getShipId())
                .inIfPresent(VoyageStockDO::getVoyageId, reqVO.getVoyageIds())
                .betweenIfPresent(VoyageStockDO::getCreateTime, reqVO.getVoyageTime()[0], reqVO.getVoyageTime()[1]+" 23:59:59")
                .orderByAsc(VoyageStockDO::getVoyageId));*/
        MPJLambdaWrapperX<VoyageStockDO> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(VoyageStockDO.class);
        wrapperX.leftJoin(VoyageDO.class, VoyageDO::getId, VoyageStockDO::getVoyageId);
        wrapperX.likeIfPresent(VoyageStockDO::getVoyageName, reqVO.getVoyageName())
                .eqIfPresent(VoyageStockDO::getShipId, reqVO.getShipId())
                .inIfPresent(VoyageStockDO::getVoyageId, reqVO.getVoyageIds())
                .betweenIfPresent(VoyageDO::getStartTime, reqVO.getVoyageTime()[0], reqVO.getVoyageTime()[1])
                .orderByAsc(VoyageDO::getStartTime);
        return selectJoinPage(reqVO, VoyageStockDO.class, wrapperX);
    }

    @TenantIgnore
    default void deleteByVoyageId(Long id) {
        delete(new LambdaQueryWrapperX<VoyageStockDO>().eq(VoyageStockDO::getVoyageId, id));
    }

    /**
     * 获取未上架过库存的航次ID
     * @param shipId
     * @return
     */
    @Select("select voyage_id from product_voyage_stock where deleted = 0 and voyage_id not in (select voyage_id from product_voyage_stock where shelfed_num <> 0 and ship_id = ${shipId})")
    @TenantIgnore
    List<Long> selectNoShelfdNumVoyageIdsByShipId(@Param("shipId") Long shipId);
    /**
     * 获取未上架过库存的航次ID
     * @return
     */
    @TenantIgnore
    List<Long> selectNoShelfdNumVoyageIdsByVoyageIds(@Param("shipId") Long shipId, @Param("voyageIds") List<Long> voyageIds);

    @TenantIgnore
    VoyageStockTotalVO selectTotal(@Param("pageReqVO") VoyageStockPageReqVO pageReqVO);
}