# SvaOperatingStatisticsDailyListener.java

    package cloud.wconnect.workperformance.messagelistener.sva;

    import cloud.wconnect.commonlib.cons.EventBodyParamEnum;
    import cloud.wconnect.commonlib.cons.EventRedisKeyPrefixEnum;
    import cloud.wconnect.commonlib.cons.FunctionIdConstant;
    import cloud.wconnect.commonlib.cons.ReportImportResultCons;
    import cloud.wconnect.commonlib.pojo.dto.EventBodyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    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.operatingstatisticsdaily.OperatingStatisticsDatasourceEnum;
    import cloud.wconnect.workperformance.cons.sva.SvaNotifyTypeEnum;
    import cloud.wconnect.workperformance.pojo.bo.sva.operatingstatistics.OperatingStatisticsListBO;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.sva.operatingstatistics.IOperStatisImportLogService;
    import cloud.wconnect.workperformance.service.sva.operatingstatistics.IOperatingStatisticsService;
    import cloud.wconnect.workperformance.service.sva.scheduleactual.ISvaSchedulerService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import jakarta.annotation.Resource;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.collections.MapUtils;
    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.Component;

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

    /**
     * 事件处理：Operating Statistics 报告 for Schedule vs Actual Operating Statistics Daily
     *
     * @author zc
     * @date 2023/03/18
     */
    @Slf4j
    @Component
    public class SvaOperatingStatisticsDailyListener {
        @Resource
        private IOperatingStatisticsService operatingStatisticsService;
        @Resource
        private IOperStatisImportLogService importLogService;
        @Resource
        private ISvaSchedulerService schedulerService;
        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private MailInternalService mailInternalService;
        @Resource
        private PropertyInternalService propertyInternalService;
        @Resource
        private RedisTemplate<String, Object> jsonRedisTemplate;
        @Resource
        private AmazonS3Service amazonS3Service;

        @Value("${default.timezone}")
        private String defaultTimezone;
        @Resource
        private CommonService commonService;

        private static final String SUBJECT = EventSubjectEnum.ACCOUNTING_OPERATING_STATISTICS.getSubject();
        private static final String BUSINESS = BusinessEnum.SVA_OPERATING_STATISTICS.getBusiness();

        /**
         * 异步监听事件处理
         *
         * @param event 事件体
         * @author zc
         * @date 2023/3/8
         */
        @Async
        @EventListener
        public void handle(SnsHttpEvent event) {
            if (!StringUtils.equals(SUBJECT, event.getSubject())) {
                return;
            }
            log.info("SvaOperatingStatisticsDailyListener.handle start...");
            if (!preventConcurrency(event.getMessageId())) {
                log.warn("SvaOperatingStatisticsDailyListener.handle end... a machine has been executed.");
                return;
            }
            if (StringUtils.isBlank(event.getBody())) {
                log.warn("SvaOperatingStatisticsDailyListener.handle end... event body is blank.");
                return;
            }

            // 解析事件体内容
            EventBodyDTO eventBodyDTO = JsonUtils.convertStringToObject(objectMapper, event.getBody(), EventBodyDTO.class);
            Map<String, Object> parameter = eventBodyDTO.getParameter();
            String tenantSymbol = parameter.get(EventBodyParamEnum.TENANT_SYMBOL.getDesc()).toString();
            List<String> propertyAbbrList = (List<String>) parameter.get(EventBodyParamEnum.PROPERTY_ABBR.getDesc());

            // 获取有效的资产数据
            List<PropertyDTO> propertyDTOList = listEffectiveProperty(tenantSymbol, propertyAbbrList);
            if (CollectionUtils.isEmpty(propertyDTOList)) {
                log.warn("SvaOperatingStatisticsDailyListener.handle end... propertyDTOList is empty.");
                return;
            }

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

            // 获取各资产的报告数据日期范围
            Map<Integer, List<Long>> propertyId2DateRange = getDateRangeMap(propertyDTOList, parameter, operatorId);
            if (MapUtils.isEmpty(propertyId2DateRange)) {
                log.warn("SvaOperatingStatisticsDailyListener.handle end... propertyId2DateRange is empty.");
                return;
            }
            // 有日期范围的资产DTO集合
            Set<Integer> validPropertyIds = propertyId2DateRange.keySet();
            List<PropertyDTO> validPropertyDTOList = propertyDTOList.stream().filter(propertyDTO -> validPropertyIds.contains(propertyDTO.getId())).toList();

            // 处理是否成功
            boolean handleResult = true;
            Map<Integer, Integer> propertyId2DataUpdateResult = new HashMap<>();
            try (InputStream in = amazonS3Service.getFile(path)) {
                // 解析报告
                List<OperatingStatisticsListBO> operatingListBOList = operatingStatisticsService.parseReport(validPropertyDTOList,
                        in, source, propertyId2DateRange);
                // 处理报告数据
                propertyId2DataUpdateResult = operatingStatisticsService.handleOperatingStatistics(validPropertyDTOList, operatingListBOList, operatorId,
                        propertyId2DateRange, OperatingStatisticsDatasourceEnum.CSV.getValue());

                log.info("SvaOperatingStatisticsDailyListener.handle end...");
            } catch (Exception e) {
                handleResult = false;
                String propertyAbbr = propertyAbbrList.stream().sorted().collect(Collectors.joining(","));
                if (RateLimitUtils.tryAcquire(this, propertyAbbr)) {
                    sendAlertEmail(propertyAbbr, e.getMessage(), path);
                }

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

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

                int importResult = handleResult ? ReportImportResultCons.IMPORT_RESULT_SUCCESS :
                        ReportImportResultCons.IMPORT_RESULT_FAIL;

                // 系统下发
                if (operatorId == 0) {
                    // 系统下发，新增导入日志
                    importLogService.addBatch(validPropertyDTOList, propertyId2DateRange, importResult, operatorId, path, propertyId2DataUpdateResult);
                } else {
                    String eventBusiness = (String) parameter.get(EventBodyParamEnum.ORIGINATE_FROM.getDesc());
                    if (BUSINESS.equals(eventBusiness)) {
                        Object logIdsObj = parameter.get(EventBodyParamEnum.ID.getDesc());
                        if (Objects.nonNull(logIdsObj)) {
                            // 手动导入或解决异常，更新导入日志
                            List<Long> importLogIds = JsonUtils.convertStringToList(objectMapper, logIdsObj.toString(), Long.class);
                            importLogService.updateBatch(importLogIds, handleResult, operatorId, path, propertyId2DataUpdateResult);
                        }
                    } else {
                        importLogService.addBatch(validPropertyDTOList, propertyId2DateRange, importResult, operatorId, path, propertyId2DataUpdateResult);
                    }
                }
            }
        }

        /**
         * 上传报告，用于日志下载报告
         *
         * @param path S3 路径
         * @author zc
         * @date 2024/6/3
         */
        private void uploadReport(String path) {
            // 上传报告，用于日志下载报告
            try (InputStream inputStream = amazonS3Service.getFile(path)) {
                amazonS3Service.uploadFile(S3BucketFunctionCons.SVA_OPERATING_IMPORT_RECORD, inputStream, path);
            } catch (Exception e) {
                log.error("SvaOperatingStatisticsDailyListener.handle end... Upload Operating statistics report exception.", e);
            }
        }

        /**
         * 获取有效的资产数据
         *
         * @param tenantSymbol     租户标识
         * @param propertyAbbrList 资产标识
         * @return PropertyDTO 集合
         * @author zc
         * @date 2024/4/30
         */
        private List<PropertyDTO> listEffectiveProperty(String tenantSymbol, List<String> propertyAbbrList) {
            if (CollectionUtils.isEmpty(propertyAbbrList)) {
                return Collections.emptyList();
            }

            // 获取资产数据
            return propertyInternalService.getProperty(propertyAbbrList, tenantSymbol).stream().filter(item ->
                    item.getStatus() && commonService.checkOpenFunction(item, FunctionIdConstant.SCHEDULE_VS_ACTUAL)).toList();
        }

        /**
         * 提取报告数据日期范围
         *
         * @param propertyDTOList 资产集合
         * @param parameter       事件体参数
         * @param operatorId      操作人 id
         * @return 报告数据日期范围(yyyyMMdd)
         * @author zc
         * @date 2024/2/28
         */
        private Map<Integer, List<Long>> getDateRangeMap(List<PropertyDTO> propertyDTOList, Map<String, Object> parameter, int operatorId) {
            /*
             * 处理数据日期范围：
             * 1、自动导入报告时，取调度配置的数据日期范围
             * 2、手动导入报告（解决异常、手动导入）时，取事件体的日期范围
             */
            Map<Integer, List<Long>> propertyId2DateRange = Maps.newHashMap();
            if (operatorId == 0) {
                propertyDTOList.forEach(propertyDTO -> {
                    Map<String, Long> dateRangeMap = schedulerService.getDateRange(propertyDTO,
                            SvaNotifyTypeEnum.MISSING_REPORT_OPERATING.getType());
                    if (dateRangeMap.isEmpty()) {
                        return;
                    }
                    long startDate = dateRangeMap.get(JobParameterDTO.START_DATE);
                    long endDate = dateRangeMap.get(JobParameterDTO.END_DATE);
                    propertyId2DateRange.put(propertyDTO.getId(), Lists.newArrayList(startDate, endDate));
                });
                return propertyId2DateRange;
            }
            Object startDateObj = parameter.get(EventBodyParamEnum.START_DATE.getDesc());
            Object endDateObj = parameter.get(EventBodyParamEnum.END_DATE.getDesc());
            propertyDTOList.forEach(propertyDTO -> {
                propertyId2DateRange.put(propertyDTO.getId(), Lists.newArrayList(Long.parseLong(startDateObj.toString()),
                        Long.parseLong(endDateObj.toString())));
            });
            return propertyId2DateRange;
        }

        /**
         * 发送预警邮件
         *
         * @param propertyAbbr     资产标识
         * @param exceptionMessage 异常信息
         * @param s3Key            文件路径
         * @author zc
         * @date 2024/2/22
         */
        private void sendAlertEmail(String propertyAbbr, String exceptionMessage, String s3Key) {
            String subject = "[Warning] - Handle the Operating Statistics Daily 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 boolean true：未并发，当前线程可以执行；false：检测到并发，当前线程应放弃执行
         * @author zc
         * @date 2023/2/8
         */
        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 2023/3/23
         */
        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);
        }

    }