# ScheduleHandleReportServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.EventBodyParamEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.EventBodyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.sns.http.processor.Sender;
    import cloud.wconnect.workperformance.cons.BusinessEnum;
    import cloud.wconnect.workperformance.cons.EventSubjectEnum;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleHistoryAndForecastImportLog;
    import cloud.wconnect.workperformance.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.schedule.IScheduleHandleReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;

    import java.util.Map;

    /**
     *
     * @author: yyt
     * 2026-02-03
     */
    @Service
    @DS("work_performance")
    public class ScheduleHandleReportServiceImpl implements IScheduleHandleReportService {

        private final AmazonS3Service amazonS3Service;
        private final ObjectMapper objectMapper;
        private final Sender snsSender;

        public ScheduleHandleReportServiceImpl(AmazonS3Service amazonS3Service, ObjectMapper objectMapper, @Qualifier("3pSender") Sender snsSender) {
            this.amazonS3Service = amazonS3Service;
            this.objectMapper = objectMapper;
            this.snsSender = snsSender;
        }

        @Value("${aws-s3.third-party.key.schedule.history-and-forecast}")
        private String historyAndForecastS3key;

        /**
         * 发送 history and forecast event
         *
         * @param propertyDTO 资产信息
         * @param pms         pms 类型
         * @param file        文件
         * @param importLog   导入记录
         * @param userId      操作人
         * @author yyt
         * @date 2026/2/3
         */
        @Override
        public void sendHistoryAndForecastEvent(PropertyDTO propertyDTO, String pms, MultipartFile file, ScheduleHistoryAndForecastImportLog importLog, Integer userId) {
            try {
                // 设置事件体
                Map<String, Object> params = Maps.newHashMapWithExpectedSize(8);
                params.put(EventBodyParamEnum.TENANT_SYMBOL.getDesc(), propertyDTO.getTenantSymbol());
                params.put(EventBodyParamEnum.PROPERTY_ABBR.getDesc(), Lists.newArrayList(propertyDTO.getAbbr()));
                params.put(EventBodyParamEnum.SOURCE.getDesc(), pms);
                params.put(EventBodyParamEnum.UID.getDesc(), userId);
                params.put(EventBodyParamEnum.ID.getDesc(), importLog.getId());
                params.put(EventBodyParamEnum.START_DATE.getDesc(), importLog.getFromDate());
                params.put(EventBodyParamEnum.END_DATE.getDesc(), importLog.getToDate());
                params.put(EventBodyParamEnum.ORIGINATE_FROM.getDesc(), BusinessEnum.SCHEDULE_HISTORY_AND_FORECAST.getBusiness());

                // 上传文件
                String key = propertyDTO.getTenantSymbol() + "/" + propertyDTO.getAbbr() + "/" +
                        historyAndForecastS3key + UploadUtils.getRandomFileName(file.getOriginalFilename());
                amazonS3Service.uploadFile(file, key);

                snsSender.publish(EventSubjectEnum.HISTORY_AND_FORECAST_SERVICE_YESTERDAY_NINETY_DAY.getSubject(),
                        JsonUtils.convertObjToString(objectMapper, new EventBodyDTO(key, params)));
            } catch (Exception e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "Failed to send report event", e);
            }
        }

    }