# GmwrManagerFlashParseServiceByOpera.java

    package cloud.wconnect.workperformance.service.gmwr.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.InternationalizationUtils;
    import cloud.wconnect.workperformance.pojo.bo.gmwr.GmwrManagerFlashBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    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.util.List;
    import java.util.Objects;

    /**
     * 解析 Opera 系统的 Manager Flash/Service 报告
     *
     * @author zc
     * @date 2024/10/17
     */
    @Component
    @DS("work_performance")
    public class GmwrManagerFlashParseServiceByOpera implements IGmwrManagerFlashParseService {

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

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @return GmwrManagerFlashBO
         * @author zc
         * @date 2024/10/17
         */
        @Override
        public GmwrManagerFlashBO parse(PropertyDTO propertyDTO, InputStream in) throws ParseReportException {
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                    .getInternationalization().getNumber().getCalculationRule());
            try {
                Document document = new SAXReader().read(in);
                List<Node> nodes = document.selectNodes("//G_MASTER_VALUE");

                GmwrManagerFlashBO managerFlashBO = new GmwrManagerFlashBO();
                for (Node node : nodes) {
                    Element dataElement = (Element) node;
                    Element descriptionElement = dataElement.element("DESCRIPTION");
                    String descriptionStr = descriptionElement.getTextTrim();
                    if ("Food And Beverage Revenue".equalsIgnoreCase(descriptionStr)) {
                        managerFlashBO.setFbRevenue(getTargetValue(dataElement, roundingMode));
                    } else if ("Other Revenue".equalsIgnoreCase(descriptionStr)) {
                        managerFlashBO.setOthersRevenue(getTargetValue(dataElement, roundingMode));
                    }
                }
                return managerFlashBO;
            } catch (DocumentException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "GM Weekly Report parse Manager Flash Opera report error", e);
            }
        }

        /**
         * 根据目标描述获取目标值
         *
         * @param dataElement  元素
         * @param roundingMode 舍入模式
         * @return java.math.BigDecimal
         * @author zc
         * @date 2025/7/24
         */
        private BigDecimal getTargetValue(Element dataElement, RoundingMode roundingMode) {
            Element listGHeading1Order = dataElement.element("LIST_G_HEADING_1_ORDER");
            List<Element> gHeading1OrderElementList = listGHeading1Order.elements("G_HEADING_1_ORDER");
            BigDecimal targetValue = null;
            for (Element gHeading1OrderElement : gHeading1OrderElementList) {
                String heading2Str = gHeading1OrderElement.element("HEADING_2").getTextTrim();
                if ("MONTH".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;
                    }
                    String sumAmountStr = sumAmount.getTextTrim();
                    targetValue = StringUtils.isBlank(sumAmountStr) ? null : new BigDecimal(sumAmountStr)
                            .setScale(BaseConstant.DATA_PRECISION, roundingMode);
                    break;
                }
            }
            return targetValue;
        }
    }