# HFNextMonthParseByOther.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    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.service.RoomsConfigInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    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.OtbAndTransientGroupBO;
    import cloud.wconnect.rateshop.pojo.dto.revenue.ResolveExceptionDTO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueAdrConfigService;
    import cloud.wconnect.rateshop.service.revenue.IRevenueImportService;
    import cloud.wconnect.rateshop.service.revenue.report.IImportRevenueReport;
    import cloud.wconnect.rateshop.service.revenue.report.IRevenueReportCommonService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.InputStream;
    import java.time.format.DateTimeFormatter;
    import java.util.Map;

    /**
     * @author: yyt
     * 2024-08-14
     */
    @Service
    @DS("rate_shop")
    public class HFNextMonthParseByOther implements IImportRevenueReport {

        @Resource
        private IRevenueImportService revenueImportService;
        @Resource
        private IRevenueReportCommonService revenueImportCommonService;
        @Resource
        private RoomsConfigInternalService roomsConfigInternalService;
        @Resource
        private IRevenueAdrConfigService revenueAdrConfigService;
        @Autowired
        private ApplicationContext applicationContext;

        @Value("${file-max-size.import-report.revenue}")
        private Long importReportFileMaxSize;

        @Value("${aws-s3.third-party.key.revenue.history-and-forecast-next-month}")
        private String s3Key;

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

        private static final int FILE_TYPE = RevenueImportCons.HISTORY_FORECAST_NEXT_MONTH;

        /**
         * 判断支持的平台
         *
         * @param source   平台
         * @param fileType 文件类型
         * @return boolean
         * @author yyt
         * @date 2024/8/14
         */
        @Override
        public boolean supports(String source, int fileType) {
            return SOURCE.equals(source) && FILE_TYPE == fileType;
        }

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

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

            Map<String, IParseHFNextMonthExcelService> beans = applicationContext.getBeansOfType(IParseHFNextMonthExcelService.class);
            for (IParseHFNextMonthExcelService bean : beans.values()) {
                if (bean.supports(propertyDTO.getPmsType())) {
                    bean.checkFile(propertyDTO, file);
                    return;
                }
            }
        }

        /**
         * 处理导入记录
         *
         * @param propertyDTO         资产
         * @param importLog           导入记录
         * @param resolveExceptionDTO 选择条件
         * @param file                文件
         * @param userId              操作人
         * @return 导入记录
         * @author yyt
         * @date 2024/8/14
         **/
        @Override
        public RevenueImportLog handleImportLog(PropertyDTO propertyDTO, RevenueImportLog importLog,
                                                ResolveExceptionDTO resolveExceptionDTO, MultipartFile file, int userId) {
            return revenueImportCommonService.handleImportLog(propertyDTO, importLog, resolveExceptionDTO, s3Key, file, userId);
        }

        /**
         * 处理文件
         *
         * @param propertyDTO 资产信息
         * @param file        文件
         * @param importLog   导入记录
         * @param userId      操作人
         * @author yyt
         * @date 2024/8/14
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleFile(PropertyDTO propertyDTO, MultipartFile file, RevenueImportLog importLog, Integer userId) {
            boolean importSuccess = true;
            try (InputStream inputStream = file.getInputStream()) {
                Map<String, IParseHFNextMonthExcelService> beans = applicationContext.getBeansOfType(IParseHFNextMonthExcelService.class);
                for (IParseHFNextMonthExcelService bean : beans.values()) {
                    if (bean.supports(propertyDTO.getPmsType())) {
                        OtbAndTransientGroupBO otbAndTransientGroupBO = bean.handleFile(propertyDTO, inputStream,
                                importLog.getStartDate(), importLog.getEndDate());

                        // 保存文件数据
                        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                        String targetDate = DateTimeUtils.toZonedDateTime(importLog.getTheDate(), propertyDTO.getTimezone()).format(dateTimeFormatter);
                        revenueImportService.saveOtbAndTransientGroupNextMonthData(propertyDTO, targetDate, otbAndTransientGroupBO, userId);
                        return;
                    }
                }
            } catch (Exception e) {
                importSuccess = false;
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse report error", e);
            } finally {
                revenueImportCommonService.updateImportStatus(importLog, importSuccess, userId);
            }
        }

    }