InterviewQuestionServiceImpl.java 2.7 KB
Newer Older
1 2 3 4 5
package com.kwan.springbootkwan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springbootkwan.entity.InterviewQuestion;
6 7
import com.kwan.springbootkwan.entity.dto.InterviewQuestionTypeDTO;
import com.kwan.springbootkwan.enums.InterviewQuestionTypeEnum;
8 9 10 11 12 13 14 15 16 17
import com.kwan.springbootkwan.mapper.InterviewQuestionMapper;
import com.kwan.springbootkwan.service.InterviewQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
18
import java.util.LinkedList;
19 20 21 22 23 24 25 26 27 28 29
import java.util.List;

/**
 * 面试题(InterviewQuestion)表服务实现类
 *
 * @author makejava
 * @since 2023-09-08 16:31:53
 */
@Slf4j
@Service("interviewQuestionService")
public class InterviewQuestionServiceImpl extends ServiceImpl<InterviewQuestionMapper, InterviewQuestion> implements InterviewQuestionService {
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    @Resource
    private InterviewQuestionMapper interviewQuestionMapper;

    @Override
    public boolean uploadFile(String path) {
        log.info("uploadFile() called with: path= {}", path);
        // 定义文件路径
        Path filePath = Paths.get(path);
        try {
            // 读取文件的所有行到List对象
            List<String> lines = Files.readAllLines(filePath);
            // 遍历List对象并输出每一行
            for (String line : lines) {
                QueryWrapper<InterviewQuestion> wrapper = new QueryWrapper<>();
                wrapper.eq("question", line);// 按照 age 字段降序排列
                final InterviewQuestion one = this.getOne(wrapper);
                if (one == null) {
                    log.info("uploadFile() called with: question= {}", line);
                    InterviewQuestion interviewQuestion = new InterviewQuestion();
                    interviewQuestion.setQuestion(line);
                    interviewQuestion.setResponse("");
                    this.save(interviewQuestion);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
60 61 62 63 64 65 66 67 68 69 70

    @Override
    public List<InterviewQuestionTypeDTO> questionType() {
        //获取种类,并按数量排序
        LinkedList<InterviewQuestionTypeDTO> types = interviewQuestionMapper.questionType();
        types.addFirst(new InterviewQuestionTypeDTO(0, "全部", 0));
        for (InterviewQuestionTypeDTO interviewQuestionTypeDTO : types) {
            interviewQuestionTypeDTO.setName(InterviewQuestionTypeEnum.getNameByCode(interviewQuestionTypeDTO.getQuestionType()));
        }
        return types;
    }
71
}