|
|
|
@ -0,0 +1,123 @@
|
|
|
|
|
package com.hnac.hzinfo.inspect.task.schedule; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.google.common.collect.Lists; |
|
|
|
|
import com.hnac.hzims.message.MessageConstants; |
|
|
|
|
import com.hnac.hzims.message.dto.BusinessMessageDTO; |
|
|
|
|
import com.hnac.hzims.message.dto.SmsImmediatelyPushDTO; |
|
|
|
|
import com.hnac.hzims.message.fegin.IMessageClient; |
|
|
|
|
import com.hnac.hzinfo.inspect.config.RedisKeyConfig; |
|
|
|
|
import com.hnac.hzinfo.inspect.task.TaskContants; |
|
|
|
|
import com.hnac.hzinfo.inspect.task.entity.TaskEntity; |
|
|
|
|
import com.hnac.hzinfo.inspect.task.entity.TaskUserEntity; |
|
|
|
|
import com.hnac.hzinfo.inspect.task.service.ITaskService; |
|
|
|
|
import com.hnac.hzinfo.inspect.task.service.ITaskUserService; |
|
|
|
|
import com.xxl.job.core.biz.model.ReturnT; |
|
|
|
|
import com.xxl.job.core.handler.annotation.XxlJob; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
|
import org.springblade.core.tool.utils.CollectionUtil; |
|
|
|
|
import org.springblade.core.tool.utils.DateUtil; |
|
|
|
|
import org.springblade.core.tool.utils.Func; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.cglib.core.Local; |
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.time.LocalTime; |
|
|
|
|
import java.time.ZoneOffset; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
import static com.hnac.hzinfo.inspect.task.schedule.XxlJobConstants.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @ClassName TaskSchedule |
|
|
|
|
* @description: 巡检任务定时任务 |
|
|
|
|
* @author: hx |
|
|
|
|
* @create: 2023-08-10 14:21 |
|
|
|
|
* @Version 4.0 |
|
|
|
|
**/ |
|
|
|
|
@Component |
|
|
|
|
@RequiredArgsConstructor |
|
|
|
|
@Slf4j |
|
|
|
|
public class TaskSchedule { |
|
|
|
|
|
|
|
|
|
private final RedisTemplate redisTemplate; |
|
|
|
|
private final ITaskService taskService; |
|
|
|
|
private final ITaskUserService taskUserService; |
|
|
|
|
private final RedisKeyConfig redisKeyConfig; |
|
|
|
|
private final IMessageClient messageClient; |
|
|
|
|
|
|
|
|
|
@Value("${hzims.inspect.task.templateCode.taskEnd}") |
|
|
|
|
private String taskEndTemplateCode; |
|
|
|
|
|
|
|
|
|
@XxlJob(TASK_END_REMIND) |
|
|
|
|
public ReturnT<String> sendTaskEndMessage(String params) { |
|
|
|
|
Set<TaskEntity> remindTasks = redisTemplate.opsForZSet().rangeByScore(redisKeyConfig.getTaskEndRemind(), |
|
|
|
|
LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toEpochSecond(ZoneOffset.of("+8")), |
|
|
|
|
LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))); |
|
|
|
|
if(CollectionUtil.isEmpty(remindTasks)) { |
|
|
|
|
return new ReturnT(ReturnT.SUCCESS_CODE,"当前无待发送消息的任务"); |
|
|
|
|
} |
|
|
|
|
// 获取redis待推送巡检任务中状态为进行中的任务
|
|
|
|
|
List<Long> idList = remindTasks.stream().map(TaskEntity::getId).collect(Collectors.toList()); |
|
|
|
|
List<TaskEntity> taskList = taskService.list( |
|
|
|
|
Wrappers.<TaskEntity>lambdaQuery() |
|
|
|
|
.in(TaskEntity::getId, idList) |
|
|
|
|
.in(TaskEntity::getStatus, Lists.newArrayList(TaskContants.TaskStatusEnum.UNDERWAY_STATUS.getStatus(),TaskContants.TaskStatusEnum.INIT_STATUS.getStatus())) |
|
|
|
|
); |
|
|
|
|
Map<Long, List<TaskUserEntity>> taskUserMap = taskUserService.list(Wrappers.<TaskUserEntity>lambdaQuery().in(TaskUserEntity::getTaskId,idList)).stream().collect(Collectors.groupingBy(TaskUserEntity::getTaskId)); |
|
|
|
|
taskList.stream().filter(task -> taskUserMap.containsKey(task.getId())).flatMap(task -> |
|
|
|
|
taskUserMap.get(task.getId()).stream().filter(taskUser -> Func.isNotEmpty(taskUser.getUserId())).map(taskUser -> { |
|
|
|
|
task.setUserId(taskUser.getUserId()); |
|
|
|
|
return task; |
|
|
|
|
}) |
|
|
|
|
).forEach(this::sendTaskEndMessage); |
|
|
|
|
|
|
|
|
|
redisTemplate.opsForZSet().remove(redisKeyConfig.getTaskEndRemind(),remindTasks.toArray(new TaskEntity[remindTasks.size()])); |
|
|
|
|
return ReturnT.SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 推送消息 |
|
|
|
|
* @param taskEntity 巡检任务 |
|
|
|
|
*/ |
|
|
|
|
private void sendTaskEndMessage(TaskEntity taskEntity) { |
|
|
|
|
// 短信推送
|
|
|
|
|
SmsImmediatelyPushDTO smsPush = SmsImmediatelyPushDTO.builder() |
|
|
|
|
.deptId(taskEntity.getCreateDept()) |
|
|
|
|
.taskId(taskEntity.getId()) |
|
|
|
|
.businessKey("巡检任务结束提醒") |
|
|
|
|
.businessClassify(MessageConstants.BusinessClassifyEnum.INSPECT.getKey()) |
|
|
|
|
.content("您有一条未执行的任务【"+taskEntity.getPlanName()+"】将于"+ DateUtil.format(taskEntity.getPlanEndTime(),DateUtil.PATTERN_DATETIME)+"截止,请及时处理!") |
|
|
|
|
.subject("巡检任务结束提醒") |
|
|
|
|
.pusher(taskEntity.getUserId().toString()) |
|
|
|
|
.createUser(taskEntity.getCreateUser()) |
|
|
|
|
.tenantId(taskEntity.getTenantId()) |
|
|
|
|
.resourceCode(taskEndTemplateCode) |
|
|
|
|
.params(new HashMap<String,String>(){{ |
|
|
|
|
put("one",taskEntity.getPlanName()); |
|
|
|
|
put("two",DateUtil.format(taskEntity.getPlanEndTime(),DateUtil.PATTERN_DATETIME)); |
|
|
|
|
}}) |
|
|
|
|
.build(); |
|
|
|
|
messageClient.sendSmsImmediatelyMsg(smsPush); |
|
|
|
|
// app推送
|
|
|
|
|
BusinessMessageDTO appMessage = new BusinessMessageDTO(); |
|
|
|
|
appMessage.setDeptId(taskEntity.getCreateDept()); |
|
|
|
|
appMessage.setBusinessKey("巡检任务结束提醒"); |
|
|
|
|
appMessage.setBusinessClassify(MessageConstants.BusinessClassifyEnum.INSPECT.getKey()); |
|
|
|
|
appMessage.setContent("您有一条未执行的任务【"+taskEntity.getPlanName()+"】将于"+ DateUtil.format(taskEntity.getPlanEndTime(),DateUtil.PATTERN_DATETIME)+"截止,请及时处理!"); |
|
|
|
|
appMessage.setTaskId(taskEntity.getId()); |
|
|
|
|
appMessage.setSubject("巡检任务结束提醒"); |
|
|
|
|
appMessage.setUserIds(taskEntity.getUserId().toString()); |
|
|
|
|
appMessage.setCreateUser(taskEntity.getCreateUser()); |
|
|
|
|
appMessage.setTenantId(taskEntity.getTenantId()); |
|
|
|
|
messageClient.sendAppAndWsMsgByUsers(appMessage); |
|
|
|
|
taskService.update(Wrappers.<TaskEntity>lambdaUpdate().set(TaskEntity::getEndRemindFlag,true).eq(TaskEntity::getId,taskEntity.getId())); |
|
|
|
|
} |
|
|
|
|
} |