# HotelKpiManagerFlashParseServiceByOpera.java

    package cloud.wconnect.workperformance.service.hotelkpi.managerflash;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.FileCheckUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.workperformance.cons.hotelkpi.HotelKpiManagerFlashCons;
    import cloud.wconnect.workperformance.pojo.bo.hotelkpi.HotelKpiManagerFlashBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiHandleReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import lombok.RequiredArgsConstructor;
    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.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;

    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/07/01
     */
    @RequiredArgsConstructor
    @Component
    @DS("work_performance")
    public class HotelKpiManagerFlashParseServiceByOpera implements IHotelKpiManagerFlashParseService {

        private final IHotelKpiHandleReportService hotelKpiHandleReportService;

        @Value("${file-max-size.import-report.hotel-kpi}")
        private long fileMaxSize;

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

        /**
         * 校验文件内容是否合法
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @author zc
         * @date 2024/8/22
         */
        @Override
        public void checkFile(PropertyDTO propertyDTO, MultipartFile file) {
            // 限制文件大小
            FileCheckUtils.checkFileSize(file, fileMaxSize);
            // 校验文件格式
            UploadUtils.checkFileFormat(file, Lists.newArrayList("xml"));
            // 校验文件内容
            try (InputStream inputStream = file.getInputStream()) {
                FileCheckUtils.checkManagerFlashFile(inputStream);
            } catch (BadRequestException e) {
                throw e;
            } catch (Exception e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Check Hotel KPI Manager Flash Opera report error", e);
            }
        }

        /**
         * 处理数据
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @param logId       日志 id
         * @param operatorId  操作人 id
         * @author zc
         * @date 2025/7/23
         */
        @Override
        public void handleData(PropertyDTO propertyDTO, MultipartFile file, List<Long> dateRange, long logId, int operatorId) {
            hotelKpiHandleReportService.sendManagerFlashEvent(propertyDTO, SystemPlatformEnum.OPERA.getDesc(), file, dateRange, logId, operatorId);
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @return HotelKpiManagerFlashBO 集合
         * @author zc
         * @date 2024/8/22
         */
        @Override
        public List<HotelKpiManagerFlashBO> parse(PropertyDTO propertyDTO, InputStream in) throws ParseReportException {
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                    .getInternationalization().getNumber().getCalculationRule());
            List<HotelKpiManagerFlashBO> boList = Lists.newArrayList();
            try {
                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 ("Arrival Rooms".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        boList.add(convert2Bo(dataElement, HotelKpiManagerFlashCons.ARRIVAL_ROOMS, roundingMode));
                    } else if ("Total Revenue".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        boList.add(convert2Bo(dataElement, HotelKpiManagerFlashCons.TOTAL_REVENUE, roundingMode));
                    } else if ("Room Revenue".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        boList.add(convert2Bo(dataElement, HotelKpiManagerFlashCons.ROOM_REVENUE, roundingMode));
                    } else if ("Rooms Occupied".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        boList.add(convert2Bo(dataElement, HotelKpiManagerFlashCons.ROOMS_OCCUPIED, roundingMode));
                    } else if ("ADR".equalsIgnoreCase(descriptionElement.getTextTrim())) {
                        boList.add(convert2Bo(dataElement, HotelKpiManagerFlashCons.ADR, roundingMode));
                    }
                }
                return boList;
            } catch (DocumentException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Hotel KPI parse Manager Flash Opera report error", e);
            }
        }

        /**
         * 转换为 BO
         *
         * @param dataElement  数据元素
         * @param category     类别
         * @param roundingMode 舍入模式
         * @return HotelKpiManagerFlashBO
         * @author zc
         * @date 2025/7/24
         */
        private HotelKpiManagerFlashBO convert2Bo(Element dataElement, String category, RoundingMode roundingMode) {
            Element listGHeading1Order = dataElement.element("LIST_G_HEADING_1_ORDER");
            List<Element> gHeading1OrderElementList = listGHeading1Order.elements("G_HEADING_1_ORDER");

            HotelKpiManagerFlashBO managerFlashBO = new HotelKpiManagerFlashBO();
            managerFlashBO.setCategory(category);
            for (Element gHeading1OrderElement : gHeading1OrderElementList) {
                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();
                BigDecimal arrivalRooms = StringUtils.isBlank(sumAmountStr) ? null :
                        new BigDecimal(sumAmountStr).setScale(BaseConstant.DATA_PRECISION, roundingMode);

                String heading2Str = gHeading1OrderElement.element("HEADING_2").getTextTrim();
                if ("DAY".equalsIgnoreCase(heading2Str)) {
                    managerFlashBO.setDay(arrivalRooms);
                } else if ("MONTH".equalsIgnoreCase(heading2Str)) {
                    managerFlashBO.setMtd(arrivalRooms);
                } else if ("YEAR".equalsIgnoreCase(heading2Str)) {
                    managerFlashBO.setYtd(arrivalRooms);
                }
            }
            return managerFlashBO;
        }
    }