| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @RestController |
| | |
| | | private ReferenceService referenceService; |
| | | @Autowired |
| | | private ReferenceMapper referenceMapper; |
| | | @Autowired |
| | | private PlatformService platformService; |
| | | |
| | | /** |
| | | * 关键词统计 EChart图 |
| | |
| | | @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(); |
| | |
| | | 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<>()); |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | @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() |
| | |
| | | @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<>()); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | @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() |
| | |
| | | )); |
| | | |
| | | //time |
| | | Map<LocalDateTime, FeedExportExcel> timeMap = references.stream() |
| | | // 定义日期格式化器 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | Map<String, FeedExportExcel> timeMap = references.stream() |
| | | .collect(Collectors.toMap( |
| | | FeedExportExcel::getCreate_time, // Key映射 |
| | | excel -> excel.getCreate_time().format(formatter), |
| | | |
| | | excel -> excel, // Value映射 |
| | | (oldValue, newValue) -> oldValue // 键冲突处理(保留旧值) |
| | | )); |
| | |
| | | errorMessages.add("发布网址不能为空"); |
| | | continue; |
| | | } |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | LocalDateTime dateTime = LocalDateTime.parse(excel.getCreate_time(), formatter); |
| | | excel.setCreateTimeAsDateTime(dateTime); |
| | | FeedExportExcel feedExportExcel = getFeedExportExcel(excel, nameMap, urlMap, titleMap, timeMap); |
| | | result.add(feedExportExcel); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private FeedExportExcel getFeedExportExcel(ReferenceExcel excel, Map<String, FeedExportExcel> nameMap, Map<String, FeedExportExcel> urlMap, Map<String, FeedExportExcel> titleMap, Map<LocalDateTime, FeedExportExcel> timeMap) { |
| | | private FeedExportExcel getFeedExportExcel(ReferenceExcel excel, Map<String, FeedExportExcel> nameMap, Map<String, FeedExportExcel> urlMap, Map<String, FeedExportExcel> titleMap, Map<String, FeedExportExcel> timeMap) { |
| | | FeedExportExcel vo=new FeedExportExcel(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | excel.setTimeString(excel.getCreate_time().format(formatter)); |
| | | vo.setPlatform_name(excel.getPlatform_name()); |
| | | vo.setTitle(excel.getTitle()); |
| | | vo.setCreate_time(excel.getCreateTimeAsDateTime()); |
| | | vo.setCreate_time(excel.getCreate_time()); |
| | | vo.setUrl(excel.getUrl()); |
| | | vo.setInclusionStatus("未收录"); |
| | | vo.setInclusionStatus("未选择"); |
| | | vo.setPlatformIs(0); |
| | | vo.setTitleIs(0); |
| | | vo.setUrlIs(0); |
| | |
| | | if (titleMap.containsKey(excel.getTitle())) { |
| | | vo.setTitleIs(1); |
| | | } |
| | | if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) { |
| | | if (timeMap.containsKey(excel.getTimeString())) { |
| | | vo.setCreateIs(1); |
| | | } |
| | | return vo; |
| | |
| | | if (titleMap.containsKey(excel.getTitle())) { |
| | | vo.setTitleIs(1); |
| | | } |
| | | if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) { |
| | | if (timeMap.containsKey(excel.getTimeString())) { |
| | | vo.setCreateIs(1); |
| | | } |
| | | return vo; |
| | |
| | | //标题 |
| | | if (titleMap.containsKey(excel.getTitle())) { |
| | | vo.setTitleIs(1); |
| | | if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) { |
| | | if (timeMap.containsKey(excel.getTimeString())) { |
| | | vo.setCreateIs(1); |
| | | } |
| | | } |
| | | //时间 |
| | | if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) { |
| | | if (timeMap.containsKey(excel.getTimeString())) { |
| | | vo.setCreateIs(1); |
| | | return vo; |
| | | } |
| | |
| | | public ResponseEntity<byte[]> exportGetResultByPlatformId(@RequestBody ExportFeedDTO dto) { |
| | | // 3. 导出Excel |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | EasyExcel.write(out, FeedExportExcel.class) |
| | | .sheet("导出投喂结果") |
| | | .doWrite(dto.getExcels()); |
| | | List<String> statusList = new ArrayList<>(); |
| | | statusList.add("未收录"); |
| | | statusList.add("已收录"); |
| | | statusList.add("未选择"); |
| | | // 3. 使用自定义的SheetWriteHandler来添加数据验证 |
| | | ExcelWriter excelWriter = EasyExcel.write(out, FeedExportExcel.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, 2, 2); // D列(第4列) |
| | | |
| | | DataValidationConstraint constraint = helper.createExplicitListConstraint( |
| | | statusList.toArray(new String[0])); |
| | | DataValidation validation = helper.createValidation(constraint, rangeList); |
| | | sheet.addValidationData(validation); |
| | | } |
| | | }) |
| | | .build(); |
| | | |
| | | // 4. 写入空数据 |
| | | WriteSheet writeSheet = EasyExcel.writerSheet("账号模板").build(); |
| | | excelWriter.write(dto.getExcels(), writeSheet); |
| | | excelWriter.finish(); |
| | | |
| | | // 4. 构建响应 |
| | | return ResponseEntity.ok() |
| | |
| | | @PostMapping("/updateKeyword") |
| | | @ApiOperation(value = "修改关键词") |
| | | public ResponseResult<String> updateKeyword(@RequestBody Keyword keyword) { |
| | | if("submitted".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("submitted".equals(keyword.getStatus())){ |
| | | return ResponseResult.error("关键词已开始采集或不允许删除!"); |
| | | } |
| | | keywordService.removeById(keywordId); |
| | | return ResponseResult.success("删除成功"); |
| | | } |
| | |
| | | return ResponseResult.error("导入失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | //根据关键词id查询任务id |
| | | |
| | | |
| | | } |