From 4375ec4e445da8516903a2e1969fdc5912a95d02 Mon Sep 17 00:00:00 2001 From: jinqiming <45981669@qq.com> Date: Tue, 29 Jun 2021 19:58:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B6=88=E6=81=AF=E4=B8=AD?= =?UTF-8?q?=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/snow/common/utils/PatternUtils.java | 9 +- .../flowable/cmd/AfterSignUserTaskCmd.java | 5 ++ .../common/SendMessageEventLister.java | 14 ++-- snow-framework/pom.xml | 6 +- .../snow/framework/util/FreemarkUtils.java | 30 +++++++ .../web/domain/common/SysSendMessageDTO.java | 14 +++- .../web/service/InnerMessageService.java | 84 +++++++++++++++++++ .../system/domain/SysMessageTemplate.java | 15 ++++ .../system/domain/SysMessageTransition.java | 33 ++++++-- .../system/SysMessageTemplateMapper.xml | 14 +++- .../system/SysMessageTransitionMapper.xml | 36 +++++++- 11 files changed, 235 insertions(+), 25 deletions(-) create mode 100644 snow-framework/src/main/java/com/snow/framework/util/FreemarkUtils.java create mode 100644 snow-framework/src/main/java/com/snow/framework/web/service/InnerMessageService.java diff --git a/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java b/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java index 85989d1..c1467ec 100644 --- a/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java +++ b/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java @@ -18,11 +18,11 @@ public class PatternUtils { /** * 替换消息模板参数 - * @param map + * @param map 后面改成freemark模板取值的方式 * @param templateBody * @return */ - public static String builderTemplateBody(Map map, String templateBody){ + public static String builderTemplateBody(Map map, String templateBody){ String re = "(?<=\\$\\{).*?(?=\\})"; Pattern p = Pattern.compile(re); Matcher m = p.matcher(templateBody); @@ -33,9 +33,8 @@ public class PatternUtils { continue; } //如果为空这取null标识 - String value = Optional.ofNullable(map.get(key)).orElse("null"); - //todo 可以记录下取到空值的异常情况 - message = message.replaceAll("\\$\\{" + key + "\\}", value); + Object value = Optional.ofNullable(map.get(key)).orElse(""); + message = message.replaceAll("\\$\\{" + key + "\\}", String.valueOf(value)); } return message; } diff --git a/snow-flowable/src/main/java/com/snow/flowable/cmd/AfterSignUserTaskCmd.java b/snow-flowable/src/main/java/com/snow/flowable/cmd/AfterSignUserTaskCmd.java index 1f4bfbb..61d7f52 100644 --- a/snow-flowable/src/main/java/com/snow/flowable/cmd/AfterSignUserTaskCmd.java +++ b/snow-flowable/src/main/java/com/snow/flowable/cmd/AfterSignUserTaskCmd.java @@ -39,6 +39,9 @@ public class AfterSignUserTaskCmd extends AbstractDynamicInjectionCmd implements private String taskId; + //表单fromKey + private String fromKey; + @Override public Void execute(CommandContext commandContext) { @@ -80,12 +83,14 @@ public class AfterSignUserTaskCmd extends AbstractDynamicInjectionCmd implements addUserTask.setId(id); addUserTask.setName(signUserTaskBuilder.getName()); addUserTask.setAssignee(signUserTaskBuilder.getAssignee()); + addUserTask.setFormKey(fromKey); process.addFlowElement(addUserTask); signUserTaskBuilder.setDynamicTaskId(id); String flowId = "sequenceFlow-"+CommandContextUtil.getProcessEngineConfiguration(commandContext).getIdGenerator().getNextId(); SequenceFlow newSequenceFlow = this.createSequenceFlow(addUserTask.getId(),currentTaskOutSequenceFlow.getTargetFlowElement().getId()); newSequenceFlow.setId(flowId); + //newSequenceFlow.set process.addFlowElement(newSequenceFlow); activity.setOutgoingFlows(Collections.singletonList(newSequenceFlow)); diff --git a/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java b/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java index 06e12e5..83cc542 100644 --- a/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java +++ b/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java @@ -204,7 +204,7 @@ public class SendMessageEventLister extends AbstractEventListener { ExecutionEntity execution = (ExecutionEntity) event.getEntity(); ProcessInstance processInstance = execution.getProcessInstance(); - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("toUser", getUserInfo(processInstance.getStartUserId()).getUserName()); map.put("starttime", DateUtil.formatDateTime(processInstance.getStartTime())); map.put("processInstance", processInstance.getProcessDefinitionName()); @@ -244,7 +244,7 @@ public class SendMessageEventLister extends AbstractEventListener { executor.execute(() -> flowCandidates.forEach(t -> { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("toUser", t.getUserName()); map.put("startUser", getUserInfo(processInstance.getStartUserId()).getUserName()); map.put("processInstance", processDefinition.getName()); @@ -280,7 +280,7 @@ public class SendMessageEventLister extends AbstractEventListener { ExecutionEntity execution = (ExecutionEntity) event.getEntity(); ProcessInstance processInstance = execution.getProcessInstance(); - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("toUser", getUserInfo(processInstance.getStartUserId()).getUserName()); map.put("starttime", DateUtil.formatDateTime(processInstance.getStartTime())); map.put("processInstance", processInstance.getProcessDefinitionName()); @@ -313,7 +313,7 @@ public class SendMessageEventLister extends AbstractEventListener { executor.execute(() -> { HistoricProcessInstance hisProcessInstance = getHisProcessInstance(event.getProcessInstanceId()); - Map map = buildSendProcessCompletedParam(hisProcessInstance); + Map map = buildSendProcessCompletedParam(hisProcessInstance); SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_COMPLETED_CODE) .receiver(getUserInfo(hisProcessInstance.getStartUserId()).getEmail()) .paramMap(map) @@ -338,7 +338,7 @@ public class SendMessageEventLister extends AbstractEventListener { executor.execute(() -> { HistoricProcessInstance hisProcessInstance = getHisProcessInstance(event.getProcessInstanceId()); - Map map = buildSendProcessCompletedParam(hisProcessInstance); + Map map = buildSendProcessCompletedParam(hisProcessInstance); SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_COMPLETED_CODE) .receiverSet(Sets.newHashSet(getUserInfo(hisProcessInstance.getStartUserId()).getDingUserId())) @@ -357,8 +357,8 @@ public class SendMessageEventLister extends AbstractEventListener { * * @param hisProcessInstance */ - private Map buildSendProcessCompletedParam(HistoricProcessInstance hisProcessInstance) { - Map map = new HashMap<>(); + private Map buildSendProcessCompletedParam(HistoricProcessInstance hisProcessInstance) { + Map map = new HashMap<>(); map.put("toUser", getUserInfo(hisProcessInstance.getStartUserId()).getUserName()); map.put("starttime", DateUtil.formatDateTime(hisProcessInstance.getStartTime())); map.put("orderNo", hisProcessInstance.getBusinessKey()); diff --git a/snow-framework/pom.xml b/snow-framework/pom.xml index d15698a..c57ae7a 100644 --- a/snow-framework/pom.xml +++ b/snow-framework/pom.xml @@ -40,7 +40,11 @@ com.alibaba druid-spring-boot-starter - + + + org.springframework.boot + spring-boot-starter-freemarker + com.github.penggle diff --git a/snow-framework/src/main/java/com/snow/framework/util/FreemarkUtils.java b/snow-framework/src/main/java/com/snow/framework/util/FreemarkUtils.java new file mode 100644 index 0000000..8b0790e --- /dev/null +++ b/snow-framework/src/main/java/com/snow/framework/util/FreemarkUtils.java @@ -0,0 +1,30 @@ +package com.snow.framework.util; + +import freemarker.cache.StringTemplateLoader; +import freemarker.template.Configuration; +import freemarker.template.Template; +import lombok.extern.slf4j.Slf4j; + +import java.io.StringWriter; +import java.util.Map; + +@Slf4j +public class FreemarkUtils { + + public static String process(String templateName,String templateValue,Map model){ + Configuration configuration = new Configuration(Configuration.VERSION_2_3_27); + StringTemplateLoader templateLoader = new StringTemplateLoader(); + configuration.setTemplateLoader(templateLoader); + configuration.setDefaultEncoding("UTF-8"); + StringWriter stringWriter = new StringWriter(); + Template template = null; + try { + template = new Template(templateName, templateValue, configuration); + template.process(model, stringWriter); + return stringWriter.toString(); + } catch (Exception e) { + log.info("构建模板消息错误",e); + throw new RuntimeException(e); + } + } +} diff --git a/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java b/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java index fb3f00c..f147b56 100644 --- a/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java +++ b/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java @@ -36,7 +36,7 @@ public class SysSendMessageDTO implements Serializable { /** * 参数List */ - private Map paramMap; + private Map paramMap; /** * 接收人 @@ -45,7 +45,7 @@ public class SysSendMessageDTO implements Serializable { /** * 接收人set集合 */ - private Set receiverSet; + private Set receiverSet; /** * 抄送人 */ @@ -73,4 +73,14 @@ public class SysSendMessageDTO implements Serializable { */ private DingTalkMessageType dingTalkMessageType; + /** + * pc端url + */ + private String pcUrl; + + /** + * app端url + */ + private String appUrl; + } diff --git a/snow-framework/src/main/java/com/snow/framework/web/service/InnerMessageService.java b/snow-framework/src/main/java/com/snow/framework/web/service/InnerMessageService.java new file mode 100644 index 0000000..b359184 --- /dev/null +++ b/snow-framework/src/main/java/com/snow/framework/web/service/InnerMessageService.java @@ -0,0 +1,84 @@ +package com.snow.framework.web.service; + +import cn.hutool.core.util.ObjectUtil; +import com.snow.common.utils.PatternUtils; +import com.snow.common.utils.StringUtils; +import com.snow.framework.util.FreemarkUtils; +import com.snow.framework.web.domain.common.SysSendMessageDTO; +import com.snow.system.domain.SysMessageTemplate; +import com.snow.system.domain.SysMessageTransition; +import com.snow.system.service.ISysMessageTemplateService; +import com.snow.system.service.ISysMessageTransitionService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Set; + +/** + * @author qimingjin + * @Title: + * @Description: + * @date 2021/6/29 16:33 + */ +@Component +@Slf4j +public class InnerMessageService { + + @Resource + private ISysMessageTemplateService sysMessageTemplateService; + + @Resource + private ISysMessageTransitionService sysMessageTransitionService; + + /** + * 发送站内信息 + * @param sysSendMessageDTO + */ + public void sendInnerMessage(SysSendMessageDTO sysSendMessageDTO) { + SysMessageTransition message = new SysMessageTransition(); + SysMessageTemplate sysMessageTemplate= sysMessageTemplateService.getSysMessageTemplateByCode(sysSendMessageDTO.getTemplateByCode()); + if(ObjectUtil.isNull(sysMessageTemplate)){ + log.error("@发送站内信是模板code不正确..."); + throw new RuntimeException("模板code不正确"); + } + Set receiverSet = sysSendMessageDTO.getReceiverSet(); + try { + + message.setProducerId(sysSendMessageDTO.getFrom()); + + message.setTemplateCode(sysMessageTemplate.getTemplateCode()); + //组装参数消息体 + String messageContext = FreemarkUtils.process(sysMessageTemplate.getTemplateCode(), sysMessageTemplate.getTemplateBody(), sysSendMessageDTO.getParamMap()); + + if(StringUtils.isNotEmpty(sysMessageTemplate.getPcUrl())){ + String pcUrl = FreemarkUtils.process(sysMessageTemplate.getTemplateCode(), sysMessageTemplate.getPcUrl(), sysSendMessageDTO.getParamMap()); + message.setPcUrl(pcUrl); + } + + if(StringUtils.isNotEmpty(sysMessageTemplate.getAppUrl())){ + String appUrl = FreemarkUtils.process(sysMessageTemplate.getTemplateCode(), sysMessageTemplate.getAppUrl(), sysSendMessageDTO.getParamMap()); + message.setAppUrl(appUrl); + } + message.setMessageContent(messageContext); + message.setMessageStatus(0L); + message.setIconClass(sysMessageTemplate.getIconClass()); + }catch (Exception e){ + log.error("调用sendSimpleMail发送邮件失败:{}",e.getMessage()); + message.setMessageStatus(1L); + } + if(ObjectUtil.isNotNull(sysSendMessageDTO.getReceiver())){ + message.setConsumerId(sysSendMessageDTO.getReceiver()); + sysMessageTransitionService.insertSysMessageTransition(message); + } + + if(!StringUtils.isEmpty(receiverSet)){ + receiverSet.forEach(t->{ + message.setConsumerId(t); + sysMessageTransitionService.insertSysMessageTransition(message); + }); + } + } +} diff --git a/snow-system/src/main/java/com/snow/system/domain/SysMessageTemplate.java b/snow-system/src/main/java/com/snow/system/domain/SysMessageTemplate.java index b5028c4..4774cd5 100644 --- a/snow-system/src/main/java/com/snow/system/domain/SysMessageTemplate.java +++ b/snow-system/src/main/java/com/snow/system/domain/SysMessageTemplate.java @@ -41,6 +41,21 @@ public class SysMessageTemplate extends BaseEntity @Excel(name = "1-钉钉消息,2--邮件,3--短信,4--站内消息") private Long templateType; + /** + * 消息pc端url + */ + private String pcUrl; + + /** + * 消息app端url + */ + private String appUrl; + + /** + * 图标样式 + */ + private String iconClass="fa fa-envelope fa-fw"; + /** 0--正常,1--禁用 */ @Excel(name = "0--正常,1--禁用") private Long templateStatus; diff --git a/snow-system/src/main/java/com/snow/system/domain/SysMessageTransition.java b/snow-system/src/main/java/com/snow/system/domain/SysMessageTransition.java index 4110711..4409b22 100644 --- a/snow-system/src/main/java/com/snow/system/domain/SysMessageTransition.java +++ b/snow-system/src/main/java/com/snow/system/domain/SysMessageTransition.java @@ -17,39 +17,56 @@ public class SysMessageTransition extends BaseEntity { private static final long serialVersionUID = 1L; - /** null */ + /** id */ private Long id; /** 生产者id */ - @Excel(name = "生产者id") private String producerId; private SysUser producerUser; /** 消费者id */ - @Excel(name = "消费者id") private String consumerId; private SysUser consumerUser; /** 消息类型 */ - @Excel(name = "消息类型") private String messageType; /** 消息外部id */ - @Excel(name = "消息外部id") private String messageOutsideId; + /** + * 模板code + */ + private String templateCode; + + /** + * 消息内容 + */ + private String messageContent; + + /** + * 消息展示1app端 2pc端 3官网 4后台 + */ + private String messageShow; + + /** + * 消息pc端url + */ + private String pcUrl; + + /** + * 消息app端url + */ + private String appUrl; /** 0--正常,1--禁用 */ - @Excel(name = "0--正常,1--禁用") private Long messageStatus; /** 0--未读,1--已读 */ - @Excel(name = "0--未读,1--已读") private Long messageReadStatus; /** 0--正常,1--删除 */ - @Excel(name = "0--正常,1--删除") private Long isDelete; diff --git a/snow-system/src/main/resources/mapper/system/SysMessageTemplateMapper.xml b/snow-system/src/main/resources/mapper/system/SysMessageTemplateMapper.xml index c6f107a..02119ce 100644 --- a/snow-system/src/main/resources/mapper/system/SysMessageTemplateMapper.xml +++ b/snow-system/src/main/resources/mapper/system/SysMessageTemplateMapper.xml @@ -11,6 +11,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + @@ -20,7 +23,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select id, template_code, template_name, template_body, template_desc, template_type, template_status, is_delete, create_by, create_time, update_by, update_time from sys_message_template + select id, template_code, template_name, template_body, pc_url,app_url,icon_class,template_desc, template_type, template_status, is_delete, create_by, create_time, update_by, update_time from sys_message_template @@ -36,6 +44,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and message_status = #{messageStatus} and message_read_status = #{messageReadStatus} and is_delete = #{isDelete} + and message_show = #{messageShow} + and template_code = #{templateCode} order by ${orderBy} @@ -56,6 +66,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" message_outside_id, message_status, message_read_status, + template_code, + message_content, + message_show, + pc_url, + app_url, + icon_class, is_delete, create_by, create_time, @@ -69,6 +85,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{messageOutsideId}, #{messageStatus}, #{messageReadStatus}, + #{templateCode}, + #{messageContent}, + #{messageShow}, + #{pcUrl}, + #{appUrl}, + #{iconClass}, #{isDelete}, #{createBy}, #{createTime}, @@ -86,6 +108,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" message_outside_id = #{messageOutsideId}, message_status = #{messageStatus}, message_read_status = #{messageReadStatus}, + template_code=#{templateCode}, + message_content=#{messageContent}, + message_show=#{messageShow}, + pc_url=#{pcUrl}, + app_url=#{appUrl}, + icon_class=#{iconClass}, is_delete = #{isDelete}, create_by = #{createBy}, create_time = #{createTime}, @@ -104,6 +132,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" message_outside_id = #{messageOutsideId}, message_status = #{messageStatus}, message_read_status = #{messageReadStatus}, + template_code=#{templateCode}, + message_content=#{messageContent}, + message_show=#{messageShow}, + pc_url=#{pcUrl}, + app_url=#{appUrl}, + icon_class=#{iconClass}, is_delete = #{isDelete}, create_by = #{createBy}, create_time = #{createTime},