From 2f7dff27637fdfc5a038cfa35afcb8bcadc5fa73 Mon Sep 17 00:00:00 2001
From: Mlxa0324 <mlx950324@163.com>
Date: Fri, 17 Mar 2023 22:53:45 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=BE=E7=A8=8B=E5=85=A8?=
 =?UTF-8?q?=E8=B7=AF=E5=BE=84ID=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=90=9C?=
 =?UTF-8?q?=E7=B4=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../ibeetl/jlw/service/CourseInfoService.java | 74 ++++++++++++++++++-
 .../ibeetl/jlw/web/CourseInfoController.java  | 25 +++++++
 .../jlw/web/ResourcesInfoController.java      | 27 ++++---
 .../jlw/web/ResourcesQuestionController.java  |  7 +-
 .../main/resources/sql/jlw/resourcesInfo.md   |  6 +-
 .../resources/sql/jlw/resourcesQuestion.md    |  6 +-
 .../templates/common/courseInfoSelect.html    |  4 +-
 7 files changed, 121 insertions(+), 28 deletions(-)

diff --git a/web/src/main/java/com/ibeetl/jlw/service/CourseInfoService.java b/web/src/main/java/com/ibeetl/jlw/service/CourseInfoService.java
index e5831626..8262ec79 100644
--- a/web/src/main/java/com/ibeetl/jlw/service/CourseInfoService.java
+++ b/web/src/main/java/com/ibeetl/jlw/service/CourseInfoService.java
@@ -155,6 +155,38 @@ public class CourseInfoService extends CoreBaseService<CourseInfo>{
     }
 
 
+    /**
+     * 获取全路径的ID
+     * 根据传入的列表查询全路径
+     *
+     * @param courseInfo
+     * @param allCourseInfoList
+     * @return
+     */
+    public String getCourseInfoFullId(CourseInfo courseInfo, final List<CourseInfo> allCourseInfoList) {
+        if (courseInfo == null) {
+            return "";
+        }
+        Long courseInfoId = courseInfo.getCourseInfoId();
+        Long courseInfoParentId = courseInfo.getCourseInfoParentId();
+
+        if (courseInfoParentId != null && !courseInfoParentId.equals(courseInfoId)) {
+            CourseInfo parent = allCourseInfoList.stream()
+                    .filter(item -> item.getCourseInfoId().equals(courseInfoParentId)).findFirst().orElse(null);
+
+            String leftId = getCourseInfoFullId(parent, allCourseInfoList);
+
+            String leftText = "";
+            if (StrUtil.isNotBlank(leftId)) {
+                leftText = leftId + "_";
+            }
+            return leftText + courseInfoId;
+        }
+
+        return courseInfoId.toString();
+    }
+
+
     public CourseInfo add(CourseInfoQuery courseInfoQuery){
 
         CourseInfo courseInfo = courseInfoQuery.pojo();
@@ -191,7 +223,7 @@ public class CourseInfoService extends CoreBaseService<CourseInfo>{
         String courseInfoFullId = getCourseInfoFullId(courseInfo);
         CourseInfo model = new CourseInfo();
         model.setCourseInfoId(courseInfo.getCourseInfoId());
-        model.setCourseInfoFullId(courseInfoFullId);
+        model.setCourseInfoFullId(StrUtil.appendIfMissing(courseInfoFullId, "_"));
         updateTemplate(model);
 
         return courseInfo;
@@ -1351,4 +1383,44 @@ public class CourseInfoService extends CoreBaseService<CourseInfo>{
             flushCache(id);
         }
     }
+
+    /**
+     * 批量自动补全现有数据的courseInfoFullId字段
+     * 用于老数据的修复
+     */
+    public int batchAutoUpdateCourseInfoFullId() {
+
+        // 如果这个字段不为空的数据有问题,那么去查找其他方法,这里只修改为空的数据
+        List<CourseInfo> allData = courseInfoDao.all();
+
+        // 这些数据需要修复
+        List<CourseInfo> allFullIdIsNullList = allData.stream()
+                .filter(item -> StrUtil.isBlank(item.getCourseInfoFullId())).collect(Collectors.toList());
+
+        List<CourseInfo> updatePOList = new ArrayList<>();
+
+        // 遍历需要修复的数据
+        for (CourseInfo courseInfo : allFullIdIsNullList) {
+            // 获取到的新的全路径ID
+            String courseInfoFullId = getCourseInfoFullId(courseInfo, allData);
+
+            // 用于批量更新
+            CourseInfo updatePO = new CourseInfo();
+            updatePO.setCourseInfoId(courseInfo.getCourseInfoId());
+            updatePO.setCourseInfoFullId(StrUtil.appendIfMissing(courseInfoFullId, "_"));
+
+            updatePOList.add(updatePO);
+        }
+
+        // 批量更新全路径字段
+        if (!updatePOList.isEmpty()) {
+            updateBatchTemplate(updatePOList);
+            log.info("修复课程全路径成功,成功修复数量:{}!", allFullIdIsNullList.size());
+        }
+        else {
+            log.info("暂无待修复的数据!");
+        }
+
+        return allFullIdIsNullList.size();
+    }
 }
\ No newline at end of file
diff --git a/web/src/main/java/com/ibeetl/jlw/web/CourseInfoController.java b/web/src/main/java/com/ibeetl/jlw/web/CourseInfoController.java
index b67535a3..967d478f 100644
--- a/web/src/main/java/com/ibeetl/jlw/web/CourseInfoController.java
+++ b/web/src/main/java/com/ibeetl/jlw/web/CourseInfoController.java
@@ -1,5 +1,6 @@
 package com.ibeetl.jlw.web;
 
+import cn.hutool.core.util.StrUtil;
 import cn.jlw.Interceptor.RFile;
 import cn.jlw.Interceptor.SCoreUser;
 import com.ibeetl.admin.core.annotation.Function;
@@ -21,6 +22,7 @@ import org.apache.commons.logging.LogFactory;
 import org.beetl.sql.core.engine.PageQuery;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.util.Assert;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -359,4 +361,27 @@ public class CourseInfoController{
         courseInfoService.drag(id, leftId, rightId);
         return JsonResult.success();
     }
+
+    /**
+     * 功能描述: <br>
+     * 开个口子,修复课程全路径ID的数据
+     *
+     * @param coreUser
+     * @return {@link JsonResult}
+     * @Author: lx
+     * @Date: 2023/3/17 22:19
+     */
+    @GetMapping(MODEL + "/batchAutoUpdateCourseInfoFullId.json")
+    @Function("batchAutoUpdateCourseInfoFullId")
+    @ResponseBody
+    public JsonResult batchAutoUpdateCourseInfoFullId(@SCoreUser CoreUser coreUser) {
+        Assert.isTrue(coreUser.isAdmin(), "该接口只允许超管访问!");
+        int updateCount = courseInfoService.batchAutoUpdateCourseInfoFullId();
+        if (updateCount == 0) {
+            return JsonResult.success("暂时没有需要修复的课程!");
+        }
+
+        return JsonResult.success(StrUtil.format("修复课程全路径成功,成功修复数量:{}!", updateCount));
+
+    }
 }
diff --git a/web/src/main/java/com/ibeetl/jlw/web/ResourcesInfoController.java b/web/src/main/java/com/ibeetl/jlw/web/ResourcesInfoController.java
index 7a76d7ab..3cbd5743 100644
--- a/web/src/main/java/com/ibeetl/jlw/web/ResourcesInfoController.java
+++ b/web/src/main/java/com/ibeetl/jlw/web/ResourcesInfoController.java
@@ -13,6 +13,7 @@ import com.ibeetl.admin.core.file.FileService;
 import com.ibeetl.admin.core.web.JsonResult;
 import com.ibeetl.jlw.entity.CourseInfo;
 import com.ibeetl.jlw.entity.ResourcesInfo;
+import com.ibeetl.jlw.entity.vo.ResourcesCourseInfoAuthDetailsVO;
 import com.ibeetl.jlw.enums.MoveEnum;
 import com.ibeetl.jlw.service.CourseInfoService;
 import com.ibeetl.jlw.service.ResourcesInfoService;
@@ -33,6 +34,7 @@ import org.springframework.web.servlet.ModelAndView;
 
 import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.NotNull;
+import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.ArrayList;
@@ -40,6 +42,8 @@ import java.util.Arrays;
 import java.util.List;
 
 import static cn.hutool.core.util.ArrayUtil.join;
+import static cn.hutool.core.util.ObjectUtil.defaultIfNull;
+import static com.ibeetl.admin.core.util.StreamUtils.listJoin;
 import static com.ibeetl.jlw.enums.AddTypeEnum.ADMIN_ADD;
 import static com.ibeetl.jlw.enums.AddTypeEnum.FACULTY_ADD;
 
@@ -145,18 +149,17 @@ public class ResourcesInfoController{
     public JsonResult<PageQuery> list(ResourcesInfoQuery condition, @SCoreUser CoreUser coreUser){
         PageQuery page = condition.getPageQuery();
 
-        if (!coreUser.isAdmin()) {
-            String courseInfoIds =
-                    universitiesCollegesJurisdictionCurriculumResourcesService.getResourcesCourseInfoAuthDetails(coreUser.getOrgId()).getAllAuthCourseInfoIds();
-
-            if (StrUtil.isNotBlank(courseInfoIds)) {
-                condition.setOrgIdPlural(join(Arrays.asList(coreUser.getOrgId(), 1L, null).toArray(), ","));
-                condition.setCourseInfoIds(courseInfoIds);
-            }
-            // 如果为空,代表没有授权课程
-            else {
-                return JsonResult.success(page);
-            }
+        if (coreUser.isUniAdmin()) {
+            condition.setOrgIdPlural(join(Arrays.asList(coreUser.getOrgId(), 1L).toArray(), ","));
+            // 查询已经授权的课程ID。
+            // 理论课程:授权表universities_colleges_jurisdiction_curriculum_resources,查询理论课程授权给院校的数据
+            Serializable courseInfoIds = defaultIfNull(condition.getCourseInfoId(), condition.getCourseInfoIds());
+            ResourcesCourseInfoAuthDetailsVO resourcesCourseInfoAuthDetails =
+                    universitiesCollegesJurisdictionCurriculumResourcesService.getResourcesCourseInfoAuthDetails(coreUser.getOrgId());
+            Serializable ifNullCourseInfoIds = defaultIfNull(courseInfoIds, listJoin(resourcesCourseInfoAuthDetails.getTheoryCourseList(), CourseInfo::getCourseInfoId));
+
+            // 如果为空字符串,那么代表这个学校,还没有授权的课程,所以是不会显示任何东西的。
+            condition.setCourseInfoIds(StrUtil.blankToDefault(String.valueOf(ifNullCourseInfoIds), "未授权应用"));
         }
 
         resourcesInfoService.queryByCondition(page);
diff --git a/web/src/main/java/com/ibeetl/jlw/web/ResourcesQuestionController.java b/web/src/main/java/com/ibeetl/jlw/web/ResourcesQuestionController.java
index 6c3260ac..443e7f1b 100644
--- a/web/src/main/java/com/ibeetl/jlw/web/ResourcesQuestionController.java
+++ b/web/src/main/java/com/ibeetl/jlw/web/ResourcesQuestionController.java
@@ -56,6 +56,7 @@ import java.util.*;
 import static cn.hutool.core.util.ArrayUtil.join;
 import static cn.hutool.core.util.ObjectUtil.defaultIfNull;
 import static cn.jlw.util.excel.ExcelUtil.standardName;
+import static com.ibeetl.admin.core.util.StreamUtils.listJoin;
 import static com.ibeetl.admin.core.util.excelGroupValidation.ExcelUtil.replaceNameName;
 
 /**
@@ -161,15 +162,13 @@ public class ResourcesQuestionController{
         }
 
         if (coreUser.isUniAdmin()) {
-            condition.setOrgIdPlural(join(Arrays.asList(coreUser.getOrgId(), 1L, null).toArray(), ","));
+            condition.setOrgIdPlural(join(Arrays.asList(coreUser.getOrgId(), 1L).toArray(), ","));
             // 查询已经授权的课程ID。
-            // 应用课程:查询resources_application_course表,应用和课程的绑定关系
             // 理论课程:授权表universities_colleges_jurisdiction_curriculum_resources,查询理论课程授权给院校的数据
-            // 考证类课程:查询,不需要二次授权
             Serializable courseInfoIds = defaultIfNull(condition.getCourseInfoId(), condition.getCourseInfoIds());
             ResourcesCourseInfoAuthDetailsVO resourcesCourseInfoAuthDetails =
                     universitiesCollegesJurisdictionCurriculumResourcesService.getResourcesCourseInfoAuthDetails(coreUser.getOrgId());
-            Serializable ifNullCourseInfoIds = defaultIfNull(courseInfoIds, resourcesCourseInfoAuthDetails.getAllAuthCourseInfoIds());
+            Serializable ifNullCourseInfoIds = defaultIfNull(courseInfoIds, listJoin(resourcesCourseInfoAuthDetails.getTheoryCourseList(), CourseInfo::getCourseInfoId));
 
             // 如果为空字符串,那么代表这个学校,还没有授权的课程,所以是不会显示任何东西的。
             condition.setCourseInfoIds(StrUtil.blankToDefault(String.valueOf(ifNullCourseInfoIds), "未授权应用"));
diff --git a/web/src/main/resources/sql/jlw/resourcesInfo.md b/web/src/main/resources/sql/jlw/resourcesInfo.md
index 542a4d7a..88fc4a80 100644
--- a/web/src/main/resources/sql/jlw/resourcesInfo.md
+++ b/web/src/main/resources/sql/jlw/resourcesInfo.md
@@ -37,11 +37,7 @@ queryByCondition
         and t.org_id =#orgId#
       @}
       @if(!isEmpty(orgIdPlural)){
-        and (find_in_set(t.org_id, #orgIdPlural#) 
-        @if(contain("null", orgIdPlural)){
-          or t.org_id is null
-        @}
-        )
+        and (find_in_set(ifnull(t.org_id, 1), #orgIdPlural#))
       @}
       @if(!isEmpty(courseInfoFullId)){
         and a.course_info_full_id like #courseInfoFullId+"%"#
diff --git a/web/src/main/resources/sql/jlw/resourcesQuestion.md b/web/src/main/resources/sql/jlw/resourcesQuestion.md
index ba409452..25e55859 100644
--- a/web/src/main/resources/sql/jlw/resourcesQuestion.md
+++ b/web/src/main/resources/sql/jlw/resourcesQuestion.md
@@ -91,11 +91,7 @@ queryByCondition
         and t.org_id =#orgId#
       @}
       @if(!isEmpty(orgIdPlural)){
-        and (find_in_set(t.org_id, #orgIdPlural#) 
-        @if(contain("null", orgIdPlural)){
-          or t.org_id is null
-        @}
-        )
+        and (find_in_set(ifnull(t.org_id, 1), #orgIdPlural#))
       @}
   @if(isEmpty(seeSelf) && !isEmpty(userId)){
   and t.user_id =#userId#
diff --git a/web/src/main/resources/templates/common/courseInfoSelect.html b/web/src/main/resources/templates/common/courseInfoSelect.html
index 29f399a9..5f8a43f3 100644
--- a/web/src/main/resources/templates/common/courseInfoSelect.html
+++ b/web/src/main/resources/templates/common/courseInfoSelect.html
@@ -8,7 +8,9 @@
    isShowGroupName  显示分组名称(默认:true);
    filterName       过滤的分类名称,多个逗号隔开;只支持【应用课程类】、【理论课程类】、【考证课程类】,showGroupName为false也会过滤数据。
 */
-var newId = !isEmpty(id) ? id : ('select_id_' + name  + '_' + @cn.hutool.core.util.RandomUtil.randomString(10));
+
+// 传入的标签ID,如果为空,则生成规则 select_id_{标签name}_生成长度为10的字符串
+var newId = !isEmpty(id) ? id : ('select_id_' + (name!'0')  + '_' + @cn.hutool.core.util.RandomUtil.randomString(10));
 -->
 <select name="${name!''}" id="${newId ! ''}" lay-verify="" lay-search  lay-filter="select_courseInfoIds">
     <option value="">请选择</option>