Browse Source

消息发送

zyh
小久哥 3 years ago
parent
commit
e8a5668da4
  1. 181
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java
  2. 5
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgDTO.java
  3. 1
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java
  4. 8
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java
  5. 3
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/dto/TaskDetailDTO.java
  6. 2
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/discuss/DiscussServiceImpl.java
  7. 34
      zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java

181
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java

@ -2,14 +2,15 @@ package cn.iocoder.yudao.module.farm.annotation;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.farm.controller.admin.project.vo.ProjectCreateReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.project.vo.ProjectRespVO;
import cn.iocoder.yudao.module.farm.controller.admin.project.vo.ProjectUpdateReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.task.vo.TaskCreateReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.task.vo.TaskRespVO;
import cn.iocoder.yudao.module.farm.controller.admin.task.vo.TaskUpdateReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.discuss.DiscussDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.logMsg.LogMsgDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.project.ProjectDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.task.TaskDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.taskCate.TaskCateDO;
@ -27,6 +28,8 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.apache.poi.ss.formula.functions.T;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
@ -34,6 +37,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -86,73 +90,81 @@ public class FarmMsgAspect {
return object;
}
// 构造一个企业推送消息
WxCpMessage message = new WxCpMessage();
if (farmMsg.type() == FarmMsgTypeEnum.PROJECT){
if (farmMsg.operation() == FarmMsgTypeEnum.CREATE){
Long id = (Long) ((CommonResult<?>) object).getData();
//处理所需要数据
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
FarmMsgDTO farmMsgDTO = this.assembleMsg(method, pjp.getArgs(), (CommonResult<?>) object);
if (farmMsg.type() == FarmMsgTypeEnum.PROJECT || farmMsg.type() == FarmMsgTypeEnum.TASK){
//处理所需要数据
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
FarmMsgDTO farmMsgDTO = this.assembleMsg(method, pjp.getArgs(), (CommonResult<?>) object);
if (farmMsgDTO.getNoSendMsg()){
//草稿不发送
return object;
}
//创建 更新 删除 发送信息
if (farmMsg.operation().equals(FarmMsgTypeEnum.CREATE) || farmMsg.operation().equals(FarmMsgTypeEnum.UPDATE)
|| farmMsg.operation().equals(FarmMsgTypeEnum.DELETE)){
//接受消息的人
String toUserStr = this.assembleCpUserId(farmMsg.type(), id);
String toUserStr = this.assembleCpUserId(farmMsg.type(), farmMsgDTO.getId());
//发起人
AdminUserDO adminUser = adminUserMapper.selectOne(AdminUserDO::getId, getLoginUserId());
CpUserDO user = cpUserMapper.selectOne(CpUserDO::getUserId, adminUser.getCpUserId());
StringBuilder description = new StringBuilder()
.append(user.getName())
.append(" ")
.append(farmMsgDTO.getOperation().getDesc())
.append(farmMsgDTO.getType().getDesc())
.append(":")
.append(farmMsgDTO.getTitle())
.append(ObjectUtil.isEmpty(farmMsgDTO.getMsg()) ? farmMsgDTO.getTitle() : farmMsgDTO.getMsg())
.append("(其中有你)");
if (!toUserStr.contains("WeiLaiKeQi")){
return object;
}
// 发企业微信
WxCpMessageService wxCpMessageService = wxCpService.getMessageService();
//消息类型 必填 此处固定为 文本卡片
message.setMsgType("textcard");
//企业应用id 必填
message.setAgentId(wxCpService.getWxCpConfigStorage().getAgentId());
//消息标题 必填
message.setTitle("回乡农场任务管理更新");
//消息描述 必填
message.setDescription(description.toString());
//点击后跳转链接 必填
message.setUrl("https://www.baidu.com");
//接受消息成员 选填
message.setToUser(toUserStr);
// WxCpMessageSendResult result = wxCpMessageService.send(message);
// log.info("企业微信消息发送结果:{}",result);
System.out.println(message);
if (!farmMsgDTO.getNoSendMsg()){
//发送信息
this.sendCpMsg(description.toString(), toUserStr);
}
}
// 跟新项目 删除项目 更新任务 删除任务。。。
// 构造一个卡片消息 消息名称 : 回乡农场任务管理更新 , 小标题: xxx 创建了项目,请查看处理。
// appid 是小程序的appid
// touser 可以传多个。
// 这里处理完删除注释
//插入日志
LogMsgDO logMsgDO = LogMsgDO.builder()
.type(farmMsgDTO.getType().getDesc())
.operation(farmMsgDTO.getOperation().getDesc())
.title(farmMsgDTO.getTitle())
.msg(farmMsgDTO.getMsg())
.userId(getLoginUserId())
.build();
logMsgMapper.insert(logMsgDO);
}
}
return object;
}
public void sendCpMsg(String desc, String userStr) throws Throwable{
// 构造一个企业推送消息
WxCpMessage message = new WxCpMessage();
// 发企业微信
WxCpMessageService wxCpMessageService = wxCpService.getMessageService();
//消息类型 必填 此处固定为 文本卡片
message.setMsgType("textcard");
//企业应用id 必填
message.setAgentId(wxCpService.getWxCpConfigStorage().getAgentId());
//消息标题 必填
message.setTitle("回乡农场通知");
//消息描述 必填
message.setDescription(desc);
//点击后跳转链接 必填
message.setUrl("https://www.baidu.com");
//接受消息成员 选填
message.setToUser(userStr);
log.info("企业微信消息发送内容:{}", message);
WxCpMessageSendResult result = wxCpMessageService.send(message);
log.info("企业微信消息发送结果:{}",result);
}
/**
* @Description 获取需要接受信息的成员
* @param id 可能是 projectId taskId discussId
@ -185,62 +197,45 @@ public class FarmMsgAspect {
public FarmMsgDTO assembleMsg(Method method, Object[] objects, Object result){
String title = "";
String msg = "";
Long id = 0L;
Boolean noSendMsg = false;
FarmMsg farmMsg = method.getAnnotation(FarmMsg.class);
if (method.getName().startsWith("get")){
for (Object object : objects) {
log.info("after用户{},{},{},{}",getLoginUserId(), farmMsg.type(), farmMsg.title(), object);
if (farmMsg.type().equals(FarmMsgTypeEnum.PROJECT)){
CommonResult<ProjectRespVO> projectDO = (CommonResult<ProjectRespVO>) result;
title = projectDO.getData().getName();
}
if (farmMsg.type().equals(FarmMsgTypeEnum.TASK)){
CommonResult<TaskRespVO> taskDO = (CommonResult<TaskRespVO>) result;
ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getData().getProjectId());
TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getData().getTaskCateId());
title = project.getName();
msg = taskCate.getName();
}
}
}
if (method.getName().startsWith("update")){
for (Object object : objects) {
if (farmMsg.type().equals(FarmMsgTypeEnum.PROJECT)){
ProjectUpdateReqVO projectDO = (ProjectUpdateReqVO) object;
title = projectDO.getName();
}
if (farmMsg.type().equals(FarmMsgTypeEnum.TASK)){
TaskUpdateReqVO taskDO = (TaskUpdateReqVO) object;
ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId());
title = project.getName();
TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId());
msg = taskCate.getName();
}
Object obj = Arrays.stream(objects).findFirst().get();
if (farmMsg.type().equals(FarmMsgTypeEnum.PROJECT)){
ProjectDO projectDO = new ProjectDO();
if (farmMsg.operation().equals(FarmMsgTypeEnum.CREATE) || farmMsg.operation().equals(FarmMsgTypeEnum.UPDATE)){
projectDO = BeanUtil.copyProperties(obj, ProjectDO.class);
}else if (farmMsg.operation().equals(FarmMsgTypeEnum.DELETE) || farmMsg.operation().equals(FarmMsgTypeEnum.SELECT)){
projectDO = projectMapper.selectOne(ProjectDO::getId, obj);
}
}
if (method.getName().startsWith("create")){
for (Object object : objects){
if (farmMsg.type().equals(FarmMsgTypeEnum.PROJECT)){
ProjectCreateReqVO projectDO = (ProjectCreateReqVO) object;
title = projectDO.getName();
}
if (farmMsg.type().equals(FarmMsgTypeEnum.TASK)){
TaskCreateReqVO taskDO = (TaskCreateReqVO) object;
ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId());
title = project.getName();
TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId());
msg = taskCate.getName();
}
title = projectDO.getName();
noSendMsg = ObjectUtil.isEmpty(projectDO.getDraft()) ? true : projectDO.getDraft();
id = farmMsg.operation() == FarmMsgTypeEnum.CREATE ? (Long) ((CommonResult<?>) result).getData() : projectDO.getId();
}else if (farmMsg.type().equals(FarmMsgTypeEnum.TASK)){
TaskDO taskDO = new TaskDO();
if (farmMsg.operation().equals(FarmMsgTypeEnum.CREATE) || farmMsg.operation().equals(FarmMsgTypeEnum.UPDATE)){
taskDO = BeanUtil.copyProperties(obj, TaskDO.class);
}else if (farmMsg.operation().equals(FarmMsgTypeEnum.DELETE) || farmMsg.operation().equals(FarmMsgTypeEnum.SELECT)){
taskDO = taskMapper.selectOne(TaskDO::getId, obj);
}
ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId());
TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId());
title = project.getName();
msg = taskCate.getName();
noSendMsg = ObjectUtil.isEmpty(taskDO.getDraft()) ? true : taskDO.getDraft();
id = farmMsg.operation() == FarmMsgTypeEnum.CREATE ? (Long) ((CommonResult<?>) result).getData() : taskDO.getId();
}
FarmMsgDTO farmMsgDTO = FarmMsgDTO.builder()
.type(FarmMsgTypeEnum.getDescBtType(farmMsg.type()))
.operation(FarmMsgTypeEnum.getDescBtType(farmMsg.operation()))
.title(title)
.msg(msg).build();
.msg(msg)
.id(id)
.noSendMsg(noSendMsg)
.build();
return farmMsgDTO;
}

5
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgDTO.java

@ -17,4 +17,9 @@ public class FarmMsgDTO {
private String title;
private String msg;
private Long id;
//草稿 不发送信息
private Boolean noSendMsg;
}

1
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java

@ -67,6 +67,7 @@ public class ProjectController {
return success(true);
}
@FarmMsg(type = FarmMsgTypeEnum.PROJECT, operation = FarmMsgTypeEnum.SELECT)
@GetMapping("/get")
@ApiOperation("获得农场项目")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)

8
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.page.PageVO;
import cn.iocoder.yudao.module.farm.annotation.FarmMsg;
import cn.iocoder.yudao.module.farm.annotation.FarmMsgTypeEnum;
import cn.iocoder.yudao.module.farm.controller.admin.task.dto.TaskAssemblyDTO;
import cn.iocoder.yudao.module.farm.controller.admin.task.dto.TaskDetailDTO;
import cn.iocoder.yudao.module.farm.controller.admin.task.dto.TaskMsgDTO;
@ -44,7 +45,7 @@ public class TaskController {
@Resource
private TaskService taskService;
// @FarmMsg(type = "任务", operation = "创建")
@FarmMsg(type = FarmMsgTypeEnum.TASK, operation = FarmMsgTypeEnum.CREATE)
@PostMapping("/create")
@ApiOperation("创建农场项目")
@PreAuthorize("@ss.hasPermission('farm:task:create')")
@ -52,7 +53,7 @@ public class TaskController {
return success(taskService.createTask(createReqVO));
}
// @FarmMsg(type = "任务", operation = "更新")
@FarmMsg(type = FarmMsgTypeEnum.TASK, operation = FarmMsgTypeEnum.UPDATE)
@PutMapping("/update")
@ApiOperation("更新农场项目")
@PreAuthorize("@ss.hasPermission('farm:task:update')")
@ -61,6 +62,7 @@ public class TaskController {
return success(true);
}
@FarmMsg(type = FarmMsgTypeEnum.TASK, operation = FarmMsgTypeEnum.DELETE)
@DeleteMapping("/delete")
@ApiOperation("删除农场项目")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@ -70,7 +72,7 @@ public class TaskController {
return success(true);
}
// @FarmMsg(type = "任务", operation = "查看")
@FarmMsg(type = FarmMsgTypeEnum.TASK, operation = FarmMsgTypeEnum.SELECT)
@GetMapping("/get")
@ApiOperation("获得农场项目")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)

3
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/dto/TaskDetailDTO.java

@ -32,6 +32,9 @@ public class TaskDetailDTO extends TaskDO {
//负责人
CpUserDO apiMainPerson;
//项目成员
List<CpUserDO> projectMemberList ;
//作物名
String apiCropName;

2
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/discuss/DiscussServiceImpl.java

@ -158,7 +158,7 @@ public class DiscussServiceImpl implements DiscussService {
PageUtil.startPage(pageDTO);
LambdaQueryWrapperX<DiscussDO> queryWrapperX = new LambdaQueryWrapperX<>();
queryWrapperX.eqIfPresent(DiscussDO::getTaskId, pageDTO.getTaskId());
queryWrapperX.orderByDesc(DiscussDO::getCreateTime);
List<DiscussDO> discussList = discussMapper.selectList(queryWrapperX);
if (ObjectUtil.isEmpty(discussList)){
return R.success(PageUtil.emptyPage(pageDTO, DiscussDTO.class));

34
zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.system.oshi.CpuInfo;
import cn.iocoder.yudao.framework.common.page.PageUtil;
import cn.iocoder.yudao.framework.common.page.PageVO;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@ -178,12 +179,16 @@ public class TaskServiceImpl implements TaskService {
TaskDO taskDO = taskMapper.selectById(id);
TaskDetailDTO taskMsg = new TaskDetailDTO();
BeanUtil.copyProperties(taskDO, taskMsg);
List<Long> userIdList = taskDO.getExecutorPerson().toJavaList(Long.class);
List<CpUserDO> userList = cpUserMapper.selectList(Wrappers.<CpUserDO>lambdaQuery().in(CpUserDO::getId, userIdList));
//负责人
taskMsg.setApiMainPerson(cpUserService.getById(String.valueOf(taskMsg.getMainPerson())));
//执行人
taskMsg.setApiExecutor(userList);
if (ObjectUtil.isNotEmpty(taskDO.getExecutorPerson())){
List<Long> userIdList = taskDO.getExecutorPerson().toJavaList(Long.class);
List<CpUserDO> userList = cpUserMapper.selectList(Wrappers.<CpUserDO>lambdaQuery().in(CpUserDO::getId, userIdList));
//执行人
taskMsg.setApiExecutor(userList);
}
if (ObjectUtil.isNotEmpty(taskMsg.getMainPerson())){
//负责人
taskMsg.setApiMainPerson(cpUserService.getById(String.valueOf(taskMsg.getMainPerson())));
}
//任务活动的累计数量
List<DiscussDO> discussDOList = discussMapper.selectList(DiscussDO::getTaskId, id);
taskMsg.setCumulativeQuantity(ObjectUtil.isEmpty(discussDOList) ? 0 : discussDOList.stream().mapToInt(DiscussDO::getCropNum).sum());
@ -195,7 +200,22 @@ public class TaskServiceImpl implements TaskService {
List<AreaDO> firstAreaList = areaMapper.selectList(AreaDO::getId, parendIdList);
taskMsg.setFirstAreaList(firstAreaList);
}
//作物名
if (ObjectUtil.isNotEmpty(taskDO.getCropId())){
taskMsg.setApiCropName(cropMapper.selectOne(CropDO::getId, taskDO.getCropId()).getName());
}
//项目名
if (ObjectUtil.isNotEmpty(taskDO.getProjectId())){
ProjectDO projectDO = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId());
if (ObjectUtil.isNotEmpty(projectDO.getName())){
taskMsg.setApiProjectName(projectDO.getName());
}
if (ObjectUtil.isNotEmpty(projectDO.getMembers())){
List<Long> projectMemberId = projectDO.getMembers().toJavaList(Long.class);
List<CpUserDO> projectMember = cpUserMapper.selectList(CpUserDO::getId,projectMemberId);
taskMsg.setProjectMemberList(projectMember);
}
}
return R.success(taskMsg);
}

Loading…
Cancel
Save