新增超时未完成的案件发送钉钉通知

This commit is contained in:
wenc000 2020-10-09 16:15:53 +08:00
parent 09f6961f03
commit 8e5c79715b
5 changed files with 241 additions and 23 deletions

View File

@ -1,23 +1,36 @@
package com.cm.inspection;
import com.alibaba.fastjson.JSONArray;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.service.user.IUserService;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.pojo.bos.UserResourceBO;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.cm.inspection.pojo.dtos.check.CheckDTO;
import com.cm.inspection.pojo.dtos.gridpersonnel.GridPersonnelDTO;
import com.cm.inspection.service.check.ICheckService;
import com.cm.inspection.service.gridpersonnel.IGridPersonnelService;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.core.parameters.P;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* 隐患排查系统
@ -26,32 +39,13 @@ import java.util.Map;
*/
@EnableSwagger2
@SpringBootApplication
@EnableScheduling
@ComponentScan("com.cm")
@MapperScan({"com.cm.**.dao"})
public class InspectionApplication {
@Autowired
private ICheckService checkService;
@Autowired
private IUserService userService;
public static void main(String[] args) {
SpringApplication.run(InspectionApplication.class, args);
}
/**
* 钉钉超时消息
*/
public void sendDingDingTimeoutMessage() {
DateTime currentDateTime = DateTime.now();
String currentDate = currentDateTime.toString(DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD));
// 获取截止到当前时间没有完成的案件
Map<String, Object> params = new HashMap<>(16);
params.put("currentDate", currentDate);
params.put("isComplete", 0);
List<CheckDTO> checkDTOs = checkService.listCheckSimple(params);
// 找出超时的未完成案件
}
}

View File

@ -0,0 +1,20 @@
package com.cm.inspection.service.dingding;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: IDingDingMsgService
* @Description:
* @Author: WangGeng
* @Date: 2020/10/9 15:48
* @Version: 1.0
**/
public interface IDingDingMsgService {
/**
* 发送钉钉超时未处理消息
*/
void sendDingDingTimeoutMessage();
}

View File

@ -0,0 +1,168 @@
package com.cm.inspection.service.dingding.impl;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.plugin.oauth.service.user.IUserService;
import com.cm.common.plugin.pojo.bos.UserResourceBO;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.cm.inspection.pojo.dtos.check.CheckDTO;
import com.cm.inspection.pojo.dtos.gridpersonnel.GridPersonnelDTO;
import com.cm.inspection.service.BaseService;
import com.cm.inspection.service.check.ICheckService;
import com.cm.inspection.service.dingding.IDingDingMsgService;
import com.cm.inspection.service.gridpersonnel.IGridPersonnelService;
import com.cm.inspection.startup.StartUp;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: DingDingMsgServiceImpl
* @Description: 钉钉消息
* @Author: WangGeng
* @Date: 2020/10/9 15:48
* @Version: 1.0
**/
@Service
public class DingDingMsgServiceImpl extends BaseService implements IDingDingMsgService {
@Autowired
private ICheckService checkService;
@Autowired
private IGridPersonnelService gridPersonnelService;
@Autowired
private IUserService userService;
@Autowired
private RestTemplateUtil restTemplateUtil;
@Autowired
private ApiPathProperties apiPathProperties;
@Override
public void sendDingDingTimeoutMessage() {
LOG.debug(">>>> 整理超时案件 start <<<<");
long startTime = System.currentTimeMillis();
DateTime currentDateTime = DateTime.now();
String currentDate = currentDateTime.toString(DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD));
// 获取截止到当前时间没有完成的案件
Map<String, Object> params = new HashMap<>(16);
params.put("currentDate", currentDate);
params.put("isComplete", 0);
List<CheckDTO> checkDTOs = checkService.listCheckSimple(params);
LOG.debug("未完成案件数量:{}", checkDTOs.size());
// 找出超时的未完成案件获取用户列表
List<String> userIds = new ArrayList<>();
for (CheckDTO checkDTO : checkDTOs) {
// 获取创建时间
DateTime gmtCreateDateTime = DateTime.parse(checkDTO.getGmtCreate().substring(0, checkDTO.getGmtCreate().length() - 2), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
DateTime timeoutDateTime = gmtCreateDateTime.plusDays(checkDTO.getRectificationDays());
// 如果超时时间在当前时间之后
if (!timeoutDateTime.isBefore(currentDateTime)) {
continue;
}
userIds.add(checkDTO.getCreator());
}
LOG.debug("通知用户数量:{}", userIds.size());
if (userIds.isEmpty()) {
LOG.debug("通知用户为空");
return;
}
// 获取网格员列表
params.clear();
params.put("userIds", userIds);
List<GridPersonnelDTO> gridPersonnelDTOs = gridPersonnelService.listGridPersonnel(params);
if (gridPersonnelDTOs.isEmpty()) {
LOG.debug("通知网格员为空");
return;
}
// 获取网格员领导userId列表
Map<String, Set<String>> leaderUserMap = new HashMap<>(16);
for (GridPersonnelDTO gridPersonnelDTO : gridPersonnelDTOs) {
String userId = gridPersonnelDTO.getUserId().split("\\|")[0];
String leaderUserId = gridPersonnelDTO.getLeaderUserId().split("\\|")[0];
Set<String> gridPersonnelUserIdSet = leaderUserMap.get(leaderUserId);
// 缓存不存在直接新增
if (gridPersonnelUserIdSet == null) {
gridPersonnelUserIdSet = new HashSet<>(16);
leaderUserMap.put(leaderUserId, gridPersonnelUserIdSet);
}
gridPersonnelUserIdSet.add(userId);
}
LOG.debug("获取网格员用户列表");
List<UserResourceBO> userResourceBOs = userService.listUserResourceByIds(userIds);
sendDingDingMessage(userResourceBOs, "您有超时的检查请登录APP进行处理。");
LOG.debug("通知领导数量:{}", leaderUserMap.size());
if (!leaderUserMap.isEmpty()) {
LOG.debug("获取领导用户列表");
List<String> leaderUserIds = new ArrayList<>();
for (Map.Entry<String, Set<String>> kv : leaderUserMap.entrySet()) {
leaderUserIds.add(kv.getKey());
}
List<UserResourceBO> leaderUserResourceBOs = userService.listUserResourceByIds(leaderUserIds);
sendDingDingMessage(leaderUserResourceBOs, "您的下级网格员有超时的检查项,请尽快处理。");
}
long endTime = System.currentTimeMillis();
LOG.debug(">>>> 整理超时案件 end耗时{}ms <<<<", endTime - startTime);
}
// 获取网格员名称
private String getGridPersonnelName(List<String> userIds, List<GridPersonnelDTO> gridPersonnelDTOs) {
StringBuilder nameSB = new StringBuilder();
for (GridPersonnelDTO gridPersonnelDTO : gridPersonnelDTOs) {
String[] gridUserIdArray = gridPersonnelDTO.getUserId().split("\\|");
String gridUserId = gridUserIdArray[0];
for (String userId : userIds) {
if (!StringUtils.equals(gridUserId, userId)) {
continue;
}
if (nameSB.length() > 0) {
nameSB.append(",");
}
nameSB.append(gridUserIdArray[2]);
}
}
return nameSB.toString();
}
/**
* 发送钉钉消息
*
* @param userResourceBOs
* @param content
*/
private void sendDingDingMessage(List<UserResourceBO> userResourceBOs, String content) {
if (userResourceBOs == null || userResourceBOs.isEmpty()) {
return;
}
StringBuilder phoneSB = new StringBuilder();
for (UserResourceBO userResourceBO : userResourceBOs) {
if (phoneSB.length() > 0) {
phoneSB.append("_");
}
phoneSB.append(userResourceBO.getUserPhone());
}
System.out.println(phoneSB.toString());
/*
Map<String, Object> params = new HashMap<>(10);
params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
params.put("phones", phoneSB.toString());
String result = restTemplateUtil.doPostFormNormal(String.format(IApiConsts.LIST_DYNAMIC_USER_INFO_BY_IDS, apiPathProperties.getUserCenter()), params);
LOG.debug("DingDing result: {}", result);
if (result == null) {
LOG.error("认证失败");
}
if (result.isEmpty()) {
LOG.error("发送钉钉消息失败");
}
*/
}
}

View File

@ -1,11 +1,30 @@
package com.cm.inspection.startup;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.plugin.oauth.service.user.IUserService;
import com.cm.common.plugin.pojo.bos.UserResourceBO;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.cm.inspection.InspectionApplication;
import com.cm.inspection.pojo.dtos.check.CheckDTO;
import com.cm.inspection.pojo.dtos.gridpersonnel.GridPersonnelDTO;
import com.cm.inspection.service.check.ICheckService;
import com.cm.inspection.service.dingding.IDingDingMsgService;
import com.cm.inspection.service.gridpersonnel.IGridPersonnelService;
import com.cm.inspection.service.process.IProcessService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -21,10 +40,21 @@ public class StartUp implements ApplicationRunner {
@Autowired
private IProcessService processService;
@Autowired
private IDingDingMsgService dingDingMsgService;
@Override
public void run(ApplicationArguments args) throws Exception {
// 部署流程
processService.deployProcess("check-self", "网格员检查上报流程");
}
/**
* 天10点统计
*/
@Scheduled(cron = "0 0 10 * * ?")
public void sendDingDingTimeoutMessage() {
dingDingMsgService.sendDingDingTimeoutMessage();
}
}

View File

@ -243,6 +243,12 @@
AND
t1.leader_user_id LIKE CONCAT(#{leaderUserId}, '%')
</if>
<if test="userIds != null and userIds.size > 0">
AND
<foreach collection="userIds" index="index" open="(" separator="OR" close=")">
t1.user_id LIKE CONCAT(#{userIds[${index}]}, '|%')
</foreach>
</if>
<if test="level != null">
AND
t1.level = #{level}