From 580d70b15ae47bc180a0b579af8c47c506eefac6 Mon Sep 17 00:00:00 2001 From: huliguo <2023611923@qq.com> Date: 星期五, 11 七月 2025 20:13:38 +0800 Subject: [PATCH] 修改 --- src/main/java/com/linghu/controller/KeywordController.java | 116 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 97 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/linghu/controller/KeywordController.java b/src/main/java/com/linghu/controller/KeywordController.java index 58358e0..3e94579 100644 --- a/src/main/java/com/linghu/controller/KeywordController.java +++ b/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图 @@ -154,12 +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); + return isValid ? ResponseResult.success(result) + : ResponseResult.success(new ArrayList<>()); }else { List<PlatformProportionVO> result = keywordMapper.getResultByTypeId(keywordId, questionId, 1, typeId); + boolean isValid = result != null && result.stream() + .anyMatch(Objects::nonNull); - return ResponseResult.success(result); + return isValid ? ResponseResult.success(result) + : ResponseResult.success(new ArrayList<>()); + } } @@ -181,13 +197,39 @@ 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() @@ -209,11 +251,19 @@ if (isNow == 0) { List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, keyword.getNum(), platformId); - return ResponseResult.success(result); + boolean isValid = result != null && result.stream() + .anyMatch(Objects::nonNull); + + return isValid ? ResponseResult.success(result) + : ResponseResult.success(new ArrayList<>()); } else { List<ResultListVO> result = keywordMapper.getResultByPlatformId(keywordId, questionId, 1, platformId); - return ResponseResult.success(result); + boolean isValid = result != null && result.stream() + .anyMatch(Objects::nonNull); + + return isValid ? ResponseResult.success(result) + : ResponseResult.success(new ArrayList<>()); } } @@ -233,11 +283,39 @@ 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() -- Gitblit v1.7.1