InterviewQuestionController.java 5.6 KB
Newer Older
1 2 3 4 5 6 7
package com.kwan.springbootkwan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springbootkwan.entity.InterviewQuestion;
import com.kwan.springbootkwan.entity.Result;
8
import com.kwan.springbootkwan.entity.dto.InterviewQuestionDTO;
9 10
import com.kwan.springbootkwan.entity.query.InterviewQuestionAdd;
import com.kwan.springbootkwan.entity.query.InterviewQuestionUpdate;
11
import com.kwan.springbootkwan.service.InterviewQuestionService;
12
import org.apache.commons.lang3.StringUtils;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 面试题(InterviewQuestion)表控制层
 *
 * @author makejava
 * @since 2023-09-08 16:31:51
 */
@RestController
@RequestMapping("interviewQuestion")
public class InterviewQuestionController {
    /**
     * 服务对象
     */
    @Resource
    private InterviewQuestionService interviewQuestionService;

    /**
     * 导入问题
     *
     * @return 所有数据
     */
    @GetMapping("/upload")
    public Result uploadFile(@RequestParam String path) {
        return Result.ok(this.interviewQuestionService.uploadFile(path));
    }

47 48 49 50 51 52 53 54 55 56
    /**
     * 获取面试题的种类的数量
     *
     * @return
     */
    @GetMapping("/questionType")
    public Result questionType() {
        return Result.ok(this.interviewQuestionService.questionType());
    }

57
    /**
58
     * 分页查询所有数据
59 60 61 62 63 64
     *
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result selectAll(@RequestParam Integer page
            , @RequestParam Integer pageSize
65
            , @RequestParam String question
66
            , @RequestParam Integer questionType) {
67 68 69 70 71
        Page<InterviewQuestion> pageParm = new Page<>();
        pageParm.setCurrent(page);
        pageParm.setSize(pageSize);
        QueryWrapper<InterviewQuestion> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
72 73
        if (questionType != 0) {
            wrapper.eq("question_type", questionType);
74
        }
75 76 77 78 79 80 81
        wrapper.eq("is_delete", 0);
        if (StringUtils.isNotEmpty(question)) {
            wrapper.like("question", question);
        }
        return Result.ok(InterviewQuestionDTO.Converter.INSTANCE.from(this.interviewQuestionService.page(pageParm, wrapper)));
    }

82
    /**
83
     * 新增问题
84 85 86
     *
     * @return 所有数据
     */
87
    @PostMapping("/add")
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    public Result add(@RequestBody InterviewQuestionAdd addInfo) {
        final Integer addType = addInfo.getAddType();
        final String question = addInfo.getQuestion();
        if (StringUtils.isEmpty(question)) {
            return Result.error("问题不能为空");
        }
        //批量添加
        if (addType == 1) {
            final String[] split = question.split("\n");
            for (String str : split) {
                str = str.trim().replace("- ", "");
                if (StringUtils.isEmpty(str)) {
                    continue;
                }
                InterviewQuestion interviewQuestion = new InterviewQuestion();
                QueryWrapper<InterviewQuestion> wrapper = new QueryWrapper<>();
                wrapper.eq("question", str);
                wrapper.eq("is_delete", 0);
                final InterviewQuestion one = this.interviewQuestionService.getOne(wrapper);
                if (one == null) {
                    interviewQuestion.setQuestion(str);
                    interviewQuestion.setQuestionType(addInfo.getQuestionType());
                    this.interviewQuestionService.save(interviewQuestion);
                }
            }
        } else {
            InterviewQuestion interviewQuestion = new InterviewQuestion();
            QueryWrapper<InterviewQuestion> wrapper = new QueryWrapper<>();
            wrapper.eq("question", question);
            wrapper.eq("is_delete", 0);
            final InterviewQuestion one = this.interviewQuestionService.getOne(wrapper);
            if (one == null) {
                interviewQuestion.setQuestion(question);
                interviewQuestion.setQuestionType(addInfo.getQuestionType());
                this.interviewQuestionService.save(interviewQuestion);
                return Result.ok();
            } else {
                return Result.error("该面试问题已存在");
            }
        }
        return Result.ok();
129 130 131
    }

    /**
132
     * 更新面试题
133
     *
134
     * @param addInfo
135 136
     * @return
     */
137 138 139 140 141 142
    @PostMapping("/update")
    public Result update(@RequestBody InterviewQuestionUpdate addInfo) {
        InterviewQuestion interviewQuestion = new InterviewQuestion();
        interviewQuestion.setId(addInfo.getId());
        interviewQuestion.setQuestion(addInfo.getQuestion());
        interviewQuestion.setQuestionType(addInfo.getQuestionType());
143 144 145
        return Result.ok(this.interviewQuestionService.updateById(interviewQuestion));
    }

146

147
    /**
148
     * 删除面试题
149
     *
150 151
     * @param id
     * @return
152
     */
153 154 155 156 157 158 159
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        InterviewQuestion interviewQuestion = new InterviewQuestion();
        interviewQuestion.setIsDelete(1);
        QueryWrapper<InterviewQuestion> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return Result.ok(this.interviewQuestionService.update(interviewQuestion, wrapper));
160 161 162
    }
}