huliguo
2025-07-16 6727c89745faa938cc053fa67ea3b9f819f5cbfe
src/main/java/com/linghu/controller/CollectController.java
@@ -835,9 +835,9 @@
     * @param domain 平台域名
     * @return 已存在或新创建的Platform
     */
    private Platform getOrCreatePlatform(String domain) {
    private Platform getOrCreatePlatform(String domain,String platformName) {
        // 1. 先尝试查询已存在的平台
        Platform platform = platformService.getPlatformByDomain(domain);
        Platform platform = platformService.getPlatformByDomain(domain,platformName);
        if (platform != null) {
            return platform;
        }
@@ -856,7 +856,12 @@
            // 2.2 构建新平台对象
            Platform newPlatform = new Platform();
            newPlatform.setDomain(domain);
            newPlatform.setPlatform_name(domain); // 平台名称默认使用域名,可根据实际需求调整
            if (platformName != null) {
                newPlatform.setPlatform_name(platformName);
            }else {
                newPlatform.setPlatform_name(domain);
            }
        // 平台名称默认使用域名,可根据实际需求调整
            newPlatform.setType_id(defaultType.getType_id());
            newPlatform.setCreate_time(LocalDateTime.now()); // 补充创建时间
@@ -867,7 +872,7 @@
        } catch (DuplicateKeyException e) {
            // 3. 若捕获到重复键异常,说明并发创建了,重新查询即可(此时数据库中已存在该平台)
            log.warn("平台domain={}已存在,无需重复创建", domain, e);
            return platformService.getPlatformByDomain(domain); // 重新查询,一定能获取到
            return platformService.getPlatformByDomain(domain,platformName); // 重新查询,一定能获取到
        } catch (Exception e) {
            // 处理其他异常(如数据库连接失败等)
            log.error("创建平台失败,domain={}", domain, e);
@@ -1076,7 +1081,9 @@
                                    reference.setNum(keyword.getNum());
                                    reference.setTask_id(result.getTask_id());
                                    reference.setKeyword_id(keyword.getKeyword_id());
                                    reference.setCreate_time(LocalDateTime.now());
                                    if (null!=ref.getPublish_time()) {
                                        reference.setCreate_time(ref.getPublish_time().atStartOfDay());
                                    }
                                   /* // 处理平台和类型关联
                                    Platform platform = platformService.getPlatformByDomain(reference.getDomain());
@@ -1109,7 +1116,7 @@
                                    }
*/
                                    // 关键:使用优化后的方法获取平台,避免重复创建
                                    Platform platform = getOrCreatePlatform(ref.getDomain());
                                    Platform platform = getOrCreatePlatform(ref.getDomain(),ref.getPlatform_name());
                                    reference.setPlatform_id(platform.getPlatform_id());
                                    reference.setType_id(platform.getType_id()); // 直接从平台获取类型ID,更可靠
                                    // 添加到结果列表