Browse Source

课表同步增加队列等待逻辑

dzx 4 months ago
parent
commit
3480da1929

+ 57 - 27
src/main/java/com/xjrsoft/module/job/JianyuekbScheduleTask.java

@@ -2,8 +2,10 @@ package com.xjrsoft.module.job;
 
 import cn.hutool.extra.spring.SpringUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.google.gson.JsonArray;
 import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
+import com.xjrsoft.common.utils.RedisUtil;
 import com.xjrsoft.module.base.entity.BaseClass;
 import com.xjrsoft.module.base.service.IBaseClassService;
 import com.xjrsoft.module.schedule.util.DataUtil;
@@ -32,12 +34,17 @@ import java.util.stream.Collectors;
 public class JianyuekbScheduleTask {
     @Autowired
     private IBaseClassService classService;
+    
+    private final static String taskKey = "jianyuekbScheduleTask";
+
+    @Autowired
+    private RedisUtil redisUtil;
 
     @Scheduled(cron = "0 */10 * * * ?")
     public void execute() throws Exception {
-        RefreshConnectionPool();
+        doExecute();
     }
-    public void RefreshConnectionPool() throws Exception {
+    public void doExecute() throws Exception {
         String active = SpringUtil.getActiveProfile();
         if(!"prod".equals(active)){
             log.info("非正式环境,无法执行获取课表数据");
@@ -48,9 +55,9 @@ public class JianyuekbScheduleTask {
         if(receiveMsgs.isEmpty()){
             return;
         }
-        JsonArray allScheduleInfo = new JsonArray();
+
+
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-        DataUtil dataUtil = new DataUtil();
         //查询出传入排课系统的年级和班级
         List<String> eduYearSerialNo1 = receiveMsgs.stream().map(x -> "'" + x.get("edu_year_serial_no").toString() + "'").collect(Collectors.toList());
 
@@ -78,11 +85,26 @@ public class JianyuekbScheduleTask {
             gradeClassMaps.put(el.get("jianyue_id").toString(), classIds);
         }
         LocalDate today = LocalDate.now();
+
+        DataUtil dataUtil = new DataUtil();
+        Set<String> ongoing = redisUtil.get(taskKey, new TypeReference<Set<String>>() {});//正在进行中的
+        if(ongoing == null){
+            ongoing = new HashSet<>();
+        }
+
+        Map<String, Map<String, String>> dataMap = initDataMap();
         for (Map<String, Object> receiveMsg : receiveMsgs) {
+            String eduYearSerialNo = receiveMsg.get("edu_year_serial_no").toString();
+            if(ongoing.contains(eduYearSerialNo)){
+                continue;
+            }
+            ongoing.add(eduYearSerialNo);
+            redisUtil.set(taskKey, ongoing);
+
             String updSql = "update course_receive_msg set is_callback = 0 where id = " + receiveMsg.get("id").toString();
             SqlRunnerAdapter.db().update(updSql);
 
-            String eduYearSerialNo = receiveMsg.get("edu_year_serial_no").toString();
+            JsonArray allScheduleInfo = new JsonArray();
             String startDateStr = receiveMsg.get("start_date").toString();
             LocalDate startDateObj = LocalDate.parse(startDateStr);
             String endDateStr = receiveMsg.get("end_date").toString();
@@ -120,31 +142,15 @@ public class JianyuekbScheduleTask {
             updSql = "UPDATE wf_course_adjust SET enabled_mark = 0 WHERE adjust_type BETWEEN '" + startDateStr + "'" +
                     " and '" + endDateStr + "' and class_id in (" + classIds + ")";
             SqlRunnerAdapter.db().update(updSql);
+
+            ongoing.remove(eduYearSerialNo);
+            redisUtil.set(taskKey, ongoing);
+            doExecute(allScheduleInfo, dataMap, dataUtil);
         }
-        doExecute(allScheduleInfo);
+
     }
 
-    void doExecute(JsonArray scheduleInfo){
-        String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
-        List<Map<String, Object>> query = SqlRunnerAdapter.db().selectList(sql);
-        Set<String> tables = new HashSet<>();
-        for (Map<String, Object> jianyueData : query) {
-            tables.add(jianyueData.get("table_name").toString());
-        }
-        sql = "SELECT * FROM jianyue_data WHERE source_id IS NOT NULL AND jianyue_id != ''";
-        List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql);
-        Map<String, Map<String, String>> dataMap = new HashMap<>();
-        for (String table : tables) {
-            Map<String, String> tableData = new HashMap<>();
-            for (Map<String, Object> jianyueData : list) {
-                if(!table.equals(jianyueData.get("table_name").toString())){
-                    continue;
-                }
-                tableData.put(jianyueData.get("jianyue_id").toString(), jianyueData.get("source_id").toString());
-            }
-            dataMap.put(table, tableData);
-        }
-        DataUtil dataUtil = new DataUtil();
+    void doExecute(JsonArray scheduleInfo, Map<String, Map<String, String>> dataMap, DataUtil dataUtil){
         //获取年级
         String tableName = "base_grade";
 //            Map<String, Long> gradeMap = dataMap.get(tableName);
@@ -168,4 +174,28 @@ public class JianyuekbScheduleTask {
         dataUtil.insertClassTime(scheduleInfo);
     }
 
+    Map<String, Map<String, String>> initDataMap(){
+        Map<String, Map<String, String>> dataMap = new HashMap<>();
+        String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
+        List<Map<String, Object>> query = SqlRunnerAdapter.db().selectList(sql);
+        Set<String> tables = new HashSet<>();
+        for (Map<String, Object> jianyueData : query) {
+            tables.add(jianyueData.get("table_name").toString());
+        }
+        sql = "SELECT * FROM jianyue_data WHERE source_id IS NOT NULL AND jianyue_id != ''";
+        List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql);
+
+        for (String table : tables) {
+            Map<String, String> tableData = new HashMap<>();
+            for (Map<String, Object> jianyueData : list) {
+                if(!table.equals(jianyueData.get("table_name").toString())){
+                    continue;
+                }
+                tableData.put(jianyueData.get("jianyue_id").toString(), jianyueData.get("source_id").toString());
+            }
+            dataMap.put(table, tableData);
+        }
+        return dataMap;
+    }
+
 }

+ 9 - 0
src/main/java/com/xjrsoft/module/schedule/util/DataUtil.java

@@ -771,6 +771,15 @@ public class DataUtil {
         }
     }
 
+    /**
+     * 保存节次数据
+     * @param infoMap
+     */
+    public void saveClassTime(Map<String, JsonObject> infoMap){
+
+    }
+
+
     /**
      * 插入记录表
      * @param tableName 表名字

+ 198 - 0
src/test/java/com/xjrsoft/module/job/JianyuekbScheduleTaskTest2.java

@@ -0,0 +1,198 @@
+package com.xjrsoft.module.job;
+
+import cn.hutool.extra.spring.SpringUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.google.gson.JsonArray;
+import com.xjrsoft.XjrSoftApplication;
+import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
+import com.xjrsoft.common.utils.RedisUtil;
+import com.xjrsoft.module.base.entity.BaseClass;
+import com.xjrsoft.module.base.service.IBaseClassService;
+import com.xjrsoft.module.schedule.util.DataUtil;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoUnit;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author dzx
+ * @date 2024/10/15
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = XjrSoftApplication.class)
+class JianyuekbScheduleTaskTest2 {
+    @Autowired
+    private IBaseClassService classService;
+
+    private final static String taskKey = "jianyuekbScheduleTask";
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    @Test
+    public void execute() throws Exception {
+        doExecute();
+    }
+    public void doExecute() throws Exception {
+        String active = SpringUtil.getActiveProfile();
+        String sql = "SELECT * FROM course_receive_msg WHERE delete_mark = 0 AND is_callback IS NULL";
+        List<Map<String, Object>> receiveMsgs = SqlRunnerAdapter.db().selectList(sql);
+        if(receiveMsgs.isEmpty()){
+            return;
+        }
+
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        //查询出传入排课系统的年级和班级
+        List<String> eduYearSerialNo1 = receiveMsgs.stream().map(x -> "'" + x.get("edu_year_serial_no").toString() + "'").collect(Collectors.toList());
+
+        //查询出传入排课系统的年级和班级
+        sql = "SELECT * FROM jianyue_data WHERE source_id IS NOT NULL AND jianyue_id != ''" +
+                " and table_name = 'base_grade'" +
+                " and jianyue_id in (" + eduYearSerialNo1.toString().replace("[","").replace("]","") + ")";
+        List<Map<String, Object>> jianyueData = SqlRunnerAdapter.db().selectList(sql);
+        Map<String, List<Long>> gradeClassMaps = new HashMap<>();//存入对应年级的所有班级id
+        for (Map<String, Object> el : jianyueData) {
+            String gradeId = el.get("source_id").toString();
+            String orgId = null;
+            if(gradeId.contains("_")){
+                String[] split = el.get("source_id").toString().split("_");
+                gradeId = split[1];
+                orgId = split[0];
+            }
+            List<BaseClass> classList = classService.list(
+                    new QueryWrapper<BaseClass>().lambda()
+                            .eq(BaseClass::getGradeId, gradeId)
+                            .eq(BaseClass::getOrgId, orgId)
+            );
+            List<Long> classIds = classList.stream().map(BaseClass::getId).collect(Collectors.toList());
+
+            gradeClassMaps.put(el.get("jianyue_id").toString(), classIds);
+        }
+        LocalDate today = LocalDate.now();
+
+        DataUtil dataUtil = new DataUtil();
+        Set<String> ongoing = redisUtil.get(taskKey, new TypeReference<Set<String>>() {});//正在进行中的
+        if(ongoing == null){
+            ongoing = new HashSet<>();
+        }
+
+        Map<String, Map<String, String>> dataMap = initDataMap();
+        for (Map<String, Object> receiveMsg : receiveMsgs) {
+            String eduYearSerialNo = receiveMsg.get("edu_year_serial_no").toString();
+            if(ongoing.contains(eduYearSerialNo)){
+                continue;
+            }
+            ongoing.add(eduYearSerialNo);
+            redisUtil.set(taskKey, ongoing);
+
+            String updSql = "update course_receive_msg set is_callback = 0 where id = " + receiveMsg.get("id").toString();
+            SqlRunnerAdapter.db().update(updSql);
+
+            JsonArray allScheduleInfo = new JsonArray();
+            String startDateStr = receiveMsg.get("start_date").toString();
+            LocalDate startDateObj = LocalDate.parse(startDateStr);
+            String endDateStr = receiveMsg.get("end_date").toString();
+            LocalDate endDateObj = LocalDate.parse(endDateStr);
+            if(today.isAfter(startDateObj)){
+                startDateStr = today.format(formatter);
+            }
+            //删除课表信息;
+            String classIds = gradeClassMaps.get(eduYearSerialNo).toString().replace("[", "").replace("]", "");
+            String delSql = "delete from course_table where schedule_date between '" + startDateStr + "'" +
+                    " and '" + endDateStr +
+                    "' and class_id in (" + classIds + ")";
+            SqlRunnerAdapter.db().delete(delSql);
+
+            long between = ChronoUnit.DAYS.between(startDateObj, endDateObj);
+            int times = Integer.parseInt(((between / 7) + 1) + "");
+
+            for (int index = 0; index < times; index ++) {
+                LocalDate statrTime = startDateObj.plusDays(index * 7L);
+                String startDate = statrTime.format(formatter);
+                LocalDate endTime = statrTime.plusDays(6L);
+                if(endTime.isAfter(endDateObj)){
+                    endTime = endDateObj;
+                }
+
+                String endDate = endTime.format(formatter);
+                //获取课表并存到数据库
+                JsonArray scheduleInfo = dataUtil.getScheduleInfoByGrade(eduYearSerialNo, startDate, endDate);
+                allScheduleInfo.addAll(scheduleInfo);
+            }
+
+            updSql = "update course_receive_msg set is_callback = 1 where id = " + receiveMsg.get("id").toString();
+            SqlRunnerAdapter.db().update(updSql);
+            //作废调课和顶课
+            updSql = "UPDATE wf_course_adjust SET enabled_mark = 0 WHERE adjust_type BETWEEN '" + startDateStr + "'" +
+                    " and '" + endDateStr + "' and class_id in (" + classIds + ")";
+            SqlRunnerAdapter.db().update(updSql);
+
+            ongoing.remove(eduYearSerialNo);
+            redisUtil.set(taskKey, ongoing);
+            doExecute(allScheduleInfo, dataMap, dataUtil);
+        }
+
+    }
+
+    void doExecute(JsonArray scheduleInfo, Map<String, Map<String, String>> dataMap, DataUtil dataUtil){
+        //获取年级
+        String tableName = "base_grade";
+//            Map<String, Long> gradeMap = dataMap.get(tableName);
+        //获取学期
+        tableName = "base_semester";
+        Map<String, String> semesterMap = dataMap.get(tableName);
+        //获取课程
+        tableName = "base_course_subject";
+        Map<String, String> courseMap = dataMap.get(tableName);
+        //获取教职工
+        tableName = "base_teacher";
+        Map<String, String> teacherMap = dataMap.get(tableName);
+        //获取行政班
+        tableName = "base_class";
+        Map<String, String> classMap = dataMap.get(tableName);
+
+        tableName = "base_classroom";
+        Map<String, String> classroomMap = dataMap.get(tableName);
+
+        dataUtil.insertCourseTableEntiy(scheduleInfo, classroomMap, courseMap, semesterMap, teacherMap, classMap);
+        dataUtil.insertClassTime(scheduleInfo);
+    }
+
+    Map<String, Map<String, String>> initDataMap(){
+        Map<String, Map<String, String>> dataMap = new HashMap<>();
+        String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
+        List<Map<String, Object>> query = SqlRunnerAdapter.db().selectList(sql);
+        Set<String> tables = new HashSet<>();
+        for (Map<String, Object> jianyueData : query) {
+            tables.add(jianyueData.get("table_name").toString());
+        }
+        sql = "SELECT * FROM jianyue_data WHERE source_id IS NOT NULL AND jianyue_id != ''";
+        List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql);
+
+        for (String table : tables) {
+            Map<String, String> tableData = new HashMap<>();
+            for (Map<String, Object> jianyueData : list) {
+                if(!table.equals(jianyueData.get("table_name").toString())){
+                    continue;
+                }
+                tableData.put(jianyueData.get("jianyue_id").toString(), jianyueData.get("source_id").toString());
+            }
+            dataMap.put(table, tableData);
+        }
+        return dataMap;
+    }
+}