# DbPmsManagerFlashLast2DayListener.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.managerflash.DbPmsManagerFlashBO;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.database.IDbPmsManagerFlashService;
    import cloud.wconnect.workperformance.service.database.IDbReportImportLogService;
    import cloud.wconnect.workperformance.service.database.IDbReportService;
    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.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.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.concurrent.TimeUnit;

    /**
     * 报告处理：manager-flash/service--2d
     *
     * @author zc
     * @date 2025/12/02
     */
    @Slf4j
    @Service
    public class DbPmsManagerFlashLast2DayListener {

        @Resource
        private IDbReportService reportService;
        @Resource
        private IDbPmsManagerFlashService managerFlashService;
        @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;

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

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

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

            if (StringUtils.isBlank(event.getBody())) {
                log.warn("DbPmsManagerFlashLast2DayListener.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("DbPmsManagerFlashLast2DayListener.handle end... Property not support this PMS type, " +
                                "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("DbPmsManagerFlashLast2DayListener.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("DbPmsManagerFlashLast2DayListener.handle end... date range is empty.");
                return;
            }

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

                log.error("DbPmsManagerFlashLast2DayListener.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.MANAGER_FLASH.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.MANAGER_FLASH.getType(), operatorId);
                    }
                }
            }

            log.info("DbPmsManagerFlashLast2DayListener.handle end...");
        }

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

            // 获取当前时间作为 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("DbPmsManagerFlashLast2DayListener.handle end... date range is empty.");
                return;
            }
            // 插入报告缺失记录
            importLogService.save(propertyDTO, businessDate, dateRange, path, ReportImportResultCons.IMPORT_RESULT_MISSED,
                    DbPmsReportTypeEnum.MANAGER_FLASH.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("DbPmsManagerFlashLast2DayListener.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_MANAGER_FLASH_LAST_2_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("DbPmsManagerFlashLast2DayListener.handle end... The property:{} is not exist", propertyAbbr);
                return null;
            }
            if (!propertyDTO.getStatus()) {
                log.warn("DbPmsManagerFlashLast2DayListener.handle end...  The property:{} is closed", propertyAbbr);
                return null;
            }
            if (!commonService.checkOpenFunction(propertyDTO, FunctionIdConstant.DATABASE)) {
                log.warn("DbPmsManagerFlashLast2DayListener.handle end... The property:{} has not opened Database function", propertyAbbr);
                return null;
            }
            return propertyDTO;
        }

        /**
         * 发送预警邮件
         *
         * @param propertyAbbr     资产简称
         * @param exceptionMessage 异常信息
         * @param s3Key            s3路径
         * @author zc
         * @date 2025/12/02
         */
        private void sendAlertEmail(String propertyAbbr, String exceptionMessage, String s3Key) {
            String subject = "[Warning] - Handle Manager Flash -2d Report error";
            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, propertyAbbr, 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);
        }
    }