# WMRevenueCancellationSummaryParseServiceByOther.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.InternalServerErrorException;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.*;
    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.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.springframework.stereotype.Component;

    import java.io.IOException;
    import java.io.InputStream;
    import java.time.LocalDate;
    import java.util.List;

    /**
     * 解析 Other 的 Cancellation Summary/Service 报告
     *
     * @author zc
     * @date 2024/05/20
     */
    @Slf4j
    @Component
    @DS("rate_shop")
    public class WMRevenueCancellationSummaryParseServiceByOther implements IWMRevenueCancellationSummaryParseService{

        /**
         * 支持的报告来源
         *
         * @param source 报告来源
         * @return boolean
         * @author zc
         * @date 2024/5/20
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.OTHER.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return WMRevenueCancellationSummaryBO 集合
         * @author zc
         * @date 2024/8/22
         */
        @Override
        public List<WMRevenueCancellationSummaryBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            try (Workbook workbook = WorkbookFactory.create(in)) {
                List<WMRevenueCancellationSummaryBO> cancellationSummaryBOList = Lists.newArrayList();

                long startDate = dateRange.get(0);
                long endDate = dateRange.get(1);

                Sheet sheet = workbook.getSheetAt(0);
                // 获取最大行数
                int lastRowNum = sheet.getLastRowNum();
                for (int i = 1; i <= lastRowNum; i++) {
                    Row row = sheet.getRow(i);

                    // 日期
                    String theDateStr = CellUtils.convertCellStr(row.getCell(0));
                    LocalDate localDate = DateTimeUtils.strToLocalDate(theDateStr, BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                    long theDate = Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
                    // 判断数据日期是否在开始日期 和 结束日期之间
                    if (theDate < startDate || theDate > endDate) {
                        continue;
                    }

                    // Rooms Nights
                    int roomsNights = Integer.parseInt(CellUtils.convertCellStr(row.getCell(1)));

                    WMRevenueCancellationSummaryBO cancellationSummaryBO = new WMRevenueCancellationSummaryBO();
                    cancellationSummaryBO.setTheDay(theDateStr);
                    cancellationSummaryBO.setRoomsNights(roomsNights);

                    cancellationSummaryBOList.add(cancellationSummaryBO);
                }

                return cancellationSummaryBOList;
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle parse Weekly Monthly Revenue Cancellation Summary Other report error.", e);
            }
        }

    }