guyue
2025-08-12 ff1fec7eae681e89e607fd441d2597ab67b5ed2a
src/main/java/com/linghu/controller/KeywordController.java
@@ -1,27 +1,44 @@
package com.linghu.controller;
import com.alibaba.excel.EasyExcel;
import com.linghu.mapper.KeywordMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linghu.config.FinalStatus;
import com.linghu.listener.KeywordExcelListener;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.entity.Keyword;
import com.linghu.model.entity.Reference;
import com.linghu.model.dto.ExportFeedDTO;
import com.linghu.model.dto.ExportGetResultByPlatformIdDTO;
import com.linghu.model.dto.ExportGetResultDTO;
import com.linghu.model.dto.ExportStaticsDTO;
import com.linghu.model.entity.*;
import com.linghu.model.excel.*;
import com.linghu.model.validator.UpdateGroup;
import com.linghu.model.vo.*;
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.OrderService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/keyword")
@@ -31,46 +48,51 @@
    private KeywordService keywordService;
    @Autowired
    private KeywordMapper keywordMapper;
    private ReferenceService referenceService;
    @Autowired
    private ReferenceService referenceService;
    private OrderService orderService;
    /**
     * 关键词统计 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);
    public ResponseResult<KeywordStaticsListVO> statics(@RequestParam("id") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId) {
        return keywordService.statics(keywordId, questionId);
    }
    @GetMapping("/getTime")
    @ApiOperation(value = "查看时间")
    public ResponseResult<GetTimeVO> getTime(@RequestParam("id") Integer keywordId,@RequestParam(value = "questionId", required = false) Integer questionId) {
        return keywordService.getTimeVOResponseResult(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());
    public ResponseEntity<byte[]> exportStatics(@Valid @RequestBody ExportStaticsDTO exportStaticsDTO) {
        // 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());
        return keywordService.exportEChart(exportStaticsDTO);
    }
    /**
     * 根据类别查看
     */
    @GetMapping("/getResultByTypeId")
    @ApiOperation(value = "根据类别查看")
    public ResponseResult<List<PlatformProportionVO>> 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);
    public ResponseResult<List<PlatformProportionVO> > getResultByTypeId(@RequestParam("keywordId") Integer keywordId,
                                                            @RequestParam(value = "questionId", required = false) Integer questionId,
                                                            @RequestParam(value = "typeId", required = false) Integer typeId,
                                                              @RequestParam(value = "isNow") Integer isNow) {
        return keywordService.getByTypeId(keywordId, questionId, typeId, isNow);
    }
    /**
@@ -78,57 +100,51 @@
     */
    @PostMapping(value = "/exportGetResultByTypeId")
    @ApiOperation(value = "导出:根据类别查看")
    public ResponseEntity<byte[]> 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<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());
    public ResponseEntity<byte[]> exportGetResultByTypeId(@Valid @RequestBody ExportGetResultDTO dto) {
        return keywordService.exportResult(dto);
    }
    /**
     * 根据平台查看
     */
    @GetMapping("/getResultByPlatformId")
    @PostMapping("/getResultByPlatformId")
    @ApiOperation(value = "根据平台查看")
    public ResponseResult<List<ResultListVO>> 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);
    public ResponseResult<List<ResultListVO>> getResultByPlatformId(@RequestParam("keywordId") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId,
            @RequestParam(value = "platformId", required = false) Integer platformId,
            @RequestParam(value = "isNow") Integer isNow) {
        return keywordService.getResultByPlatform(keywordId, questionId, platformId, isNow);
    }
    /**
     * 根据平台查看 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());
    @PostMapping("/exportGetResultByPlatformId")
    @ApiOperation(value = "导出:根据平台查看")
    public ResponseEntity<byte[]> exportGetResultByPlatformId(@Valid @RequestBody ExportGetResultByPlatformIdDTO dto) {
        return keywordService.exportResultByPlatform(dto);
    }
    /**
     * 根据平台查看 0-当前轮 1-代表前1轮 2-代表前2轮
     */
    @PostMapping("/exportGetResultByPlatformId2")
    @ApiOperation(value = "导出:根据平台查看2")
    public ResponseEntity<byte[]> exportGetResultByPlatformId2(@Valid @RequestBody ExportGetResultByPlatformIdDTO dto) {
        return keywordService.exportResultByPlatform2(dto);
    }
    /**
     * 查看详情
     */
    @GetMapping("/getResultById")
    @ApiOperation(value = "根据平台查看")
    @ApiOperation(value = "查看")
    public ResponseResult getResultById(@RequestParam("referenceId") Integer referenceId) {
        Reference reference = referenceService.getById(referenceId);
        if (reference == null) {
@@ -136,14 +152,120 @@
        }
        return ResponseResult.success(reference);
    }
    /**
     * 下载模版
     */
    @GetMapping("/download")
    @ApiOperation("下载投喂模板")
    public ResponseEntity<byte[]> downTemplate() throws IOException {
        return keywordService.downloadKeywrodFeed();
    }
    /**
     * 投喂
     */
    @PostMapping("/importFeed/{keywordId}")
    @ApiOperation("投喂")
    public ResponseResult<List<FeedExportExcel>> importTemplate(@PathVariable("keywordId") Integer keywordId,@RequestParam("file") MultipartFile file) {
        return keywordService.importFeedByKeyWord(keywordId, file);
    }
    /**
     * 导出投喂结果
     */
    @PostMapping("/exportFeed")
    @ApiOperation(value = "导出投喂结果")
    public ResponseEntity<byte[]> exportGetResultByPlatformId( @Valid @RequestBody ExportFeedDTO dto) {
        return keywordService.exportFeedResult(dto);
    }
    /**
     * 根据订单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(@Validated(UpdateGroup.class) @RequestBody Keyword keyword) {
        if(FinalStatus.SUBMITTED.getValue().equals(keyword.getStatus())){
            return ResponseResult.error("关键词已开始采集不允许修改!");
        }
        keywordService.updateById(keyword);
        return ResponseResult.success("修改成功");
    }
    /**
     * 删除关键词
     */
    @DeleteMapping("/deleteKeyword")
    @ApiOperation(value = "删除关键词")
    public ResponseResult<String> deleteKeyword(@RequestParam("keywordId") Integer keywordId) {
        Keyword keyword = keywordService.getById(keywordId);
        if(FinalStatus.SUBMITTED.getValue().equals(keyword.getStatus())){
            return ResponseResult.error("关键词已开始采集或不允许删除!");
        }
        keywordService.removeById(keywordId);
        //找到订单
        Orders order = orderService.getById(keyword.getOrder_id());
        //更新关键词数量
        LambdaQueryWrapper<Keyword> queryKeywordsQueryWrapper =  new LambdaQueryWrapper<>();
        queryKeywordsQueryWrapper.eq(Keyword::getOrder_id, keyword.getOrder_id());
        int count1 = (int) keywordService.count(queryKeywordsQueryWrapper);
        order.setKeyword_num(count1);
        orderService.updateById(order);
        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());
        }
    }
    //根据关键词id查询任务id
}