# OooRoomInternalServiceImpl.java

    package cloud.wconnect.frontoffice.service.oooroom.internal.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.oooroom.OooRoom;
    import cloud.wconnect.frontoffice.pojo.dto.oooroom.internal.OooRoomCountInternalDTO;
    import cloud.wconnect.frontoffice.service.oooroom.IOooRoomService;
    import cloud.wconnect.frontoffice.service.oooroom.internal.IOooRoomInternalService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.stereotype.Service;

    import java.time.LocalDate;
    import java.util.Arrays;
    import java.util.List;

    /**
     * Service: Internal
     *
     * @author zc
     * @date 2024/10/20
     */
    @Service
    @DS("ratelog_manage")
    public class OooRoomInternalServiceImpl implements IOooRoomInternalService {
        @Resource
        private IOooRoomService oooRoomService;
        @Resource
        private PropertyInternalService propertyInternalService;

        /**
         * 获取指定日期未完成的 OOO 房间数量
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期(yyyyMMdd)
         * @param endDate    结束日期(yyyyMMdd)
         * @return OooRoomCountInternalDTO 集合
         * @author zc
         * @date 2024/10/20
         */
        @Override
        public List<OooRoomCountInternalDTO> list(int propertyId, long startDate, long endDate) {
            PropertyDTO propertyDTO = propertyInternalService.getOne(propertyId);
            String timezone = propertyDTO.getTimezone();

            LocalDate startLocalDate = DateTimeUtils.strToLocalDate(String.valueOf(startDate), BaseConstant.DATE_FORMAT_YYYYMMDD);
            long startDateTimestamp = DateTimeUtils.localDateToLong(startLocalDate, timezone);

            LocalDate endLocalDate = DateTimeUtils.strToLocalDate(String.valueOf(endDate), BaseConstant.DATE_FORMAT_YYYYMMDD);
            long endDateTimestamp = DateTimeUtils.localDateToLong(endLocalDate, timezone);

            List<OooRoom> oooRoomList = oooRoomService.list(new LambdaQueryWrapper<OooRoom>()
                    .eq(OooRoom::getPropertyId, propertyId)
                    .and(wrapper -> wrapper
                            .le(OooRoom::getFromDate, endDateTimestamp)
                            .ge(OooRoom::getToDate, startDateTimestamp)));

            List<OooRoomCountInternalDTO> resultList = Lists.newArrayList();
            for (LocalDate indexDate = startLocalDate; !indexDate.isAfter(endLocalDate); indexDate = indexDate.plusDays(1)) {
                OooRoomCountInternalDTO oooRoomCountInternalDTO = new OooRoomCountInternalDTO();
                long date = Long.parseLong(DateTimeUtils.localDateToStr(indexDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
                oooRoomCountInternalDTO.setDate(date);

                long dateTimestamp = DateTimeUtils.localDateToLong(indexDate, timezone);
                long count = oooRoomList.stream()
                        .filter(oooRoom -> oooRoom.getFromDate() <= dateTimestamp && oooRoom.getToDate() >= dateTimestamp
                                && (oooRoom.getCompletedDate() == 0 || oooRoom.getCompletedDate() > dateTimestamp))
                        .map(oooRoom -> oooRoom.getRoomNumbers().split(",")).flatMap(Arrays::stream).distinct().count();
                oooRoomCountInternalDTO.setOooRoomsCount((int) count);
                resultList.add(oooRoomCountInternalDTO);
            }
            return resultList;
        }
    }