# WMRevenueCancellationSummaryParseServiceByOpera.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.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.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    import org.springframework.stereotype.Component;

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

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

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

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

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

                Document document = new SAXReader().read(in);
                List<Node> nodes = document.selectNodes("//G_COL");
                for (Node node : nodes) {
                    Element dataElement = (Element) node;

                    String theDateStr = dataElement.elementTextTrim("COL1");
                    long theDate = Long.parseLong(theDateStr);
                    if (theDate < startDate || theDate > endDate) {
                        continue;
                    }

                    int totalRoomsNights = 0;

                    Element listGBookIdElement = dataElement.element("LIST_G_BOOK_ID");
                    List<Element> gBookIdElementList = listGBookIdElement.elements("G_BOOK_ID");
                    if (CollectionUtils.isEmpty(gBookIdElementList)) {
                        continue;
                    }

                    for (Element gBookIdElement : gBookIdElementList) {
                        // RMS_NIGHTS
                        String rmsNights = gBookIdElement.elementTextTrim("RMS_NIGHTS");
                        if (StringUtils.isNotBlank(rmsNights)) {
                            totalRoomsNights += Integer.parseInt(rmsNights);
                        }
                    }

                    WMRevenueCancellationSummaryBO cancellationSummaryBO = new WMRevenueCancellationSummaryBO();
                    LocalDate localDate = DateTimeUtils.strToLocalDate(theDateStr, BaseConstant.DATE_FORMAT_YYYYMMDD);
                    cancellationSummaryBO.setTheDay(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));
                    cancellationSummaryBO.setRoomsNights(totalRoomsNights);

                    cancellationSummaryBOList.add(cancellationSummaryBO);
                }

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

    }