# RevenueHurdlerPointListener.java

    package cloud.wconnect.rateshop.springlistener.revenue;

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.MailInternalService;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.operation.PropertyOperationDTO;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.operation.PropertyOperationValueDTO;
    import cloud.wconnect.rateshop.springlistener.PropertyOperationEvent;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.event.EventListener;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;

    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;

    /**
     * 监听 hurdler point
     *
     * @author: yyt
     * 2024-03-28
     */
    @Slf4j
    @Service
    public class RevenueHurdlerPointListener {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private MailInternalService mailInternalService;
        @Autowired
        private RevenueHurdlerPointHandle revenueHurdlerPointHandle;

        /**
         * 异步监听事件处理
         *
         * @param event 事件对象
         * @author yyt
         * @date 2024/3/28
         */
        @Async
        @EventListener(classes = {PropertyOperationEvent.class})
        public void handle(PropertyOperationEvent event) {
            log.info("Revenue - RevenueHurdlerPointListener.start...");

            try {
                PropertyOperationDTO propertyOperation = event.getPropertyOperation();
                String tenant = propertyOperation.getTenant();
                String property = propertyOperation.getProperty();
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                if (Objects.isNull(propertyDTO)) {
                    log.warn("Revenue - RevenueHurdlerPointListener.end... The property:{} is not exist", property);
                    return;
                }
                if (!propertyDTO.getStatus()) {
                    log.warn("Revenue - RevenueHurdlerPointListener.end... The property:{} is closed", property);
                    return;
                }
                List<PropertyOperationValueDTO> propertyOperationValueDTOList = propertyOperation.getValues();
                if (CollectionUtils.isEmpty(propertyOperationValueDTOList)) {
                    log.warn("Revenue - RevenueHurdlerPointListener.end... The values is empty, property:{}", property);
                    return;
                }

                List<PropertyOperationValueDTO> values = propertyOperation.getValues();
                if(CollectionUtils.isNotEmpty(values)) {
                    revenueHurdlerPointHandle.handleHurdlePointData(propertyDTO, values);
                }
            }catch (Exception e){
                log.error(e.getMessage(),e);
                sendAlertEmail(e);
            }

            log.info("Revenue - RevenueHurdlerPointListener.end...");
        }

        /**
         * 发送alertEmail
         *
         * @param e 异常对象
         * @author yyt
         * @date 2024/3/25
         */
        public void sendAlertEmail(Exception e) {
            //主题
            String subject = "[Warning] - Revenue Handle Hurdler Point Listener Process Error";
            //邮件内容
            String mailContent = MessageFormat.format("Exception:{0}<br/>", e.getMessage());
            //发送alert
            mailInternalService.sendAlertEmail(subject, mailContent);
        }

    }