فهرست منبع

dataRule courseProperty

deason 4 سال پیش
والد
کامیت
09129711b4

+ 19 - 26
examcloud-core-questions-api-provider/src/main/java/cn/com/qmth/examcloud/core/questions/api/controller/CoursePropertyController.java

@@ -1,33 +1,27 @@
 package cn.com.qmth.examcloud.core.questions.api.controller;
 
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.domain.Sort;
-import org.springframework.data.domain.Sort.Direction;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.ModelAttribute;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-
+import cn.com.qmth.examcloud.api.commons.enums.DataRuleType;
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.questions.base.Constants;
 import cn.com.qmth.examcloud.core.questions.dao.entity.CourseProperty;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyDto;
 import cn.com.qmth.examcloud.core.questions.service.CoursePropertyService;
+import cn.com.qmth.examcloud.web.security.DataRule;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * @author weiwenhai
@@ -53,19 +47,18 @@ public class CoursePropertyController extends ControllerSupport {
         return new ResponseEntity<>(courseProperties, HttpStatus.OK);
     }
 
+    @DataRule(type = DataRuleType.COURSE)
     @ApiOperation(value = "根据orgId查询所有课程属性(分页)")
     @GetMapping(value = "/courseProperty/all/{curPage}/{pageSize}")
     public ResponseEntity<Object> findAllByOrgId(@ModelAttribute CoursePropertyDto coursePropertyDto,
-            @PathVariable Integer curPage, @PathVariable Integer pageSize) {
+                                                 @PathVariable Integer curPage, @PathVariable Integer pageSize) {
         User user = getAccessUser();
-        if (user == null) {
-            throw new StatusException(Constants.SYS_CODE_500, "请先登录!");
-        }
+        UserDataRule userDataRule = super.getUserDataRule(DataRuleType.COURSE);
 
         coursePropertyDto.setOrgId(user.getRootOrgId());
 
-        PageRequest pageRequest = PageRequest.of(curPage - 1, pageSize, Sort.by(Direction.DESC, "updateTime"));
-        Page<CourseProperty> coursePropertiesPage = coursePropertyService.findList(coursePropertyDto, pageRequest);
+        PageRequest pageable = PageRequest.of(curPage - 1, pageSize);
+        Page<CourseProperty> coursePropertiesPage = coursePropertyService.findList(coursePropertyDto, pageable, userDataRule);
 
         return new ResponseEntity<>(coursePropertiesPage, HttpStatus.OK);
     }

+ 2 - 1
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/CoursePropertyService.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.core.questions.service;
 
+import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
 import cn.com.qmth.examcloud.core.questions.dao.entity.CourseProperty;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyDto;
 import org.springframework.data.domain.Page;
@@ -22,7 +23,7 @@ public interface CoursePropertyService {
     /**
      * 查询所有课程属性带分页
      */
-    Page<CourseProperty> findList(CoursePropertyDto dto, Pageable pageable);
+    Page<CourseProperty> findList(CoursePropertyDto dto, Pageable pageable, UserDataRule userDataRule);
 
     /**
      * 保存课程属性

+ 40 - 23
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/CoursePropertyServiceImpl.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.core.questions.service.impl;
 
+import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.questions.base.Constants;
 import cn.com.qmth.examcloud.core.questions.base.Model;
@@ -12,10 +13,13 @@ import cn.com.qmth.examcloud.support.cache.bean.CourseCacheBean;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Example;
-import org.springframework.data.domain.ExampleMatcher;
 import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.stereotype.Service;
 
 import java.util.Date;
@@ -32,6 +36,9 @@ public class CoursePropertyServiceImpl implements CoursePropertyService {
     @Autowired
     private CoursePropertyRepo coursePropertyRepo;
 
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
     @Override
     public List<CourseProperty> findAllByOrgId(Long orgId) {
         return coursePropertyRepo.findByOrgId(orgId);
@@ -73,47 +80,57 @@ public class CoursePropertyServiceImpl implements CoursePropertyService {
     }
 
     @Override
-    public Page<CourseProperty> findList(CoursePropertyDto dto, Pageable pageable) {
-        CourseProperty params = new CourseProperty();
+    public Page<CourseProperty> findList(CoursePropertyDto dto, Pageable pageable, UserDataRule userDataRule) {
+        if (userDataRule.assertEmptyQueryResult()) {
+            return Page.empty();
+        }
+
+        Query query = new Query();
 
         if (dto.getOrgId() != null) {
-            params.setOrgId(dto.getOrgId());
+            query.addCriteria(Criteria.where("orgId").is(dto.getOrgId()));
         }
 
         if (dto.getCourseId() != null) {
-            params.setCourseId(dto.getCourseId());
+            if (!userDataRule.getRefIds().contains(dto.getCourseId())) {
+                return Page.empty();
+            }
+            query.addCriteria(Criteria.where("courseId").is(dto.getCourseId()));
+        } else {
+            if (userDataRule.assertNeedQueryRefIds()) {
+                query.addCriteria(Criteria.where("courseId").in(userDataRule.getRefIds()));
+            }
         }
 
-        if (dto.getEnable() != null) {
-            params.setEnable(dto.getEnable());
+        if (StringUtils.isNotBlank(dto.getName())) {
+            query.addCriteria(Criteria.where("name").regex(dto.getName().trim()));
         }
 
-        Example<CourseProperty> query;
-        if (!StringUtils.isEmpty(dto.getName())) {
-            params.setName(dto.getName());
-
-            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", match -> match.contains());
-
-            query = Example.of(params, matcher);
-        } else {
-            query = Example.of(params);
+        if (dto.getEnable() != null) {
+            query.addCriteria(Criteria.where("enable").is(dto.getEnable()));
         }
 
-        Page<CourseProperty> page = coursePropertyRepo.findAll(query, pageable);
+        long total = mongoTemplate.count(query, CourseProperty.class);
+        if (total == 0) {
+            return Page.empty();
+        }
 
-        List<CourseProperty> list = page.getContent();
-        if (CollectionUtils.isEmpty(list)) {
-            return page;
+        query.with(Sort.by(new Sort.Order(Sort.Direction.DESC, "updateTime")));
+        query.skip(pageable.getOffset());
+        query.limit(pageable.getPageSize());
+        List<CourseProperty> courseProperties = mongoTemplate.find(query, CourseProperty.class);
+        if (CollectionUtils.isEmpty(courseProperties)) {
+            return Page.empty();
         }
 
-        for (CourseProperty courseProperty : list) {
+        for (CourseProperty courseProperty : courseProperties) {
             CourseCacheBean course = CacheHelper.getCourse(courseProperty.getCourseId());
             if (course != null) {
                 courseProperty.setCourseName(course.getName());
             }
         }
 
-        return page;
+        return new PageImpl<>(courseProperties, pageable, total);
     }
 
     @Override