# WMRevenueGroupPickupParseServiceByOpera.java

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

    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.WMRevenueGroupPickupBO;
    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.time.format.DateTimeFormatter;
    import java.util.List;
    import java.util.Locale;

    /**
     * 解析 Opera 系统的 Group Pickup/Service 报告
     *
     * @author zc
     * @date 2024/05/18
     */
    @Slf4j
    @Component
    @DS("rate_shop")
    public class WMRevenueGroupPickupParseServiceByOpera implements IWMRevenueGroupPickupParseService {

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

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

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

                Document document = new SAXReader().read(in);
                List<Node> nodes = document.selectNodes("//G_ALLOTMENT_CODE");
                List<WMRevenueGroupPickupBO> resultList = Lists.newArrayListWithCapacity(nodes.size());
                for (Node node : nodes) {
                    Element dataElement = (Element) node;

                    Element listAllotmentDateElement = dataElement.element("LIST_G_ALLOTMENT_DATE");
                    List<Element> allotmentDateList = listAllotmentDateElement.elements("G_ALLOTMENT_DATE");
                    if (CollectionUtils.isEmpty(allotmentDateList)) {
                        continue;
                    }

                    for (Element allotmentDateElement : allotmentDateList) {
                        Element listBlockSize = allotmentDateElement.element("LIST_G_BLOCKSIZE");
                        List<Element> blocksizeList = listBlockSize.elements("G_BLOCKSIZE");
                        if (CollectionUtils.isEmpty(blocksizeList)) {
                            continue;
                        }

                        // Allotment date
                        String allotmentDateStr = allotmentDateElement.element("ALLOTMENT_DATE").getTextTrim();
                        long allotmentDate = parseDate(allotmentDateStr, dateTimeFormatter);
                        // 判断数据日期是否在开始日期 和 结束日期之间
                        if (allotmentDate < startDate || allotmentDate > endDate) {
                            continue;
                        }

                        // 转换为 BO 日期格式 yyyy-MM-dd
                        LocalDate localDate = DateTimeUtils.strToLocalDate(String.valueOf(allotmentDate), BaseConstant.DATE_FORMAT_YYYYMMDD);
                        String theDay = DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD);

                        for (Element blocksizeElement : blocksizeList) {
                            // Origin INIT
                            String initStr = blocksizeElement.element("INIT").getTextTrim();
                            int origin = StringUtils.isEmpty(initStr) ? BaseConstant.ZERO : Integer.parseInt(initStr);

                            WMRevenueGroupPickupBO groupPickupBO = new WMRevenueGroupPickupBO();
                            groupPickupBO.setTheDay(theDay);
                            groupPickupBO.setOrigin(origin);

                            resultList.add(groupPickupBO);
                        }
                    }
                }
                return resultList;
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle parse Weekly Monthly Revenue Group Pickup Opera report error.", e);
            }
        }

        /**
         * 解析报告中的日期
         *
         * @param dateStr           Date
         * @param dateTimeFormatter 日期格式
         * @return 日期（yyyyMMdd）
         * @author zc
         * @date 2024/5/18
         */
        private long parseDate(String dateStr, DateTimeFormatter dateTimeFormatter) {
            String[] dateArr = StringUtils.split(dateStr, "-");
            dateArr[1] = DateTimeUtils.capitalize(dateArr[1]);
            LocalDate localDate = LocalDate.parse(String.join("-", dateArr), dateTimeFormatter);
            return Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
        }

    }