|
@@ -1,8 +1,14 @@
|
|
package com.gis.cms.service.impl;
|
|
package com.gis.cms.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.gis.cms.entity.dto.AuditDto;
|
|
import com.gis.cms.entity.dto.AuditDto;
|
|
import com.gis.cms.entity.dto.QuestionGroupDto;
|
|
import com.gis.cms.entity.dto.QuestionGroupDto;
|
|
import com.gis.cms.entity.dto.StatusPageDataDto;
|
|
import com.gis.cms.entity.dto.StatusPageDataDto;
|
|
|
|
+import com.gis.cms.entity.dto.TopicDto;
|
|
import com.gis.cms.entity.po.AuditLogEntity;
|
|
import com.gis.cms.entity.po.AuditLogEntity;
|
|
import com.gis.cms.entity.po.GoodsEntity;
|
|
import com.gis.cms.entity.po.GoodsEntity;
|
|
import com.gis.cms.entity.po.QuestionAnswerEntity;
|
|
import com.gis.cms.entity.po.QuestionAnswerEntity;
|
|
@@ -16,6 +22,7 @@ import com.gis.common.base.mapper.IBaseMapper;
|
|
import com.gis.common.base.service.impl.IBaseServiceImpl;
|
|
import com.gis.common.base.service.impl.IBaseServiceImpl;
|
|
import com.gis.common.constant.MsgCode;
|
|
import com.gis.common.constant.MsgCode;
|
|
import com.gis.common.constant.TypeCode;
|
|
import com.gis.common.constant.TypeCode;
|
|
|
|
+import com.gis.common.util.ExcelUtils;
|
|
import com.gis.common.util.Result;
|
|
import com.gis.common.util.Result;
|
|
import com.gis.cms.service.FileService;
|
|
import com.gis.cms.service.FileService;
|
|
import com.github.pagehelper.PageInfo;
|
|
import com.github.pagehelper.PageInfo;
|
|
@@ -23,6 +30,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
@@ -31,6 +39,7 @@ import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@@ -159,6 +168,135 @@ public class QuestionGroupServiceImpl extends IBaseServiceImpl<QuestionGroupEnti
|
|
return Result.success(entity);
|
|
return Result.success(entity);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Result uploadExcel(MultipartFile file) {
|
|
|
|
+ // 保存文件
|
|
|
|
+ Map<String, Object> uploadMap = fileUtils.uploadMap(file, "/excel", false);
|
|
|
|
+ parseExcel(uploadMap.get("filePath").toString());
|
|
|
|
+
|
|
|
|
+ return Result.success();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 解决excel数据并保存数据库
|
|
|
|
+ * @param filePath 相对路径
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private void parseExcel(String filePath){
|
|
|
|
+ // 读取excel数据
|
|
|
|
+ String excelPath = configConstant.serverBasePath + filePath;
|
|
|
|
+ List<List<Object>> read = ExcelUtils.readExcel(excelPath);
|
|
|
|
+
|
|
|
|
+ // 解析excel数据
|
|
|
|
+
|
|
|
|
+ // 题组标题
|
|
|
|
+ List<Object> group = read.get(1);
|
|
|
|
+ String groupTitle = group.get(1).toString();
|
|
|
|
+ if (StrUtil.isBlank(groupTitle)){
|
|
|
|
+ throw new BaseRuntimeException("题组名称不能为空");
|
|
|
|
+ }
|
|
|
|
+ log.info("题组名称:{}", groupTitle );
|
|
|
|
+
|
|
|
|
+ // 重第三行读取,直接读取题目跟选项
|
|
|
|
+ HashMap<String, TopicDto> topicMap = new HashMap<>();
|
|
|
|
+ String topic = null;
|
|
|
|
+ for (int i = 2; i < read.size(); i++) {
|
|
|
|
+ List<Object> list = read.get(i);
|
|
|
|
+ String tips = (String)list.get(0);
|
|
|
|
+ if (StrUtil.isBlank(tips)){
|
|
|
|
+ log.warn("提示行为空,默认为结束行");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ // 题目
|
|
|
|
+ if (tips.contains("题目")){
|
|
|
|
+ topic = (String)list.get(1);
|
|
|
|
+ // 题目为空,结束循环
|
|
|
|
+ if (StrUtil.isBlank(topic)){
|
|
|
|
+ log.warn(tips + ": 为空,结束循环");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 保存题目
|
|
|
|
+ TopicDto topicDto = new TopicDto();
|
|
|
|
+ topicDto.setTopic(topic);
|
|
|
|
+ topicMap.put(topic, topicDto);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 选项
|
|
|
|
+ if (tips.contains("选项")){
|
|
|
|
+ String option = (String)list.get(1);
|
|
|
|
+ if (StrUtil.isBlank(option)){
|
|
|
|
+ // 选项为空, 执行下一次循环
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ TopicDto topicDto = topicMap.get(topic);
|
|
|
|
+ String random = RandomUtil.randomString(8);
|
|
|
|
+
|
|
|
|
+ // 正确答案
|
|
|
|
+ String correct = (String)list.get(2);
|
|
|
|
+ if (StrUtil.isNotBlank(correct)){
|
|
|
|
+ topicDto.setCorrect(random);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Map<String, String> options = topicDto.getOptions();
|
|
|
|
+ if (CollectionUtil.isEmpty(options)){
|
|
|
|
+ options = new HashMap<>();
|
|
|
|
+ }
|
|
|
|
+ options.put(random, option);
|
|
|
|
+
|
|
|
|
+ topicDto.setOptions(options);
|
|
|
|
+ // 设置题目到题组
|
|
|
|
+ topicMap.put(topic, topicDto);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("excel数据封装完成");
|
|
|
|
+ // 题组保存数据库
|
|
|
|
+ QuestionGroupEntity groupEntity = new QuestionGroupEntity();
|
|
|
|
+ groupEntity.setName(groupTitle);
|
|
|
|
+ groupEntity.setUserName(getTokenUserName());
|
|
|
|
+ this.save(groupEntity);
|
|
|
|
+ Long groupEntityId = groupEntity.getId();
|
|
|
|
+ log.info("保存题组完成, 题组id: {}", groupEntityId);
|
|
|
|
+
|
|
|
|
+ // 题目保存数据库
|
|
|
|
+ for(Map.Entry<String, TopicDto> entry : topicMap.entrySet()){
|
|
|
|
+ TopicDto value = entry.getValue();
|
|
|
|
+ QuestionAnswerEntity answerEntity = new QuestionAnswerEntity();
|
|
|
|
+ answerEntity.setQuestionGroupId(groupEntityId);
|
|
|
|
+ answerEntity.setQuestion(value.getTopic());
|
|
|
|
+ answerEntity.setCorrect(value.getCorrect());
|
|
|
|
+ answerEntity.setAnswer(parseAnswer(value.getOptions()));
|
|
|
|
+
|
|
|
|
+ questionAnswerService.save(answerEntity);
|
|
|
|
+ }
|
|
|
|
+ log.info("保存题目完成");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 解析选项
|
|
|
|
+ * @param options
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String parseAnswer(Map<String, String> options){
|
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
|
+ for(Map.Entry<String, String> entry : options.entrySet()){
|
|
|
|
+ String key = entry.getKey();
|
|
|
|
+ String value = entry.getValue();
|
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
|
+ obj.put("val", key);
|
|
|
|
+ obj.put("name", value);
|
|
|
|
+ array.add(obj);
|
|
|
|
+ }
|
|
|
|
+ return JSONObject.toJSONString(array);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
private void updateStatus(Long id, Integer status){
|
|
private void updateStatus(Long id, Integer status){
|
|
QuestionGroupEntity entity = this.findById(id);
|
|
QuestionGroupEntity entity = this.findById(id);
|