# OccupancySummaryParseByPep.java

    package cloud.wconnect.workperformance.service.gmwr.occupancysummary;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.pojo.bo.gmwr.OccupancySummaryReportBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Component;

    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.time.LocalDate;
    import java.util.Iterator;
    import java.util.List;

    /**
     *
     * @author: yyt
     * 2026-02-05
     */
    @RequiredArgsConstructor
    @Slf4j
    @Component
    @DS("work_performance")
    public class OccupancySummaryParseByPep implements IOccupancySummaryParse{

        /**
         * 支持的报告来源
         *
         * @param source 报告来源
         * @return boolean
         * @author yyt
         * @date 2026/2/5
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.PEP.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param in        文件
         * @param property  资产
         * @param dateRange 日期范围
         * @return OccupancySummaryReportBO
         * @author yyt
         * @date 2026/2/5
         */
        @Override
        public List<OccupancySummaryReportBO> parse(InputStream in, PropertyDTO property, List<Long> dateRange) throws ParseReportException {
            Integer propertyId = property.getId();
            long startDate = dateRange.get(0);
            long endDate = dateRange.get(1);
            try (InputStreamReader inputStreamReader = new InputStreamReader(in)) {
                List<OccupancySummaryReportBO> boList = Lists.newArrayList();
                //获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);
                Iterator<CSVRecord> iterator = iterable.iterator();
                //跳过第一行标题行
                iterator.next();
                while (iterator.hasNext()) {
                    CSVRecord csvRecord = iterator.next();
                    String dateStr = CsvFileUtils.get(csvRecord, "A");
                    if (StringUtils.isEmpty(dateStr)) continue;
                    LocalDate localDate = DateTimeUtils.strToLocalDate(dateStr, BaseConstant.DATE_FORMAT_MMMDDYYYY_USA);
                    long targetDate = Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
                    //判断目标日期是否在范围中
                    if (targetDate >= startDate && targetDate <= endDate) {
                        OccupancySummaryReportBO bo = new OccupancySummaryReportBO();
                        bo.setPropertyId(propertyId);
                        bo.setDate(targetDate);
                        String compRoomsStr = CsvFileUtils.getAndFormatNumber(csvRecord, "O");
                        bo.setCompRooms(StringUtils.isEmpty(compRoomsStr) ? 0 : Integer.parseInt(compRoomsStr));
                        boList.add(bo);
                    }
                }
                return boList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse pep [Occupancy Summary] report error", e);
            }
        }

    }