guyue
1 天以前 286cc1963d6d5a26f26c78d542974691b80a86f7
src/main/java/com/linghu/controller/KeywordController.java
@@ -1,8 +1,13 @@
package com.linghu.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linghu.listener.KeywordExcelListener;
import com.linghu.mapper.KeywordMapper;
@@ -13,24 +18,24 @@
import com.linghu.model.dto.ExportGetResultByPlatformIdDTO;
import com.linghu.model.dto.ExportGetResultDTO;
import com.linghu.model.dto.ExportStaticsDTO;
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.entity.*;
import com.linghu.model.excel.*;
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.PlatformService;
import com.linghu.service.ReferenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -51,6 +56,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@RestController
@@ -67,6 +73,8 @@
    private ReferenceService referenceService;
    @Autowired
    private ReferenceMapper referenceMapper;
    @Autowired
    private PlatformService platformService;
    /**
     * 关键词统计 EChart图
@@ -75,14 +83,58 @@
    @ApiOperation(value = "EChart图")
    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) {
        //
        GetTimeVO vo = new GetTimeVO();
        Keyword keyword = keywordService.getById(keywordId);
        LambdaQueryWrapper<Reference> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reference::getKeyword_id, keywordId);
        if (questionId != null) {
            queryWrapper.eq(Reference::getQuestion_id, questionId);
        }
        queryWrapper.eq(Reference::getNum,keyword.getNum());
        List<Reference> list = referenceService.list(queryWrapper);
        if (null != list && !list.isEmpty()) {
            vo.setNow(list.get(0).getCreate_time());
        }
        //首次
        LambdaQueryWrapper<Reference> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Reference::getKeyword_id, keywordId);
        if (questionId != null) {
            queryWrapper2.eq(Reference::getQuestion_id, questionId);
        }
        queryWrapper2.eq(Reference::getNum,1);
        List<Reference> list2 = referenceService.list(queryWrapper2);
        if (null != list2 && !list2.isEmpty()) {
            vo.setFirst(list2.get(0).getCreate_time());
        }
        return ResponseResult.success(vo);
    }
    @PostMapping(value = "/exportStatics")
    @ApiOperation(value = "EChart图导出")
    public ResponseEntity<byte[]> exportStatics(@RequestBody ExportStaticsDTO exportStaticsDTO) {
        Keyword keyword = keywordMapper.selectById(exportStaticsDTO.getKeywordId());
        List<KeywordStaticsVO> voList = keywordMapper.statics(exportStaticsDTO.getKeywordId(), exportStaticsDTO.getQuestionId(), keyword.getNum());
        List<KeywordStaticsVO> voList=new ArrayList<>();
        if (exportStaticsDTO.getIsFirst()==0){
            voList = keywordMapper.statics(exportStaticsDTO.getKeywordId(), exportStaticsDTO.getQuestionId(), keyword.getNum());
        }else {
            voList = keywordMapper.statics(exportStaticsDTO.getKeywordId(), exportStaticsDTO.getQuestionId(), 1);
        }
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -110,16 +162,20 @@
        if (isNow == 0) {
            List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(keywordId, questionId,
                    keyword.getNum() , typeId);
            // 检查列表是否为空或只包含null元素
            boolean isValid = result != null && result.stream()
                    .anyMatch(Objects::nonNull);
            return ResponseResult.success(result);
        }
        if (isNow > 0 && keyword.getNum()>isNow){
            List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(keywordId, questionId,
                    keyword.getNum() - isNow, typeId);
            return ResponseResult.success(result);
            return isValid ? ResponseResult.success(result)
                    : ResponseResult.success(new ArrayList<>());
        }else {
            return ResponseResult.success(new ArrayList<>());
            List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(keywordId, questionId, 1, typeId);
            boolean isValid = result != null && result.stream()
                    .anyMatch(Objects::nonNull);
            return isValid ? ResponseResult.success(result)
                    : ResponseResult.success(new ArrayList<>());
        }
    }
@@ -131,13 +187,49 @@
    @ApiOperation(value = "导出:根据类别查看")
    public ResponseEntity<byte[]> exportGetResultByTypeId(@RequestBody ExportGetResultDTO dto) {
        Keyword keyword = keywordService.getById(dto.getKeywordId());
        List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(dto.getKeywordId(), dto.getQuestionId(),
                keyword.getNum() - dto.getIsNow(), dto.getTypeId());
        List<PlatformProportionVO> result =new ArrayList<>();
        if (dto.getIsNow()==0){
            result = keywordMapper.getResultByTypeId(dto.getKeywordId(), dto.getQuestionId(),
                    keyword.getNum() , dto.getTypeId());
        }else {
            result = keywordMapper.getResultByTypeId(dto.getKeywordId(), dto.getQuestionId(),
                    1 , dto.getTypeId());
        }
        //查询所有平台名称
        List<String> typeNameList = result.stream()
                .map(PlatformProportionVO::getType_name)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, PlatformProportionVO.class)
                .sheet("引用数据")
                .doWrite(result);
        // 3. 使用自定义的SheetWriteHandler来添加数据验证
        ExcelWriter excelWriter = EasyExcel.write(out, PlatformProportionVO.class)
                .registerWriteHandler(new SheetWriteHandler() {
                    @Override
                    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
                                                 WriteSheetHolder writeSheetHolder) {
                        Sheet sheet = writeSheetHolder.getSheet();
                        // 添加数据验证(下拉框)
                        DataValidationHelper helper = sheet.getDataValidationHelper();
                        CellRangeAddressList rangeList = new CellRangeAddressList(
                                1, 65535, 0, 0); // D列(第4列)
                        DataValidationConstraint constraint = helper.createExplicitListConstraint(
                                typeNameList.toArray(new String[0]));
                        DataValidation validation = helper.createValidation(constraint, rangeList);
                        sheet.addValidationData(validation);
                    }
                })
                .build();
        WriteSheet writeSheet = EasyExcel.writerSheet("平台分布占比").build();
        excelWriter.write(result, writeSheet);
        excelWriter.finish();
        // 4. 构建响应
        return ResponseEntity.ok()
@@ -156,19 +248,24 @@
            @RequestParam(value = "platformId", required = false) Integer platformId,
            @RequestParam(value = "isNow") Integer isNow) {
        Keyword keyword = keywordService.getById(keywordId);
        if (isNow==0){
        if (isNow == 0) {
            List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum(),
                    platformId);
            return ResponseResult.success(result);
        }
        if (isNow > 0 && keyword.getNum()>isNow){
        List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum() - isNow,
                platformId);
            boolean isValid = result != null && result.stream()
                    .anyMatch(Objects::nonNull);
        return ResponseResult.success(result);
        }else {
            return ResponseResult.success(new ArrayList<>());
            return isValid ? ResponseResult.success(result)
                    : ResponseResult.success(new ArrayList<>());
        } else {
            List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, 1,
                    platformId);
            boolean isValid = result != null && result.stream()
                    .anyMatch(Objects::nonNull);
            return isValid ? ResponseResult.success(result)
                    : ResponseResult.success(new ArrayList<>());
        }
    }
    /**
@@ -178,13 +275,47 @@
    @ApiOperation(value = "导出:根据平台查看")
    public ResponseEntity<byte[]> exportGetResultByPlatformId(@RequestBody ExportGetResultByPlatformIdDTO dto) {
        Keyword keyword = keywordService.getById(dto.getKeywordId());
        List<ResultListVO> result = keywordMapper.getResultByPlatformId(dto.getKeywordId(), dto.getQuestionId(), keyword.getNum() - dto.getIsNow(),
                dto.getPlatformId());
        List<ResultListVO> result=new ArrayList<>();
        if (dto.getIsNow()==0){
            result = keywordMapper.getResultByPlatformId(dto.getKeywordId(), dto.getQuestionId(), keyword.getNum(),
                    dto.getPlatformId());
        }else {
            result = keywordMapper.getResultByPlatformId(dto.getKeywordId(), dto.getQuestionId(), 1,
                    dto.getPlatformId());
        }
        //查询所有平台名称
        List<String> platfromNames = result.stream()
                .map(ResultListVO::getPlatform_name)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        // 3. 导出Excel
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, ResultListVO.class)
                .sheet("引用数据")
                .doWrite(result);
        // 3. 使用自定义的SheetWriteHandler来添加数据验证
        ExcelWriter excelWriter = EasyExcel.write(out, ResultListVO.class)
                .registerWriteHandler(new SheetWriteHandler() {
                    @Override
                    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
                                                 WriteSheetHolder writeSheetHolder) {
                        Sheet sheet = writeSheetHolder.getSheet();
                        // 添加数据验证(下拉框)
                        DataValidationHelper helper = sheet.getDataValidationHelper();
                        CellRangeAddressList rangeList = new CellRangeAddressList(
                                1, 65535, 0, 0); // D列(第4列)
                        DataValidationConstraint constraint = helper.createExplicitListConstraint(
                                platfromNames.toArray(new String[0]));
                        DataValidation validation = helper.createValidation(constraint, rangeList);
                        sheet.addValidationData(validation);
                    }
                })
                .build();
        WriteSheet writeSheet = EasyExcel.writerSheet("平台分布占比").build();
        excelWriter.write(result, writeSheet);
        excelWriter.finish();
        // 4. 构建响应
        return ResponseEntity.ok()
@@ -477,5 +608,7 @@
            return ResponseResult.error("导入失败:" + e.getMessage());
        }
    }
    //根据关键词id查询任务id
}