Browse Source

考题列表excel 导入

huangpeng 9 months ago
parent
commit
fb09039f20

+ 19 - 0
js-common/src/main/java/com/rongwei/safecommon/utils/SaveConstans.java

@@ -624,4 +624,23 @@ public class SaveConstans {
          */
         public static final String YEAR = "3";
     }
+
+
+    /**
+     * 考题导入中考题类型
+     */
+    public static final Map<String, String> TRAINING_SUBJECT_TYPE_IMPOER = new HashMap<String, String>() {{
+        put("判断", "30");
+        put("多选", "20");
+        put("单选", "10");
+    }};
+
+    public static final Map<Integer, String> TRAINING_SUBJECT_OPTION_OPTIONSCODE = new HashMap<Integer, String>() {{
+        put(3, "A");
+        put(5, "B");
+        put(7, "C");
+        put(9, "D");
+        put(11, "E");
+        put(13, "F");
+    }};
 }

+ 3 - 0
js-training/training-common/src/main/java/com/rongwei/trainingcommon/sys/service/SubjectService.java

@@ -3,6 +3,7 @@ package com.rongwei.trainingcommon.sys.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.training.domain.SubjectDo;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 
@@ -16,5 +17,7 @@ import java.util.List;
  */
 public interface SubjectService extends IService<SubjectDo> {
     R checkInfo(List<String> subjectIds);
+
+    R importFile(MultipartFile multipartFile);
 }
 

+ 104 - 0
js-training/training-common/src/main/java/com/rongwei/trainingcommon/sys/service/impl/SubjectServiceImpl.java

@@ -3,22 +3,36 @@ package com.rongwei.trainingcommon.sys.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.utils.SecurityUtil;
+import com.rongwei.rwcommon.utils.StringUtils;
+import com.rongwei.safecommon.utils.JSCommonUtils;
 import com.rongwei.training.domain.ExamPaperDo;
 import com.rongwei.training.domain.PaperSubjectAssignDo;
 import com.rongwei.training.domain.SubjectDo;
+import com.rongwei.training.domain.SubjectOptionsDo;
 import com.rongwei.trainingcommon.sys.dao.*;
+import com.rongwei.trainingcommon.sys.service.SubjectOptionsService;
 import com.rongwei.trainingcommon.sys.service.SubjectService;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.*;
 import java.util.stream.Collectors;
 
+import static com.rongwei.safecommon.utils.SaveConstans.TRAINING_SUBJECT_OPTION_OPTIONSCODE;
+import static com.rongwei.safecommon.utils.SaveConstans.TRAINING_SUBJECT_TYPE_IMPOER;
+
 
 @Service("subjectService")
 public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectDo> implements SubjectService {
@@ -36,6 +50,9 @@ public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectDo> imple
     @Autowired
     private SubjectService subjectService;
 
+    @Autowired
+    private SubjectOptionsService subjectOptionsService;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public R checkInfo(List<String> subjectIds) {
@@ -84,4 +101,91 @@ public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectDo> imple
         msg.append(")").append("使用,不可删除.");
         return R.errorWithMsg(msg.toString());
     }
+
+    @Override
+    public R importFile(MultipartFile multipartFile) {
+        try {
+            List<SubjectDo> subjectDoList =new ArrayList<>();
+            SubjectDo subjectDo;
+            List<SubjectOptionsDo> subjectOptionsDoList =new ArrayList<>();
+            SubjectOptionsDo subjectOptionsDo;
+            Date date = new Date();
+            Sheet sheet = new XSSFWorkbook(multipartFile.getInputStream()).getSheetAt(0);
+            Row row ;
+            int lastRowNum = sheet.getLastRowNum();
+            for (int i=1;i<=lastRowNum;i++){
+                //从第二行还是读取数据
+                 row = sheet.getRow(i);
+
+                 //主表
+                subjectDo=new SubjectDo();
+                subjectDo.setId(SecurityUtil.getUUID());
+                subjectDo.setSubjecttype(TRAINING_SUBJECT_TYPE_IMPOER.getOrDefault(row.getCell(0).getStringCellValue(),"")); //考题类型
+                subjectDo.setSubjectname( row.getCell(1).getStringCellValue());  //考题名称
+                SysUserVo currentUser = JSCommonUtils.getCurrentUser();
+                subjectDo.setTenantid(JSCommonUtils.getCurrentUserFactoryId(currentUser));
+                subjectDo.setCreatedate(date);
+                subjectDo.setModifydate(date);
+                subjectDo.setCreateusername(currentUser.getName());
+                subjectDo.setCreateuserid(currentUser.getId());
+                subjectDo.setModifyusername(currentUser.getName());
+                subjectDo.setModifyuserid(currentUser.getId());
+                subjectDoList.add(subjectDo);
+
+                //子表
+                Set<Integer> indexs = TRAINING_SUBJECT_OPTION_OPTIONSCODE.keySet();
+                for (Integer index :indexs){
+                    if (row.getCell(index)!=null){
+                        subjectOptionsDo =new SubjectOptionsDo();
+                        subjectOptionsDo.setId(SecurityUtil.getUUID());
+                        subjectOptionsDo.setSubjectid(subjectDo.getId());
+                        subjectOptionsDo.setOptionscode(TRAINING_SUBJECT_OPTION_OPTIONSCODE.get(index));  // 选项编号
+                        subjectOptionsDo.setOptionsname(row.getCell(index).getStringCellValue());  //选项内容
+                        subjectOptionsDo.setOptionsparsing(row.getCell(index+1)==null?"":row.getCell(index+1).toString());  //选项解析
+                        subjectOptionsDo.setTrueoptions(getTrueoptionsBySubTpye(row.getCell(2).getStringCellValue(),subjectOptionsDo,subjectDo.getSubjecttype()));  //是否正确答案
+                        subjectOptionsDo.setTenantid(subjectDo.getTenantid());
+                        subjectOptionsDo.setCreatedate(date);
+                        subjectOptionsDo.setModifydate(date);
+                        subjectOptionsDo.setCreateusername(currentUser.getName());
+                        subjectOptionsDo.setCreateuserid(currentUser.getId());
+                        subjectOptionsDo.setModifyusername(currentUser.getName());
+                        subjectOptionsDo.setModifyuserid(currentUser.getId());
+                        subjectOptionsDoList.add(subjectOptionsDo);
+                    }
+                }
+            }
+
+            if (subjectDoList.size()>0){
+                subjectService.saveBatch(subjectDoList,100);
+            }
+
+            if (subjectOptionsDoList.size()>0){
+                subjectOptionsService.saveBatch(subjectOptionsDoList,100);
+            }
+
+        } catch (Exception e) {
+            log.error("导出失败:{} ",e.getMessage());
+            R.error("考题导入异常");
+        }
+        return R.ok();
+    }
+
+    /**
+     * 根据开题类型 判断考题选项是否正确答案
+     * @param answer
+     * @param subjectOptionsDo
+     * @param subjecttype
+     * @return
+     */
+    public String getTrueoptionsBySubTpye(String answer, SubjectOptionsDo subjectOptionsDo, String subjecttype){
+
+        if (subjecttype.equals("30")) { //判断
+            return subjectOptionsDo.getOptionsname().equals("对")?"y":"n";
+        } else if (subjecttype.equals("20")) {  //多选
+            return answer.indexOf(subjectOptionsDo.getOptionscode())>-1?"y":"n";
+        }else if (subjecttype.equals("10")) {  //单选
+            return answer.equals(subjectOptionsDo.getOptionscode())?"y":"n";
+        }
+        return null;
+    }
 }

+ 17 - 4
js-training/training-server/src/main/java/com/rongwei/training/controller/SubjectController.java

@@ -5,10 +5,8 @@ import com.rongwei.rwcommon.utils.StringUtils;
 import com.rongwei.trainingcommon.sys.service.SubjectService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 
@@ -37,4 +35,19 @@ public class SubjectController {
         }
 
     }
+
+
+    @PostMapping("/importFile")
+    public R importFile(@RequestParam("file") MultipartFile multipartFile){
+        if (multipartFile == null) {
+            return R.error("文件解析失败");
+        }
+        try {
+            log.info("进入接口:/importFile/importFile");
+            return subjectService.importFile(multipartFile);
+            // return null;
+        }catch (Exception e){
+            return R.error(e.getMessage());
+        }
+    }
 }