# HotelKpiManagerFlashParseServiceByShiji.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.ShijiFileCheckUtils;
    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.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.Iterator;
    import java.util.List;

    /**
     * 解析 Opera 系统的 Manager Flash/Service 报告
     *
     * @author zc
     * @date 2025/07/24
     */
    @RequiredArgsConstructor
    @Component
    @DS("work_performance")
    public class HotelKpiManagerFlashParseServiceByShiji implements IHotelKpiManagerFlashParseService {

        private final IHotelKpiHandleReportService hotelKpiHandleReportService;

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

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

        /**
         * 校验文件内容是否合法
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @author zc
         * @date 2025/7/24
         */
        @Override
        public void checkFile(PropertyDTO propertyDTO, MultipartFile file) {
            // 限制文件大小
            FileCheckUtils.checkFileSize(file, fileMaxSize);
            // 校验文件格式
            UploadUtils.checkFileFormat(file, Lists.newArrayList("csv"));
            // 校验文件内容
            try (InputStream inputStream = file.getInputStream()) {
                ShijiFileCheckUtils.checkManagerFlash(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 Shiji report error", e);
            }
        }

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

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @return HotelKpiManagerFlashBO 集合
         * @author zc
         * @date 2025/7/24
         */
        @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 (InputStreamReader inputStreamReader = new InputStreamReader(in)) {
                // 获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);
                Iterator<CSVRecord> iterator = iterable.iterator();
                // 跳过标题行
                iterator.next();
                while (iterator.hasNext()) {
                    CSVRecord csvRecord = iterator.next();
                    String description = ShijiFileCheckUtils.get(csvRecord, "A");
                    if ("Arrival - Rooms".equalsIgnoreCase(description)) {
                        boList.add(convert2Bo(csvRecord, HotelKpiManagerFlashCons.ARRIVAL_ROOMS, roundingMode));
                    } else if ("Total Revenue".equalsIgnoreCase(description)) {
                        boList.add(convert2Bo(csvRecord, HotelKpiManagerFlashCons.TOTAL_REVENUE, roundingMode));
                    } else if ("Revenue - Room Total".equalsIgnoreCase(description)) {
                        boList.add(convert2Bo(csvRecord, HotelKpiManagerFlashCons.ROOM_REVENUE, roundingMode));
                    } else if ("Occupied Rooms".equalsIgnoreCase(description)) {
                        boList.add(convert2Bo(csvRecord, HotelKpiManagerFlashCons.ROOMS_OCCUPIED, roundingMode));
                    } else if ("ADR".equalsIgnoreCase(description)) {
                        boList.add(convert2Bo(csvRecord, HotelKpiManagerFlashCons.ADR, roundingMode));
                    }
                }
                return boList;
            } catch (IOException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle parse Hotel KPI Manager Flash Shiji report error.", e);
            }
        }

        /**
         * 转换为BO
         *
         * @param csvRecord    CSV数据行
         * @param category     类别
         * @param roundingMode 舍入模式
         * @return HotelKpiManagerFlashBO
         * @author zc
         * @date 2025/7/24
         */
        private HotelKpiManagerFlashBO convert2Bo(CSVRecord csvRecord, String category, RoundingMode roundingMode) {
            HotelKpiManagerFlashBO managerFlashBO = new HotelKpiManagerFlashBO();
            managerFlashBO.setCategory(category);
            String recordDayStr = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "B");
            managerFlashBO.setDay(StringUtils.isEmpty(recordDayStr) ? null : new BigDecimal(recordDayStr)
                    .setScale(BaseConstant.DATA_PRECISION, roundingMode));
            String recordMtdStr = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "C");
            managerFlashBO.setMtd(StringUtils.isEmpty(recordMtdStr) ? null : new BigDecimal(recordMtdStr)
                    .setScale(BaseConstant.DATA_PRECISION, roundingMode));
            String recordYtdStr = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "D");
            managerFlashBO.setYtd(StringUtils.isEmpty(recordYtdStr) ? null : new BigDecimal(recordYtdStr)
                    .setScale(BaseConstant.DATA_PRECISION, roundingMode));
            return managerFlashBO;
        }
    }