# GmwrManagerFlashParseServiceByPep.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.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.PropertyRevenueDescriptionDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationNumberDTO;
    import cloud.wconnect.commonlib.pojo.query.PropertyRevenueDescriptionQuery;
    import cloud.wconnect.commonlib.service.PropertyRevenueDescriptionInternalService;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.workperformance.pojo.bo.gmwr.GmwrManagerFlashBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import lombok.RequiredArgsConstructor;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Component;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * 解析 PEP 系统的 Manager Flash/Service 报告
     *
     * @author yyt
     * @date 2026/2/5
     */
    @RequiredArgsConstructor
    @Component
    @DS("work_performance")
    public class GmwrManagerFlashParseServiceByPep implements IGmwrManagerFlashParseService {

        private final PropertyRevenueDescriptionInternalService propertyRevenueDescriptionInternalService;

        /**
         * 支持的报告来源
         *
         * @param source 报告来源
         * @return boolean
         * @author yyt
         * @date 2026/2/5
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.PEP.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @return GmwrManagerFlashBO
         * @author yyt
         * @date 2026/2/5
         */
        @Override
        public GmwrManagerFlashBO parse(PropertyDTO propertyDTO, InputStream in) throws ParseReportException {
            // 查询资产 revenue description 配置
            Integer propertyId = propertyDTO.getId();
            List<PropertyRevenueDescriptionDTO> revenueDescriptionDTOS = propertyRevenueDescriptionInternalService.list(new PropertyRevenueDescriptionQuery(propertyId));
            if (CollectionUtils.isEmpty(revenueDescriptionDTOS)) new GmwrManagerFlashBO();

            Map<Integer, List<String>> namesByType = revenueDescriptionDTOS.stream().collect(Collectors.groupingBy(PropertyRevenueDescriptionDTO::getType,
                    Collectors.mapping(item -> item.getName().toLowerCase(), Collectors.toList())));
            List<String> fbNames = namesByType.getOrDefault(PropertyRevenueDescriptionTypeCons.FB, Collections.emptyList());
            List<String> otherNames = namesByType.getOrDefault(PropertyRevenueDescriptionTypeCons.OTHER, Collections.emptyList());

            BigDecimal totalFbRevenue = null;
            BigDecimal totalOthersRevenue = null;

            InternationalizationNumberDTO number = propertyDTO.getInternationalization().getNumber();
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(number.getCalculationRule());
            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 = CsvFileUtils.get(csvRecord, "A");
                    if (StringUtils.isEmpty(description) || description.equalsIgnoreCase("Description")) continue;

                    if (fbNames.contains(description.toLowerCase())) {
                        BigDecimal revenue = getRevenue(csvRecord, "C", roundingMode);
                        totalFbRevenue = totalFbRevenue == null ? revenue : totalFbRevenue.add(revenue);
                    }
                    if (otherNames.contains(description.toLowerCase())) {
                        BigDecimal revenue = getRevenue(csvRecord, "C", roundingMode);
                        totalOthersRevenue = totalOthersRevenue == null ? revenue : totalOthersRevenue.add(revenue);
                    }
                }

                GmwrManagerFlashBO managerFlashBO = new GmwrManagerFlashBO();
                managerFlashBO.setFbRevenue(totalFbRevenue);
                managerFlashBO.setOthersRevenue(totalOthersRevenue);
                return managerFlashBO;
            } catch (IOException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "GM Weekly Report parse Manager Flash pep report error", e);
            }
        }

        /**
         * 获取收入
         *
         * @param csvRecord CSVRecord
         * @param column    列
         * @return java.math.BigDecimal
         * @author yyt
         * @date 2026/2/5
         */
        private BigDecimal getRevenue(CSVRecord csvRecord, String column, RoundingMode roundingMode) {
            String fbRevenueStr = CsvFileUtils.getAndFormatNumber(csvRecord, column);
            if (StringUtils.isNotEmpty(fbRevenueStr)) {
                return new BigDecimal(fbRevenueStr).setScale(BaseConstant.DATA_PRECISION, roundingMode);
            }
            return null;
        }

    }