# History30DaysListenerFileParseByPepImpl.java

    package cloud.wconnect.rateshop.service.revenue.marketsegmentsummary;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.PropertyRoomSoldTypeCons;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.query.PropertyRoomSoldQuery;
    import cloud.wconnect.commonlib.service.PropertyRoomSoldInternalService;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.revenue.RevenueAdrConfig;
    import cloud.wconnect.rateshop.entity.revenue.RevenueHistoryAndForecast;
    import cloud.wconnect.rateshop.pojo.bo.revenue.HistoryAndForecastBO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueAdrConfigService;
    import cloud.wconnect.rateshop.service.revenue.IRevenueHistoryAndForecastService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import lombok.RequiredArgsConstructor;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * @author yyt
     * 2026/3/3
     */
    @RequiredArgsConstructor
    @Service
    @DS("rate_shop")
    public class History30DaysListenerFileParseByPepImpl implements IHistory30DaysListenerFileParse {

        private final IRevenueHistoryAndForecastService revenueHistoryAndForecastService;
        private final IRevenueAdrConfigService revenueAdrConfigService;
        private final PropertyRoomSoldInternalService propertyRoomSoldInternalService;

        private static final String SOURCE = SystemPlatformEnum.PEP.getDesc();

        /**
         * 判断支持的平台
         *
         * @param source 平台
         * @return boolean
         * @author yyt
         * @date 2026/3/3
         */
        @Override
        public boolean supports(String source) {
            return SOURCE.equals(source);
        }

        /**
         * 解析 History and Forecast 报告数据
         *
         * @param propertyDTO 资产
         * @param in          文件流
         * @param startDate   开始日期
         * @param endDate     结束日期
         * @return java.util.List<cloud.wconnect.rateshop.pojo.bo.revenue.HistoryAndForecastBO>
         * @author yyt
         * @date 2026/3/3
         */
        @Override
        public List<HistoryAndForecastBO> parseReport(PropertyDTO propertyDTO, InputStream in,
                                                                               Long startDate, Long endDate) throws Exception {
            // 解析文件数据
            Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(new InputStreamReader(in));
            Iterator<CSVRecord> iterator = records.iterator();
            if (!iterator.hasNext()) return Collections.emptyList();

            Integer propertyId = propertyDTO.getId();
            List<String> marketSymbols = propertyRoomSoldInternalService.list(new PropertyRoomSoldQuery(propertyId))
                    .stream().filter(item -> item.getType() == PropertyRoomSoldTypeCons.GROUP)
                    .map(item-> item.getMarketSymbol().toLowerCase())
                    .toList();
            if (CollectionUtils.isEmpty(marketSymbols)) return Collections.emptyList();

            RevenueAdrConfig revenueAdrConfig = revenueAdrConfigService.get(propertyDTO.getId());
            boolean checkGroupDeduct = Objects.nonNull(revenueAdrConfig) && revenueAdrConfig.getCheckGroupDeduct();
            if (!checkGroupDeduct) return Collections.emptyList();

            // 最多31天数据
            List<HistoryAndForecastBO> historyAndForecastBOS = new ArrayList<>();
            String timezone = propertyDTO.getTimezone();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);

            // 跳过标题
            iterator.next();
            while (iterator.hasNext()) {
                CSVRecord record = iterator.next();
                // 空行
                if (record.size() == BaseConstant.ZERO) continue;

                // 解析每天数据
                String date = CsvFileUtils.get(record, "A");
                if (StringUtils.isEmpty(date)) continue;

                LocalDate localDate = DateTimeUtils.strToLocalDate(date, BaseConstant.DATE_FORMAT_MMMDDYYYY_USA);
                long dataDate = DateTimeUtils.localDateToLong(localDate, timezone);
                if (dataDate < startDate || dataDate > endDate) {
                    continue;
                }

                HistoryAndForecastBO historyAndForecastBO = new HistoryAndForecastBO();
                historyAndForecastBO.setTargetDate(DateTimeUtils.strToLocalDate(date, BaseConstant.DATE_FORMAT_MMMDDYYYY_USA).format(dateTimeFormatter2));

                String marketSegment = CsvFileUtils.get(record, "C");
                if(StringUtils.isEmpty(marketSegment)) continue;
                if(!marketSymbols.contains(marketSegment.toLowerCase())){
                    continue;
                }

                String staysStr = CsvFileUtils.getAndFormatNumber(record, "H");
                if (StringUtils.isNotEmpty(staysStr)) {
                    historyAndForecastBO.setGroupRoomNum(Integer.parseInt(staysStr));
                }
                historyAndForecastBOS.add(historyAndForecastBO);
            }

            if(CollectionUtils.isEmpty(historyAndForecastBOS)) return Collections.emptyList();

            // 数据按照日期汇总，每天的数据汇总 group room number 作为一天的数据
            List<HistoryAndForecastBO> result = Lists.newArrayListWithExpectedSize(30);
            Map<String, List<Integer>> groupRoomNumbersByDate = historyAndForecastBOS.stream().collect(Collectors.groupingBy(HistoryAndForecastBO::getTargetDate,
                    Collectors.collectingAndThen(Collectors.toList(), list -> list.stream().map(HistoryAndForecastBO::getGroupRoomNum)
                            .filter(Objects::nonNull).toList())));
            groupRoomNumbersByDate.forEach((date, groupRoomNumbers) -> {
                HistoryAndForecastBO historyAndForecastBO = new HistoryAndForecastBO();
                historyAndForecastBO.setTargetDate(date);
                historyAndForecastBO.setGroupRoomNum(groupRoomNumbers.stream().mapToInt(item -> item).sum());
                result.add(historyAndForecastBO);
            });

            return result;
        }

        /**
         * 保存报告数据
         *
         * @param propertyDTO 资产
         * @param boList      报告数据
         * @param operatorId  操作人
         * @author yyt
         * @date 2026/3/3
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveReportData(PropertyDTO propertyDTO, List<HistoryAndForecastBO> boList, int operatorId) {
            if(CollectionUtils.isEmpty(boList)) return;

            Integer propertyId = propertyDTO.getId();
            // 查询已存在的数据，根据团客房间数，计算散客房间数
            List<Integer> dates = boList.stream().map(item -> Integer.parseInt(item.getTargetDate().replaceAll("-", "")))
                    .sorted().toList();
            Integer startDate = dates.get(0);
            Integer endDate = dates.get(dates.size() - 1);
            Map<Integer, RevenueHistoryAndForecast> existHistoryAndForecastByDate = revenueHistoryAndForecastService.listByDate(propertyId, startDate, endDate)
                    .stream().collect(Collectors.toMap(RevenueHistoryAndForecast::getTargetDate, v -> v));

            long now = DateTimeUtils.nowTimestamp();

            List<RevenueHistoryAndForecast> historyAndForecasts = boList.stream().map(item -> {
                int date = Integer.parseInt(StringUtils.replace(item.getTargetDate(), "-", ""));
                RevenueHistoryAndForecast existHistoryAndForecast = existHistoryAndForecastByDate.getOrDefault(date, new RevenueHistoryAndForecast());
                item.setSoldRooms(existHistoryAndForecast.getSoldRooms());

                return new RevenueHistoryAndForecast()
                        .setPropertyId(propertyId)
                        .setTargetDate(date)
                        .setTransientRoomNum(revenueHistoryAndForecastService.calculateTransientRoomNum(item))
                        .setGroupRoomNum(item.getGroupRoomNum())
                        .setUpdateBy(operatorId)
                        .setUpdateTime(now);
            }).collect(Collectors.toList());

            revenueHistoryAndForecastService.saveDataForGroupData(historyAndForecasts);
        }

    }