From c4abb3711bcee356935339f7c9fd9e2b58dee2bf Mon Sep 17 00:00:00 2001
From: huliguo <2023611923@qq.com>
Date: 星期日, 13 七月 2025 00:05:02 +0800
Subject: [PATCH] 修改bug

---
 src/main/java/com/linghu/model/excel/FeedExportExcel.java   |   12 +++
 src/main/java/com/linghu/controller/CollectController.java  |   56 +++++++++++++++++-
 src/main/java/com/linghu/model/excel/ReferenceExcel.java    |   15 +++-
 src/main/resources/mapper/keywordMapper.xml                 |    3 
 src/main/java/com/linghu/controller/UserController.java     |    8 ++
 src/main/java/com/linghu/controller/QuestionController.java |    2 
 src/main/java/com/linghu/controller/KeywordController.java  |   58 ++++++++++++++-----
 7 files changed, 126 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/linghu/controller/CollectController.java b/src/main/java/com/linghu/controller/CollectController.java
index cfeab53..4f1f7bd 100644
--- a/src/main/java/com/linghu/controller/CollectController.java
+++ b/src/main/java/com/linghu/controller/CollectController.java
@@ -16,6 +16,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.http.*;
 import org.springframework.web.reactive.function.client.WebClient;
 
@@ -567,7 +568,50 @@
 //            }
 //        });
 //    }
+    /**
+     * 获取或创建平台(确保同一domain只创建一次)
+     * @param domain 平台域名
+     * @return 已存在或新创建的Platform
+     */
+    private Platform getOrCreatePlatform(String domain) {
+        // 1. 先尝试查询已存在的平台
+        Platform platform = platformService.getPlatformByDomain(domain);
+        if (platform != null) {
+            return platform;
+        }
 
+        // 2. 若不存在,尝试创建(处理并发场景)
+        try {
+            // 2.1 获取或创建“默认”类型(Type也需避免重复,建议Type表的type_name也加唯一约束)
+            Type defaultType = typeService.getOne(new LambdaQueryWrapper<Type>()
+                    .eq(Type::getType_name, "默认"));
+            if (defaultType == null) {
+                defaultType = new Type();
+                defaultType.setType_name("默认");
+                typeService.save(defaultType); // 若Type可能重复,此处也需处理DuplicateKeyException
+            }
+
+            // 2.2 构建新平台对象
+            Platform newPlatform = new Platform();
+            newPlatform.setDomain(domain);
+            newPlatform.setPlatform_name(domain); // 平台名称默认使用域名,可根据实际需求调整
+            newPlatform.setType_id(defaultType.getType_id());
+            newPlatform.setCreate_time(LocalDateTime.now()); // 补充创建时间
+
+            // 2.3 尝试保存,若因唯一约束冲突失败,则捕获异常
+            platformService.save(newPlatform);
+            return newPlatform; // 保存成功,返回新创建的平台
+
+        } catch (DuplicateKeyException e) {
+            // 3. 若捕获到重复键异常,说明并发创建了,重新查询即可(此时数据库中已存在该平台)
+            log.warn("平台domain={}已存在,无需重复创建", domain, e);
+            return platformService.getPlatformByDomain(domain); // 重新查询,一定能获取到
+        } catch (Exception e) {
+            // 处理其他异常(如数据库连接失败等)
+            log.error("创建平台失败,domain={}", domain, e);
+            throw new RuntimeException("创建平台失败", e);
+        }
+    }
     private Mono<Void> updateQuestionAndReference(TaskResultResponse result) {
         return Mono.fromRunnable(() -> {
             try {
@@ -725,14 +769,14 @@
                                                     return reference;
                                                 })
                                                 .collect(Collectors.toList());*/
-// 初始化引用列表(避免null)
+                                // 初始化引用列表(避免null)
                                 List<Reference> references = new ArrayList<>();
                                 List<TaskResultResponse.Reference> originalReferences = questionResult.getReferences();
                                 if (originalReferences == null) {
                                     originalReferences = Collections.emptyList();
                                 }
 
-// 遍历原始引用列表,转换为Reference对象
+                                // 遍历原始引用列表,转换为Reference对象
                                 for (TaskResultResponse.Reference ref : originalReferences) { // 注意:需将“原引用类型”替换为实际类型(如QuestionResult中的引用类型)
                                     Reference reference = new Reference();
                                     // 设置基本字段
@@ -745,7 +789,7 @@
                                     reference.setKeyword_id(keyword.getKeyword_id());
                                     reference.setCreate_time(LocalDateTime.now());
 
-                                    // 处理平台和类型关联
+                                   /* // 处理平台和类型关联
                                     Platform platform = platformService.getPlatformByDomain(reference.getDomain());
                                     if (platform == null) {
                                         // 平台不存在,创建新平台(类型默认为“默认”)
@@ -774,7 +818,11 @@
                                             reference.setType_id(type.getType_id());
                                         }
                                     }
-
+*/
+                                    // 关键:使用优化后的方法获取平台,避免重复创建
+                                    Platform platform = getOrCreatePlatform(ref.getDomain());
+                                    reference.setPlatform_id(platform.getPlatform_id());
+                                    reference.setType_id(platform.getType_id()); // 直接从平台获取类型ID,更可靠
                                     // 添加到结果列表
                                     references.add(reference);
                                 }
diff --git a/src/main/java/com/linghu/controller/KeywordController.java b/src/main/java/com/linghu/controller/KeywordController.java
index 3e94579..d14a57e 100644
--- a/src/main/java/com/linghu/controller/KeywordController.java
+++ b/src/main/java/com/linghu/controller/KeywordController.java
@@ -382,9 +382,12 @@
                 ));
 
         //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  // 键冲突处理(保留旧值)
                 ));
@@ -423,9 +426,6 @@
                     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);
 
@@ -448,13 +448,15 @@
         }
     }
 
-    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);
@@ -468,7 +470,7 @@
             if (titleMap.containsKey(excel.getTitle())) {
                 vo.setTitleIs(1);
             }
-            if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) {
+            if (timeMap.containsKey(excel.getTimeString())) {
                 vo.setCreateIs(1);
             }
             return vo;
@@ -479,7 +481,7 @@
             if (titleMap.containsKey(excel.getTitle())) {
                 vo.setTitleIs(1);
             }
-            if (timeMap.containsKey(excel.getCreateTimeAsDateTime())) {
+            if (timeMap.containsKey(excel.getTimeString())) {
                 vo.setCreateIs(1);
             }
             return vo;
@@ -487,12 +489,12 @@
         //标题
         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;
         }
@@ -508,9 +510,35 @@
     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()
diff --git a/src/main/java/com/linghu/controller/QuestionController.java b/src/main/java/com/linghu/controller/QuestionController.java
index be640e1..0d90313 100644
--- a/src/main/java/com/linghu/controller/QuestionController.java
+++ b/src/main/java/com/linghu/controller/QuestionController.java
@@ -148,7 +148,7 @@
         EasyExcel.write(out, QuestionExcel.class).sheet("提问词模板").doWrite(new ArrayList<>());
 
         return ResponseEntity.ok()
-                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=question_template.xlsx")
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=提问词条导入模板.xlsx")
                 .contentType(MediaType.APPLICATION_OCTET_STREAM)
                 .body(out.toByteArray());
     }
diff --git a/src/main/java/com/linghu/controller/UserController.java b/src/main/java/com/linghu/controller/UserController.java
index 619e913..ab47d9b 100644
--- a/src/main/java/com/linghu/controller/UserController.java
+++ b/src/main/java/com/linghu/controller/UserController.java
@@ -82,6 +82,10 @@
         if (list != null && list.size() > 0) {
             return ResponseResult.success("该邮箱已存在");
         }
+        if (!user1.getUser_email().equals(user.getUser_email())||!user1.getPassword().equals(user.getPassword())) {
+            user1.setStatus("正常");
+        }
+
         userService.updateById(user);
         return ResponseResult.success();
 
@@ -212,7 +216,9 @@
             List<User> insertUserList = new ArrayList<>();
             List<String> errorMessages = new ArrayList<>();
             Set<String> emailSet = new HashSet<>(); // 用于校验导入数据中的邮箱重复
-
+            if (excelDataList.isEmpty()) {
+                return ResponseResult.error("导入失败,数据不能为空");
+            }
             for (ExcelDataWithRow<UserExcel> excelData : excelDataList) {
                 int rowNum = excelData.getRowNumber();
                 UserExcel excelRowData = excelData.getData();
diff --git a/src/main/java/com/linghu/model/excel/FeedExportExcel.java b/src/main/java/com/linghu/model/excel/FeedExportExcel.java
index 44d510c..7586d81 100644
--- a/src/main/java/com/linghu/model/excel/FeedExportExcel.java
+++ b/src/main/java/com/linghu/model/excel/FeedExportExcel.java
@@ -1,10 +1,13 @@
 package com.linghu.model.excel;
 
+import com.alibaba.excel.annotation.ExcelIgnore;
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
 
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 
 @Data
@@ -13,27 +16,32 @@
     @ApiModelProperty("平台名称")
     private String platform_name;
     @ApiModelProperty("平台是否匹配 0-否 1-是")
+    @ExcelIgnore
     private Integer platformIs;
 
     @ExcelProperty("标题")
     @ApiModelProperty("标题")
     private String title;
     @ApiModelProperty("标题是否匹配 0-否 1-是")
+    @ExcelIgnore
     private Integer titleIs;
 
 
     @ExcelProperty("发布时间")
     @ApiModelProperty("发布时间")
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-    private LocalDateTime create_time;
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
+    private LocalDate create_time;
 
     @ApiModelProperty("发布时间是否匹配 0-否 1-是")
+    @ExcelIgnore
     private Integer createIs;
 
     @ExcelProperty("发布网址")
     @ApiModelProperty("发布网址")
     private String url;
     @ApiModelProperty("发布网址是否匹配 0-否 1-是")
+    @ExcelIgnore
     private Integer urlIs;
 
     @ExcelProperty("收录情况")
diff --git a/src/main/java/com/linghu/model/excel/ReferenceExcel.java b/src/main/java/com/linghu/model/excel/ReferenceExcel.java
index b8de81b..b5d6217 100644
--- a/src/main/java/com/linghu/model/excel/ReferenceExcel.java
+++ b/src/main/java/com/linghu/model/excel/ReferenceExcel.java
@@ -3,25 +3,32 @@
 import com.alibaba.excel.annotation.ExcelIgnore;
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
 
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 
 
 @Data
 public class ReferenceExcel {
-    @ExcelProperty("平台名称")
+    @ExcelProperty("发布平台")
     private String platform_name;
 
     @ExcelProperty("标题")
     private String title;
     @ExcelProperty("发布时间")
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-    private String create_time;
+    // 关键:指定日期格式为 yyyy-MM-dd HH:mm:ss
+    @DateTimeFormat(pattern = "yyyy/MM/dd")
+    @JsonFormat(pattern = "yyyy/MM/dd")
+    private LocalDate create_time;
 
     @ExcelProperty("发布网址")
     private String url;
     @ExcelIgnore
     private LocalDateTime createTimeAsDateTime;
-
+    @ExcelIgnore
+    @ApiModelProperty(hidden = true)
+    private String timeString;
 }
\ No newline at end of file
diff --git a/src/main/resources/mapper/keywordMapper.xml b/src/main/resources/mapper/keywordMapper.xml
index eb69865..6618988 100644
--- a/src/main/resources/mapper/keywordMapper.xml
+++ b/src/main/resources/mapper/keywordMapper.xml
@@ -50,6 +50,7 @@
         p.platform_name
         ORDER BY
         total_repetitions DESC
+        LIMIT 30
     </select>
     <select id="getResultByTypeId" resultType="com.linghu.model.vo.PlatformProportionVO">
         SELECT
@@ -95,7 +96,7 @@
             </if>
         GROUP BY t.type_name,p.platform_name
         ORDER BY
-        r.repetition_num  DESC
+        total_repetitions  DESC
     </select>
     <select id="getResultByPlatformId" resultType="com.linghu.model.vo.ResultListVO">
         SELECT

--
Gitblit v1.7.1