# RevenueSendEventServiceImpl.java

    package cloud.wconnect.rateshop.service.revenue.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.MoudleEventSubjectEnum;
    import cloud.wconnect.commonlib.pojo.bo.revenue.RevenueBO;
    import cloud.wconnect.commonlib.pojo.bo.revenue.RevenueDataBO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.YearMonthUtils;
    import cloud.wconnect.rateshop.entity.revenue.RevenueBudgetOtb;
    import cloud.wconnect.rateshop.entity.revenue.RevenueOtbRevenue;
    import cloud.wconnect.rateshop.entity.revenue.RevenueOtbRevenueFuture;
    import cloud.wconnect.rateshop.service.revenue.IRevenueSendEventService;
    import cloud.wconnect.sns.http.processor.Sender;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;

    import java.time.YearMonth;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.List;
    import java.util.Locale;
    import java.util.stream.Collectors;

    /**
     * @author yyt
     * 2023/9/1
     */
    @Service
    @DS("rate_shop")
    public class RevenueSendEventServiceImpl implements IRevenueSendEventService {

        @Autowired
        private ObjectMapper objectMapper;
        private final Sender snsSender;

        public RevenueSendEventServiceImpl(@Qualifier("moduleEventSender") Sender snsSender) {
            this.snsSender = snsSender;
        }

        /**
         * 发送 otb revenue 变动事件
         *
         * @param propertyDTO           资产
         * @param revenueOtbRevenueList otb revenue 集合
         * @author yyt
         * @date 2023/10/27
         */
        @Override
        public void sendOtbRevenueEvent(PropertyDTO propertyDTO, List<RevenueOtbRevenue> revenueOtbRevenueList) {
            RevenueBO revenueBO = new RevenueBO();
            revenueBO.setPropertyId(propertyDTO.getId());

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYYMM, Locale.ENGLISH);
            List<RevenueDataBO> dataList = revenueOtbRevenueList.stream().map(otbRevenue -> {
                ZonedDateTime zonedDateTime = DateTimeUtils.toZonedDateTime(otbRevenue.getTheDate(), propertyDTO.getTimezone());
                return new RevenueDataBO(Long.valueOf(dateTimeFormatter.format(zonedDateTime)), otbRevenue.getOtbRevenue());
            }).toList();
            revenueBO.setData(dataList);

            snsSender.publish(MoudleEventSubjectEnum.REVENUE_OTB_REVENUE.getSubject(), JsonUtils.convertObjToString(objectMapper, revenueBO));
        }

        /**
         * 发送 otb revenue 变动事件
         *
         * @param propertyDTO           资产
         * @param revenueOtbRevenueList otb revenue 集合
         * @author yyt
         * @date 2024/5/11
         */
        @Override
        public void sendOtbRevenueFutureEvent(PropertyDTO propertyDTO, List<RevenueOtbRevenueFuture> revenueOtbRevenueList) {
            RevenueBO revenueBO = new RevenueBO();
            revenueBO.setPropertyId(propertyDTO.getId());

            List<RevenueDataBO> dataList = revenueOtbRevenueList.stream().map(otbRevenue ->
                    new RevenueDataBO(YearMonthUtils.parseYearMonthToLong(YearMonth.of(otbRevenue.getTheYear(), otbRevenue.getTheMonth())),
                            otbRevenue.getOtbRevenue())).toList();
            revenueBO.setData(dataList);

            snsSender.publish(MoudleEventSubjectEnum.REVENUE_OTB_REVENUE.getSubject(), JsonUtils.convertObjToString(objectMapper, revenueBO));
        }

        /**
         * 发送 budget 变动事件
         *
         * @param propertyDTO 资产
         * @param budgets     budget
         * @author yyt
         * @date 2023/10/27
         */
        @Override
        public void sendBudgetEvent(PropertyDTO propertyDTO, List<RevenueBudgetOtb> budgets) {
            RevenueBO revenueBO = new RevenueBO();
            revenueBO.setPropertyId(propertyDTO.getId());
            revenueBO.setData(budgets.stream().map(item ->
                    new RevenueDataBO(YearMonthUtils.parseYearMonthToLong(YearMonth.of(item.getTheYear(), item.getTheMonth())),
                            item.getRoomRevenue())).collect(Collectors.toList()));
            snsSender.publish(MoudleEventSubjectEnum.REVENUE_BUDGET.getSubject(), JsonUtils.convertObjToString(objectMapper, revenueBO));
        }

    }