xiatian 2 роки тому
батько
коміт
835d9ef938

+ 47 - 28
examcloud-core-questions-api-provider/src/main/java/cn/com/qmth/examcloud/core/questions/api/controller/PaperController.java

@@ -1,5 +1,46 @@
 package cn.com.qmth.examcloud.core.questions.api.controller;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.validation.constraints.NotNull;
+
+import org.apache.commons.collections4.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.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.DeleteMapping;
+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 org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.google.gson.Gson;
+
 import cn.com.qmth.examcloud.api.commons.enums.AdminOperateType;
 import cn.com.qmth.examcloud.api.commons.enums.DataRuleType;
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
@@ -37,33 +78,9 @@ import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.web.security.DataRule;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.web.support.Naked;
-import com.google.gson.Gson;
 import freemarker.template.TemplateException;
 import io.swagger.annotations.ApiOperation;
 
-import org.apache.commons.collections4.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.Page;
-import org.springframework.data.domain.PageImpl;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-
-import javax.servlet.http.HttpServletResponse;
-import javax.validation.constraints.NotNull;
-import java.io.File;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.math.BigDecimal;
-import java.util.*;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
 /**
  * Created by songyue on 16/12/28.
  * updated by weiwenhai on 2018.9.28
@@ -290,11 +307,13 @@ public class PaperController extends ControllerSupport {
      */
     @ResponseBody
     @ApiOperation(value = "删除试卷", notes = "删除试卷")
-    @DeleteMapping(value = "/paper/{paperIds}")
-    public void delPaper(@PathVariable String paperIds) {
+    @PostMapping(value = "/paper")
+    public void delPaper(@RequestParam List<String> paperIds) {
         User user = getAccessUser();
-        List<String> paperList = Stream.of(paperIds.split(",")).collect(Collectors.toList());
-        paperService.deletePapers(paperList, user);
+        if(CollectionUtils.isEmpty(paperIds)) {
+        	throw new StatusException("请选择试卷");
+        }
+        paperService.deletePapersPlus(paperIds, user);
     }
 
     /**

+ 2 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/PaperService.java

@@ -318,4 +318,6 @@ public interface PaperService {
 
 	public List<String> findPaperId(Long fromRootOrgId);
 
+	public void deletePapersPlus(List<String> paperList, User user);
+
 }

+ 2 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/QuestionAudioService.java

@@ -63,5 +63,7 @@ public interface QuestionAudioService {
      * 根据试题集合删除音频
      */
     public void deleteAudio(List<Question> questions);
+
+	void deleteAudioByQuestionId(List<String> questionIds);
 }
 

+ 122 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/PaperServiceImpl.java

@@ -30,6 +30,7 @@ import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.bson.types.ObjectId;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.nlpcn.commons.lang.util.StringUtil;
@@ -107,6 +108,7 @@ import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperExp;
 import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperQuestionStructureInfo;
 import cn.com.qmth.examcloud.core.questions.service.bean.dto.SubjectiveQuestionStructure;
 import cn.com.qmth.examcloud.core.questions.service.bean.paper.PaperAnswerDomain;
+import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.PaperDetailUnitDto;
 import cn.com.qmth.examcloud.core.questions.service.cache.BasePaperCache;
 import cn.com.qmth.examcloud.core.questions.service.cache.ExtractConfigPaperCache;
 import cn.com.qmth.examcloud.core.questions.service.cache.QuestionAnswerCache;
@@ -588,6 +590,7 @@ public class PaperServiceImpl implements PaperService {
 	 *
 	 * @param paperIds
 	 */
+	@Override
 	public void deletePapers(List<String> paperIds, User user) {
 		List<Paper> papers = CommonUtils.toList(paperRepo.findByIdIn(paperIds));
 		if (papers.get(0).getPaperType() == PaperType.IMPORT) {
@@ -656,6 +659,125 @@ public class PaperServiceImpl implements PaperService {
 			}
 		}
 	}
+	private List<PaperDetailUnitDto> findUnitByPaperId(String paperId) {
+		Object id ;
+		if (paperId.length() > 24) {
+			id=paperId;
+		} else {
+			id=new ObjectId(paperId);
+		}
+		Query query = new Query();
+		query.addCriteria(Criteria.where("paper.$id").is(id));
+		List<PaperDetailUnitDto> units = this.mongoTemplate.find(query, PaperDetailUnitDto.class, "paperDetailUnit");
+		return units;
+	}
+	private boolean existGenerateQuestion(List<String> questionIds) {
+		List<Object> ids = new ArrayList<>();
+		for (String pid : questionIds) {
+			if (pid.length() > 24) {
+				ids.add(pid);
+			} else {
+				ids.add(new ObjectId(pid));
+			}
+		}
+		Query query = new Query();
+		query.addCriteria(Criteria.where("question.$id").in(ids));
+		query.addCriteria(Criteria.where("paperType").is(PaperType.GENERATE.name()));
+		long count=mongoTemplate.count(query,"paperDetailUnit");
+		return count>0;
+	}
+	private void removeByIds(List<String> stringIds,String collectionName) {
+		List<Object> ids = new ArrayList<>();
+		for (String pid : stringIds) {
+			if (pid.length() > 24) {
+				ids.add(pid);
+			} else {
+				ids.add(new ObjectId(pid));
+			}
+		}
+		Query query = new Query();
+		query.addCriteria(Criteria.where("_id").in(ids));
+		mongoTemplate.remove(query,collectionName);
+	}
+	private void removeDetailByPaperIds(List<String> paperIds) {
+		List<Object> ids = new ArrayList<>();
+		for (String pid : paperIds) {
+			if (pid.length() > 24) {
+				ids.add(pid);
+			} else {
+				ids.add(new ObjectId(pid));
+			}
+		}
+		Query query = new Query();
+		query.addCriteria(Criteria.where("paper.$id").in(ids));
+		mongoTemplate.remove(query,"paperDetail");
+	}
+	@Override
+	public void deletePapersPlus(List<String> paperIds, User user) {
+		List<Paper> papers = paperRepo.findByIdIn(paperIds);
+		List<PaperDetailUnitDto> unitList=new ArrayList<>();
+		if (papers.get(0).getPaperType() == PaperType.IMPORT) {
+			List<String> quesList = new ArrayList<>();
+			for (Paper paper : papers) {
+				List<PaperDetailUnitDto> paperUnits = findUnitByPaperId(paper.getId());
+				if(CollectionUtils.isEmpty(paperUnits)) {
+					continue;
+				}
+				List<String> questionIds=paperUnits.stream().map(e->e.getQuestion().id).collect(Collectors.toList());
+				if(existGenerateQuestion(questionIds)) {
+					throw new StatusException("试卷[" + paper.getName() + "]中有试题被组卷使用,不能删除");
+				}
+				if(randomPaperQuestionService.existQuestion(questionIds)) {
+					throw new StatusException("试卷[" + paper.getName() + "]中有试题被抽题模板使用,不能删除");
+				}
+				unitList.addAll(paperUnits);
+				quesList.addAll(questionIds);
+			}
+			if(CollectionUtils.isNotEmpty(quesList)) {
+				questionAudioService.deleteAudioByQuestionId(quesList);
+				removeByIds(quesList, "question");
+			}
+		} else if (papers.get(0).getPaperType() == PaperType.GENERATE) {
+			for (Paper paper : papers) {
+				if (paper.getInUse() != null && paper.getInUse() == 1) {
+					throw new StatusException("试卷[" + paper.getName() + "]已调用,不能删除");
+				}
+				if(randomPaperService.existPaper(Long.valueOf(paper.getCourse().getId()),paper.getId())) {
+					throw new StatusException("试卷[" + paper.getName() + "]被抽题模板使用,不能删除");
+				}
+				List<PaperDetailUnitDto> paperUnits = findUnitByPaperId(paper.getId());
+				if(CollectionUtils.isEmpty(paperUnits)) {
+					continue;
+				}
+				unitList.addAll(paperUnits);
+			}
+		}
+		List<String> unitids=unitList.stream().map(e->e.getId()).collect(Collectors.toList());
+		if(CollectionUtils.isNotEmpty(unitids)) {
+			removeByIds(unitids, "paperDetailUnit");
+		}
+		removeDetailByPaperIds(paperIds);
+		removeByIds(paperIds, "paper");
+
+		for (String paperId : paperIds) {
+			// 清除缓存
+			this.clearPaperCache(paperId);
+		}
+
+		for (Paper paper : papers) {
+			StringBuilder paperInfo = new StringBuilder();
+			paperInfo.append("课程:" + paper.getCourse().getName() + "(" + paper.getCourse().getCode() + ")");
+			paperInfo.append(" 试卷名称:" + paper.getName());
+			if (PaperType.IMPORT.equals(paper.getPaperType())) {
+				ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(),
+						AdminOperateType.TYPE46.getDesc(), paperInfo.toString()));
+			} else {
+				ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(),
+						AdminOperateType.TYPE47.getDesc(), paperInfo.toString()));
+
+			}
+		}
+	}
 
 	/**
 	 * 批量通过试卷

+ 17 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/QuestionAudioServiceImpl.java

@@ -202,6 +202,23 @@ public class QuestionAudioServiceImpl implements QuestionAudioService {
         while (audioIterator.hasNext()) {
             QuestionAudio audio = audioIterator.next();
 //            UpYun upYun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
+//            upYun.deleteFile(audio.getFileUrl());
+        	//通用存储
+            FileStorageUtil.deleteFile(audio.getFileUrl());
+        }
+        questionAudioRepo.deleteAll(questionAudios);
+    }
+    
+    @Override
+    public void deleteAudioByQuestionId(List<String> questionIds) {
+    	if(CollectionUtils.isEmpty(questionIds)) {
+    		return;
+    	}
+        List<QuestionAudio> questionAudios = questionAudioRepo.findByQuestionIdIn(questionIds);
+        Iterator<QuestionAudio> audioIterator = questionAudios.iterator();
+        while (audioIterator.hasNext()) {
+            QuestionAudio audio = audioIterator.next();
+//            UpYun upYun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
 //            upYun.deleteFile(audio.getFileUrl());
         	//通用存储
             FileStorageUtil.deleteFile(audio.getFileUrl());