# WMRevenueManagerFlashParseServiceByOpera.java

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

    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.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.rateshop.pojo.bo.weeklymonthlyrevenue.WMRevenueManagerFlashBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import lombok.extern.slf4j.Slf4j;
    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.math.BigDecimal;
    import java.math.RoundingMode;
    import java.time.LocalDate;
    import java.util.*;

    /**
     * 解析 Opera 系统的 Manager Flash/Service 报告
     *
     * @author lichen
     * @date 2025/12/15
     */
    @Slf4j
    @Component
    @DS("rate_shop")
    public class WMRevenueManagerFlashParseServiceByOpera implements IWMRevenueManagerFlashParseService {

        /**
         * 支持的报告来源
         *
         * @param source 报告来源
         * @return boolean
         * @author lichen
         * @date 2025/12/15
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.OPERA.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param in          报告文件流
         * @param propertyDTO 资产
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return java.util.List<cloud.wconnect.rateshop.pojo.bo.weeklymonthlyrevenue.WMRevenueManagerFlashBO>
         * @author lichen
         * @date 2025/12/15
         */
        @Override
        public List<WMRevenueManagerFlashBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            try {
                RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                        .getInternationalization().getNumber().getCalculationRule());

                WMRevenueManagerFlashBO managerFlashBO = new WMRevenueManagerFlashBO();

                Document document = new SAXReader().read(in);
                List<Node> nodes = document.selectNodes("//G_MASTER_VALUE");
                for (Node node : nodes) {
                    Element dataElement = (Element) node;
                    Element descriptionElement = dataElement.element("DESCRIPTION");
                    if ("% Rooms Occupied".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String occStr = getSumAmount(dataElement);
                        BigDecimal occ = StringUtils.isEmpty(occStr) ? null : new BigDecimal(occStr)
                                .setScale(BaseConstant.DATA_PRECISION, roundingMode);
                        managerFlashBO.setOcc(occ);
                    } else if ("ADR".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String adrStr = getSumAmount(dataElement);
                        BigDecimal adr = StringUtils.isEmpty(adrStr) ? null : new BigDecimal(adrStr)
                                .setScale(BaseConstant.DATA_PRECISION, roundingMode);
                        managerFlashBO.setAdr(adr);
                    } else if ("Complimentary Rooms".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String compRoomStr = getSumAmount(dataElement);
                        Integer compRoom = StringUtils.isEmpty(compRoomStr) ? null : Integer.valueOf(compRoomStr);
                        managerFlashBO.setComplimentaryRooms(compRoom);
                    } else if ("No Show Rooms".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String noShowRoomStr = getSumAmount(dataElement);
                        Integer noShowRoom = StringUtils.isEmpty(noShowRoomStr) ? null : Integer.valueOf(noShowRoomStr);
                        managerFlashBO.setNoShowRooms(noShowRoom);
                    } else if ("Out of Order Rooms".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String oooRoomStr = getSumAmount(dataElement);
                        Integer oooRoom = StringUtils.isEmpty(oooRoomStr) ? null : Integer.valueOf(oooRoomStr);
                        managerFlashBO.setOoo(oooRoom);
                    } else if ("Room Revenue".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String roomRevenueStr = getSumAmount(dataElement);
                        BigDecimal roomRevenue = StringUtils.isEmpty(roomRevenueStr) ? null : new BigDecimal(roomRevenueStr)
                                .setScale(BaseConstant.DATA_PRECISION, roundingMode);
                        managerFlashBO.setRoomRevenue(roomRevenue);
                    } else if ("Rooms Occupied".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String roomsSoldStr = getSumAmount(dataElement);
                        Integer roomsSold = StringUtils.isEmpty(roomsSoldStr) ? null : Integer.valueOf(roomsSoldStr);
                        managerFlashBO.setRoomsSold(roomsSold);
                    } else if ("Total Rooms in Hotel".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String totalRoomsStr = getSumAmount(dataElement);
                        Integer totalRooms = StringUtils.isEmpty(totalRoomsStr) ? null : Integer.valueOf(totalRoomsStr);
                        managerFlashBO.setTotalRooms(totalRooms);
                    }
                }

                LocalDate localDate = DateTimeUtils.strToLocalDate(dateRange.get(0).toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
                managerFlashBO.setTheDay(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));

                return Collections.singletonList(managerFlashBO);
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle parse Weekly Monthly Revenue Manager Flash Opera report error.", e);
            }
        }

        /**
         * 获取Sum Amount的值
         *
         * @param dataElement xml节点
         * @return java.lang.String
         * @author lichen
         * @date 2025/12/15
         */
        private String getSumAmount(Element dataElement) {
            String sumAmountStr = null;
            Element listGHeading1Order = dataElement.element("LIST_G_HEADING_1_ORDER");
            List<Element> gHeading1OrderElementList = listGHeading1Order.elements("G_HEADING_1_ORDER");
            for (Element gHeading1OrderElement : gHeading1OrderElementList) {
                String heading2Str = gHeading1OrderElement.element("HEADING_2").getTextTrim();
                //获取DAY下的数值
                if ("DAY".equalsIgnoreCase(heading2Str)) {
                    Element listGSumAmount = gHeading1OrderElement.element("LIST_G_SUM_AMOUNT");
                    Element gSumAmount = listGSumAmount.element("G_SUM_AMOUNT");
                    if (Objects.isNull(gSumAmount)) {
                        break;
                    }
                    Element sumAmount = gSumAmount.element("SUM_AMOUNT");
                    if (Objects.isNull(sumAmount)) {
                        break;
                    }
                    sumAmountStr = StringUtils.isBlank(sumAmount.getTextTrim()) ? null : sumAmount.getTextTrim();
                }
            }
            return sumAmountStr;
        }

    }