# HotelKpiHandleReportServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.EventBodyParamEnum;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    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.service.AmazonS3Service;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiHandleReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.fasterxml.jackson.databind.ObjectMapper;
    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.HashMap;
    import java.util.List;

    /**
     *
     * @author: yyt
     * 2026-02-06
     */
    @Service
    @DS("work_performance")
    public class HotelKpiHandleReportServiceImpl implements IHotelKpiHandleReportService {

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

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

        @Value("${aws-s3.third-party.key.hotel-kpi.history-and-forecast}")
        private String historyAdnForecastS3FilePath;
        @Value("${aws-s3.third-party.key.hotel-kpi.manager-flash}")
        private String managerFlashS3FilePath;

        /**
         * 发送 history and forecast 事件
         *
         * @param propertyDTO 资产
         * @param pms         pms 类型
         * @param file        文件
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @param logId       日志 id
         * @param operatorId  操作人 id
         * @author yyt
         * @date 2026/2/6
         **/
        @Override
        public void sendHistoryAndForecastEvent(PropertyDTO propertyDTO, String pms, MultipartFile file, List<Long> dateRange, long logId, int operatorId) {
            try {
                String key = propertyDTO.getTenantSymbol() + "/" + propertyDTO.getAbbr() + "/" + historyAdnForecastS3FilePath
                        + UploadUtils.getRandomFileName(file.getOriginalFilename());

                // 上传报告到 S3 服务器，用于事件监听解析报告
                amazonS3Service.uploadFile(file, key);

                // 封装事件体
                HashMap<String, Object> parameter = Maps.newHashMap();
                parameter.put(EventBodyParamEnum.TENANT_SYMBOL.getDesc(), propertyDTO.getTenantSymbol());
                parameter.put(EventBodyParamEnum.PROPERTY_ABBR.getDesc(), List.of(propertyDTO.getAbbr()));
                parameter.put(EventBodyParamEnum.ORIGINATE_FROM.getDesc(), BusinessEnum.HOTEL_KPI.getBusiness());
                parameter.put(EventBodyParamEnum.SOURCE.getDesc(), pms);
                parameter.put(EventBodyParamEnum.UID.getDesc(), operatorId);
                parameter.put(EventBodyParamEnum.ID.getDesc(), logId);
                parameter.put(EventBodyParamEnum.START_DATE.getDesc(), dateRange.get(0));
                parameter.put(EventBodyParamEnum.END_DATE.getDesc(), dateRange.get(1));

                // 手动发送事件通知
                snsSender.publish(EventSubjectEnum.HISTORY_AND_FORECAST_SERVICE_HISTORY_THIRTY_DAY_HUNDRED_DAY.getSubject(),
                        JsonUtils.convertObjToString(objectMapper, new EventBodyDTO(key, parameter)));
            } catch (Exception e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle import Hotel KPI History and forecast report exception", e);
            }

        }

        /**
         * 发送 manager flash 事件
         *
         * @param propertyDTO 资产
         * @param pms         pms 类型
         * @param file        文件
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @param logId       日志 id
         * @param operatorId  操作人 id
         * @author yyt
         * @date 2026/2/7
         **/
        @Override
        public void sendManagerFlashEvent(PropertyDTO propertyDTO, String pms, MultipartFile file, List<Long> dateRange, long logId, int operatorId) {
            try {
                String key = propertyDTO.getTenantSymbol() + "/" + propertyDTO.getAbbr() + "/" + managerFlashS3FilePath
                        + UploadUtils.getRandomFileName(file.getOriginalFilename());

                // 上传报告到 S3 服务器，用于事件监听解析报告
                amazonS3Service.uploadFile(file, key);

                // 封装事件体
                HashMap<String, Object> parameter = Maps.newHashMap();
                parameter.put(EventBodyParamEnum.TENANT_SYMBOL.getDesc(), propertyDTO.getTenantSymbol());
                parameter.put(EventBodyParamEnum.PROPERTY_ABBR.getDesc(), List.of(propertyDTO.getAbbr()));
                parameter.put(EventBodyParamEnum.ORIGINATE_FROM.getDesc(), BusinessEnum.HOTEL_KPI.getBusiness());
                parameter.put(EventBodyParamEnum.SOURCE.getDesc(), pms);
                parameter.put(EventBodyParamEnum.UID.getDesc(), operatorId);
                parameter.put(EventBodyParamEnum.ID.getDesc(), logId);
                parameter.put(EventBodyParamEnum.START_DATE.getDesc(), dateRange.get(0));
                parameter.put(EventBodyParamEnum.END_DATE.getDesc(), dateRange.get(1));

                // 手动发送事件通知
                snsSender.publish(EventSubjectEnum.MANAGER_FLASH_SERVICE_MTD_YESTERDAY.getSubject(),
                        JsonUtils.convertObjToString(objectMapper, new EventBodyDTO(key, parameter)));
            } catch (Exception e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle import Hotel KPI Manager Flash report exception", e);
            }
        }

    }