# DbPmsMarketCodeStatisticsLast1DayListener.java

    package cloud.wconnect.workperformance.messagelistener.database;

    import cloud.wconnect.commonlib.cons.*;
    import cloud.wconnect.commonlib.pojo.dto.EventBodyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyBusinessDateRedisService;
    import cloud.wconnect.commonlib.service.CommonService;
    import cloud.wconnect.commonlib.service.MailInternalService;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.RateLimitUtils;
    import cloud.wconnect.sns.http.entity.SnsHttpEvent;
    import cloud.wconnect.workperformance.cons.BusinessEnum;
    import cloud.wconnect.workperformance.cons.EventSubjectEnum;
    import cloud.wconnect.workperformance.cons.S3BucketFunctionCons;
    import cloud.wconnect.workperformance.cons.database.DbNotifyTypeEnum;
    import cloud.wconnect.workperformance.cons.database.DbPmsReportTypeEnum;
    import cloud.wconnect.workperformance.pojo.bo.database.marketcode.DbPmsMarketCodeStatisticsBO;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.database.IDbPmsMarketCodeStatisticsService;
    import cloud.wconnect.workperformance.service.database.IDbReportImportLogService;
    import cloud.wconnect.workperformance.service.database.IDbReportService;
    import cloud.wconnect.workperformance.service.database.marketcode.IDbPmsMarketCodeStatisticsParseService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.event.EventListener;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;

    import java.io.IOException;
    import java.io.InputStream;
    import java.text.MessageFormat;
    import java.util.*;
    import java.util.concurrent.TimeUnit;

    /**
     * 报告处理：market-code-statistics/service--1d
     *
     * @author zc
     * @date 2025/12/12
     */
    @Slf4j
    @Service
    public class DbPmsMarketCodeStatisticsLast1DayListener {

        @Resource
        private IDbReportService reportService;
        @Resource
        private IDbPmsMarketCodeStatisticsService marketCodeStatisticsService;
        @Resource
        private IDbReportImportLogService importLogService;
        @Resource
        private MailInternalService mailInternalService;
        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private PropertyInternalService propertyInternalService;
        @Resource
        private PropertyBusinessDateRedisService propertyBusinessDateRedisService;
        @Resource
        private RedisTemplate<String, Object> jsonRedisTemplate;
        @Value("${default.timezone}")
        private String defaultTimezone;
        @Resource
        private AmazonS3Service amazonS3Service;
        @Resource
        private CommonService commonService;
        @Resource
        private ApplicationContext applicationContext;

        private final String SUBJECT = EventSubjectEnum.MARKET_CODE_STATISTICS_LAST_1_DAY.getSubject();
        private final String BUSINESS = BusinessEnum.DATABASE.getBusiness();

        /**
         * 异步监听事件处理
         *
         * @param event 事件对象
         * @author zc
         * @date 2025/12/12
         */
        @Async
        @EventListener
        public void handle(SnsHttpEvent event) {
            if (!StringUtils.equals(SUBJECT, event.getSubject())) {
                return;
            }
            if (!preventConcurrency(event.getMessageId())) {
                return;
            }

            log.info("DbPmsMarketCodeStatisticsLast1DayListener.handle start...");

            if (StringUtils.isBlank(event.getBody())) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... event body is blank.");
                return;
            }

            // 解析事件体内容
            EventBodyDTO eventBodyDTO = JsonUtils.convertStringToObject(objectMapper, event.getBody(), EventBodyDTO.class);
            Map<String, Object> parameter = eventBodyDTO.getParameter();
            // 获取租户标识
            String tenantSymbol = (String) parameter.get(EventBodyParamEnum.TENANT_SYMBOL.getDesc());
            // 资产标识
            List<String> abbrNameList = (List<String>) parameter.get(EventBodyParamEnum.PROPERTY_ABBR.getDesc());
            String propertyAbbr = abbrNameList.get(0);

            // 获取有效的资产数据
            PropertyDTO propertyDTO = getEffectiveProperty(tenantSymbol, propertyAbbr);
            if (Objects.isNull(propertyDTO)) {
                return;
            }

            // 获取资产 PMS 类型
            String source = (String) parameter.get(EventBodyParamEnum.SOURCE.getDesc());

            // 校验事件 PMS 类型是否与资产配置匹配
            if (!StringUtils.equals(propertyDTO.getPmsType(), source)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... Property not support this PMS type, " +
                                "property:{}, supportPMSType:{}, currentPMSType:{}",
                        propertyDTO.getAbbr(), propertyDTO.getPmsType(), source);
                return;
            }

            // 校验解析器是否支持
            IDbPmsMarketCodeStatisticsParseService parser = getReportParser(source);
            if (Objects.isNull(parser)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... parser not find, " +
                                "property:{}, supportPMSType:{}, currentPMSType:{}",
                        propertyDTO.getAbbr(), propertyDTO.getPmsType(), source);
                return;
            }

            Long logId = Objects.isNull(parameter.get(EventBodyParamEnum.ID.getDesc())) ? null :
                    Long.parseLong(parameter.get(EventBodyParamEnum.ID.getDesc()).toString());

            int operatorId = Integer.parseInt(parameter.get(EventBodyParamEnum.UID.getDesc()).toString());
            String path = eventBodyDTO.getPath();

            // 获取营业日期
            Long businessDate = getBusinessDate(propertyDTO, operatorId, parameter);
            if (Objects.isNull(businessDate)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... business date is null.");
                // 处理营业日期失效或为空的情况
                handleMissingBusinessDate(propertyDTO, propertyAbbr, parameter, operatorId, path);
                return;
            }

            // 获取数据日期范围
            List<Long> dateRange = getDateRange(businessDate, parameter, operatorId);
            if (CollectionUtils.isEmpty(dateRange)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... date range is empty.");
                return;
            }

            // 处理是否成功
            boolean handleResult = true;
            try (InputStream in = amazonS3Service.getFile(path)) {
                // 解析报告
                DbPmsMarketCodeStatisticsBO bo = reportService.parseMarketCodeStatisticsReport(propertyDTO, in, source, dateRange);
                // 保存数据
                marketCodeStatisticsService.handle(propertyDTO, bo, operatorId);
            } catch (Exception e) {
                handleResult = false;
                if (RateLimitUtils.tryAcquire(this, propertyAbbr)) {
                    sendAlertEmail(propertyDTO, e.getMessage(), path);
                }

                log.error("DbPmsMarketCodeStatisticsLast1DayListener.handle end... Process error.", e);
            } finally {
                // 自动导入报告时插入处理时机，用于定时检查报告缺失逻辑
                if (operatorId == 0) {
                    updateLastTime(propertyAbbr);
                }

                // 上传报告，用于日志下载报告
                uploadReport(path);

                int importResult = handleResult ? ReportImportResultCons.IMPORT_RESULT_SUCCESS :
                        ReportImportResultCons.IMPORT_RESULT_FAIL;
                if (Objects.isNull(logId)) {
                    // 生成导入记录
                    importLogService.save(propertyDTO, businessDate, dateRange, path, importResult,
                            DbPmsReportTypeEnum.MARKET_CODE_STATISTICS.getType(), 0);
                } else {
                    String eventBusiness = (String) parameter.get(EventBodyParamEnum.ORIGINATE_FROM.getDesc());
                    if (BUSINESS.equals(eventBusiness)) {
                        // 修改导入记录状态
                        importLogService.update(logId, operatorId, path, handleResult);
                    } else {
                        importLogService.save(propertyDTO, businessDate, dateRange, path, importResult,
                                DbPmsReportTypeEnum.MARKET_CODE_STATISTICS.getType(), operatorId);
                    }
                }
            }
            log.info("DbPmsMarketCodeStatisticsLast1DayListener.handle end...");
        }

        /**
         * 获取报告解析器
         *
         * @param source 来源
         * @return 解析器
         * @author zc
         * @date 2026/3/6
         */
        private IDbPmsMarketCodeStatisticsParseService getReportParser(String source) {
            Collection<IDbPmsMarketCodeStatisticsParseService> parserList = applicationContext
                    .getBeansOfType(IDbPmsMarketCodeStatisticsParseService.class).values();
            for (IDbPmsMarketCodeStatisticsParseService item : parserList) {
                if (item.supports(source)) {
                    return item;
                }
            }
            return null;
        }

        /**
         * 处理缺少营业日期的情况
         *
         * @param propertyDTO  资产信息
         * @param propertyAbbr 资产标识
         * @param parameter    参数
         * @param operatorId   操作员ID
         * @param path         文件路径
         * @author zc
         * @date 2025/12/12
         */
        private void handleMissingBusinessDate(PropertyDTO propertyDTO, String propertyAbbr, Map<String, Object> parameter,
                                               int operatorId, String path) {
            if (operatorId != 0) {
                return;
            }
            // 自动导入报告时插入处理时机，用于定时检查报告缺失逻辑
            updateLastTime(propertyAbbr);

            // 上传报告，用于日志下载报告
            uploadReport(path);

            // 获取当前时间作为 BusinessDate
            long businessDate = Long.parseLong(DateTimeUtils.dateNowStr(propertyDTO.getTimezone(), BaseConstant.DATE_FORMAT_YYYYMMDD));

            // 获取数据日期范围
            List<Long> dateRange = getDateRange(businessDate, parameter, operatorId);
            if (CollectionUtils.isEmpty(dateRange)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... date range is empty.");
                return;
            }
            // 插入报告缺失记录
            importLogService.save(propertyDTO, businessDate, dateRange, path, ReportImportResultCons.IMPORT_RESULT_MISSED,
                    DbPmsReportTypeEnum.MARKET_CODE_STATISTICS.getType(), 0);
        }

        /**
         * 上传报告，用于日志下载报告
         *
         * @param path S3 路径
         * @author zc
         * @date 2025/12/02
         */
        private void uploadReport(String path) {
            // 上传报告，用于日志下载报告
            try (InputStream in = amazonS3Service.getFile(path)) {
                amazonS3Service.uploadFile(S3BucketFunctionCons.DATABASE_IMPORT_RECORD, in, path);
            } catch (IOException e) {
                log.error("DbPmsMarketCodeStatisticsLast1DayListener.handle end... Upload report error.", e);
            }
        }

        /**
         * 获取资产营业日期
         *
         * @param propertyDTO 资产
         * @param operatorId  操作人 id
         * @param parameter   事件体参数
         * @return 资产营业日期(yyyyMMdd)
         * @author zc
         * @date 2025/12/03
         */
        private Long getBusinessDate(PropertyDTO propertyDTO, int operatorId, Map<String, Object> parameter) {
            // 自动下发的报告时，使用缓存中的业务日期
            if (operatorId == 0) {
                return propertyBusinessDateRedisService.getEffectiveBusinessDate(propertyDTO.getId(), propertyDTO.getTimezone());
            }
            Object businessDateObj = parameter.get(EventBodyParamEnum.BUSINESS_DATE.getDesc());
            if (Objects.isNull(businessDateObj)) {
                return null;
            }
            return Long.parseLong(businessDateObj.toString());
        }

        /**
         * 获取报告数据日期范围
         *
         * @param businessDate 营业日期（yyyyMMdd）
         * @param parameter    事件体参数
         * @param operatorId   操作人 id
         * @return 数据日期范围
         * @author zc
         * @date 2025/12/02
         */
        private List<Long> getDateRange(long businessDate, Map<String, Object> parameter, int operatorId) {
            // 自动下发
            if (operatorId == 0) {
                return reportService.getDateRange(businessDate, DbNotifyTypeEnum.MISSING_REPORT_MARKET_CODE_STATISTICS_LAST_1_DAY.getType());
            }
            // 手动导入
            Object startDateObj = parameter.get(EventBodyParamEnum.START_DATE.getDesc());
            Object endDateObj = parameter.get(EventBodyParamEnum.END_DATE.getDesc());
            if (Objects.isNull(startDateObj) || Objects.isNull(endDateObj)) {
                return Collections.emptyList();
            }
            return List.of(Long.parseLong(startDateObj.toString()), Long.parseLong(endDateObj.toString()));
        }

        /**
         * 获取有效的资产数据
         *
         * @param tenantSymbol 租户标识
         * @param propertyAbbr 资产标识
         * @return PropertyDTO
         * @author zc
         * @date 2025/12/02
         */
        private PropertyDTO getEffectiveProperty(String tenantSymbol, String propertyAbbr) {
            PropertyDTO propertyDTO = propertyInternalService.getOne(propertyAbbr, tenantSymbol);
            if (Objects.isNull(propertyDTO)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... The property:{} is not exist", propertyAbbr);
                return null;
            }
            if (!propertyDTO.getStatus()) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end...  The property:{} is closed", propertyAbbr);
                return null;
            }
            if (!commonService.checkOpenFunction(propertyDTO, FunctionIdConstant.DATABASE)) {
                log.warn("DbPmsMarketCodeStatisticsLast1DayListener.handle end... The property:{} has not opened Database function", propertyAbbr);
                return null;
            }
            return propertyDTO;
        }

        /**
         * 发送预警邮件
         *
         * @param propertyDTO      资产
         * @param exceptionMessage 异常信息
         * @param s3Key            s3路径
         * @author zc
         * @date 2025/12/02
         */
        private void sendAlertEmail(PropertyDTO propertyDTO, String exceptionMessage, String s3Key) {
            String reportName = propertyInternalService.getPmsReportConfig(propertyDTO.getId(),
                    propertyDTO.getPmsType(), PmsReportEnum.MARKET_CODE.getSymbol()).getReportName();
            String subject = "[Warning] - Handle Report error[" + reportName + "(-1d)]";
            String content = "Process error: " + exceptionMessage;
            String mailContent = MessageFormat.format("SNS Subject:{0}<br/>Business:{1}<br/>Property:{2}<br/>Content:{3}<br/>S3Key:{4}",
                    SUBJECT, BUSINESS, propertyDTO.getAbbr(), content, s3Key);
            mailInternalService.sendAlertEmail(subject, mailContent);
        }

        /**
         * 防止并发操作
         *
         * @param messageId 消息事件 id
         * @return java.lang.Boolean
         * @author zc
         * @date 2025/12/02
         */
        private Boolean preventConcurrency(String messageId) {
            String key = EventRedisKeyPrefixEnum.KEY_PREFIX_FOR_MESSAGE.getDesc() +
                    StringUtils.join(Lists.newArrayList(SUBJECT, BUSINESS, messageId), ".");
            return jsonRedisTemplate.opsForValue().setIfAbsent(key, 0L, 1, TimeUnit.DAYS);
        }

        /**
         * 保存事件最近处理时刻
         *
         * @param propertyAbbr 资产标识
         * @author zc
         * @date 2025/12/02
         */
        private void updateLastTime(String propertyAbbr) {
            String key = EventRedisKeyPrefixEnum.KEY_PREFIX_FOR_PROCESSING.getDesc() +
                    StringUtils.join(Lists.newArrayList(SUBJECT, BUSINESS, propertyAbbr), ".");
            String value = String.valueOf(DateTimeUtils.dateTimeNow(defaultTimezone).toEpochSecond());
            jsonRedisTemplate.opsForValue().set(key, value);
        }
    }