From 383f6e2dd13823b8e7578335b07b8b5122062624 Mon Sep 17 00:00:00 2001 From: wangzhijiang <12345678> Date: Fri, 10 Mar 2023 22:02:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A4=E8=AF=81=E6=B8=85=E5=8D=95-=E6=88=AA?= =?UTF-8?q?=E6=AD=A2=E5=BD=93=E5=A4=A9=E3=80=81=E6=88=AA=E6=AD=A2=E5=89=8D?= =?UTF-8?q?=E4=B8=89=E5=A4=A9=E3=80=81=E9=80=BE=E6=9C=9F=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=A3=9E=E4=B9=A6=E6=B6=88=E6=81=AF=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/CertificationInventoryJob.java | 328 ++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/job/CertificationInventoryJob.java diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/job/CertificationInventoryJob.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/job/CertificationInventoryJob.java new file mode 100644 index 000000000..55146d016 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/job/CertificationInventoryJob.java @@ -0,0 +1,328 @@ +package com.jero.modules.project.job; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.jero.common.util.DateUtils; +import com.jero.modules.feishu.enums.TemplateInfoEnum2; +import com.jero.modules.feishu.service.IFeishuService; +import com.jero.modules.project.entity.ProjectCertificationInventoryEO; +import com.jero.modules.project.entity.ProjectLibraryBase; +import com.jero.modules.project.enums.CertificationInventoryFlowStatusEnum; +import com.jero.modules.project.service.IProjectCertificationInventoryEOService; +import com.jero.modules.project.service.IProjectLibraryBaseService; +import com.jero.modules.system.entity.SysUser; +import com.jero.modules.system.service.ISysUserService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.springframework.beans.factory.annotation.Autowired; + +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.stream.Collectors; + +/** + * 认证清单定时器 + * @Author: wzj + * @Date: 2023/3/10 18:46 + **/ +@Slf4j +public class CertificationInventoryJob implements Job { + + private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + @Autowired + private IProjectLibraryBaseService projectLibraryBaseService; + @Autowired + private IProjectCertificationInventoryEOService projectCertificationInventoryEOService; + @Autowired + private ISysUserService sysUserService; + @Autowired + private IFeishuService feishuService; + + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + log.info("认证清单,定时任务开启 ====================================================="); + + List flowStatusList = new ArrayList<>(); + flowStatusList.add(CertificationInventoryFlowStatusEnum.LIST_TO_BE_CHECKED.getValue()); + flowStatusList.add(CertificationInventoryFlowStatusEnum.TASK_TO_BE_CONFIRMED.getValue()); + flowStatusList.add(CertificationInventoryFlowStatusEnum.REFUSAL_OF_RESPONSIBLE_PERSON.getValue()); + flowStatusList.add(CertificationInventoryFlowStatusEnum.RESULTS_TO_BE_SUBMITTED.getValue()); + flowStatusList.add(CertificationInventoryFlowStatusEnum.RESULTS_TO_BE_REVIEWED.getValue()); + flowStatusList.add(CertificationInventoryFlowStatusEnum.REVIEW_AND_RETURN.getValue()); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().in(ProjectCertificationInventoryEO::getFlowStatus,flowStatusList); + List projectCertificationInventoryEOList = this.projectCertificationInventoryEOService.list(queryWrapper); + + if(CollectionUtils.isNotEmpty(projectCertificationInventoryEOList)){ + Map> dataMaps = projectCertificationInventoryEOList + .stream().collect(Collectors.groupingBy(ProjectCertificationInventoryEO::getProjectLibraryId)); + + if(ObjectUtils.isNotEmpty(dataMaps)){ + + for (Map.Entry> dataMap : dataMaps.entrySet()) { + String projectLibraryId = dataMap.getKey(); + List projectCertificationInventoryEOS = dataMap.getValue(); + + if(CollectionUtils.isNotEmpty(projectCertificationInventoryEOS)){ + ProjectLibraryBase projectLibraryBase = this.projectLibraryBaseService.selectById(projectLibraryId); + + if(StringUtils.isNotEmpty(projectLibraryBase.getCertificationEngineer())){ + // 消息提醒 认证工程师 认证清单数据 + this.remindCertificationEngineer(projectCertificationInventoryEOS,projectLibraryBase); + } + // 消息提醒 责任人 认证清单数据 + this.remindDutyPerson(projectCertificationInventoryEOS,projectLibraryBase); + } + } + } + } + log.info("认证清单,定时任务结束 ====================================================="); + } + + /** + * 提醒认证工程师 + * @param projectCertificationInventoryEOS + */ + private void remindCertificationEngineer( List projectCertificationInventoryEOS,ProjectLibraryBase projectLibraryBase) { + List certificationInventoryEOList = projectCertificationInventoryEOS.stream().filter(data -> { + boolean flag = ( + StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.LIST_TO_BE_CHECKED.getValue()) + || StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.REFUSAL_OF_RESPONSIBLE_PERSON.getValue()) + || StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.RESULTS_TO_BE_REVIEWED.getValue()) + ); + return flag; + }).collect(Collectors.toList()); + + if(CollectionUtils.isNotEmpty(certificationInventoryEOList)){ + Map sendMsgDataListMap = this.disposeSendMsgDataList(certificationInventoryEOList); + + List certificationEngineerIdList = Arrays.asList(projectLibraryBase.getCertificationEngineer().split(",")); + List certificationEngineerList = this.sysUserService.querySysUserListByIdList(certificationEngineerIdList); + + // 三天后结束的 + List threeDaysList = (List) sendMsgDataListMap.get("threeDaysList"); + if(CollectionUtils.isNotEmpty(threeDaysList)){ + for (String certificationEngineerId : certificationEngineerIdList) { + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + certificationEngineerList, + certificationEngineerId, + threeDaysList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE6.getValue() + ); + } + } + // 当天结束的 + List currentDaysList = (List) sendMsgDataListMap.get("currentDaysList"); + if(CollectionUtils.isNotEmpty(currentDaysList)){ + for (String certificationEngineerId : certificationEngineerIdList) { + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + certificationEngineerList, + certificationEngineerId, + currentDaysList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE7.getValue() + ); + } + } + // 逾期的 + List overdueList = (List) sendMsgDataListMap.get("overdueList"); + if(CollectionUtils.isNotEmpty(overdueList)){ + for (String certificationEngineerId : certificationEngineerIdList) { + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + certificationEngineerList, + certificationEngineerId, + overdueList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE8.getValue() + ); + } + } + } + } + + /** + * 提醒责任人 + * @param projectCertificationInventoryEOS + * @param projectLibraryBase + */ + private void remindDutyPerson(List projectCertificationInventoryEOS,ProjectLibraryBase projectLibraryBase) { + Map> certifycationInventoryListByDutyPersonMap = projectCertificationInventoryEOS.stream().filter(data -> { + boolean flag = ( + StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.TASK_TO_BE_CONFIRMED.getValue()) + || StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.RESULTS_TO_BE_SUBMITTED.getValue()) + || StringUtils.equals(data.getFlowStatus(), CertificationInventoryFlowStatusEnum.REVIEW_AND_RETURN.getValue()) + ); + return flag; + }).collect(Collectors.groupingBy(ProjectCertificationInventoryEO::getDutyPerson)); + + if(ObjectUtils.isNotEmpty(certifycationInventoryListByDutyPersonMap)){ + List dutyPersonIdList = projectCertificationInventoryEOS.stream().map(ProjectCertificationInventoryEO::getDutyPerson).distinct().collect(Collectors.toList()); + List dutyPersonList = this.sysUserService.querySysUserListByIdList(dutyPersonIdList); + + for (Map.Entry> dataByDutyPersonMap : certifycationInventoryListByDutyPersonMap.entrySet()) { + String dutyPersonUserId = dataByDutyPersonMap.getKey(); + List dutyPersonDataList = dataByDutyPersonMap.getValue(); + if(CollectionUtils.isEmpty(dutyPersonDataList)){ + continue; + } + + Map sendMsgDataListMap = this.disposeSendMsgDataList(dutyPersonDataList); + // 三天后结束的 + List threeDaysList = (List) sendMsgDataListMap.get("threeDaysList"); + if(CollectionUtils.isNotEmpty(threeDaysList)){ + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + dutyPersonList, + dutyPersonUserId, + threeDaysList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE6.getValue() + ); + } + // 当天结束的 + List currentDaysList = (List) sendMsgDataListMap.get("currentDaysList"); + if(CollectionUtils.isNotEmpty(currentDaysList)){ + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + dutyPersonList, + dutyPersonUserId, + currentDaysList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE7.getValue() + ); + } + // 逾期的 + List overdueList = (List) sendMsgDataListMap.get("overdueList"); + if(CollectionUtils.isNotEmpty(overdueList)){ + this.sendLarkCardMsgByTemplate( + projectLibraryBase, + dutyPersonList, + dutyPersonUserId, + overdueList, + TemplateInfoEnum2.CERTIFICATION_MESSAGE8.getValue() + ); + } + } + } + } + + /** + * 处理需要发送消息的数据 + * @param dutyPersonDataList + * @return + */ + private Map disposeSendMsgDataList(List dutyPersonDataList) { + Map result = new HashMap<>(); + Date currentDate = new Date(); + + //获取后三天前的时间 + Calendar calendar=new GregorianCalendar(); + calendar.setTime(new Date()); + calendar.add(Calendar.DATE,3); + Date threeDays = calendar.getTime(); + + String threeDaysStr = sdf.format(threeDays); + String currentDateStr = sdf.format(currentDate); + + // 当天结束的 + List currentDaysList = dutyPersonDataList.stream().filter(data -> { + boolean flag = false; + //结束日期是当前日期 + if (StringUtils.equals(currentDateStr, sdf.format(data.getEndTime()))) { + flag = true; + } + return flag; + }).collect(Collectors.toList()); + + // 三天后结束的 + List threeDaysList = dutyPersonDataList.stream().filter(data -> { + boolean flag = false; + //结束日期是当前日期 + if (StringUtils.equals(threeDaysStr, sdf.format(data.getEndTime()))) { + flag = true; + } + return flag; + }).collect(Collectors.toList()); + + // 逾期的 + List overdueList = dutyPersonDataList.stream().filter(data -> { + // 逾期3天、七天、14天 + Calendar day3Later = new GregorianCalendar(); + day3Later.setTime(data.getEndTime()); + day3Later.add(Calendar.DATE, 3); + Date overdue3Days = day3Later.getTime(); + + Calendar day7Later = new GregorianCalendar(); + day7Later.setTime(data.getEndTime()); + day7Later.add(Calendar.DATE, 7); + Date overdue7Days = day7Later.getTime(); + + Calendar day14Later = new GregorianCalendar(); + day14Later.setTime(data.getEndTime()); + day14Later.add(Calendar.DATE, 14); + Date overdue14Days = day14Later.getTime(); + + boolean flag = ( + StringUtils.equals(sdf.format(overdue3Days), sdf.format(currentDate)) + || StringUtils.equals(sdf.format(overdue7Days), sdf.format(currentDate)) + || StringUtils.equals(sdf.format(overdue14Days), sdf.format(currentDate)) + ); + return flag; + }).collect(Collectors.toList()); + + result.put("currentDaysList",currentDaysList); + result.put("threeDaysList",threeDaysList); + result.put("overdueList",overdueList); + return result; + } + + /** + * 根据模板 发送飞书消息 + * @param projectLibraryBase + * @param userList + * @param userId + * @param list + */ + private void sendLarkCardMsgByTemplate(ProjectLibraryBase projectLibraryBase, List userList,String userId,List list,String templateId) { + String thirdId = this.sysUserService.getUserThirdIdByUserId(userList,userId); + + if(StringUtils.isNotEmpty(thirdId)){ + String PRN_EN = projectLibraryBase.getProjectNameEn();//项目名称 英文 + String PRN_CN = projectLibraryBase.getProjectNameCn();//项目名称 中文 + + // 获取认证清单 - 飞书跳转链接 + Map hrefFeishuMap = this.projectCertificationInventoryEOService.getCertificationInventoryLinkHrefFeishu(projectLibraryBase.getId(), PRN_CN, PRN_EN); + String hrefFeishu_CN = (String) hrefFeishuMap.get("hrefFeishu_CN"); + String hrefFeishu_EN = (String) hrefFeishuMap.get("hrefFeishu_EN"); + + Map standNameAndItemName = this.projectCertificationInventoryEOService.getStandNameAndItemName(list); + String standName = (String) standNameAndItemName.get("standName"); + String itemName = (String) standNameAndItemName.get("itemName"); + + try { + JSONObject larkMesJson = new JSONObject(); + larkMesJson.put("template_id", templateId); + larkMesJson.put("userIds",thirdId); + + Map templateVariableMap = new HashMap<>(); + templateVariableMap.put("projectNameCn", PRN_CN); + templateVariableMap.put("projectNameEn", PRN_EN); + templateVariableMap.put("standName",standName); + templateVariableMap.put("itemName",itemName); + templateVariableMap.put("Initiator", projectLibraryBase.getStudioEngineerName()); + templateVariableMap.put("endTime", DateUtils.formatDate(list.get(0).getEndTime())); + templateVariableMap.put("viewBtnUrlCn",hrefFeishu_CN); + templateVariableMap.put("viewBtnUrlEn",hrefFeishu_EN); + + larkMesJson.put("templateVariableMap",templateVariableMap); + this.feishuService.batchSendLarkCardMsgByTemplate2(larkMesJson); + } catch (Exception e) { + log.error("飞书消息推送失败"); + } + } + } +}