New file |
| | |
| | | package com.ycl.timer; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ycl.common.dingding.DingCommon; |
| | | import com.ycl.entity.message.DingMessage; |
| | | import com.ycl.mapper.message.DingMessageMapper; |
| | | import com.ycl.service.message.IDingMessageService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.ApplicationRunner; |
| | | import org.springframework.context.ApplicationListener; |
| | | import org.springframework.context.event.ContextRefreshedEvent; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class SendDingMsg implements ApplicationListener<ContextRefreshedEvent> { |
| | | @Autowired |
| | | private DingMessageMapper dingMessageMapper; |
| | | @Autowired |
| | | private DingCommon dingCommon; |
| | | @Autowired |
| | | private IDingMessageService dingMessageService; |
| | | @Value("${spring.profiles.active}") |
| | | private String env; |
| | | |
| | | public final static Short STATE_READY = 0; |
| | | public final static Short STATE_SUCCESS = 1; |
| | | public final static Short STATE_FAILURE = 2; |
| | | public final static Short SCAN_READY = 0; |
| | | public final static Short SCAN_FINISH = 1; |
| | | |
| | | @Scheduled(cron = "0 0/2 * * * ?") |
| | | public void runAction() { |
| | | if ("online".equals(env)) { |
| | | QueryWrapper<DingMessage> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("state", STATE_READY); |
| | | wrapper.eq("if_scan", SCAN_READY); |
| | | List<DingMessage> dingMessages = dingMessageMapper.selectList(wrapper); |
| | | if (CollectionUtils.isNotEmpty(dingMessages)) { |
| | | int size = dingMessages.size(); |
| | | log.info("有待发送消息{}条", size); |
| | | for (DingMessage dingMessage : dingMessages) { |
| | | String baseCaseCode = dingMessage.getBaseCaseCode(); |
| | | Long accountId = dingMessage.getAccountId(); |
| | | Long messageId = dingMessage.getId(); |
| | | String text = "遂昌云执法:有待处理的任务\n" + |
| | | "·您有1条待处理事件。事件编号:\n" + |
| | | baseCaseCode; |
| | | String title = "您有一条工作通知"; |
| | | try { |
| | | dingMessage.setIfScan(SCAN_FINISH); |
| | | dingMessage.setSendTime(new Date()); |
| | | dingCommon.sendDingMsgStr(accountId + "", text); |
| | | log.info("发送一条工作通知,消息id{}", messageId); |
| | | dingMessage.setState(STATE_SUCCESS); |
| | | } catch (Exception e) { |
| | | log.error("发送钉钉消息出现异常,消息id{}", messageId, e); |
| | | dingMessage.setState(STATE_FAILURE); |
| | | size--; |
| | | } |
| | | } |
| | | dingMessageService.updateBatchById(dingMessages); |
| | | log.info("发送成功{}条,失败{}条", size, dingMessages.size() - size); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { |
| | | if (contextRefreshedEvent.getApplicationContext().getParent() == null) { |
| | | // runAction(); |
| | | } |
| | | } |
| | | } |