# DbReportServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.exception.ListenerException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.cons.database.DbPmsReportDataRangeEnum;
    import cloud.wconnect.workperformance.pojo.bo.database.historyforecast.DbPmsHfBO;
    import cloud.wconnect.workperformance.pojo.bo.database.managerflash.DbPmsManagerFlashBO;
    import cloud.wconnect.workperformance.pojo.bo.database.marketcode.DbPmsMarketCodeStatisticsBO;
    import cloud.wconnect.workperformance.service.database.IDbReportService;
    import cloud.wconnect.workperformance.service.database.historyforecast.IDbPmsHfParseService;
    import cloud.wconnect.workperformance.service.database.managerflash.IDbPmsManagerFlashParseService;
    import cloud.wconnect.workperformance.service.database.marketcode.IDbPmsMarketCodeStatisticsParseService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import jakarta.annotation.Resource;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.ApplicationContext;
    import org.springframework.stereotype.Service;

    import java.io.InputStream;
    import java.text.MessageFormat;
    import java.time.LocalDate;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;

    /**
     * <p>
     * Database Report 服务类
     * </p>
     *
     * @author zc
     * @date 2025/12/02
     */
    @Slf4j
    @Service
    @DS("database")
    public class DbReportServiceImpl implements IDbReportService {
        @Resource
        private ApplicationContext applicationContext;

        /**
         * 解析报告 Manager Flash
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param source      报告来源
         * @param dateRange   数据日期范围（yyyyMMdd）
         * @return DbPmsManagerFlashBO
         * @author zc
         * @date 2025/12/02
         */
        @Override
        public DbPmsManagerFlashBO parseManagerFlashReport(PropertyDTO propertyDTO, InputStream in, String source, List<Long> dateRange) {
            Collection<IDbPmsManagerFlashParseService> parserList = applicationContext.getBeansOfType(IDbPmsManagerFlashParseService.class).values();
            for (IDbPmsManagerFlashParseService parser : parserList) {
                if (parser.supports(source)) {
                    return parser.parse(propertyDTO, in, dateRange);
                }
            }
            throw new ListenerException(MessageFormat.format("Not found report parser, Report Type: Manager Flash, " +
                    "Source: {0}", source));
        }

        /**
         * 解析报告 Market Code Statistics
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param source      报告来源
         * @param dateRange   数据日期范围（yyyyMMdd）
         * @return DbPmsMarketCodeStatisticsBO
         * @author zc
         * @date 2025/12/12
         */
        @Override
        public DbPmsMarketCodeStatisticsBO parseMarketCodeStatisticsReport(PropertyDTO propertyDTO, InputStream in, String source, List<Long> dateRange) {
            Collection<IDbPmsMarketCodeStatisticsParseService> parserList = applicationContext.getBeansOfType(IDbPmsMarketCodeStatisticsParseService.class).values();
            for (IDbPmsMarketCodeStatisticsParseService parser : parserList) {
                if (parser.supports(source)) {
                    return parser.parse(propertyDTO, in, dateRange);
                }
            }
            throw new ListenerException(MessageFormat.format("Not found report parser, Report Type: Market Code Statistics, " +
                    "Source: {0}", source));
        }

        /**
         * 解析报告 History and Forecast
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param source      报告来源
         * @param dateRange   数据日期范围（yyyyMMdd）
         * @return List<DbPmsHfBO>
         * @author zc
         * @date 2026/1/5
         */
        @Override
        public List<DbPmsHfBO> parseHistoryAndForecastReport(PropertyDTO propertyDTO, InputStream in, String source, List<Long> dateRange) {
            Collection<IDbPmsHfParseService> parserList = applicationContext.getBeansOfType(IDbPmsHfParseService.class).values();
            for (IDbPmsHfParseService parser : parserList) {
                if (parser.supports(source)) {
                    return parser.parse(propertyDTO, in, dateRange);
                }
            }
            throw new ListenerException(MessageFormat.format("Not found report parser, Report Type: History and Forecast, " +
                    "Source: {0}", source));
        }

        /**
         * 获取数据的时间范围
         *
         * @param businessDate 营业日期（yyyyMMdd）
         * @param type         类型
         * @return 时间范围
         * @author zc
         * @date 2025/12/02
         */
        @Override
        public List<Long> getDateRange(long businessDate, int type) {
            LocalDate localDate = DateTimeUtils.strToLocalDate(String.valueOf(businessDate),
                    BaseConstant.DATE_FORMAT_YYYYMMDD);
            List<Long> dateRange = Arrays.stream(DbPmsReportDataRangeEnum.getDateRange(type)
                    .split(",")).map(Long::parseLong).toList();

            return List.of(Long.parseLong(DateTimeUtils.localDateToStr(localDate.plusDays(dateRange.get(0)),
                            BaseConstant.DATE_FORMAT_YYYYMMDD)),
                    Long.parseLong(DateTimeUtils.localDateToStr(localDate.plusDays(dateRange.get(1)),
                            BaseConstant.DATE_FORMAT_YYYYMMDD)));
        }
    }