From 8e0decd25b9ec86b02d58de53dee1451f83d1566 Mon Sep 17 00:00:00 2001 From: huliguo <2023611923@qq.com> Date: 星期四, 10 七月 2025 23:48:35 +0800 Subject: [PATCH] 新增 --- src/main/java/com/linghu/controller/QuestionController.java | 149 +++++++++++++++++++++++++++++++++++-------------- 1 files changed, 106 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/linghu/controller/QuestionController.java b/src/main/java/com/linghu/controller/QuestionController.java index 6c15d93..be640e1 100644 --- a/src/main/java/com/linghu/controller/QuestionController.java +++ b/src/main/java/com/linghu/controller/QuestionController.java @@ -1,21 +1,35 @@ package com.linghu.controller; +import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.linghu.listener.QuestionExcelListener; import com.linghu.model.common.ResponseResult; import com.linghu.model.dto.KeywordDto; import com.linghu.model.entity.Question; import com.linghu.model.excel.KeywordExcel; +import com.linghu.model.excel.QuestionExcel; import com.linghu.service.QuestionService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; + +import static jdk.nashorn.internal.runtime.regexp.joni.Config.log; @RestController @RequestMapping("/question") @@ -48,36 +62,57 @@ return ResponseResult.error("添加提问词失败"); } - @DeleteMapping("/{questionId}") - @ApiOperation(value = "删除提问词") - public ResponseResult<Void> delete(@PathVariable Integer questionId) { - - boolean success = questionService.removeById(questionId); - if (success) { - return ResponseResult.success(); - } - return ResponseResult.error("删除提问词失败"); - } - - // @PutMapping - // @Transactional - // public ResponseResult<Void> update(@RequestBody List<Question> questions) { - // boolean success = questionService.updateBatchById(questions); - // // 不存在的问题id就新增 - // List<Question> newQuestions = questions.stream() - // .filter(q -> q.getQuestion_id() == null) - // .collect(Collectors.toList()); - // if (!newQuestions.isEmpty()) { - // questionService.saveBatch(newQuestions); - // } - // if (success) { - // return ResponseResult.success(); - // } - // return ResponseResult.error("更新提问词失败"); - // } +// @DeleteMapping("/{questionId}") +// @ApiOperation(value = "删除提问词") +// public ResponseResult<Void> delete(@PathVariable Integer questionId) { +// +// boolean success = questionService.removeById(questionId); +// if (success) { +// return ResponseResult.success(); +// } +// return ResponseResult.error("删除提问词失败"); +// } @PutMapping @ApiOperation(value = "修改提问词") + @Transactional + public ResponseResult<List<Question>> update(@RequestBody KeywordDto keywordDto) { + //查询question的状态 + //遍历 + for (Question q : keywordDto.getQuestionList()) { + if ("success".equals(q.getStatus())) { + return ResponseResult.error("该提问词已采集"); + } + } + questionService.updateBatchById(keywordDto.getQuestionList()); + +// if (!"notSubmitted".equals(keywordDto.getStatus() )){ +// return ResponseResult.error("该关键词已提交或者已采集完成不允许修改提问词!"); +// } +// LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>(); +// queryWrapper.eq(Question::getKeyword_id, keywordDto.getKeyword_id()); +// questionService.remove(queryWrapper); + +// List<Question> questionList = Arrays.stream(keywordDto.getQuestions().split("\\n")) +// .filter(q -> !q.trim().isEmpty()) +// .map(q -> { +// Question question = new Question(); +// question.setKeyword_id(keywordDto.getKeyword_id()); +// question.setQuestion(q.trim()); +// question.setStatus("pending"); +// +// return question; +// }).collect(Collectors.toList()); + +// questionService.saveBatch(questionList); + + return ResponseResult.success(); + + + } + + @PutMapping("/update") + @ApiOperation(value = "修改单个提问词") public ResponseResult<Void> update(@RequestBody Question questions) { boolean success = questionService.updateById(questions); if (success) { @@ -86,22 +121,14 @@ return ResponseResult.error("更新提问词失败"); } - // @DeleteMapping("/batch") - // @Transactional - // public ResponseResult<Void> batchDelete(@RequestBody List<Integer> - // questionIds) { - // List<Question> questions = questionIds.stream().map(id -> { - // Question question = new Question(); - // question.setQuestion_id(id); - // return question; - // }).collect(Collectors.toList()); - - // boolean success = questionService.updateBatchById(questions); - // if (success) { - // return ResponseResult.success(); - // } - // return ResponseResult.error("批量删除提问词失败"); - // } + @DeleteMapping("/batch") + @Transactional + @ApiOperation(value = "批量删除提问词") + public ResponseResult<Void> batchDelete(@RequestBody List<Integer> questionIds) { + System.out.println(questionIds.toString()); + questionService.removeByIds(questionIds); + return ResponseResult.success(); + } @GetMapping("/list") @ApiOperation("根据关键词查询提问词列表") @@ -112,4 +139,40 @@ return ResponseResult.success(list); } + + // 下载模板 + @PostMapping("/downloadTemplate") + @ApiOperation("下载模板") + public ResponseEntity<byte[]> downloadTemplate() throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + EasyExcel.write(out, QuestionExcel.class).sheet("提问词模板").doWrite(new ArrayList<>()); + + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=question_template.xlsx") + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(out.toByteArray()); + } + + // 导入文件 + @PostMapping("/import") + @ApiOperation("导入提问词词数据") + public ResponseResult<String> importPlatforms(@RequestParam("file") MultipartFile file) { + try { + if (file.isEmpty()) { + return ResponseResult.error("上传文件不能为空"); + } + // 创建数据监听器 + QuestionExcelListener listener = new QuestionExcelListener(); + EasyExcel.read(file.getInputStream(), QuestionExcel.class, listener) + .sheet() + .doRead(); + // 获取并合并关键词 + String mergedKeywords = String.join("\n", listener.getMergedKeywords()); + return ResponseResult.success(mergedKeywords); + } catch (IOException e) { + return ResponseResult.error("文件读取失败:" + e.getMessage()); + } catch (Exception e) { + return ResponseResult.error("导入失败:" + e.getMessage()); + } + } } \ No newline at end of file -- Gitblit v1.7.1