guyue
12 小时以前 c2a9635771370e7ec908db7e11a5cdd1b5f7bbf0
1
2
3
4
5
6
7
8
9
10
11
12
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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.Keyword;
import com.linghu.model.entity.Question;
import com.linghu.model.excel.KeywordExcel;
import com.linghu.model.excel.QuestionExcel;
import com.linghu.service.KeywordService;
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.*;
import java.util.stream.Collectors;
 
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
 
@RestController
@RequestMapping("/question")
@Api(value = "提问词相关接口", tags = "订单管理-提问词")
public class QuestionController {
 
    @Autowired
    private QuestionService questionService;
    @Autowired
    private KeywordService keywordService;
 
    @PostMapping
    @ApiOperation(value = "添加提问词")
    @Transactional
    public ResponseResult<List<Question>> add(@RequestBody KeywordDto keywordDto) {
 
        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());
 
        boolean success = questionService.saveBatch(questionList);
        if (success) {
            return ResponseResult.success(questionList);
        }
        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的状态
       //遍历
 
            if ("submitted".equals(keywordDto.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) {
 
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Question::getQuestion_id, questions.getQuestion_id());
        Question keyword = questionService.getOne(queryWrapper);
        if ("submitted".equals(keyword.getStatus()) ) {
            return ResponseResult.error("该关键词在采集中");
        }
 
            boolean success = questionService.updateById(questions);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("更新提问词失败");
    }
 
     @DeleteMapping("/batch")
     @Transactional
     @ApiOperation(value = "批量删除提问词")
     public ResponseResult<Void> batchDelete(@RequestBody List<Integer> questionIds) {
 
 
         // 2. 批量查询所有提问词
         List<Question> questions = questionService.listByIds(questionIds);
         // 3. 校验所有提问词关联的关键词是否处于采集中
         for (Question question : questions) {
             Keyword keyword = keywordService.getById(question.getKeyword_id());
             // 关键词不存在也需处理(可选)
             if (keyword == null) {
                 return ResponseResult.error("提问词关联的关键词不存在,ID:" + question.getKeyword_id());
             }
             // 核心校验:若有任何关键词在采集中,禁止删除
             if ("submitted".equals(keyword.getStatus())) {
                 return ResponseResult.error("该关键词在采集中,无法删除");
             }
         }
        questionService.removeByIds(questionIds);
        return ResponseResult.success();
     }
 
    @GetMapping("/list")
    @ApiOperation("根据关键词查询提问词列表")
    public ResponseResult<List<Question>> list(Integer keyword_id) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Question::getKeyword_id, keyword_id);
        List<Question> list = questionService.list(queryWrapper);
        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=提问词条导入模板.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());
        }
    }
}