# From30DaysAgoToYesterdayParseByOther.java

    package cloud.wconnect.rateshop.service.revenue.report.historyandforecast.from30daysagotoyesterday;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.FileCheckUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.rateshop.cons.revenue.RevenueImportCons;
    import cloud.wconnect.rateshop.entity.revenue.RevenueImportLog;
    import cloud.wconnect.rateshop.pojo.bo.revenue.HistoryAndForecastBO;
    import cloud.wconnect.rateshop.pojo.dto.revenue.HandleImportReportDTO;
    import cloud.wconnect.rateshop.pojo.dto.revenue.ResolveExceptionDTO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueImportService;
    import cloud.wconnect.rateshop.service.revenue.report.IImportRevenueReport;
    import cloud.wconnect.rateshop.service.revenue.report.IParseRevenueExcelService;
    import cloud.wconnect.rateshop.service.revenue.report.IRevenueReportCommonService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.InputStream;
    import java.util.List;
    import java.util.Map;

    /**
     * Other平台History and Forecast from thirty days ago to yesterday报告解析类
     */
    @Service
    @DS("rate_shop")
    public class From30DaysAgoToYesterdayParseByOther implements IImportRevenueReport {

        @Resource
        private IRevenueImportService revenueImportService;
        @Resource
        private IRevenueReportCommonService revenueReportCommonService;
        @Resource
        private IParseRevenueExcelService parseRevenueExcelService;
        @Value("${file-max-size.import-report.revenue}")
        private Long importReportFileMaxSize;

        @Value("${aws-s3.third-party.key.revenue.history-and-forecast-from-thirty-days-ago-to-yesterday}")
        private String historyAndForecastS3Key;
        @Value("${aws-s3.third-party.key.revenue.market-segment-summary-history-30-days}")
        private String marketSegmentSummaryS3Key;

        private static final String SOURCE = SystemPlatformEnum.OTHER.getDesc();

        /**
         * 判断支持的平台
         *
         * @param handleImportReportDTO 处理报告
         * @return boolean
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        public boolean supports(HandleImportReportDTO handleImportReportDTO) {
            return SOURCE.equals(handleImportReportDTO.getSource()) && getFileType().containsKey(handleImportReportDTO.getReportType());
        }

        /**
         * 获取文件类型
         *
         * @return java.util.Map<java.lang.Integer, java.lang.String>
         * @author yyt
         * @date 2026/3/4
         */
        private Map<Integer, String> getFileType() {
            return ImmutableMap.of(RevenueImportCons.HISTORY_AND_FORECAST_FROM_THIRTY_DAYS_AGO_TO_YESTERDAY, historyAndForecastS3Key,
                    RevenueImportCons.MARKET_SEGMENT_SUMMARY_HISTORY_30_DAYS, marketSegmentSummaryS3Key);
        }

        /**
         * 检查文件
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @return void
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        public void checkFile(PropertyDTO propertyDTO, MultipartFile file) {
            // 限制文件大小
            FileCheckUtils.checkFileSize(file, importReportFileMaxSize);

            // 校验文件格式
            UploadUtils.checkFileFormat(file, Lists.newArrayList("xls", "xlsx"));

            parseRevenueExcelService.checkHistoryAndForecastFromThirtyDaysAgoToYesterday(propertyDTO, file);
        }

        /**
         * 处理导入记录
         *
         * @param propertyDTO         资产
         * @param importLog           导入记录
         * @param resolveExceptionDTO 选择条件
         * @param file                文件
         * @param userId              操作人
         * @return cloud.wconnect.rateshop.entity.revenue.RevenueImportLog
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        public RevenueImportLog handleImportLog(PropertyDTO propertyDTO, RevenueImportLog importLog,
                                                ResolveExceptionDTO resolveExceptionDTO, MultipartFile file, int userId) {
            return revenueReportCommonService.handleImportLog(propertyDTO, importLog, resolveExceptionDTO, getFileType().get(resolveExceptionDTO.getFileType()), file, userId);
        }

        /**
         * 处理文件
         *
         * @param propertyDTO 资产信息
         * @param file        文件
         * @param importLog   导入记录
         * @param userId      操作人
         * @return void
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleFile(PropertyDTO propertyDTO, MultipartFile file, RevenueImportLog importLog, Integer userId) {
            boolean importSuccess = true;
            try (InputStream inputStream = file.getInputStream()) {
                List<HistoryAndForecastBO> historyAndForecastBOS = parseRevenueExcelService.parseHistoryAndForecastFromThirtyDaysAgoToYesterday(propertyDTO, inputStream,
                        importLog.getStartDate(), importLog.getEndDate());
                // 保存文件数据
                revenueImportService.saveHistoryAndForecastData(propertyDTO, historyAndForecastBOS, userId);
            } catch (Exception e) {
                importSuccess = false;
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse report error", e);
            } finally {
                revenueReportCommonService.updateImportStatus(importLog, importSuccess, userId);
            }
        }

    }