# ManagerFlashParseImplByOpera.java

    package cloud.wconnect.rateshop.service.revenue.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.pojo.dto.internationalization.InternationalizationNumberDTO;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.rateshop.pojo.dto.revenue.ManagerFlashDTO;
    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.Service;

    import java.io.InputStream;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.List;
    import java.util.Objects;

    /**
     * Manager Flash报告的解析实现类
     *
     * @author lichen
     * @date 2025/1/10
     */
    @Service
    @Slf4j
    @DS("rate_shop")
    public class ManagerFlashParseImplByOpera implements IManagerFlashParse {

        /**
         * 判断支持的平台
         *
         * @param source 平台
         * @return boolean
         * @author lichen
         * @date 2025/1/10
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.OPERA.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param in          报告文件流
         * @param propertyDTO 资产
         * @return java.lang.Integer
         * @author lichen
         * @date 2025/1/10
         */
        @Override
        public ManagerFlashDTO parse(InputStream in, PropertyDTO propertyDTO) throws ParseReportException {
            InternationalizationNumberDTO number = propertyDTO.getInternationalization().getNumber();
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(number.getCalculationRule());
            try {
                ManagerFlashDTO managerFlashBO = new ManagerFlashDTO();

                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 ("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);
                    }
                    if ("No Show Rooms".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String noShowRoomsStr = getSumAmount(dataElement);
                        Integer noShow = StringUtils.isEmpty(noShowRoomsStr) ? null : Integer.valueOf(noShowRoomsStr);
                        managerFlashBO.setNoShow(noShow);
                    }
                    if ("Late Reservation Cancellations for Today".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        String cancelledStr = getSumAmount(dataElement);
                        Integer cancelled = StringUtils.isEmpty(cancelledStr) ? null : Integer.valueOf(cancelledStr);
                        managerFlashBO.setCancelled(cancelled);
                    }
                }
                return managerFlashBO;
            } catch (DocumentException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Hotel KPI parse Manager Flash Opera report error", e);
            }
        }

        /**
         * 获取Sum Amount的值
         *
         * @param dataElement xml节点
         * @return java.lang.String
         * @author lichen
         * @date 2025/1/10
         */
        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;
        }
    }