huliguo
4 天以前 b36ba9d3da79245376f5a8a1c31cddb1f9a6d1b5
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.linghu.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.linghu.listener.KeywordExcelListener;
import com.linghu.mapper.KeywordMapper;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.dto.ExportFeedDTO;
 
import com.linghu.model.entity.Keyword;
import com.linghu.model.entity.Platform;
import com.linghu.model.entity.Reference;
import com.linghu.model.entity.Type;
import com.linghu.model.excel.FeedExportExcel;
import com.linghu.model.excel.PlatformExcel;
import com.linghu.model.excel.ReferenceExcel;
import com.linghu.model.vo.*;
import com.linghu.model.excel.KeywordExcel;
import com.linghu.model.excel.PlatformExcel;
import com.linghu.model.vo.KeywordStaticsListVO;
import com.linghu.model.vo.KeywordStaticsVO;
import com.linghu.model.vo.PlatformProportionVO;
import com.linghu.model.vo.ResultListVO;
import com.linghu.service.KeywordService;
import com.linghu.service.ReferenceService;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@RestController
@RequestMapping("/keyword")
@Api(value = "采集详情相关接口", tags = "订单列表-采集列表-采集详情")
public class KeywordController {
    @Autowired
    private KeywordService keywordService;
 
    @Autowired
    private KeywordMapper keywordMapper;
 
    @Autowired
    private ReferenceService referenceService;
 
    /**
     * 关键词统计 EChart图
     */
    @GetMapping("/statics")
    @ApiOperation(value = "EChart图")
    public ResponseResult<KeywordStaticsListVO> statics(@RequestParam("id") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId) {
        return keywordService.statics(keywordId, questionId);
    }
 
    @PostMapping(value = "/exportStatics")
    @ApiOperation(value = "EChart图导出")
    public ResponseEntity<byte[]> exportStatics(@RequestParam("id") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId, HttpServletResponse response) {
        Keyword keyword = keywordMapper.selectById(keywordId);
        List<KeywordStaticsVO> voList = keywordMapper.statics(keywordId, questionId, keyword.getNum());
 
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, KeywordStaticsVO.class)
                .sheet("引用数据")
                .doWrite(voList);
 
        // 4. 构建响应
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=references_export.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
 
    /**
     * 根据类别查看
     */
    @GetMapping("/getResultByTypeId")
    @ApiOperation(value = "根据类别查看")
    public ResponseResult<ResultByTypeIdVO> getResultByTypeId(@RequestParam("keywordId") Integer keywordId,
                                                            @RequestParam(value = "questionId", required = false) Integer questionId,
                                                            @RequestParam(value = "typeId", required = false) Integer typeId) {
        ResultByTypeIdVO vo = new ResultByTypeIdVO();
        Keyword keyword = keywordService.getById(keywordId);
        List<PlatformProportionVO> nowList = keywordMapper.getResultByTypeId(keywordId, questionId,
                keyword.getNum(), typeId);
        vo.setNowList(nowList);
        if (keyword.getNum()>1){
            List<PlatformProportionVO> beforeList = keywordMapper.getResultByTypeId(keywordId, questionId,
                    keyword.getNum()-1, typeId);
            vo.setBeforeList(beforeList);
        }
 
        return ResponseResult.success(vo);
    }
 
    /**
     * 导出:根据类别查看
     */
    @PostMapping(value = "/exportGetResultByTypeId")
    @ApiOperation(value = "导出:根据类别查看")
    public ResponseEntity<byte[]> exportGetResultByTypeId(@RequestParam("keywordId") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId,
            @RequestParam(value = "typeId", required = false) Integer typeId,
            @RequestParam(value = "isNow") Integer isNow) {
        Keyword keyword = keywordService.getById(keywordId);
        List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(keywordId, questionId,
                keyword.getNum() - isNow, typeId);
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, PlatformProportionVO.class)
                .sheet("引用数据")
                .doWrite(result);
 
        // 4. 构建响应
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=references_export.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
 
    /**
     * 根据平台查看
     */
    @GetMapping("/getResultByPlatformId")
    @ApiOperation(value = "根据平台查看")
    public ResponseResult<ResultByPlatformVO> getResultByPlatformId(@RequestParam("keywordId") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId,
            @RequestParam(value = "platformId", required = false) Integer platformId) {
        ResultByPlatformVO vo = new ResultByPlatformVO();
        Keyword keyword = keywordService.getById(keywordId);
 
        List<ResultListVO> nowList = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum(),
                platformId);
        vo.setNowList(nowList);
 
        if (keyword.getNum()>1){
            List<ResultListVO> beforeList = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum()-1,
                    platformId);
            vo.setBeforeList(beforeList);
        }
 
        return ResponseResult.success(vo);
    }
 
    /**
     * 根据平台查看 0-当前轮 1-代表前1轮 2-代表前2轮
     */
    @GetMapping("/exportGetResultByPlatformId")
    @ApiOperation(value = "根据平台查看")
    public ResponseEntity<byte[]> exportGetResultByPlatformId(@RequestParam("id") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId,
            @RequestParam(value = "platformId", required = false) Integer platformId,
            @RequestParam(value = "isNow") Integer isNow) {
        Keyword keyword = keywordService.getById(keywordId);
        List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum() - isNow,
                platformId);
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, ResultListVO.class)
                .sheet("引用数据")
                .doWrite(result);
 
        // 4. 构建响应
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=references_export.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
 
    /**
     * 查看详情
     */
    @GetMapping("/getResultById")
    @ApiOperation(value = "查看")
    public ResponseResult getResultById(@RequestParam("referenceId") Integer referenceId) {
        Reference reference = referenceService.getById(referenceId);
        if (reference == null) {
            return ResponseResult.error("该结果不存在");
        }
        return ResponseResult.success(reference);
    }
    /**
     * 下载模版
     */
    @GetMapping("/download")
    @ApiOperation("下载投喂模板")
    public ResponseEntity<byte[]> downTemplate() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, ReferenceExcel.class).sheet("投喂模板").doWrite(new ArrayList<>());
 
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=platform_template.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
    /**
     * 投喂
     */
    @PostMapping("/importFeed")
    @ApiOperation("投喂")
    public ResponseResult<List<FeedExportExcel>> importTemplate(@RequestParam("file") MultipartFile file) {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return ResponseResult.error("上传文件不能为空");
            }
 
            // 读取Excel数据
            List<ReferenceExcel> excelList = EasyExcel.read(file.getInputStream())
                    .head(ReferenceExcel.class)
                    .sheet()
                    .doReadSync();
 
            // 数据转换与验证
            List<String> errorMessages = new ArrayList<>();
            List<FeedExportExcel> result = new ArrayList<>();
            for (ReferenceExcel excel : excelList) {
                // 检查必要字段
                if (!StringUtils.hasText(excel.getPlatform_name())) {
                    errorMessages.add("平台名称不能为空");
                    continue;
                }
                if (!StringUtils.hasText(excel.getTitle())) {
                    errorMessages.add("标题不能为空");
                    continue;
                }
                if (!StringUtils.hasText(String.valueOf(excel.getCreate_time()))) {
                    errorMessages.add("发布时间不能为空");
                    continue;
                }
                if (!StringUtils.hasText(excel.getUrl())) {
                    errorMessages.add("发布网址不能为空");
                    continue;
                }
                //todo 查询出当前这次的结果 做出对比
 
            }
 
            // 处理错误
            if (!errorMessages.isEmpty()) {
                return ResponseResult.error("数据验证失败: " + String.join("; ", errorMessages));
            }
 
            // 返回信息
            return ResponseResult.success(result);
        } catch (Exception e) {
            // 记录详细异常信息
 
            return ResponseResult.error("文件解析失败:" + e.getMessage());
        }
    }
 
    /**
     * 导出投喂结果
     */
    @PostMapping("/exportFeed")
    @ApiOperation(value = "导出投喂结果")
    public ResponseEntity<byte[]> exportGetResultByPlatformId(@RequestBody ExportFeedDTO dto) {
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, FeedExportExcel.class)
                .sheet("导出投喂结果")
                .doWrite(dto.getExcels());
 
        // 4. 构建响应
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=references_export.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
 
 
    /**
     * 批量新增关键词
     */
    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量新增关键词")
    public ResponseResult<String> batchAdd(String keywords, String order_id) {
        Boolean saveKeywords = keywordService.saveKeywords(keywords, order_id);
 
        if (saveKeywords) {
            return ResponseResult.success("新增成功");
        } else {
            return ResponseResult.error("新增失败");
        }
 
    }
 
    /**
     * 根据订单ID查询关键词
     */
    @GetMapping("/getKeywordsByOrderId")
    @ApiOperation(value = "根据订单ID查询关键词")
    public ResponseResult<List<Keyword>> getKeywordsByOrderId(@RequestParam("orderId") String orderId) {
        List<Keyword> keywords = keywordService.getKeywordsByOrderId(orderId);
        return ResponseResult.success(keywords);
    }
 
    /**
     * 修改关键词
     */
    @PostMapping("/updateKeyword")
    @ApiOperation(value = "修改关键词")
    public ResponseResult<String> updateKeyword(@RequestBody Keyword keyword) {
        keywordService.updateById(keyword);
        return ResponseResult.success("修改成功");
    }
 
    /**
     * 删除关键词
     */
    @DeleteMapping("/deleteKeyword")
    @ApiOperation(value = "删除关键词")
    public ResponseResult<String> deleteKeyword(@RequestParam("keywordId") Integer keywordId) {
        keywordService.removeById(keywordId);
        return ResponseResult.success("删除成功");
    }
 
    // 下载模板
    @PostMapping("/downloadTemplate")
    @ApiOperation("下载模板")
    public ResponseEntity<byte[]> downloadTemplate() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, KeywordExcel.class).sheet("关键词模板").doWrite(new ArrayList<>());
 
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=keyword_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("上传文件不能为空");
            }
            // 创建数据监听器
            KeywordExcelListener listener = new KeywordExcelListener();
            EasyExcel.read(file.getInputStream(), KeywordExcel.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());
        }
    }
 
}