# DbAutoResolveDbMssService.java

    package cloud.wconnect.workperformance.service.database.marketsegsummary;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.workperformance.cons.S3BucketFunctionCons;
    import cloud.wconnect.workperformance.cons.database.DbPmsReportTypeEnum;
    import cloud.wconnect.workperformance.entity.database.DbReportImportLog;
    import cloud.wconnect.workperformance.pojo.bo.database.historyforecast.DbPmsHfBO;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.database.IDbAutoResolveReportService;
    import cloud.wconnect.workperformance.service.database.IDbPmsHfHistoryService;
    import cloud.wconnect.workperformance.service.database.IDbReportImportLogService;
    import cloud.wconnect.workperformance.service.database.IDbReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * 自动解决 Market Segment Summary 报告
     *
     * @author zc
     * @date 2026/3/5
     */
    @Component
    @DS("database")
    public class DbAutoResolveDbMssService implements IDbAutoResolveReportService {

        @Resource
        private IDbReportImportLogService importLogService;
        @Resource
        private AmazonS3Service amazonS3Service;
        @Resource
        private IDbReportService reportService;
        @Resource
        private IDbPmsHfHistoryService hfHistoryService;

        /**
         * 根据判断是否支持此报告类型
         *
         * @param reportType 报告分类
         * @return boolean
         * @author zc
         * @date 2026/3/5
         */
        @Override
        public boolean supports(int reportType) {
            return reportType == DbPmsReportTypeEnum.MARKET_SEGMENT_SUMMARY.getType();
        }

        /**
         * 解决报告异常
         *
         * @param propertyDTO 资产
         * @param importLog   导入日志
         * @param operatorId  操作人 id
         * @param source      来源
         * @author zc
         * @date 2026/3/5
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void autoResolve(PropertyDTO propertyDTO, DbReportImportLog importLog, int operatorId, String source) {
            // 处理是否成功
            boolean handleResult = true;
            try (InputStream in = amazonS3Service.getFile(S3BucketFunctionCons.DATABASE_IMPORT_RECORD, importLog.getUrl())) {
                List<Long> dateRange = Lists.newArrayList(importLog.getStartDate(), importLog.getEndDate());
                // 解析报告
                List<DbPmsHfBO> boList = reportService.parseMarketSegmentSummaryReport(propertyDTO, in, source, dateRange);
                // 保存数据
                hfHistoryService.handle(propertyDTO, boList, importLog.getBusinessDate(), operatorId);
            } catch (Exception e) {
                handleResult = false;
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Auto Resolve Database Market Segment Summary report error, source=" + source, e);
            } finally {
                // 更新导入记录
                importLogService.autoSolve(importLog.getId(), handleResult, operatorId);
            }
        }
    }