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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.module.product.controller.admin.voyagestockdistribute.vo.QueryCanSelectRoomListReqVO;
import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageStockDistributeDO;
import org.apache.ibatis.annotations.Mapper;
import com.yc.ship.module.product.controller.admin.voyagestockdistribute.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 航次库存分配 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface VoyageStockDistributeMapper extends BaseMapperX<VoyageStockDistributeDO> {

    IPage selectPage(IPage<VoyageStockDistributeRespPageVO> page, @Param("params") VoyageStockDistributePageReqVO reqVO);


    default List<VoyageStockDistributeDO> selectListByVoyageId(Long voyageId) {
        return selectList(new LambdaQueryWrapperX<VoyageStockDistributeDO>()
                .eq(VoyageStockDistributeDO::getVoyageId, voyageId));
    }

    @Select("select distributor_type,distributor_id,store_id,sum(num) num,sum(room_model_num) room_model_num, room_nums from product_voyage_stock_distribute where voyage_id = #{voyageId} and deleted = 0 GROUP BY distributor_type,distributor_id,store_id")
    List<VoyageStockDistributeTotalRespVO.TotalGroupByDistributorVO> selectListGroupByDistributor(Long voyageId);

    @Select("select room_model_id,floor,sum(num) num from product_voyage_stock_distribute where voyage_id = #{voyageId} and deleted = 0 and distributor_id is null and store_id is null GROUP BY room_model_id,floor")
    List<VoyageStockDistributeTotalRespVO.TotalGroupByRoomModelFloorVO> selectListGroupByRoomModelFloor(Long voyageId);

    default void deleteByVoyageId(Long getVoyageId) {
        delete(new LambdaQueryWrapperX<VoyageStockDistributeDO>()
                .eq(VoyageStockDistributeDO::getVoyageId, getVoyageId));
    }

    default List<VoyageStockDistributeDO> selectList(QueryCanSelectRoomListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoyageStockDistributeDO>()
                .eqIfPresent(VoyageStockDistributeDO::getDistributorId, reqVO.getDistributorId())
                .eqIfPresent(VoyageStockDistributeDO::getStoreId, reqVO.getStoreId())
                .eqIfPresent(VoyageStockDistributeDO::getVoyageId, reqVO.getVoyageId())
                .inIfPresent(VoyageStockDistributeDO::getRoomModelId, reqVO.getRoomModelIds())
                .inIfPresent(VoyageStockDistributeDO::getFloor, reqVO.getFloors())
        );
    }

    default List<VoyageStockDistributeDO> selectDetailList(VoyageStockDistributeReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoyageStockDistributeDO>()
                .eqIfPresentOrIsNull(VoyageStockDistributeDO::getDistributorId, reqVO.getDistributorId())
                .eqIfPresentOrIsNull(VoyageStockDistributeDO::getStoreId, reqVO.getStoreId())
                .eqIfPresent(VoyageStockDistributeDO::getVoyageId, reqVO.getVoyageId())
                .eqIfPresent(VoyageStockDistributeDO::getShipId, reqVO.getShipId())
        );
    }
}