优化代码

This commit is contained in:
none 2023-03-14 15:24:03 +08:00
parent 97061c7678
commit 1867d5fa1a
7 changed files with 25 additions and 18 deletions

View File

@ -66,9 +66,8 @@ public class CourseController {
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE) @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@GetMapping("/create") @GetMapping("/create")
public JsonResponse create() { public JsonResponse create() {
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("categories", categories); data.put("categories", categoryService.groupByParent());
return JsonResponse.data(data); return JsonResponse.data(data);
} }

View File

@ -44,11 +44,8 @@ public class DepartmentController {
@GetMapping("/index") @GetMapping("/index")
public JsonResponse index() { public JsonResponse index() {
Map<Integer, List<Department>> departments = departmentService.all().stream().collect(Collectors.groupingBy(Department::getParentId));
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("departments", departments); data.put("departments", departmentService.groupByParent());
return JsonResponse.data(data); return JsonResponse.data(data);
} }
@ -61,11 +58,8 @@ public class DepartmentController {
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD) @BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@GetMapping("/create") @GetMapping("/create")
public JsonResponse create() { public JsonResponse create() {
Map<Integer, List<Department>> departments = departmentService.all().stream().collect(Collectors.groupingBy(Department::getParentId));
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("departments", departments); data.put("departments", departmentService.groupByParent());
return JsonResponse.data(data); return JsonResponse.data(data);
} }

View File

@ -45,11 +45,8 @@ public class ResourceCategoryController {
@GetMapping("/index") @GetMapping("/index")
public JsonResponse index() { public JsonResponse index() {
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("categories", categories); data.put("categories", categoryService.groupByParent());
return JsonResponse.data(data); return JsonResponse.data(data);
} }
@ -61,11 +58,8 @@ public class ResourceCategoryController {
@GetMapping("/create") @GetMapping("/create")
public JsonResponse create() { public JsonResponse create() {
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("categories", categories); data.put("categories", categoryService.groupByParent());
return JsonResponse.data(data); return JsonResponse.data(data);
} }

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.exception.NotFoundException; import xyz.playedu.api.exception.NotFoundException;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author tengteng * @author tengteng
@ -40,4 +41,6 @@ public interface DepartmentService extends IService<Department> {
void changeParent(Integer id, Integer parentId, List<Integer> ids) throws NotFoundException; void changeParent(Integer id, Integer parentId, List<Integer> ids) throws NotFoundException;
void resetSort(List<Integer> ids); void resetSort(List<Integer> ids);
Map<Integer, List<Department>> groupByParent();
} }

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.exception.NotFoundException; import xyz.playedu.api.exception.NotFoundException;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author tengteng * @author tengteng
@ -37,4 +38,6 @@ public interface ResourceCategoryService extends IService<ResourceCategory> {
void changeParent(Integer id, Integer parentId, List<Integer> ids) throws NotFoundException; void changeParent(Integer id, Integer parentId, List<Integer> ids) throws NotFoundException;
Map<Integer, List<ResourceCategory>> groupByParent();
} }

View File

@ -18,6 +18,8 @@ import xyz.playedu.api.service.internal.UserDepartmentService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author tengteng * @author tengteng
@ -225,6 +227,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
} }
updateBatchById(departments); updateBatchById(departments);
} }
@Override
public Map<Integer, List<Department>> groupByParent() {
return list(query().getWrapper().orderByAsc("sort")).stream().collect(Collectors.groupingBy(Department::getParentId));
}
} }

View File

@ -16,6 +16,8 @@ import xyz.playedu.api.service.internal.ResourceCourseCategoryService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author tengteng * @author tengteng
@ -208,6 +210,11 @@ public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMap
// 重置排序 // 重置排序
resetSort(ids); resetSort(ids);
} }
@Override
public Map<Integer, List<ResourceCategory>> groupByParent() {
return list(query().getWrapper().orderByAsc("sort")).stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
}
} }