# WMRevenueCancellationSummaryParseServiceByShiji.java

    package cloud.wconnect.rateshop.service.weeklymonthlyrevenue.cancellationsummary;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    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.rateshop.pojo.bo.weeklymonthlyrevenue.WMRevenueCancellationSummaryBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    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.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.time.LocalDate;
    import java.util.Iterator;
    import java.util.List;

    /**
     * 解析 Shiji Cancellation Summary 报告
     *
     * @author zc
     * @date 2026/02/07
     */
    @Slf4j
    @Component
    @DS("rate_shop")
    public class WMRevenueCancellationSummaryParseServiceByShiji implements IWMRevenueCancellationSummaryParseService{

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

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return WMRevenueReservationForecastBO 集合
         * @author zc
         * @date 2026/2/7
         */
        @Override
        public List<WMRevenueCancellationSummaryBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            long startDate = dateRange.get(0);
            long endDate = dateRange.get(1);

            try (InputStreamReader inputStreamReader = new InputStreamReader(in)) {
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);
                Iterator<CSVRecord> iterator = iterable.iterator();

                // 跳过标题行
                iterator.next();

                List<WMRevenueCancellationSummaryBO> boList = Lists.newArrayList();
                while (iterator.hasNext()) {
                    CSVRecord record = iterator.next();

                    String colC = CsvFileUtils.get(record, "C");
                    // 校验 colC 是否是 MM/dd/yy 格式的日期
                    if (!DateTimeUtils.isValidPatternDate(colC, BaseConstant.DATE_TIME_FORMAT_MM_DD_YY)) {
                        continue;
                    }

                    // 忽略掉非指定日期的数据
                    LocalDate localDate = DateTimeUtils.strToLocalDate(colC, BaseConstant.DATE_TIME_FORMAT_MM_DD_YY);
                    long theDate = Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
                    // 判断数据日期是否在开始日期 和 结束日期之间
                    if (theDate < startDate || theDate > endDate) {
                        continue;
                    }

                    // 格式化日期为 yyyy-MM-dd
                    String formattedDateStr = DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD);

                    WMRevenueCancellationSummaryBO bo = new WMRevenueCancellationSummaryBO();
                    bo.setTheDay(formattedDateStr);
                    bo.setRoomsNights(parseInt(record, "J"));

                    boList.add(bo);
                }

                return boList;
            } catch (IOException e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Parse Monthly Revenue Shiji [Cancellation Summary] report error", e);
            }
        }

        /**
         * 解析数字列
         *
         * @param record CSV记录
         * @param column 列名
         * @return int
         * @author zc
         * @date 2026/2/7
         */
        private int parseInt(CSVRecord record, String column) {
            String value = CsvFileUtils.getAndFormatNumber(record, column);
            if (StringUtils.isBlank(value)) return 0;
            return Integer.parseInt(value);
        }
    }