dcs 4 月之前
父節點
當前提交
9e4f074d05

+ 19 - 1
virgo.api/src/main/java/com/bosshand/virgo/api/controller/ReminderController.java

@@ -1,6 +1,8 @@
 package com.bosshand.virgo.api.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.bosshand.virgo.api.model.Reminder;
+import com.bosshand.virgo.api.service.QuartzService;
 import com.bosshand.virgo.api.service.ReminderService;
 import com.bosshand.virgo.core.response.Response;
 import io.swagger.annotations.Api;
@@ -20,6 +22,9 @@ public class ReminderController {
     @Autowired
     ReminderService reminderService;
 
+    @Autowired
+    QuartzService quartzService;
+
     @ApiOperation("获取分页")
     @RequestMapping(value = "/{currPage}/{pageSize}", method = RequestMethod.POST)
     public Response list(@RequestBody Reminder reminder, @PathVariable int currPage, @PathVariable int pageSize) {
@@ -40,7 +45,9 @@ public class ReminderController {
     @ApiOperation("保存")
     @RequestMapping(value = "", method = RequestMethod.POST)
     public Response insert(@RequestBody Reminder reminder) {
-        return Response.ok(reminderService.insert(reminder));
+        Reminder r = reminderService.insert(reminder);
+        quartzService.executeJobReminderQuartz(r);
+        return Response.ok(r);
     }
 
     @ApiOperation("获取")
@@ -63,4 +70,15 @@ public class ReminderController {
         return Response.ok();
     }
 
+    @ApiOperation("system发送消息")
+    @RequestMapping(value = "/send", method = RequestMethod.POST)
+    public Response send(@RequestBody JSONObject jsonObject) {
+        String userId = jsonObject.getString("userId");
+        String data = jsonObject.getString("data");
+        String type = jsonObject.getString("type");
+        reminderService.send(userId, data, type);
+        return Response.ok();
+    }
+
+
 }

+ 34 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/job/JobReminderQuartz.java

@@ -0,0 +1,34 @@
+package com.bosshand.virgo.api.job;
+
+import com.alibaba.fastjson.JSONObject;
+import com.bosshand.virgo.api.service.ReminderService;
+import org.quartz.JobDataMap;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.quartz.QuartzJobBean;
+
+/**
+ * 提醒任务
+ */
+public class JobReminderQuartz extends QuartzJobBean {
+
+    @Autowired
+    ReminderService reminderService;
+
+    static Logger log = LoggerFactory.getLogger(JobReminderQuartz.class);
+
+    @Override
+    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info("=====================发送提醒====================");
+        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
+        String data = mergedJobDataMap.getString("data");
+        JSONObject json = JSONObject.parseObject(data);
+        long userId = json.getLongValue("creator");
+        int type = json.getIntValue("type");
+        reminderService.send(String.valueOf(userId), json.toJSONString(), String.valueOf(type));
+    }
+
+}

+ 45 - 4
virgo.api/src/main/java/com/bosshand/virgo/api/service/QuartzService.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.bosshand.virgo.api.model.JobInfo;
 import com.bosshand.virgo.api.model.Project;
+import com.bosshand.virgo.api.model.Reminder;
 import org.quartz.*;
 import org.quartz.impl.matchers.GroupMatcher;
 import org.slf4j.Logger;
@@ -11,10 +12,9 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.*;
 
 @Service
 public class QuartzService {
@@ -88,6 +88,24 @@ public class QuartzService {
         deleteCronJob(project.getId() + "_" + project.getName(), DEFAULT_JOB_GROUP, TRIGGER_PRE + project.getId() + "_" + project.getName(), DEFAULT_TRIGGER_GROUP);
     }
 
+    /**
+     * 提醒任务
+     */
+    public void executeJobReminderQuartz(Reminder reminder){
+
+        String jobName = reminder.getId() + "_" + reminder.getName();
+        String jobClassName = "com.bosshand.virgo.api.job.JobReminderQuartz";
+
+        // 示例:2023年10月1日12点(UTC时间)
+        Instant futureTime = Instant.parse(reminder.getDate());
+
+        // 计算延迟时间(毫秒)
+        long delay = Duration.between(Instant.now(), futureTime).toMillis();
+
+        // 执行任务
+        executeOnce(delay, jobName, jobClassName, JSONObject.toJSONString(reminder));
+    }
+
     public String addCronJob(String jobName, String cron, String jobClassName, JSONObject data) {
         try {
             // 当前任务不存在才进行添加
@@ -179,6 +197,29 @@ public class QuartzService {
         }
     }
 
+    public String executeOnce(long delay, String jobName, String jobClassName, String data) {
+        try {
+            JobKey jobKey = JobKey.jobKey(jobName, DEFAULT_JOB_GROUP);
+            JobDetail job = JobBuilder.newJob(getClass(jobClassName).getClass())
+                    .withIdentity(jobKey).build();
+
+            job.getJobDataMap().put("data", data);
+
+            Trigger trigger = TriggerBuilder.newTrigger()
+                    .startAt(new Date(System.currentTimeMillis() + delay))
+                    .withIdentity(TriggerKey.triggerKey(TRIGGER_PRE + jobName, DEFAULT_TRIGGER_GROUP))
+                    .build();
+
+            // 启动调度器
+            scheduler.scheduleJob(job, trigger);
+            scheduler.start();
+            return "SUCCESS";
+        } catch (Exception e) {
+            log.error("[指定时间点执行一次任务]失败,报错:", e);
+            return "FAIL";
+        }
+    }
+
 
     /**
      * 查询定时任务列表

+ 52 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/service/ReminderService.java

@@ -1,7 +1,13 @@
 package com.bosshand.virgo.api.service;
 
+import com.alibaba.fastjson.JSONObject;
 import com.bosshand.virgo.api.dao.ReminderDao;
 import com.bosshand.virgo.api.model.Reminder;
+import com.bosshand.virgo.core.utils.StringUtil;
+import com.bosshand.virgo.core.utils.WeChatUtil;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -10,6 +16,8 @@ import java.util.List;
 @Service
 public class ReminderService {
 
+    static Logger log = LoggerFactory.getLogger(ReminderService.class);
+
     @Autowired
     ReminderDao reminderDao;
 
@@ -44,4 +52,48 @@ public class ReminderService {
         return reminderDao.delete(id);
     }
 
+    private String getToken() {
+        JSONObject js = new JSONObject();
+        String userId = "system";
+        long timestamp = System.currentTimeMillis() + 30000;
+        String yei = "50abd47112ebe8c5a73f4694c96a49ce";
+        String sign = DigestUtils.md5Hex(userId + timestamp + yei);
+
+        js.put("userId", userId);
+        js.put("timestamp", timestamp);
+        js.put("sign", sign);
+        String s = WeChatUtil.httpPost("https://www.waywish.com/im/user/token/get", js, null);
+        if (StringUtil.notBlank(s)) {
+            String token = JSONObject.parseObject(s).getJSONObject("data").getString("token");
+            return token;
+        }
+        return null;
+    }
+
+    public void send(String userId, String data, String type) {
+        long m = System.currentTimeMillis();
+        JSONObject js = new JSONObject();
+        js.put("conversationId", userId);
+        js.put("conversationType", "private");
+        js.put("from", "system");
+        js.put("to", userId);
+        js.put("type", "text");
+        js.put("isRead", 0);
+        js.put("isRevoke", 0);
+        js.put("isDeleted", 0);
+        js.put("status", "unSend");
+        js.put("time", m);
+        js.put("extra", data);
+        JSONObject jo = new JSONObject();
+        jo.put("nickname", "系统通知");
+        jo.put("avatarUrl", "https://file-node.oss-cn-shanghai.aliyuncs.com/youji/3156449b8a1a4874981b2a76d5947721");
+        js.put("fromUserInfo", jo);
+        JSONObject j = new JSONObject();
+        j.put("text", type);
+        js.put("body", j);
+        String s = WeChatUtil.httpPost("https://www.waywish.com/im/message/save", js, getToken());
+        log.info("在" + m + "时间点给userId:" + userId + "发送消息成功.");
+    }
+
+
 }

+ 1 - 1
virgo.api/src/main/resources/mapper/ReminderMapper.xml

@@ -22,7 +22,7 @@
     </resultMap>
 
     <select id="get" resultMap="result">
-        select * from reminder where id = #{id}
+        SELECT a.*, b.name as creatorName, b.portrait as creatorPortrait FROM reminder a LEFT JOIN mgr_user b ON a.creator = b.id where a.id = #{id}
     </select>
 
     <select id="getList" resultMap="result">

+ 1 - 0
virgo.core/src/main/java/com/bosshand/virgo/core/utils/WeChatUtil.java

@@ -61,6 +61,7 @@ public class WeChatUtil {
             // 设置通用的请求属性
             if(token != null){
                 conn.setRequestProperty("Token", token);
+                conn.setRequestProperty("token", token);
             }
             conn.setRequestProperty("accept", "*/*");
             conn.setRequestProperty("connection", "Keep-Alive");