Forráskód Böngészése

修改定时发送

luowei 2 éve
szülő
commit
9820e5c142

+ 51 - 0
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/utils/TimerUtils.java

@@ -0,0 +1,51 @@
+package com.xunmei.common.core.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.util.Hashtable;
+import java.util.Timer;
+
+/**
+ * @author :LuoWei
+ * @date : 2023/10/8
+ */
+@Component
+public class TimerUtils {
+    private  Hashtable<String, Timer> timerHt= new Hashtable<>();
+    private  Hashtable<String, Long> delayTimeHt= new Hashtable<>();
+    private static TimerUtils instance;
+
+    public static synchronized TimerUtils getInstance() {
+        if (instance==null){
+            synchronized (TimerUtils.class){
+                if (instance == null) {
+                    instance = new TimerUtils();
+                }
+                return instance;
+            }
+        }else {
+            return instance;
+        }
+    }
+
+    private TimerUtils(){
+
+    }
+
+    public  Hashtable<String, Timer> getTimerHt() {
+        return timerHt;
+    }
+
+    public  void setTimerHt(String numID ,Timer timer) {
+        this.timerHt.put(numID,timer);
+    }
+
+    public Hashtable<String, Long> getDelayTimeHt() {
+        return delayTimeHt;
+    }
+
+    public  void setDelayTimeHt(String numID , Long delayTime) {
+        this.delayTimeHt.put(numID,delayTime);
+    }
+
+}

+ 0 - 2
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/SocCoreApplication.java

@@ -5,11 +5,9 @@ import com.xunmei.common.security.annotation.EnableCustomConfig;
 import com.xunmei.common.security.annotation.EnableSocFeignClients;
 import com.xunmei.common.swagger.annotation.EnableCustomSwagger2;
 import org.mybatis.spring.annotation.MapperScan;
-import org.mybatis.spring.annotation.MapperScans;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-import java.time.LocalDate;
 
 @MapperScan("com.xunmei.core.**.mapper")
 @EnableCustomConfig

+ 2 - 2
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/information/mapper/CoreMessageSendMapper.java

@@ -55,10 +55,10 @@ public interface CoreMessageSendMapper extends BaseMapper<CoreMessageSend> {
     /**
      * 根据发送时间查询
      *
-     * @param coreMessageSend
+     * @param sendTime
      * @return
      */
-    List<CoreMessageSend> selectBySendTime(CoreMessageSend coreMessageSend);
+    List<CoreMessageSend> selectBySendTime(@Param("sendTime") String sendTime);
 
     /**
      * 删除【请填写功能名称】

+ 44 - 40
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/information/service/impl/CoreMessageSendServiceImpl.java

@@ -4,12 +4,14 @@ package com.xunmei.core.information.service.impl;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.message.domain.CoreAnnouncementNotificationToRole;
 import com.xunmei.common.core.utils.DateUtils;
 import com.xunmei.common.core.utils.SpringUtils;
+import com.xunmei.common.core.utils.TimerUtils;
 import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.common.security.utils.SecurityUtils;
 import com.xunmei.core.information.domain.CoreMessageCenter;
@@ -79,6 +81,9 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
      */
     @Override
     public int insertCoreMessageSend(CoreMessageSend coreMessageSend) {
+        TimerUtils util = TimerUtils.getInstance();
+        Hashtable<String, Long> delayTimeHt = util.getDelayTimeHt();
+        Hashtable<String, Timer> timerHt = util.getTimerHt();
         coreMessageSend.setCreateTime(DateUtils.getNowDate());
         coreMessageSend.setCreateBy(SecurityUtils.getUsername());
         coreMessageSend.setUpdateTime(DateUtils.getNowDate());
@@ -87,28 +92,6 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
             coreMessageSend.setSendTime(DateUtils.getNowDate());
         }
         if ("1".equals(coreMessageSend.getSendTimeType())) {
-//            List<Long> collect = new ArrayList<>();
-//            if (null != coreMessageSend.getSendRole()) {
-//                List<CoreAnnouncementNotificationToRole> notificationToRoles = new ArrayList<>();
-//                CoreAnnouncementNotificationToRole coreAnnouncementNotificationToRole = new CoreAnnouncementNotificationToRole();
-//                coreAnnouncementNotificationToRole.setRoleId(coreMessageSend.getSendRole());
-//                notificationToRoles.add(coreAnnouncementNotificationToRole);
-//                List<SysUser> userListByRole = remoteUserService.getUserListByRole(notificationToRoles, SecurityConstants.INNER);
-//                collect = userListByRole.stream().map(SysUser::getId).collect(Collectors.toList());
-//            }
-//            collect.add(coreMessageSend.getSendUser());
-//            List<CoreMessageCenter> coreMessageCenterList = new ArrayList<>();
-//            for (Long userId : collect) {
-//                CoreMessageCenter coreMessageCenter = new CoreMessageCenter();
-//                coreMessageCenter.setTitle(coreMessageSend.getTitle());
-//                coreMessageCenter.setContent(coreMessageSend.getContent());
-//                coreMessageCenter.setIsRead("0");
-//                coreMessageCenter.setType("3");
-//                coreMessageCenter.setCreateTime(DateUtils.getNowDate());
-//                coreMessageCenter.setCreateBy(coreMessageSend.getCreateBy());
-//                coreMessageCenter.setUserId(userId);
-//                coreMessageCenterList.add(coreMessageCenter);
-//            }
             List<CoreMessageCenter> info = getInfo(coreMessageSend);
             coreMessageSend.setSendStatus("1");
             //立即生成消息
@@ -120,9 +103,12 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
             long sTime = sendTime.getTime();
             coreMessageSend.setSendStatus("2");
             List<CoreMessageCenter> info = getInfo(coreMessageSend);
-            Task task = new Task(info,coreMessageSend);
+            Task task = new Task(info, coreMessageSend);
             long n = sTime - nowTime;
             Timer timer = new Timer();
+            long id = IdWorker.getId();
+            coreMessageSend.setId(id);
+            timerHt.put(String.valueOf(id), timer);
             timer.schedule(task, n);
         }
         return coreMessageSendMapper.insert(coreMessageSend);
@@ -131,15 +117,16 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
     class Task extends TimerTask {
         List<CoreMessageCenter> coreMessageCenterList;
         CoreMessageSend coreMessageSend;
+
         public Task(List<CoreMessageCenter> coreMessageCenterList, CoreMessageSend coreMessageSend) {
             this.coreMessageCenterList = coreMessageCenterList;
-            this.coreMessageSend=  coreMessageSend;
+            this.coreMessageSend = coreMessageSend;
         }
 
         @Override
         public void run() {
             boolean b = coreMessageCenterService.saveBatch(coreMessageCenterList);
-            if (b){
+            if (b) {
                 coreMessageSend.setSendStatus("1");
                 coreMessageSendMapper.updateById(coreMessageSend);
             }
@@ -180,16 +167,42 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
      */
     @Override
     public int updateCoreMessageSend(CoreMessageSend coreMessageSend) {
+        cancelTask(coreMessageSend.getId());
+        TimerUtils util = TimerUtils.getInstance();
+        Hashtable<String, Timer> timerHt = util.getTimerHt();
         coreMessageSend.setUpdateTime(DateUtils.getNowDate());
         coreMessageSend.setUpdateBy(SecurityUtils.getUsername());
         if ("1".equals(coreMessageSend.getSendTimeType())) {
             coreMessageSend.setSendStatus("1");
+            List<CoreMessageCenter> info = getInfo(coreMessageSend);
+            //立即生成消息
+            coreMessageCenterService.saveBatch(info);
         } else {
             coreMessageSend.setSendStatus("2");
+            Date sendTime = coreMessageSend.getSendTime();
+            Date date = new Date();
+            long nowTime = date.getTime();
+            long sTime = sendTime.getTime();
+            List<CoreMessageCenter> info = getInfo(coreMessageSend);
+            Task task = new Task(info, coreMessageSend);
+            long n = sTime - nowTime;
+            Timer timer = new Timer();
+            timerHt.put(String.valueOf(coreMessageSend.getId()), timer);
+            timer.schedule(task, n);
         }
+
         return coreMessageSendMapper.updateById(coreMessageSend);
     }
 
+    public void cancelTask(Long id) {
+        TimerUtils util = TimerUtils.getInstance();
+        Hashtable<String, Timer> timerHt = util.getTimerHt();
+        Timer timer = timerHt.get(String.valueOf(id));
+        if (timer != null) {
+            timer.cancel();
+        }
+    }
+
     /**
      * 批量删除【请填写功能名称】
      *
@@ -204,6 +217,7 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
             coreMessageSend.setId(id);
             coreMessageSend.setDelFlag("2");
             i = coreMessageSendMapper.updateCoreMessageSend(coreMessageSend);
+            cancelTask(id);
             if (i < 1) {
                 return i;
             }
@@ -213,25 +227,15 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
 
     @Override
     public void timedTransmission(LocalDate taskTime) {
-/*        if (ObjectUtil.isEmpty(taskTime)) {
+        if (ObjectUtil.isEmpty(taskTime)) {
             taskTime = LocalDate.now();
         }
-        CoreMessageSend coreMessageSend = new CoreMessageSend();
-        Date nowTime = null;
-        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
-        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        String format = sd.format(new Date()) + " 00:00:00";
-        try {
-            nowTime = simpleDateFormat.parse(format);
-        } catch (Exception e) {
-            e.getMessage();
-        }
-        coreMessageSend.setSendTime(nowTime);
+        LocalDate today = LocalDate.now();
+        LocalDate yesterday = today.minusDays(1);
         //查询符合条件的
-        List<CoreMessageSend> coreMessageSends = baseMapper.selectBySendTime(coreMessageSend);
+        List<CoreMessageSend> coreMessageSends = baseMapper.selectBySendTime(yesterday.toString());
         if (CollectionUtil.isNotEmpty(coreMessageSends)) {
             List<Long> collect = new ArrayList<>();
-
             for (CoreMessageSend c : coreMessageSends) {
                 List<CoreMessageCenter> coreMessageCenters = new ArrayList<>();
                 if (null != c.getSendRole()) {
@@ -262,7 +266,7 @@ public class CoreMessageSendServiceImpl extends ServiceImpl<CoreMessageSendMappe
                 }
             }
 
-        }*/
+        }
     }
 
     /**

+ 3 - 3
soc-modules/soc-modules-core/src/main/resources/mapper/information/CoreMessageSendMapper.xml

@@ -94,9 +94,9 @@
         del_flag = 0
         AND send_time_type = 2
         AND send_status =2
-        <if test="sendTime != null ">
-            and send_time = #{sendTime}
-        </if>
+            and send_time
+            like concat( #{sendTime}, '%')
+
     </select>
 
     <insert id="insertCoreMessageSend" parameterType="com.xunmei.core.information.domain.CoreMessageSend">