支持统计筛选

This commit is contained in:
2026-11-04 13:45:57 +08:00
parent b76a7ccce4
commit 0d8fdbd35a
9 changed files with 523 additions and 17 deletions
@@ -0,0 +1,74 @@
package com.banben.modules.resource.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.banben.common.Result;
import com.banben.modules.resource.entity.ResourceSource;
import com.banben.modules.resource.mapper.ResourceSourceMapper;
import com.banben.modules.resource.entity.VersionResource;
import com.banben.modules.resource.mapper.VersionResourceMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 版本资源多来源管理接口。
*
* <p>对应检索概览页「一条结果可有多个来源,点击可下载」的能力:
* 一条文献可挂载到多个资源库 / 来源馆,概览页据此渲染多个来源并分别下载。</p>
*/
@Tag(name = "版本资源-多来源管理", description = "文献多来源(资源库)的增删改查")
@RestController
@RequestMapping("/api/resource")
@RequiredArgsConstructor
public class ResourceSourceController {
private final ResourceSourceMapper sourceMapper;
private final VersionResourceMapper resourceMapper;
@Operation(summary = "查询某条文献的全部来源(按展示顺序)")
@GetMapping("/{id}/sources")
public Result<List<ResourceSource>> list(@PathVariable Long id) {
QueryWrapper<ResourceSource> w = new QueryWrapper<>();
w.eq("resource_id", id).eq("deleted", 0).orderByAsc("sort_no");
return Result.success(sourceMapper.selectList(w));
}
@Operation(summary = "为某条文献新增一个来源")
@PostMapping("/{id}/sources")
public Result<Long> add(@PathVariable Long id, @RequestBody ResourceSource source) {
VersionResource r = resourceMapper.selectById(id);
if (r == null) {
return Result.failed("文献不存在:" + id);
}
source.setId(null);
source.setResourceId(id);
sourceMapper.insert(source);
return Result.success(source.getId());
}
@Operation(summary = "更新某个来源")
@PutMapping("/sources/{sourceId}")
public Result<Void> update(@PathVariable Long sourceId, @RequestBody ResourceSource source) {
source.setId(sourceId);
source.setResourceId(null); // 不允许跨文献改归属
sourceMapper.updateById(source);
return Result.success();
}
@Operation(summary = "删除某个来源(逻辑删除)")
@DeleteMapping("/sources/{sourceId}")
public Result<Void> remove(@PathVariable Long sourceId) {
sourceMapper.deleteById(sourceId);
return Result.success();
}
}
@@ -0,0 +1,66 @@
package com.banben.modules.resource.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 版本资源多来源(一条文献可来自多个资源库 / 来源馆)。
*
* <p>对应招标文件检索结果概览页要求:「一条结果可有多个来源,按资源库图标+名称展示,点击可下载」。
* 主表 {@link VersionResource} 上的 source_library / source_repo / fulltext_url 等字段作为
* 「主来源」兜底;本表保存该文献的全部来源,概览页据此渲染多个可分别下载的来源。</p>
*/
@Data
@TableName("resource_source")
@Schema(description = "版本资源多来源")
public class ResourceSource implements Serializable {
@TableId(type = IdType.AUTO)
@Schema(description = "来源记录主键")
private Long id;
@Schema(description = "关联版本资源主记录ID(version_resource.id)")
private Long resourceId;
@Schema(description = "来源资源库名")
private String sourceRepo;
@Schema(description = "来源馆")
private String sourceLibrary;
@Schema(description = "来源馆地址(IP:端口或域名)")
private String sourceLibraryAddr;
@Schema(description = "全文地址(下载地址)")
private String fulltextUrl;
@Schema(description = "加工编号(该来源库内的编号)")
private String processNo;
@Schema(description = "媒体类型(纸质/电子,可选,便于概览页区分)")
private String mediaType;
@Schema(description = "展示顺序,数值小的靠前")
private Integer sortNo;
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建时间")
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
@Schema(description = "更新时间")
private LocalDateTime updateTime;
@TableLogic
@Schema(description = "逻辑删除:0未删 1已删")
private Integer deleted;
}
@@ -153,9 +153,17 @@ public class VersionResource implements Serializable {
@Schema(description = "作者单位(责任者所属机构)")
private String authorOrg;
@Schema(description = "来源资源库名")
@Schema(description = "来源资源库名(主来源兜底,多来源详见 sources)")
private String sourceRepo;
/**
* 多来源列表(一条文献可来自多个资源库)。
* 非数据库字段,由检索/细览接口按 resource_id 批量挂载,供概览页按来源展示并分别下载。
*/
@TableField(exist = false)
@Schema(description = "多来源列表:一条文献可来自多个资源库,概览页按来源图标+名称展示并可分别下载")
private java.util.List<ResourceSource> sources;
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建时间")
private LocalDateTime createTime;
@@ -0,0 +1,12 @@
package com.banben.modules.resource.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.banben.modules.resource.entity.ResourceSource;
import org.apache.ibatis.annotations.Mapper;
/**
* 版本资源多来源 Mapper。
*/
@Mapper
public interface ResourceSourceMapper extends BaseMapper<ResourceSource> {
}
@@ -7,7 +7,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.banben.common.PageResult;
import com.banben.modules.admin.entity.SensitiveWord;
import com.banben.modules.admin.mapper.SensitiveWordMapper;
import com.banben.modules.resource.entity.ResourceSource;
import com.banben.modules.resource.entity.VersionResource;
import com.banben.modules.resource.mapper.ResourceSourceMapper;
import com.banben.modules.resource.mapper.VersionResourceMapper;
import com.banben.modules.search.dto.SearchQuery;
import com.banben.modules.search.entity.ClcCategory;
@@ -26,8 +28,11 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 检索发现服务实现。
@@ -45,6 +50,7 @@ public class SearchServiceImpl implements SearchService {
private static final int FACET_TOP = 20;
private final VersionResourceMapper resourceMapper;
private final ResourceSourceMapper sourceMapper;
private final SearchLogMapper searchLogMapper;
private final ClcCategoryMapper clcCategoryMapper;
private final SensitiveWordMapper sensitiveWordMapper;
@@ -74,6 +80,7 @@ public class SearchServiceImpl implements SearchService {
Page.of(query.getCurrent(), query.getSize()), wrapper);
SearchResultVO vo = new SearchResultVO();
attachSources(page.getRecords());
vo.setPage(PageResult.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize()));
// 命中但被屏蔽/下架而不展示的记录数(含管理状态口径的总数 - 实际可见总数)。
@@ -110,7 +117,11 @@ public class SearchServiceImpl implements SearchService {
@Override
public VersionResource detail(Long id) {
return resourceMapper.selectById(id);
VersionResource r = resourceMapper.selectById(id);
if (r != null) {
attachSources(List.of(r));
}
return r;
}
@Override
@@ -141,7 +152,9 @@ public class SearchServiceImpl implements SearchService {
});
}
wrapper.orderByDesc("publish_year").last("limit " + Math.max(1, limit));
return resourceMapper.selectList(wrapper);
List<VersionResource> list = resourceMapper.selectList(wrapper);
attachSources(list);
return list;
}
@Override
@@ -149,7 +162,51 @@ public class SearchServiceImpl implements SearchService {
QueryWrapper<VersionResource> wrapper = buildWrapper(query);
applyOrder(wrapper, query);
wrapper.last("limit " + Math.max(1, limit));
return resourceMapper.selectList(wrapper);
List<VersionResource> list = resourceMapper.selectList(wrapper);
attachSources(list);
return list;
}
/**
* 批量挂载多来源:按主记录 ID 一次查出全部来源,挂到各 {@link VersionResource#sources} 上。
*
* <p>来源为空(旧数据未维护多来源)时,{@code sources} 保持为空列表,
* 前端会回退到主表单来源字段展示与下载。</p>
*/
private void attachSources(List<VersionResource> records) {
if (records == null || records.isEmpty()) {
return;
}
List<Long> ids = records.stream()
.map(VersionResource::getId)
.filter(Objects::nonNull)
.distinct()
.toList();
if (ids.isEmpty()) {
return;
}
try {
List<ResourceSource> all = sourceMapper.selectList(
new QueryWrapper<ResourceSource>()
.in("resource_id", ids)
.eq("deleted", 0)
.orderByAsc("sort_no"));
Map<Long, List<ResourceSource>> byRes = all.stream()
.collect(Collectors.groupingBy(
ResourceSource::getResourceId,
LinkedHashMap::new,
Collectors.toList()));
for (VersionResource r : records) {
r.setSources(byRes.getOrDefault(r.getId(), List.of()));
}
} catch (Exception e) {
log.warn("批量挂载多来源失败(不影响主结果返回):{}", e.getMessage());
for (VersionResource r : records) {
if (r.getSources() == null) {
r.setSources(List.of());
}
}
}
}
// ==================== 内部方法 ====================
@@ -0,0 +1,37 @@
-- =====================================================================
-- 版本资源多来源表
-- 支持「一条文献可有多个来源,概览页按来源图标+名称展示并可分别下载」
-- 兼容 PostgreSQL / 瀚高(HighGo)
-- =====================================================================
CREATE TABLE IF NOT EXISTS resource_source
(
id BIGSERIAL NOT NULL PRIMARY KEY,
resource_id BIGINT NOT NULL,
source_repo VARCHAR(256),
source_library VARCHAR(256),
source_library_addr VARCHAR(256),
fulltext_url VARCHAR(1024),
process_no VARCHAR(128),
media_type VARCHAR(32),
sort_no INT DEFAULT 0,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted SMALLINT DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_rs_resource_id ON resource_source (resource_id);
CREATE INDEX IF NOT EXISTS idx_rs_deleted ON resource_source (deleted);
COMMENT ON TABLE resource_source IS '版本资源多来源表(一条文献可来自多个资源库/来源馆)';
COMMENT ON COLUMN resource_source.resource_id IS '关联版本资源主记录ID';
COMMENT ON COLUMN resource_source.source_repo IS '来源资源库名';
COMMENT ON COLUMN resource_source.fulltext_url IS '全文下载地址';
-- 仅在无来源数据时为样例文献补全多来源(演示用,可重复执行)
INSERT INTO resource_source (resource_id, source_repo, source_library, source_library_addr, fulltext_url, process_no, media_type, sort_no)
SELECT v.id, '版本资源总库', v.source_library, v.source_library_addr, v.fulltext_url, v.process_no, v.media_type, 1
FROM version_resource v
WHERE v.id IN (1, 2)
AND v.deleted = 0
AND NOT EXISTS (SELECT 1 FROM resource_source s WHERE s.resource_id = v.id AND s.deleted = 0);