diff --git a/src/main/java/com/cm/inspection/InspectionApplication.java b/src/main/java/com/cm/inspection/InspectionApplication.java index 6256009..532c839 100644 --- a/src/main/java/com/cm/inspection/InspectionApplication.java +++ b/src/main/java/com/cm/inspection/InspectionApplication.java @@ -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 params = new HashMap<>(16); - params.put("currentDate", currentDate); - params.put("isComplete", 0); - List checkDTOs = checkService.listCheckSimple(params); - // 找出超时的未完成案件 - - } - } diff --git a/src/main/java/com/cm/inspection/service/dingding/IDingDingMsgService.java b/src/main/java/com/cm/inspection/service/dingding/IDingDingMsgService.java new file mode 100644 index 0000000..f18914b --- /dev/null +++ b/src/main/java/com/cm/inspection/service/dingding/IDingDingMsgService.java @@ -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(); + +} diff --git a/src/main/java/com/cm/inspection/service/dingding/impl/DingDingMsgServiceImpl.java b/src/main/java/com/cm/inspection/service/dingding/impl/DingDingMsgServiceImpl.java new file mode 100644 index 0000000..37a4848 --- /dev/null +++ b/src/main/java/com/cm/inspection/service/dingding/impl/DingDingMsgServiceImpl.java @@ -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 params = new HashMap<>(16); + params.put("currentDate", currentDate); + params.put("isComplete", 0); + List checkDTOs = checkService.listCheckSimple(params); + LOG.debug("未完成案件数量:{}", checkDTOs.size()); + // 找出超时的未完成案件,获取用户列表 + List 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 gridPersonnelDTOs = gridPersonnelService.listGridPersonnel(params); + if (gridPersonnelDTOs.isEmpty()) { + LOG.debug("通知网格员为空"); + return; + } + // 获取网格员领导userId列表 + Map> leaderUserMap = new HashMap<>(16); + for (GridPersonnelDTO gridPersonnelDTO : gridPersonnelDTOs) { + String userId = gridPersonnelDTO.getUserId().split("\\|")[0]; + String leaderUserId = gridPersonnelDTO.getLeaderUserId().split("\\|")[0]; + Set gridPersonnelUserIdSet = leaderUserMap.get(leaderUserId); + // 缓存不存在,直接新增 + if (gridPersonnelUserIdSet == null) { + gridPersonnelUserIdSet = new HashSet<>(16); + leaderUserMap.put(leaderUserId, gridPersonnelUserIdSet); + } + gridPersonnelUserIdSet.add(userId); + } + LOG.debug("获取网格员用户列表"); + List userResourceBOs = userService.listUserResourceByIds(userIds); + sendDingDingMessage(userResourceBOs, "您有超时的检查,请登录APP进行处理。"); + LOG.debug("通知领导数量:{}", leaderUserMap.size()); + if (!leaderUserMap.isEmpty()) { + LOG.debug("获取领导用户列表"); + List leaderUserIds = new ArrayList<>(); + for (Map.Entry> kv : leaderUserMap.entrySet()) { + leaderUserIds.add(kv.getKey()); + } + List leaderUserResourceBOs = userService.listUserResourceByIds(leaderUserIds); + sendDingDingMessage(leaderUserResourceBOs, "您的下级网格员有超时的检查项,请尽快处理。"); + } + long endTime = System.currentTimeMillis(); + LOG.debug(">>>> 整理超时案件 end,耗时:{}ms <<<<", endTime - startTime); + } + + // 获取网格员名称 + private String getGridPersonnelName(List userIds, List 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 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 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("发送钉钉消息失败"); + } + */ + } +} diff --git a/src/main/java/com/cm/inspection/startup/StartUp.java b/src/main/java/com/cm/inspection/startup/StartUp.java index 46c645c..9c9df07 100644 --- a/src/main/java/com/cm/inspection/startup/StartUp.java +++ b/src/main/java/com/cm/inspection/startup/StartUp.java @@ -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(); + } + } diff --git a/src/main/resources/mybatis/mapper/gridpersonnel/gridpersonnel-mapper.xml b/src/main/resources/mybatis/mapper/gridpersonnel/gridpersonnel-mapper.xml index 4f46b86..9199cd7 100644 --- a/src/main/resources/mybatis/mapper/gridpersonnel/gridpersonnel-mapper.xml +++ b/src/main/resources/mybatis/mapper/gridpersonnel/gridpersonnel-mapper.xml @@ -243,6 +243,12 @@ AND t1.leader_user_id LIKE CONCAT(#{leaderUserId}, '%') + + AND + + t1.user_id LIKE CONCAT(#{userIds[${index}]}, '|%') + + AND t1.level = #{level}