huliguo
5 天以前 b36ba9d3da79245376f5a8a1c31cddb1f9a6d1b5
src/main/java/com/linghu/controller/KeywordController.java
@@ -1,10 +1,23 @@
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;
@@ -17,10 +30,15 @@
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;
@@ -72,10 +90,21 @@
     */
    @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<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);
    }
    /**
@@ -83,7 +112,7 @@
     */
    @PostMapping(value = "/exportGetResultByTypeId")
    @ApiOperation(value = "导出:根据类别查看")
    public ResponseEntity<byte[]> exportGetResultByTypeId(@RequestParam("id") Integer keywordId,
    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) {
@@ -108,10 +137,23 @@
     */
    @GetMapping("/getResultByPlatformId")
    @ApiOperation(value = "根据平台查看")
    public ResponseResult<List<ResultListVO>> getResultByPlatformId(@RequestParam("id") Integer keywordId,
    public ResponseResult<ResultByPlatformVO> getResultByPlatformId(@RequestParam("keywordId") Integer keywordId,
            @RequestParam(value = "questionId", required = false) Integer questionId,
            @RequestParam(value = "platformId", required = false) Integer platformId) {
        return keywordService.getResultByPlatformId(keywordId, questionId, 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);
    }
    /**
@@ -143,7 +185,7 @@
     * 查看详情
     */
    @GetMapping("/getResultById")
    @ApiOperation(value = "根据平台查看")
    @ApiOperation(value = "查看")
    public ResponseResult getResultById(@RequestParam("referenceId") Integer referenceId) {
        Reference reference = referenceService.getById(referenceId);
        if (reference == null) {
@@ -151,10 +193,96 @@
        }
        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());
    }
    /**
     * 批量新增关键词
@@ -202,4 +330,40 @@
        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());
        }
    }
}