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.entity.Keyword; import com.linghu.model.entity.Reference; 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.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 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 exportStatics(@RequestParam("id") Integer keywordId, @RequestParam(value = "questionId", required = false) Integer questionId, HttpServletResponse response) { Keyword keyword = keywordMapper.selectById(keywordId); List 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> getResultByTypeId(@RequestParam("id") Integer keywordId, @RequestParam(value = "questionId", required = false) Integer questionId, @RequestParam(value = "typeId", required = false) Integer typeId) { return keywordService.getResultByTypeId(keywordId, questionId, typeId); } /** * 导出:根据类别查看 */ @PostMapping(value = "/exportGetResultByTypeId") @ApiOperation(value = "导出:根据类别查看") public ResponseEntity exportGetResultByTypeId(@RequestParam("id") 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 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> getResultByPlatformId(@RequestParam("id") Integer keywordId, @RequestParam(value = "questionId", required = false) Integer questionId, @RequestParam(value = "platformId", required = false) Integer platformId) { return keywordService.getResultByPlatformId(keywordId, questionId, platformId); } /** * 根据平台查看 0-当前轮 1-代表前1轮 2-代表前2轮 */ @GetMapping("/exportGetResultByPlatformId") @ApiOperation(value = "根据平台查看") public ResponseEntity 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 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); } /** * 投喂 */ /** * 批量新增关键词 */ @PostMapping("/batchAdd") @ApiOperation(value = "批量新增关键词") public ResponseResult 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> getKeywordsByOrderId(@RequestParam("orderId") String orderId) { List keywords = keywordService.getKeywordsByOrderId(orderId); return ResponseResult.success(keywords); } /** * 修改关键词 */ @PostMapping("/updateKeyword") @ApiOperation(value = "修改关键词") public ResponseResult updateKeyword(@RequestBody Keyword keyword) { keywordService.updateById(keyword); return ResponseResult.success("修改成功"); } /** * 删除关键词 */ @DeleteMapping("/deleteKeyword") @ApiOperation(value = "删除关键词") public ResponseResult deleteKeyword(@RequestParam("keywordId") Integer keywordId) { keywordService.removeById(keywordId); return ResponseResult.success("删除成功"); } // 下载模板 @PostMapping("/downloadTemplate") @ApiOperation("下载模板") public ResponseEntity 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 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()); } } }