# WMRevenueManagerFlashParseImplByPEP.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.PropertyRevenueDescriptionTypeCons;
    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.service.PropertyRevenueDescriptionInternalService;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    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.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.time.LocalDate;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Objects;

    /**
     * Manager Flash报告的 PEP 解析实现类
     *
     * @author yyt
     * @date 2026/3/12
     */
    @RequiredArgsConstructor
    @Service
    @Slf4j
    @DS("rate_shop")
    public class WMRevenueManagerFlashParseImplByPEP implements IWMRevenueManagerFlashParseService {

        private final PropertyRevenueDescriptionInternalService propertyRevenueDescriptionInternalService;

        /**
         * 判断支持的平台
         *
         * @param source 来源系统
         * @return boolean
         * @author yyt
         * @date 2025/12/15
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.PEP.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return WMRevenueManagerFlashBO 集合
         * @author yyt
         * @date 2026/3/12
         */
        @Override
        public List<WMRevenueManagerFlashBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            // 查询 revenue description 配置，用于计算 room revenue
            BigDecimal totalRoomRevenue = null;
            Integer propertyId = propertyDTO.getId();
            List<String> roomTypeNames = propertyRevenueDescriptionInternalService.list(propertyId)
                    .stream()
                    .filter(item -> item.getType() == PropertyRevenueDescriptionTypeCons.ROOM)
                    .map(item -> item.getName().toLowerCase())
                    .toList();

            InternationalizationNumberDTO number = propertyDTO.getInternationalization().getNumber();
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(number.getCalculationRule());
            try (InputStreamReader inputStreamReader = new InputStreamReader(in)) {
                WMRevenueManagerFlashBO managerFlashBO = new WMRevenueManagerFlashBO();
                //获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);
                Iterator<CSVRecord> iterator = iterable.iterator();

                //跳过标题行
                iterator.next();

                while (iterator.hasNext()) {
                    CSVRecord csvRecord = iterator.next();

                    String description = CsvFileUtils.get(csvRecord, "A");

                    if ("OCCUPANCY INCLUDING DOWN, COMP, HOUSE USE ROOMS".equalsIgnoreCase(description)) {
                        // Occ
                        BigDecimal occ = getBigDecimal(csvRecord, roundingMode);
                        managerFlashBO.setOcc(occ);
                    } else if ("ADR INCLUDING COMP, HOUSE USE ROOMS".equalsIgnoreCase(description)) {
                        // ADR
                        BigDecimal adr = getBigDecimal(csvRecord, roundingMode);
                        managerFlashBO.setAdr(adr);
                    } else if ("Comp Rooms".equalsIgnoreCase(description)) {
                        Integer compRoom = getInteger(csvRecord);
                        managerFlashBO.setComplimentaryRooms(compRoom);
                    } else if ("NO SHOWS".equalsIgnoreCase(description)) {
                        Integer noShowRooms = getInteger(csvRecord);
                        managerFlashBO.setNoShowRooms(noShowRooms);
                    } else if ("OUT OF ORDER".equalsIgnoreCase(description)) {
                        Integer oooRooms = getInteger(csvRecord);
                        managerFlashBO.setOoo(oooRooms);
                    } else if (roomTypeNames.contains(description.toLowerCase())) {
                        // 累加 Room Revenue
                        BigDecimal roomRevenue = getBigDecimal(csvRecord, roundingMode);
                        if (Objects.nonNull(roomRevenue)) {
                            totalRoomRevenue = Objects.isNull(totalRoomRevenue) ? roomRevenue : totalRoomRevenue.add(roomRevenue);
                        }
                    } else if ("ROOM SOLD".equalsIgnoreCase(description)) {
                        Integer roomSold = getInteger(csvRecord);
                        managerFlashBO.setRoomsSold(roomSold);
                    } else if ("Total Rooms".equalsIgnoreCase(description)) {
                        Integer physicalRooms = getInteger(csvRecord);
                        managerFlashBO.setTotalRooms(physicalRooms);
                    }
                }
                LocalDate localDate = DateTimeUtils.strToLocalDate(dateRange.get(0).toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
                managerFlashBO.setTheDay(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));
                // 设置 room revenue
                managerFlashBO.setRoomRevenue(totalRoomRevenue);

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

        /**
         * 获取BigDecimal
         *
         * @param csvRecord    CSVRecord
         * @param roundingMode 舍入模式
         * @return java.math.BigDecimal
         * @author yyt
         * @date 2025/12/15
         */
        private BigDecimal getBigDecimal(CSVRecord csvRecord, RoundingMode roundingMode) {
            String value = CsvFileUtils.getAndFormatNumber(csvRecord, "B");
            return StringUtils.isEmpty(value) ? null : new BigDecimal(value)
                    .setScale(BaseConstant.DATA_PRECISION, roundingMode);
        }

        /**
         * 获取Integer
         *
         * @param csvRecord CSVRecord
         * @return java.math.BigDecimal
         * @author yyt
         * @date 2025/12/15
         */
        private Integer getInteger(CSVRecord csvRecord) {
            String value = CsvFileUtils.getAndFormatNumber(csvRecord, "B");
            return StringUtils.isEmpty(value) ? null : Integer.valueOf(value);
        }

    }