Parcourir la source

考生获取数据分类列表(树结构)

deason il y a 1 an
Parent
commit
0ea418e5db

+ 7 - 6
src/main/java/com/qmth/exam/reserve/controller/student/CategoryController.java

@@ -4,17 +4,16 @@ import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.exam.reserve.bean.category.CategoryInfo;
 import com.qmth.exam.reserve.controller.BaseController;
+import com.qmth.exam.reserve.service.CategoryService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import io.swagger.annotations.ApiParam;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.ArrayList;
 import java.util.List;
 
 @RestController
@@ -25,11 +24,13 @@ public class CategoryController extends BaseController {
 
     private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
 
+    @Autowired
+    private CategoryService categoryService;
+
     @ApiOperation(value = "获取数据分类列表(树结构)")
     @PostMapping(value = "/list")
-    public List<CategoryInfo> list(@ApiParam("父级ID") @RequestParam(required = false) Long parentId) {
-        //todo
-        return new ArrayList<>();
+    public List<CategoryInfo> list() {
+        return categoryService.getCategoryTreeForStudent(curLoginStudent());
     }
 
 }

+ 6 - 2
src/main/java/com/qmth/exam/reserve/service/CategoryService.java

@@ -1,13 +1,17 @@
 package com.qmth.exam.reserve.service;
 
-import java.util.List;
-
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.exam.reserve.bean.category.CategoryInfo;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 
+import java.util.List;
+
 public interface CategoryService extends IService<CategoryEntity> {
 
     List<CategoryVO> listTeaching(LoginUser user);
+
+    List<CategoryInfo> getCategoryTreeForStudent(LoginUser loginUser);
+
 }

+ 122 - 5
src/main/java/com/qmth/exam/reserve/service/impl/CategoryServiceImpl.java

@@ -1,25 +1,33 @@
 package com.qmth.exam.reserve.service.impl;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import org.springframework.stereotype.Service;
-
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
+import com.qmth.exam.reserve.bean.category.CategoryInfo;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.dao.CategoryDao;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.enums.CategoryLevel;
 import com.qmth.exam.reserve.enums.Role;
+import com.qmth.exam.reserve.service.ApplyTaskService;
 import com.qmth.exam.reserve.service.CategoryService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @Service
 public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
 
+    @Autowired
+    private ApplyTaskService applyTaskService;
+
     @Override
     public List<CategoryVO> listTeaching(LoginUser user) {
         QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
@@ -47,4 +55,113 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
         return cgList;
     }
 
+    /**
+     * 获取数据分类列表(树结构)
+     */
+    @Override
+    public List<CategoryInfo> getCategoryTreeForStudent(LoginUser student) {
+        if (Role.STUDENT != student.getRole()) {
+            throw new StatusException("请使用学生账号登录!");
+        }
+
+        List<CategoryInfo> list = new ArrayList<>();
+
+        // 获取当前启用的预约任务
+        CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask();
+        if (curApplyTask.getSelfApplyEndTime() > System.currentTimeMillis() - 1000000) {
+            // 自主预约截止时间 之前,只能预约本教学点下的考点
+
+            CategoryEntity curEntity = this.getById(student.getCategoryId());
+            if (curEntity == null) {
+                return list;
+            }
+
+            CategoryInfo curInfo = new CategoryInfo();
+            curInfo.setId(curEntity.getId());
+            curInfo.setName(curEntity.getName());
+            curInfo.setLevel(curEntity.getLevel());
+            curInfo.setParentId(curEntity.getParentId());
+            curInfo.setSubNodes(new ArrayList<>());
+
+            // 根节点
+            CategoryInfo root = this.fillParentCategory(curInfo);
+            list.add(root);
+
+            return list;
+        }
+
+        // 开放式预约时间,可以在不同教学点间预约
+        LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(CategoryEntity::getOrgId, student.getOrgId());
+        wrapper.eq(CategoryEntity::getEnable, Boolean.TRUE);
+        List<CategoryEntity> categoryList = baseMapper.selectList(wrapper);
+
+        Map<Long, List<CategoryEntity>> categoryMaps = categoryList.stream().collect(Collectors.groupingBy(CategoryEntity::getParentId));
+        for (CategoryEntity curEntity : categoryList) {
+            if (curEntity.getLevel() > 1) {
+                continue;
+            }
+
+            // 从根节点开始封装
+            CategoryInfo curInfo = new CategoryInfo();
+            curInfo.setId(curEntity.getId());
+            curInfo.setName(curEntity.getName());
+            curInfo.setLevel(curEntity.getLevel());
+            curInfo.setParentId(curEntity.getParentId());
+            this.fillSubCategory(curInfo, categoryMaps);
+
+            list.add(curInfo);
+        }
+
+        return list;
+    }
+
+    /**
+     * 递归封装子级分类
+     */
+    private void fillSubCategory(CategoryInfo cur, Map<Long, List<CategoryEntity>> categoryMaps) {
+        List<CategoryInfo> subNodes = new ArrayList<>();
+
+        if (categoryMaps.containsKey(cur.getId())) {
+            categoryMaps.get(cur.getId()).forEach(entity -> {
+                CategoryInfo info = new CategoryInfo();
+                info.setId(entity.getId());
+                info.setName(entity.getName());
+                info.setLevel(entity.getLevel());
+                info.setParentId(entity.getParentId());
+                this.fillSubCategory(info, categoryMaps);
+                subNodes.add(info);
+            });
+        }
+
+        cur.setSubNodes(subNodes);
+    }
+
+    /**
+     * 递归封装父级分类
+     */
+    private CategoryInfo fillParentCategory(CategoryInfo cur) {
+        if (cur.getLevel() <= 1) {
+            // 根节点,直接返回
+            return cur;
+        }
+
+        CategoryEntity parent = this.getById(cur.getParentId());
+        if (parent == null) {
+            return cur;
+        }
+
+        CategoryInfo info = new CategoryInfo();
+        info.setId(parent.getId());
+        info.setName(parent.getName());
+        info.setLevel(parent.getLevel());
+        info.setParentId(parent.getParentId());
+
+        List<CategoryInfo> subNodes = new ArrayList<>();
+        subNodes.add(cur);
+        info.setSubNodes(subNodes);
+
+        return fillParentCategory(info);
+    }
+
 }