package com.yc.ship.module.resource.api.room;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.api.room.dto.RoomRespDTO;
import com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.service.room.ResourceRoomService;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class RoomApiImpl implements RoomApi {

    @Resource
    private ResourceRoomService resourceRoomService;

    @Resource
    private ResourceRoomModelService resourceRoomModelService;
    @Override
    public List<RoomRespDTO> getRoomList(Long shipId) {
        List<ResourceRoomDO> list = resourceRoomService.getRoomListByShipId(String.valueOf(shipId));
        List<RoomRespDTO> respList = BeanUtils.toBean(list, RoomRespDTO.class);
        List<Long> roomModelIds = CollectionUtils.convertList(respList, RoomRespDTO::getRoomModelId);
        List<ResourceRoomModelDO> roomModelList = resourceRoomModelService.getList(roomModelIds);
        Map<Long, ResourceRoomModelDO> roomModelDOMap = CollectionUtils.convertMap(roomModelList, ResourceRoomModelDO::getId);
        respList.stream().forEach(item -> {
            if(roomModelDOMap.get(item.getRoomModelId()) != null) {
                item.setRoomModelName(roomModelDOMap.get(item.getRoomModelId()).getName());
            }else {
                item.setRoomModelName("已删除");
            }
            item.setIsSelected(false);
        });
        return respList;
    }

    @Override
    public List<RoomRespDTO> getRoomListByIds(List<Long> ids) {
        if(CollectionUtils.isAnyEmpty(ids)) {
            return Collections.emptyList();
        }
        List<ResourceRoomDO> list = resourceRoomService.getRoomListByIds(ids);
        List<RoomRespDTO> respList = BeanUtils.toBean(list, RoomRespDTO.class);
        List<Long> roomModelIds = CollectionUtils.convertList(respList, RoomRespDTO::getRoomModelId);
        List<ResourceRoomModelDO> roomModelList = resourceRoomModelService.getList(roomModelIds);
        Map<Long, ResourceRoomModelDO> roomModelDOMap = CollectionUtils.convertMap(roomModelList, ResourceRoomModelDO::getId);
        respList.stream().forEach(item -> {
            if(roomModelDOMap.get(item.getRoomModelId()) != null) {
                item.setRoomModelName(roomModelDOMap.get(item.getRoomModelId()).getName());
            }else {
                item.setRoomModelName("已删除");
            }
            item.setIsSelected(false);
        });
        return respList;
    }

    @Override
    public List<RoomRespDTO> getCanSelectRoomList(Long shipId, List<String> roomModelIdFloorList) {
        List<ResourceRoomDO> list = resourceRoomService.getRoomListByRoomModelIdAndFloor(shipId, roomModelIdFloorList);
        return BeanUtils.toBean(list, RoomRespDTO.class);
    }
}
