“djh”
2024-11-11 8458e64aab474c0fc2f49ae4ff22fb11ce5cf6e2
exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExQuestionServiceImpl.java
@@ -5,7 +5,9 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.domain.model.LoginUserDetails;
import com.gkhy.exam.common.enums.PaperStudentStateEnum;
import com.gkhy.exam.common.enums.PrivatizeEnum;
import com.gkhy.exam.common.enums.QuestionTypeEnum;
@@ -23,15 +25,16 @@
import com.gkhy.exam.system.mapper.ExQuestionBankMapper;
import com.gkhy.exam.system.mapper.ExQuestionMapper;
import com.gkhy.exam.system.service.ExQuestionService;
import org.apache.poi.ss.usermodel.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
/**
 * <p>
@@ -276,4 +279,157 @@
    public List<ExQuestion> selectQuestionByPaperId(Long paperId) {
        return baseMapper.selectQuestionByPaperId(paperId);
    }
    @Override
    public CommonResult uploadQuestion(@NotNull MultipartFile file) throws IOException {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheetAt = workbook.getSheetAt(0);
        List<ExQuestion> exQuestions = new ArrayList<>();
        LoginUserDetails loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i <sheetAt.getLastRowNum(); i++) {
            Row row = sheetAt.getRow(i + 1);
            ExQuestion exQuestion = new ExQuestion();
            if (row!=null){
                exQuestion.setQuestionType((int)row.getCell(0).getNumericCellValue());
                exQuestion.setBankId((long)row.getCell(1).getNumericCellValue());
                exQuestion.setAnswer(row.getCell(2).getStringCellValue());
                exQuestion.setTitle(row.getCell(3).getStringCellValue());
                NhoooRootBean nhoooRootBean = new NhoooRootBean();
                List<Items> items1 = new ArrayList<>();
                String cellValue4 = getCellValue(row.getCell(4));
                if (cellValue4!=null){
                    Items items = new Items();
                    items.setPrefix("A");
                    items.setContent(cellValue4);
                    items1.add(items);
                }
                String cellValue5 = getCellValue(row.getCell(5));
                if (cellValue5!=null){
                    Items items = new Items();
                    items.setPrefix("B");
                    items.setContent(cellValue5);
                    items1.add(items);
                }
                String cellValue6 = getCellValue(row.getCell(6));
                if (cellValue6!=null){
                    Items items = new Items();
                    items.setPrefix("C");
                    items.setContent(cellValue6);
                    items1.add(items);
                }
                String cellValue7 = getCellValue(row.getCell(7));
                if (cellValue7!=null){
                    Items items = new Items();
                    items.setPrefix("D");
                    items.setContent(cellValue7);
                    items1.add(items);
                }
                String cellValue8 = getCellValue(row.getCell(8));
                if (cellValue8!=null){
                    Items items = new Items();
                    items.setPrefix("E");
                    items.setContent(cellValue8);
                    items1.add(items);
                }
                String cellValue9 = getCellValue(row.getCell(8));
                if (cellValue9!=null){
                    Items items = new Items();
                    items.setPrefix("F");
                    items.setContent(cellValue9);
                    items1.add(items);
                }
                String cellValue10 = getCellValue(row.getCell(10));
                if (cellValue10!=null){
                    Items items = new Items();
                    items.setPrefix("G");
                    items.setContent(cellValue10);
                    items1.add(items);
                }
                String cellValue11 = getCellValue(row.getCell(11));
                if (cellValue11!=null){
                    Items items = new Items();
                    items.setPrefix("H");
                    items.setContent(cellValue11);
                    items1.add(items);
                }
                nhoooRootBean.setAnalyze("");
                nhoooRootBean.setItems(items1);
                exQuestion.setContent(JSONObject.toJSONString(nhoooRootBean));
                exQuestion.setPrivatize(0);
                exQuestion.setCreateBy(loginUser.getUsername());
                exQuestion.setCreateTime(LocalDateTime.now());
                exQuestion.setUpdateBy(loginUser.getUsername());
                exQuestion.setUpdateTime(LocalDateTime.now());
                exQuestion.setCompanyId(loginUser.getUser().getCompanyId());
                exQuestion.setStatus(0);
                exQuestions.add(exQuestion);
            }
        }
        int i = baseMapper.saveBatch(exQuestions);
        if (i<1){
            throw new ApiException("导入题目失败");
        }
        workbook.close();
        return CommonResult.success();
    }
    private String getCellValue(Cell cell) {
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return null;
        }
    }
    public class NhoooRootBean {
        private String analyze;
        private List<Items> itemsList;
        public void setAnalyze(String analyze) {
            this.analyze = analyze;
        }
        public String getAnalyze() {
            return analyze;
        }
        public void setItems(List<Items> items) {
            this.itemsList = items;
        }
        public List getItems() {
            return itemsList;
        }
    }
    public class Items {
        private String prefix;
        private String content;
        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }
        public String getPrefix() {
            return prefix;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getContent() {
            return content;
        }
    }
}