宋悦 преди 8 години
родител
ревизия
1cccea816f

+ 5 - 0
core-api/pom.xml

@@ -63,6 +63,11 @@
             <artifactId>springfox-swagger-ui</artifactId>
             <version>2.6.1</version>
         </dependency>
+        <dependency>
+            <groupId>commons-fileupload</groupId>
+            <artifactId>commons-fileupload</artifactId>
+            <version>1.3.2</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 18 - 1
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/CourseApi.java

@@ -37,6 +37,7 @@ import cn.com.qmth.examcloud.service.core.entity.Course;
 import cn.com.qmth.examcloud.service.core.repo.CourseRepo;
 import cn.com.qmth.examcloud.service.core.service.CourseService;
 import cn.com.qmth.examcloud.service.core.util.ExportUtils;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 /**
  * 课程服务API
@@ -134,7 +135,15 @@ public class CourseApi {
     
     @ApiOperation(value="导入课程",notes = "导入")
     @PostMapping("/import")
-    public ResponseEntity importCourse(@RequestParam Long orgId,@RequestParam MultipartFile file){
+    public ResponseEntity importCourse(HttpServletRequest request,
+                                       @RequestParam CommonsMultipartFile file){
+        AccessUser accessUser = (AccessUser) request.getAttribute("accessUser");
+        Long orgId = null;
+        if(accessUser != null){
+            orgId = accessUser.getRootOrgId();
+        }else{
+            return new ResponseEntity(new ErrorMsg("用户token不存在或已失效"),HttpStatus.NOT_FOUND);
+        }
     	try {
     		List<ExcelError> errors = courseService.importCourse(orgId,file.getInputStream());
     		return new ResponseEntity(errors,HttpStatus.OK);
@@ -143,6 +152,14 @@ public class CourseApi {
 			return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
 		}
     }
+
+    @ApiOperation(value="下载导入模板",notes = "下载导入模板")
+    @GetMapping("/download")
+    public void importFileTemplate(HttpServletResponse response){
+        List<CourseDTO> list= new ArrayList<CourseDTO>();
+        list.add(new CourseDTO("大学英语", "000001", "0"));
+        ExportUtils.exportEXCEL("课程导入模板", CourseDTO.class, list, response);
+    }
     
     @ApiOperation(value="导出课程",notes = "导出")
     @GetMapping("/export")

+ 23 - 3
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/OrgApi.java

@@ -1,10 +1,13 @@
 package cn.com.qmth.examcloud.service.core.api;
 
 import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
+import cn.com.qmth.examcloud.service.core.dto.CourseDTO;
+import cn.com.qmth.examcloud.service.core.util.ExportUtils;
 import io.swagger.annotations.ApiOperation;
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -35,6 +38,7 @@ import cn.com.qmth.examcloud.common.util.excel.ExcelError;
 import cn.com.qmth.examcloud.service.core.entity.Org;
 import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
 import cn.com.qmth.examcloud.service.core.service.OrgService;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -153,10 +157,18 @@ public class OrgApi {
     }
 
     @ApiOperation(value = "按父ID导入子机构", notes = "导入子机构")
-    @PostMapping("/{id}/import")
-    public ResponseEntity importLearnCenter(@PathVariable Long id, @RequestParam MultipartFile file) {
+    @PostMapping("/import")
+    public ResponseEntity importLearnCenter(HttpServletRequest request,
+                                            @RequestParam CommonsMultipartFile file) {
+        AccessUser accessUser = (AccessUser) request.getAttribute("accessUser");
+        Long orgId = null;
+        if(accessUser != null){
+            orgId = accessUser.getRootOrgId();
+        }else{
+            return new ResponseEntity(new ErrorMsg("用户token不存在或已失效"),HttpStatus.NOT_FOUND);
+        }
         try {
-            List<ExcelError> excelErrors = orgService.importLearnCenter(id, file.getInputStream());
+            List<ExcelError> excelErrors = orgService.importLearnCenter(orgId, file.getInputStream());
             return new ResponseEntity(excelErrors, HttpStatus.OK);
         } catch (IOException e) {
             e.printStackTrace();
@@ -164,6 +176,14 @@ public class OrgApi {
         }
     }
 
+    @ApiOperation(value="下载导入模板",notes = "下载导入模板")
+    @GetMapping("/download")
+    public void importFileTemplate(HttpServletResponse response){
+        List<Org> list= new ArrayList<Org>();
+        list.add(new Org("学习中心", "000001", "张三","13687653477"));
+        ExportUtils.exportEXCEL("学习中心导入模板", Org.class, list, response);
+    }
+
     @ApiOperation(value = "启用机构", notes = "启用")
     @PutMapping("/enable/{ids}")
     public ResponseEntity enableSchool(@PathVariable String ids) {

+ 11 - 5
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/CourseService.java

@@ -15,7 +15,6 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
 
 import cn.com.qmth.examcloud.common.util.excel.ExcelError;
 import cn.com.qmth.examcloud.common.util.excel.ExcelReader;
@@ -24,6 +23,7 @@ import cn.com.qmth.examcloud.service.core.dto.CourseDTO;
 import cn.com.qmth.examcloud.service.core.entity.Course;
 import cn.com.qmth.examcloud.service.core.enums.CourseLevel;
 import cn.com.qmth.examcloud.service.core.repo.CourseRepo;
+import org.springframework.util.StringUtils;
 
 @Service
 public class CourseService {
@@ -43,13 +43,13 @@ public class CourseService {
             	course.setOrgId(orgId);
             	course.setCreateTime(new Date());
 				course.setEnable(true);
-            	if("0".equalsIgnoreCase(dto.getLevel())){
+            	if("ZSB".equalsIgnoreCase(dto.getLevel())){
             		course.setLevel(CourseLevel.ZSB);
             	}
-            	if("1".equalsIgnoreCase(dto.getLevel())){
+            	if("GQZ".equalsIgnoreCase(dto.getLevel())){
             		course.setLevel(CourseLevel.GQZ);
             	}
-            	if("2".equalsIgnoreCase(dto.getLevel())){
+            	if("ALL".equalsIgnoreCase(dto.getLevel())){
             		course.setLevel(CourseLevel.ALL);
             	}
                 ExcelError error = importCheck(course);
@@ -64,8 +64,14 @@ public class CourseService {
 	}
 
 	private ExcelError importCheck(Course course) {
+		if(StringUtils.isEmpty(course.getCode())){
+			return new ExcelError("代码不能为空");
+		}
+		if(StringUtils.isEmpty(course.getName())){
+			return new ExcelError("名称不能为空");
+		}
 		if(null == course.getLevel()){
-			return new ExcelError("层次不存在");
+			return new ExcelError("层次不能为空");
 		}
 		Course domain = courseRepo.findByOrgIdAndCode(course.getOrgId(),course.getCode());
 		if(domain != null){

+ 7 - 0
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/OrgService.java

@@ -6,6 +6,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
+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;
@@ -56,6 +57,12 @@ public class OrgService {
 	}
 
 	private ExcelError importCheck(Org dto) {
+		if(StringUtils.isEmpty(dto.getCode())){
+			return new ExcelError("代码不能为空");
+		}
+		if(StringUtils.isEmpty(dto.getName())){
+			return new ExcelError("名称不能为空");
+		}
 		Org org = orgRepo.findFirstByParentIdAndCode(dto.getParentId(),dto.getCode());
 		if(org!=null){
 			return new ExcelError("代码已存在");

+ 19 - 6
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/UserService.java

@@ -11,7 +11,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
-import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.*;
 
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -87,11 +87,24 @@ public class UserService {
      * @return
      */
     public Page<User> findAll(User userCriteria, Pageable pageable){
-        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-                .withMatcher("name", contains())
-                .withMatcher("loginName", contains()).withIgnoreNullValues();
-        Example<User> example = Example.of(userCriteria, exampleMatcher);
-        Page<User> users = userRepo.findAll(example, pageable);
+        Specification<User> userSpecification = (root, query, cb) -> {
+            List<Predicate> predicates = new ArrayList<>();
+//            Subquery<UserRole> subquery = query.subquery(UserRole.class);
+//            Root<UserRole> subRootEntity = subquery.from(UserRole.class);
+//            Predicate predicate = cb.notEqual(subRootEntity.get("roleCode"), RoleMeta.MARKER.name());
+//            subquery.select(subRootEntity).where(predicate);
+//            predicates.add(cb.exists(subquery));
+            predicates.add(cb.equal(root.get("type"),userCriteria.getType()));
+            predicates.add(cb.equal(root.get("rootOrgId"),userCriteria.getRootOrgId()));
+            if(StringUtils.isNotEmpty(userCriteria.getLoginName())){
+                predicates.add(cb.like(root.get("loginName"),"%"+userCriteria.getLoginName()+"%"));
+            }
+            if(StringUtils.isNotEmpty(userCriteria.getName())){
+                predicates.add(cb.like(root.get("name"),"%"+userCriteria.getName()+"%"));
+            }
+            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+        };
+        Page<User> users = userRepo.findAll(userSpecification, pageable);
         return users;
     }
 

+ 7 - 1
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/dto/CourseDTO.java

@@ -19,7 +19,7 @@ public class CourseDTO{
 
 	private Long orgId;
 	
-    @ExcelProperty(index = 2,name = "层次")
+    @ExcelProperty(index = 2,name = "层次(ZSB,GQZ,ALL)")
 	private String level;
     
 //    @ExcelProperty(index = 3,name = "状态",type = 1)
@@ -65,6 +65,12 @@ public class CourseDTO{
 		this.enable = enable;
 	}
 
+	public CourseDTO(String name,String code,String level) {
+		this.name = name;
+		this.code = code;
+		this.level = level;
+	}
+
 	public CourseDTO() {
 	}
 }

+ 12 - 5
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/entity/Org.java

@@ -35,14 +35,14 @@ public class Org implements Serializable{
 
     private Integer level;
     
-    @ExcelProperty(index = 0)
+    @ExcelProperty(name = "机构名称",index = 0)
     @NotNull
     private String name;
     
     /**
      * 学习中心代码
      */
-    @ExcelProperty(index = 1)
+    @ExcelProperty(name = "机构代码",index = 1)
     private String code;
 
     private String logo;
@@ -60,12 +60,12 @@ public class Org implements Serializable{
     /**
      * 联系电话
      */
-    @ExcelProperty(index = 3)
+    @ExcelProperty(name = "联系电话",index = 3)
     private String telphone;
     /**
      * 联系人
      */
-    @ExcelProperty(index = 2)
+    @ExcelProperty(name = "联系人",index = 2)
     private String contacts;
 
     @Temporal(value = TemporalType.DATE)
@@ -192,6 +192,13 @@ public class Org implements Serializable{
 		this.contacts = contacts;
 	}
 
-	public Org() {
+    public Org(String name, String code, String contacts,String telphone) {
+        this.name = name;
+        this.code = code;
+        this.telphone = telphone;
+        this.contacts = contacts;
+    }
+
+    public Org() {
     }
 }

+ 16 - 0
core-main/src/main/java/cn/com/qmth/examcloud/service/core/Application.java

@@ -1,10 +1,14 @@
 package cn.com.qmth.examcloud.service.core;
 
 import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 import org.springframework.cloud.netflix.feign.EnableFeignClients;
 import org.springframework.context.annotation.Bean;
+import org.springframework.web.multipart.MultipartResolver;
+import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@@ -12,9 +16,21 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
 @SpringBootApplication
 @EnableEurekaClient
 @EnableFeignClients
+@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
 public class Application {
 
     public static void main(String[] args) throws Exception {
         SpringApplication.run(Application.class, args);
     }
+
+    // 显示声明CommonsMultipartResolver为mutipartResolver
+    @Bean(name = "multipartResolver")
+    public MultipartResolver multipartResolver() {
+        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
+        resolver.setDefaultEncoding("UTF-8");
+        resolver.setResolveLazily(true);// resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
+        resolver.setMaxInMemorySize(40960);
+        resolver.setMaxUploadSize(200 * 1024 * 1024);// 上传文件大小 200M 50*1024*1024
+        return resolver;
+    }
 }