Ver código fonte

增加学习中心导入、应用获取

ting.yin 8 anos atrás
pai
commit
97b5e9253e

+ 25 - 0
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/AppApi.java

@@ -0,0 +1,25 @@
+package cn.com.qmth.examcloud.service.core.api;
+
+import io.swagger.annotations.ApiOperation;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import cn.com.qmth.examcloud.common.config.AppConfig;
+
+/**
+ * 应用服务API Created by ting.yin on 17/3/9.
+ */
+@RestController
+@RequestMapping("${app.api.root}/app")
+public class AppApi {
+
+	@ApiOperation(value = "查询所有应用")
+	@GetMapping
+	public ResponseEntity getAllApp() {
+		return new ResponseEntity(AppConfig.findAll(), HttpStatus.OK);
+	}
+}

+ 18 - 0
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/OrgApi.java

@@ -1,13 +1,19 @@
 package cn.com.qmth.examcloud.service.core.api;
 
+import java.io.IOException;
+import java.util.List;
+
+import cn.com.qmth.examcloud.common.util.excel.ExcelError;
 import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
 import cn.com.qmth.examcloud.service.core.entity.Org;
 import cn.com.qmth.examcloud.service.core.service.OrgService;
 import io.swagger.annotations.ApiOperation;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 机构服务API
@@ -65,4 +71,16 @@ public class OrgApi {
         orgRepo.delete(id);
         return new ResponseEntity(HttpStatus.OK);
     }
+    
+    @ApiOperation(value="按父ID导入子机构",notes="导入子机构")
+    @PostMapping("/{id}/import")
+    public ResponseEntity importLearnCenter(@PathVariable Long id,@RequestParam MultipartFile file){
+    	try {
+			List<ExcelError> excelErrors = orgService.importLearnCenter(id,file.getInputStream());
+			return new ResponseEntity(excelErrors,HttpStatus.OK);
+		} catch (IOException e) {
+			e.printStackTrace();
+			return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
+		}
+    }
 }

+ 42 - 1
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/OrgService.java

@@ -1,9 +1,19 @@
 package cn.com.qmth.examcloud.service.core.service;
 
-import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import cn.com.qmth.examcloud.common.util.excel.ExcelError;
+import cn.com.qmth.examcloud.common.util.excel.ExcelReader;
+import cn.com.qmth.examcloud.common.util.excel.ExcelReaderHandle;
+import cn.com.qmth.examcloud.service.core.entity.Org;
+import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
+
 /**
  * 学校服务类
  * Created by songyue on 17/1/14.
@@ -14,5 +24,36 @@ public class OrgService {
     @Autowired
     OrgRepo orgRepo;
 
+	public List<ExcelError> importLearnCenter(Long orgId, InputStream inputStream) {
+		List<Org> list = new ArrayList<Org>();
+		Org parentOrg = orgRepo.findById(orgId);
+		ExcelReader excelReader = new ExcelReader(Org.class);
+        List<ExcelError> excelErrors = excelReader.reader(inputStream, new ExcelReaderHandle() {
+            @Override
+            public ExcelError handle(Object obj) {
+            	Org dto = (Org) obj;
+                dto.setRootId(orgId);
+                dto.setParentId(orgId);
+                dto.setApps(parentOrg.getApps());
+                dto.setCreateTime(new Date());
+                dto.setEnable(true);
+                ExcelError error = importCheck(dto);
+                if (error == null) {
+                	list.add(dto);
+                }
+                return error;
+            }
+        });
+        orgRepo.save(list);
+		return excelErrors;
+	}
+
+	private ExcelError importCheck(Org dto) {
+		Org org = orgRepo.findByParentIdAndCode(dto.getParentId(),dto.getCode());
+		if(org!=null){
+			return new ExcelError("代码已存在");
+		}
+		return null;
+	}
 
 }

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

@@ -2,8 +2,11 @@ package cn.com.qmth.examcloud.service.core.entity;
 
 import org.springframework.format.annotation.DateTimeFormat;
 
+import cn.com.qmth.examcloud.common.util.excel.ExcelProperty;
+
 import javax.persistence.*;
 import javax.validation.constraints.NotNull;
+
 import java.io.Serializable;
 import java.util.Date;
 
@@ -31,18 +34,24 @@ public class Org implements Serializable{
     private Long parentId;
 
     private Integer level;
-
+    
+    @ExcelProperty(index = 0)
     @NotNull
     private String name;
+    
     /**
      * 学习中心代码
      */
+    @ExcelProperty(index = 1)
     private String code;
 
     private String logo;
 
     private String address;
 
+    /**
+     * 可使用的应用模块
+     */
     @NotNull
     private String apps;
 
@@ -125,15 +134,15 @@ public class Org implements Serializable{
         this.apps = apps;
     }
 
-    public Boolean getEnable() {
-        return enable;
-    }
+    public boolean isEnable() {
+		return enable;
+	}
 
-    public void setEnable(Boolean enable) {
-        this.enable = enable;
-    }
+	public void setEnable(boolean enable) {
+		this.enable = enable;
+	}
 
-    public Date getCreateTime() {
+	public Date getCreateTime() {
         return createTime;
     }
 

+ 3 - 0
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/repo/OrgRepo.java

@@ -1,6 +1,7 @@
 package cn.com.qmth.examcloud.service.core.repo;
 
 import cn.com.qmth.examcloud.service.core.entity.Org;
+
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.repository.query.QueryByExampleExecutor;
 
@@ -18,4 +19,6 @@ public interface OrgRepo extends JpaRepository<Org,Long>,QueryByExampleExecutor<
     List<Org> findByParentId(long parentId);
 
     List<Org> findByLevel(int level);
+
+	Org findByParentIdAndCode(Long parentId, String code);
 }

+ 2 - 2
core-main/src/main/resources/application.properties

@@ -1,6 +1,6 @@
-spring.datasource.url=jdbc:mysql://localhost:3306/exam_cloud_test?useUnicode=true&characterEncoding=UTF-8
+spring.datasource.url=jdbc:mysql://localhost:3306/exam_cloud?useUnicode=true&characterEncoding=UTF-8
 spring.datasource.username=root
-spring.datasource.password=root
+spring.datasource.password=123456
 spring.datasource.validation-query=SELECT 1 FROM DUAL
 spring.datasource.test-on-borrow=true
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver