package com.yc.ship.module.product.service.voyagestockdistribute;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.product.controller.admin.voyagestock.vo.VoyageStockPageReqVO;
import com.yc.ship.module.product.controller.admin.voyagestock.vo.VoyageStockRespVO;
import com.yc.ship.module.product.controller.admin.voyagestockdistributeNew.vo.*;
import com.yc.ship.module.product.controller.app.voyage.vo.AppCanSelectRoomReqVO;
import com.yc.ship.module.product.dal.dataobject.voyagestock.VoyageStockRoomUsedDO;
import com.yc.ship.module.product.dal.dataobject.voyagestockdetail.VoyageStockDetailDO;
import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageStockDistributeNewDO;
import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageStockDistributeRoomDO;
import com.yc.ship.module.product.dal.mysql.voyagestock.VoyageStockRoomUsedMapper;
import com.yc.ship.module.product.dal.mysql.voyagestockdistribute.VoyageStockDistributeNewMapper;
import com.yc.ship.module.product.enums.DistributorOrStoreEnum;
import com.yc.ship.module.product.enums.YesOrNoEnum;
import com.yc.ship.module.product.framework.lock.ProductRedisKeyConstants;
import com.yc.ship.module.product.service.voyagestock.VoyageStockService;
import com.yc.ship.module.product.service.voyagestockdetail.VoyageStockDetailService;
import com.yc.ship.module.resource.api.room.RoomApi;
import com.yc.ship.module.resource.api.room.dto.RoomRespDTO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.yc.ship.framework.security.core.util.SecurityFrameworkUtils.getLoginDistributorId;


/**
 * 航次库存分配 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
@Slf4j
public class VoyageStockDistributeNewServiceImpl implements VoyageStockDistributeNewService {

    @Resource
    private VoyageStockDistributeNewMapper voyageStockDistributeNewMapper;

    @Resource
    private VoyageStockDistributeRoomService voyageStockDistributeRoomService;

    @Resource
    private RoomApi roomApi;

    @Resource
    private ResourceRoomModelService resourceRoomModelService;

    @Resource
    private VoyageStockDetailService voyageStockDetailService;
    @Resource
    private RedissonClient redissonClient;

    @Resource
    private VoyageStockService voyageStockService;


    @Resource
    private VoyageStockRoomUsedMapper voyageStockRoomUsedMapper;


    @Override
    public void createVoyageStockDistributeNew(Long voyageId, Integer type, List<VoyageStockDistributeNewSaveReqVO> createReqVO) {
        BigDecimal zero = BigDecimal.ZERO;
        //现在给整个航次的库存加锁
        String lockKey = String.format(ProductRedisKeyConstants.STOCK_REDIS_KEY_PREFIX, voyageId);
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock(60, TimeUnit.SECONDS);
            //删除分配的房间
            voyageStockDistributeRoomService.deleteByVoyageId(voyageId, type);
            // 分配的房间列表
            List<VoyageStockDistributeRoomDO> roomList = new ArrayList<>();
            createReqVO.stream().forEach(item -> {
                if(!CollectionUtils.isAnyEmpty(item.getRoomList())) {
                    roomList.addAll(BeanUtils.toBean(item.getRoomList(), VoyageStockDistributeRoomDO.class));
                }
            });
            voyageStockDistributeRoomService.createVoyageStockDistributeRoomBatch(roomList);
            //修改或插入库存表
            List<VoyageStockDistributeNewDO> list = voyageStockDistributeNewMapper.selectListByVoyageId(voyageId, type);
            if(CollectionUtils.isAnyEmpty(list)) { // 为空则新增
                list = BeanUtils.toBean(createReqVO, VoyageStockDistributeNewDO.class);
                list.stream().forEach(item -> {
                    item.setBookNum(zero);
                });
                if(!CollectionUtils.isAnyEmpty(list)) {
                    voyageStockDistributeNewMapper.insertBatch(list);
                }
            }else {
                Map<String, BigDecimal> map = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor(), item -> item.getNum());
                Map<String, VoyageStockDistributeNewSaveReqVO> mapVO = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
                List<String> existList = CollectionUtils.convertList(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
                Map<String, VoyageStockDistributeNewDO> existMap = CollectionUtils.convertMap(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
                List<VoyageStockDistributeNewDO> insertList = new ArrayList<>();// 插入数据
                List<VoyageStockDistributeNewDO> updateList = new ArrayList<>(); //  修改数据
                map.keySet().stream().forEach(key -> {
                    if(existList.contains(key)) { //存在，做修改
                        VoyageStockDistributeNewDO voyageStockDistributeNewDO = existMap.get(key);
                        voyageStockDistributeNewDO.setNum(map.get(key));
                        voyageStockDistributeNewDO.setRoomModelNum(mapVO.get(key).getRoomModelNum());
                        updateList.add(voyageStockDistributeNewDO);
                    }else { //不存在，做插入
                        VoyageStockDistributeNewDO voyageStockDistributeNewDO = BeanUtils.toBean(mapVO.get(key), VoyageStockDistributeNewDO.class);
                        insertList.add(voyageStockDistributeNewDO);
                    }
                });
                if(!CollectionUtils.isAnyEmpty(updateList)) {
                    voyageStockDistributeNewMapper.updateBatch(updateList);
                }
                if(!CollectionUtils.isAnyEmpty(insertList)) {
                    voyageStockDistributeNewMapper.insertBatch(insertList);
                }
            }
            BigDecimal distributeNum = getDistributeNum(voyageId);
            List<VoyageStockDistributeNewDO> newList = voyageStockDistributeNewMapper.selectListByVoyageId(voyageId, type);
            voyageStockDetailService.handleShareNum(voyageId,  distributeNum, newList);
        }finally {
            if(lock.isHeldByCurrentThread() && lock.isLocked()){
                lock.unlock();
            }
        }
    }

    @Override
    public List<VoyageStockDistributeNewRespVO> getByVoyageId(Long voyageId, Integer type) {
        List<VoyageStockDistributeNewDO> list = voyageStockDistributeNewMapper.selectListByVoyageId(voyageId, type);
        List<VoyageStockDistributeNewRespVO> respList = BeanUtils.toBean(list, VoyageStockDistributeNewRespVO.class);
        List<VoyageStockDistributeRoomDO> roomList = voyageStockDistributeRoomService.getListByVoyageId(voyageId, type);
        Map<String, List<VoyageStockDistributeRoomDO>> map = CollectionUtils.convertMultiMap(roomList, (item) -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
        respList.stream().forEach(item -> {
            item.setRoomList(BeanUtils.toBean(map.get(item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor()),  VoyageStockDistributorRoomRespVO.class));
        });
        return respList;
    }

    @Override
    public List<RoomRespDTO> getRoomList(Long voyageId, Long shipId) {
        //获取该游轮的所有房间
        List<RoomRespDTO> roomList = roomApi.getRoomList(shipId);
        // 获取已被分配的房间
        List<VoyageStockDistributeRoomDO> distributeRoomList = voyageStockDistributeRoomService.getListByVoyageId(voyageId, DistributorOrStoreEnum.DISTRIBUTOR.getValue());
        List<Long> selectedRoomIds = CollectionUtils.convertList(distributeRoomList, VoyageStockDistributeRoomDO::getRoomId);
        //获取已被使用的房间
        List<VoyageStockRoomUsedDO> roomUsedList = voyageStockRoomUsedMapper.selectListByVoyageId(voyageId);
        List<Long> usedRoomIds = CollectionUtils.convertList(roomUsedList, VoyageStockRoomUsedDO::getRoomId);
        roomList.stream().forEach(item -> {
            item.setIsSelected(selectedRoomIds.contains(item.getId()));
            item.setDisabled(false);//当前全部都可操作
            if(usedRoomIds.contains(item.getId())) {
                item.setDisabled(true);
            }else {
                item.setDisabled(false);
            }
        });
        return roomList;
    }

    @Override
    public List<RoomRespDTO> getCanSelectRoomList(QueryCanSelectRoomListNewReqVO reqVO) {
        //获取当前分销商选择的房型和楼层对象的房间列表
        Integer type = reqVO.getType();
        //TODO:此处当前默认为1分销商，暂无门店
        type = DistributorOrStoreEnum.DISTRIBUTOR.getValue();
        List<VoyageStockDistributeRoomDO> roomList;
        if(type.equals(DistributorOrStoreEnum.DISTRIBUTOR.getValue())) {
            roomList = voyageStockDistributeRoomService.getRoomListByRoomModelIdAndFloor(reqVO.getDistributorId(), reqVO.getVoyageId(), reqVO.getRoomModelIdFloorList());
        }else if(type.equals(DistributorOrStoreEnum.STORE.getValue())) {
            roomList = voyageStockDistributeRoomService.getRoomListByRoomModelIdAndFloor(reqVO.getStoreId(), reqVO.getVoyageId(), reqVO.getRoomModelIdFloorList());
        }else {
            roomList = new ArrayList<>();
        }
        List<Long> roomIds = CollectionUtils.convertList(roomList, VoyageStockDistributeRoomDO::getRoomId);
        Long loginDistributorId = getLoginDistributorId();
        if(CollectionUtils.isAnyEmpty(roomIds) && loginDistributorId == null) {
            List<RoomRespDTO> canSelectRoomList = roomApi.getCanSelectRoomList(reqVO.getShipId(), reqVO.getRoomModelIdFloorList());
            roomIds = CollectionUtils.convertList(canSelectRoomList, RoomRespDTO::getId);
        }
        //获取完全被使用的房间
        List<VoyageStockRoomUsedDO> allUsedList = voyageStockRoomUsedMapper.selectAllUsedList(reqVO.getVoyageId());
        List<Long> allUserRoomId = CollectionUtils.convertList(allUsedList, VoyageStockRoomUsedDO::getRoomId);
        //过滤掉完全被使用的房间
        List<Long> canSelectRoomIds = roomIds.stream().filter(item -> !allUserRoomId.contains(item)).collect(Collectors.toList());
        List<RoomRespDTO> roomRespList = roomApi.getRoomListByIds(canSelectRoomIds);
        // 获取使用一半的房间
        List<VoyageStockRoomUsedDO> halfUsedList = voyageStockRoomUsedMapper.selectHalfUsedRoom(reqVO.getVoyageId());
        List<Long> halfUsedRoomIds = CollectionUtils.convertList(halfUsedList, VoyageStockRoomUsedDO::getRoomId);
        roomRespList.stream().forEach(item -> {
           if(halfUsedRoomIds.contains(item.getId())) {
               item.setNum(BigDecimal.valueOf(0.5));
           }else {
               item.setNum(BigDecimal.ONE);
           }
        });
        return roomRespList;
    }

    @Override
    public PageResult<VoyageStockDistributeNewPageRespVO> getPage(VoyageStockDistributeNewPageReqVO reqVO) {
        VoyageStockPageReqVO pageParams = BeanUtils.toBean(reqVO, VoyageStockPageReqVO.class);
        PageResult<VoyageStockRespVO> voyageStockPage = voyageStockService.getVoyageStockPage(pageParams);
        List<VoyageStockRespVO> list = voyageStockPage.getList();
        List<Long> voyageIds = CollectionUtils.convertList(list, VoyageStockRespVO::getVoyageId);
        List<VoyageStockDistributeNewPageRespVO> distributeNewList = voyageStockDistributeNewMapper.selectGroupList(voyageIds, reqVO);
        Map<Long, BigDecimal> idNumMap = CollectionUtils.convertMap(distributeNewList, VoyageStockDistributeNewPageRespVO::getVoyageId, VoyageStockDistributeNewPageRespVO::getNum);
        Map<Long, BigDecimal> idRoomModelNumMap = CollectionUtils.convertMap(distributeNewList, VoyageStockDistributeNewPageRespVO::getVoyageId, VoyageStockDistributeNewPageRespVO::getRoomModelNum);
        List<VoyageStockDistributeNewPageRespVO> resultList = new ArrayList<>();
        list.forEach(item -> {
            BigDecimal num = idNumMap.get(item.getVoyageId());
            BigDecimal roomModelNum = idRoomModelNumMap.get(item.getVoyageId());
            VoyageStockDistributeNewPageRespVO respVO = new VoyageStockDistributeNewPageRespVO();
            respVO.setVoyageId(item.getVoyageId());
            respVO.setVoyageName(item.getVoyageName());
            respVO.setShipId(item.getShipId());
            respVO.setShipName(item.getShipName());
            if(num != null) {
                respVO.setNum(num);
                respVO.setEnableShare(YesOrNoEnum.NO.getType());
            }else {
                respVO.setNum(item.getShareNum());
                respVO.setEnableShare(YesOrNoEnum.YES.getType());
            }
            if(roomModelNum != null) {
                respVO.setRoomModelNum(roomModelNum);
            }else {
                respVO.setRoomModelNum(BigDecimal.ZERO);
            }
            resultList.add(respVO);
        });
        return new PageResult<>(resultList, voyageStockPage.getTotal());
    }

    @Override
    public List<VoyageStockDistributeNewRespVO> getDetail(VoyageStockDistributeNewDetailReqVO reqVO) {
        Integer enableShare = reqVO.getEnableShare();
        if(enableShare!=null && enableShare.equals(YesOrNoEnum.YES.getType())) {
            return getShareStockDetail(reqVO);
        }else {
            return getDistributorStockDetail(reqVO);
        }
    }

    /**
     * 获取共享库存详情
     * @param reqVO
     */
    private List<VoyageStockDistributeNewRespVO> getShareStockDetail(VoyageStockDistributeNewDetailReqVO reqVO) {
        List<VoyageStockDistributeNewRespVO> list = new ArrayList<>();
        // 获取所有库存
        List<VoyageStockDetailDO> stockDetailDOList = voyageStockDetailService.getListByVoyageId(reqVO.getVoyageId());
        //该处注释，按要求库存为0的也要放出来
//        List<VoyageStockDetailDO> stockDetailDOList1 = stockDetailDOList.stream().filter(item -> item.getCanSellNum().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
        //减去已分配的库存，剩下的就是共享库存
        List<VoyageStockDistributeNewDO> distributeNewList = voyageStockDistributeNewMapper.selectListByVoyageId(reqVO.getVoyageId(), reqVO.getType());
        Map<String, BigDecimal> distributeNewMap = CollectionUtils.convertMap(distributeNewList, item -> item.getRoomModelId() + "_" + item.getFloor(), item -> item.getNum());

        stockDetailDOList.forEach(item -> {
            BigDecimal bigDecimal = distributeNewMap.get(item.getRoomModelId() + "_" + item.getFloor());
            if (bigDecimal == null) {
                bigDecimal = BigDecimal.ZERO;
            }
            VoyageStockDistributeNewRespVO respVO = new VoyageStockDistributeNewRespVO();
            respVO.setNum(item.getCanSellNum().subtract(bigDecimal))
                    .setFloor(item.getFloor())
                    .setRoomModelId(item.getRoomModelId())
                    .setRoomModelName(item.getRoomModelName());
            list.add(respVO);
        });
       return list;
    }

    /**
     * 获取分销商库存详情
     * @param reqVO
     */
    private List<VoyageStockDistributeNewRespVO> getDistributorStockDetail(VoyageStockDistributeNewDetailReqVO reqVO) {
        List<VoyageStockDistributeNewDO> list = voyageStockDistributeNewMapper.selectListByVoyageIdAndObjectId(reqVO);
        List<VoyageStockDistributeNewRespVO> respVO = BeanUtils.toBean(list, VoyageStockDistributeNewRespVO.class);
        List<VoyageStockDistributeRoomDO> roomList = voyageStockDistributeRoomService.getListByVoyageIdAndObjectId(reqVO);
        List<VoyageStockDistributorRoomRespVO> roomRespList = BeanUtils.toBean(roomList, VoyageStockDistributorRoomRespVO.class);
        List<Long> roomIds = CollectionUtils.convertList(roomRespList, VoyageStockDistributorRoomRespVO::getRoomId);
        List<RoomRespDTO> roomListByIds = roomApi.getRoomListByIds(roomIds);
        Map<Long, RoomRespDTO> roomMap = CollectionUtils.convertMap(roomListByIds, RoomRespDTO::getId);
        roomRespList.forEach(item -> {
            if(roomMap.get(item.getRoomId()) != null) {
                item.setRoomNum(roomMap.get(item.getRoomId()).getRoomNum());
            }else {
                item.setRoomNum(0);
            }
        });
        Map<String, List<VoyageStockDistributorRoomRespVO>> map = CollectionUtils.convertMultiMap(roomRespList, (item) -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());

        List<Long> roomModelIds = CollectionUtils.convertList(respVO, VoyageStockDistributeNewRespVO::getRoomModelId);
        List<ResourceRoomModelDO> roomModelList = resourceRoomModelService.getList(roomModelIds);
        Map<Long, ResourceRoomModelDO> roomModelDOMap = CollectionUtils.convertMap(roomModelList, ResourceRoomModelDO::getId);
        respVO.forEach(item -> {
            if(roomModelDOMap.get(item.getRoomModelId()) == null) {
                item.setRoomModelName("已删除");
            }else {
                item.setRoomModelName(roomModelDOMap.get(item.getRoomModelId()).getName());
            }
            item.setRoomList(map.get(item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor()));
        });
        return respVO;
    }

    @Override
    public List<RoomRespDTO> getDistributorRoomList(Long voyageId, Long shipId, Long distributorId) {
        //获取该游轮的所有房间
        List<RoomRespDTO> roomList = roomApi.getRoomList(shipId);
        // 获取当前分销商已被分配的房间
        VoyageStockDistributeNewDetailReqVO reqVO = new VoyageStockDistributeNewDetailReqVO();
        reqVO.setVoyageId(voyageId);
        reqVO.setObjectId(distributorId);
        reqVO.setType(DistributorOrStoreEnum.DISTRIBUTOR.getValue());
        List<VoyageStockDistributeRoomDO> distributeRoomList = voyageStockDistributeRoomService.getListByVoyageIdAndObjectId(reqVO);
        List<Long> selectedRoomIds = CollectionUtils.convertList(distributeRoomList, VoyageStockDistributeRoomDO::getRoomId);
        roomList = roomList.stream().filter(item -> selectedRoomIds.contains(item.getId())).collect(Collectors.toList());

        //获取已被门店选择的房间
        List<VoyageStockDistributeRoomDO> storeRoomList = voyageStockDistributeRoomService.getListByVoyageId(voyageId, DistributorOrStoreEnum.STORE.getValue());
        List<Long> selectedStoreRoomIds = CollectionUtils.convertList(storeRoomList, VoyageStockDistributeRoomDO::getRoomId);
        //获取已被使用的房间
        List<VoyageStockRoomUsedDO> roomUsedList = voyageStockRoomUsedMapper.selectListByVoyageId(voyageId);
        List<Long> usedRoomIds = CollectionUtils.convertList(roomUsedList, VoyageStockRoomUsedDO::getRoomId);
        roomList.forEach(item -> {
            item.setIsSelected(selectedStoreRoomIds.contains(item.getId()));
            item.setDisabled(usedRoomIds.contains(item.getId()));
        });
        return roomList;
    }

    @Override
    public BigDecimal getDistributeNum(Long voyageId) {
        return voyageStockDistributeNewMapper.selectTotalDistributeNum(voyageId);
    }

    @Override
    public List<RoomRespDTO> getAppCanSelectRoomList(AppCanSelectRoomReqVO reqVO) {
        Long distributeId = reqVO.getDistributeId();
        if(distributeId != null) {
            return getAppDistributorCanSelectRoomList(reqVO);
        } else {
            return getAppShareCanSelectRoomList(reqVO);
        }
    }

    /**
     * 获取共享房间可以选择的房间列表
     * @param reqVO
     * @return
     */
    private List<RoomRespDTO> getAppShareCanSelectRoomList(AppCanSelectRoomReqVO reqVO) {
        //获取该游轮的所有房间
        List<RoomRespDTO> roomList = roomApi.getRoomList(reqVO.getShipId());
        Integer floor = reqVO.getFloor();
        if(floor != null) {
            //获取当前楼层的房间
            roomList = roomList.stream().filter(item -> item.getFloors().equals(floor)).collect(Collectors.toList());
        }
        Long roomModelId = reqVO.getRoomModelId();
        if(roomModelId != null) {
            roomList = roomList.stream().filter(item -> item.getRoomModelId().equals(roomModelId)).collect(Collectors.toList());
        }
        // 获取已被分配的房间
        VoyageStockDistributeNewDetailReqVO queryVO = new VoyageStockDistributeNewDetailReqVO();
        queryVO.setVoyageId(reqVO.getVoyageId());
        queryVO.setType(DistributorOrStoreEnum.DISTRIBUTOR.getValue());
        List<VoyageStockDistributeRoomDO> distributeRoomList = voyageStockDistributeRoomService.getListByVoyageIdAndObjectId(queryVO);
        List<Long> selectedRoomIds = CollectionUtils.convertList(distributeRoomList, VoyageStockDistributeRoomDO::getRoomId);
        //过滤掉已被分配的房间
        roomList = roomList.stream().filter(item -> !selectedRoomIds.contains(item.getId())).collect(Collectors.toList());

        //获取已被门店选择的房间
        List<VoyageStockDistributeRoomDO> storeRoomList = voyageStockDistributeRoomService.getListByVoyageId(reqVO.getVoyageId(), DistributorOrStoreEnum.STORE.getValue());
        List<Long> selectedStoreRoomIds = CollectionUtils.convertList(storeRoomList, VoyageStockDistributeRoomDO::getRoomId);
        roomList = roomList.stream().filter(item -> !selectedStoreRoomIds.contains(item.getId())).collect(Collectors.toList());
        //获取已被使用的房间
        List<VoyageStockRoomUsedDO> roomUsedList = voyageStockRoomUsedMapper.selectListByVoyageId(reqVO.getVoyageId());
        List<Long> usedRoomIds = CollectionUtils.convertList(roomUsedList, VoyageStockRoomUsedDO::getRoomId);
        //过滤掉已被使用的房间
        roomList = roomList.stream().filter(item -> !usedRoomIds.contains(item.getId())).collect(Collectors.toList());
        roomList.forEach(item -> {
            item.setNum(BigDecimal.ONE);
        });
        return roomList;
    }

    /**
     * 获取分销商可以选择的房间列表
     * @param reqVO
     * @return
     */
    private List<RoomRespDTO> getAppDistributorCanSelectRoomList(AppCanSelectRoomReqVO reqVO) {
        //获取当前分销商选择的房型和楼层对象的房间列表
        //TODO:此处当前默认为1分销商，暂无门店
        List<VoyageStockDistributeRoomDO> roomList  = voyageStockDistributeRoomService.getAppCanSelectRoomList(reqVO);
        List<Long> roomIds = CollectionUtils.convertList(roomList, VoyageStockDistributeRoomDO::getRoomId);
        //获取完全被使用的房间
        List<VoyageStockRoomUsedDO> allUsedList = voyageStockRoomUsedMapper.selectAllUsedList(reqVO.getVoyageId());
        List<Long> allUserRoomId = CollectionUtils.convertList(allUsedList, VoyageStockRoomUsedDO::getRoomId);
        //过滤掉完全被使用的房间
        List<Long> canSelectRoomIds = roomIds.stream().filter(item -> !allUserRoomId.contains(item)).collect(Collectors.toList());
        List<RoomRespDTO> roomRespList = roomApi.getRoomListByIds(canSelectRoomIds);
        // 获取使用一半的房间
        List<VoyageStockRoomUsedDO> halfUsedList = voyageStockRoomUsedMapper.selectHalfUsedRoom(reqVO.getVoyageId());
        List<Long> halfUsedRoomIds = CollectionUtils.convertList(halfUsedList, VoyageStockRoomUsedDO::getRoomId);
        roomRespList.forEach(item -> {
            if(halfUsedRoomIds.contains(item.getId())) {
                item.setNum(BigDecimal.valueOf(0.5));
            }else {
                item.setNum(BigDecimal.ONE);
            }
        });
        return roomRespList;
    }
}