# TaNotifyServiceTask.java

    package cloud.wconnect.finance.businesstask.ta;

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.finance.cons.ta.TaRecordsStatusEnum;
    import cloud.wconnect.finance.entity.ta.TaRecordsStatus;
    import cloud.wconnect.finance.entity.ta.TaScheduler;
    import cloud.wconnect.finance.service.ta.ITaRecordsStatusService;
    import cloud.wconnect.finance.service.ta.ITaSendNotifyService;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;

    import jakarta.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    /**
     * TA: 通知定时任务
     *
     * @author zc
     * @date 2024/01/19
     */
    @Slf4j
    @Component
    public class TaNotifyServiceTask {
        @Resource
        private ITaSendNotifyService sendNotifyService;
        @Resource
        private ITaRecordsStatusService recordsStatusService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;

        /**
         * 每天的 11:00:00 发送比对异常数据的通知
         *
         * <p>获取当前三种异常类型所有异常数据的数量</P>
         *
         * @param schedulerJob 调度数据
         * @author zc
         * @date 2024/3/17
         */
        @Async
        public void comparisonExceptionDataNotify(TaScheduler schedulerJob) {
            log.info("TaNotifyServiceTask.comparisonExceptionDataNotify start...");

            int propertyId = schedulerJob.getPropertyId();
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            if (!propertyDTO.getStatus()) {
                log.warn("TaNotifyServiceTask.comparisonExceptionDataNotify end... property is off");
                return;
            }

            // 异常状态
            List<Integer> exceptionStatus = Lists.newArrayList(TaRecordsStatusEnum.MISS_PMS.getCode(),
                    TaRecordsStatusEnum.MISS_TA.getCode(), TaRecordsStatusEnum.PRICE_VARIANCE.getCode());

            // 获取比对异常的数据
            List<TaRecordsStatus> recordsStatusList = new ArrayList<>();
            for (Integer status : exceptionStatus) {
                List<TaRecordsStatus> sublist = recordsStatusService.listLimitByStatus(propertyId, status, 10);
                recordsStatusList.addAll(sublist);
            }
            // 若没有异常数据，则不需要发送通知
            if (CollectionUtils.isEmpty(recordsStatusList)) {
                log.info("TaNotifyServiceTask.comparisonExceptionDataNotify end... recordsStatusList is empty");
                return;
            }

            // 获取各状态数据数量
            Map<Integer, Long> status2Total = recordsStatusService.getTotalByStatus(propertyId, exceptionStatus);
            sendNotifyService.sendNotifyByCompareException(propertyDTO, status2Total, recordsStatusList,
                    exceptionStatus, schedulerJob.getCategory());

            log.info("TaNotifyServiceTask.comparisonExceptionDataNotify end...");
        }
    }