# WMRevenueManagerFlashParseImplByShiji.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.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.DateTimeUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.ShijiFileCheckUtils;
    import cloud.wconnect.rateshop.pojo.bo.weeklymonthlyrevenue.WMRevenueManagerFlashBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    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;

    /**
     * Manager Flash报告的Shiji解析实现类
     *
     * @author lichen
     * @date 2025/7/5
     */
    @Service
    @Slf4j
    @DS("rate_shop")
    public class WMRevenueManagerFlashParseImplByShiji implements IWMRevenueManagerFlashParseService {

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

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return WMRevenueCancellationSummaryBO 集合
         * @author lichen
         * @date 2025/12/12
         */
        @Override
        public List<WMRevenueManagerFlashBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            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 = ShijiFileCheckUtils.get(csvRecord, "A");

                    if ("Occupancy (%)".equalsIgnoreCase(description)) {
                        // Occ
                        BigDecimal occ = getBigDecimal(csvRecord, roundingMode);
                        managerFlashBO.setOcc(occ);
                    } else if ("ADR".equalsIgnoreCase(description)) {
                        // ADR
                        BigDecimal adr = getBigDecimal(csvRecord, roundingMode);
                        managerFlashBO.setAdr(adr);
                    } else if ("Complimentary - Rooms".equalsIgnoreCase(description)) {
                        Integer compRoom = getInteger(csvRecord);
                        managerFlashBO.setComplimentaryRooms(compRoom);
                    } else if ("No Show - Rooms".equalsIgnoreCase(description)) {
                        Integer noShowRooms = getInteger(csvRecord);
                        managerFlashBO.setNoShowRooms(noShowRooms);
                    } else if ("OOO - Rooms".equalsIgnoreCase(description)) {
                        Integer oooRooms = getInteger(csvRecord);
                        managerFlashBO.setOoo(oooRooms);
                    } else if ("Revenue - Room Total".equalsIgnoreCase(description)) {
                        // Room Revenue
                        BigDecimal roomRevenue = getBigDecimal(csvRecord, roundingMode);
                        managerFlashBO.setRoomRevenue(roomRevenue);
                    } else if ("Occupied Rooms".equalsIgnoreCase(description)) {
                        Integer roomSold = getInteger(csvRecord);
                        managerFlashBO.setRoomsSold(roomSold);
                    } else if ("Physical 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));

                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 lichen
         * @date 2025/12/15
         */
        private BigDecimal getBigDecimal(CSVRecord csvRecord, RoundingMode roundingMode) {
            String value = ShijiFileCheckUtils.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 lichen
         * @date 2025/12/15
         */
        private Integer getInteger(CSVRecord csvRecord) {
            String value = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "B");
            return StringUtils.isEmpty(value) ? null : Integer.valueOf(value);
        }

    }