fix:公告添加租户隔离
This commit is contained in:
parent
7afb57c9bf
commit
764f6eec4b
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.competition.common.result.PageResult;
|
||||
import com.competition.common.result.Result;
|
||||
import com.competition.common.util.SecurityUtil;
|
||||
import com.competition.modules.biz.contest.dto.CreateNoticeDto;
|
||||
import com.competition.modules.biz.contest.entity.BizContestNotice;
|
||||
import com.competition.modules.biz.contest.service.IContestNoticeService;
|
||||
@ -39,6 +40,8 @@ public class ContestNoticeController {
|
||||
if (StringUtils.hasText(dto.getPublishTime())) {
|
||||
notice.setPublishTime(LocalDateTime.parse(dto.getPublishTime()));
|
||||
}
|
||||
// 设置当前租户 ID(租户隔离)
|
||||
notice.setTenantId(SecurityUtil.getCurrentTenantId());
|
||||
noticeService.save(notice);
|
||||
return Result.success(notice);
|
||||
}
|
||||
@ -47,9 +50,11 @@ public class ContestNoticeController {
|
||||
@RequirePermission("notice:read")
|
||||
@Operation(summary = "查询赛事下的公告列表")
|
||||
public Result<List<BizContestNotice>> findByContest(@PathVariable Long contestId) {
|
||||
Long tenantId = SecurityUtil.getCurrentTenantId();
|
||||
List<BizContestNotice> list = noticeService.list(
|
||||
new LambdaQueryWrapper<BizContestNotice>()
|
||||
.eq(BizContestNotice::getContestId, contestId)
|
||||
.eq(BizContestNotice::getTenantId, tenantId)
|
||||
.orderByDesc(BizContestNotice::getCreateTime));
|
||||
return Result.success(list);
|
||||
}
|
||||
@ -60,10 +65,21 @@ public class ContestNoticeController {
|
||||
public Result<PageResult<BizContestNotice>> findAll(
|
||||
@RequestParam(defaultValue = "1") Long page,
|
||||
@RequestParam(defaultValue = "10") Long pageSize,
|
||||
@RequestParam(required = false) String title) {
|
||||
@RequestParam(required = false) String title,
|
||||
@RequestParam(required = false) String status) {
|
||||
Long tenantId = SecurityUtil.getCurrentTenantId();
|
||||
LambdaQueryWrapper<BizContestNotice> wrapper = new LambdaQueryWrapper<BizContestNotice>()
|
||||
.like(StringUtils.hasText(title), BizContestNotice::getTitle, title)
|
||||
.orderByDesc(BizContestNotice::getCreateTime);
|
||||
.eq(BizContestNotice::getTenantId, tenantId) // 租户隔离
|
||||
.like(StringUtils.hasText(title), BizContestNotice::getTitle, title);
|
||||
|
||||
// 发布状态过滤
|
||||
if ("published".equals(status)) {
|
||||
wrapper.isNotNull(BizContestNotice::getPublishTime);
|
||||
} else if ("unpublished".equals(status)) {
|
||||
wrapper.isNull(BizContestNotice::getPublishTime);
|
||||
}
|
||||
|
||||
wrapper.orderByDesc(BizContestNotice::getCreateTime);
|
||||
Page<BizContestNotice> result = noticeService.page(new Page<>(page, pageSize), wrapper);
|
||||
return Result.success(PageResult.from(result));
|
||||
}
|
||||
|
||||
@ -8,24 +8,36 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 赛事公告实体
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("t_biz_contest_notice")
|
||||
public class BizContestNotice extends BaseEntity {
|
||||
|
||||
/** 赛事 ID */
|
||||
@TableField("contest_id")
|
||||
private Long contestId;
|
||||
|
||||
/** 租户 ID(用于租户隔离) */
|
||||
@TableField("tenant_id")
|
||||
private Long tenantId;
|
||||
|
||||
/** 公告标题 */
|
||||
private String title;
|
||||
|
||||
/** 公告内容(富文本) */
|
||||
private String content;
|
||||
|
||||
/** system/manual/urgent */
|
||||
/** 公告类型:system/manual/urgent */
|
||||
@TableField("notice_type")
|
||||
private String noticeType;
|
||||
|
||||
/** 优先级 */
|
||||
private Integer priority;
|
||||
|
||||
/** 发布时间 */
|
||||
@TableField("publish_time")
|
||||
private LocalDateTime publishTime;
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ spring:
|
||||
|
||||
# Flyway 数据库迁移
|
||||
flyway:
|
||||
enabled: false
|
||||
enabled: true
|
||||
locations: classpath:db/migration
|
||||
baseline-on-migrate: true
|
||||
baseline-version: 0
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
-- Flyway migration:
|
||||
-- 兼容部分环境没有跑过最新的 init.sql,
|
||||
-- 导致 t_biz_contest_work 缺少 deleted 字段,从而逻辑删除查询报错。
|
||||
SET @column_cnt := (
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 't_biz_contest_work'
|
||||
AND column_name = 'deleted'
|
||||
);
|
||||
|
||||
SET @sql := IF(
|
||||
@column_cnt = 0,
|
||||
'ALTER TABLE t_biz_contest_work ADD COLUMN deleted tinyint NOT NULL DEFAULT ''0'' COMMENT ''逻辑删除:0-未删除,1-已删除''',
|
||||
'SELECT 1'
|
||||
);
|
||||
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
-- 活动公告表添加租户隔离字段
|
||||
-- 执行时间:2026-04-03
|
||||
|
||||
-- 1. 添加 tenant_id 字段
|
||||
ALTER TABLE t_biz_contest_notice
|
||||
ADD COLUMN tenant_id BIGINT COMMENT '租户 ID(用于租户隔离)' AFTER contest_id;
|
||||
|
||||
-- 2. 为现有数据设置默认租户 ID(根据实际业务,可能需要手动设置)
|
||||
-- 假设现有公告都属于当前租户(需要根据实际情况修改 tenant_id)
|
||||
UPDATE t_biz_contest_notice
|
||||
SET tenant_id = 9
|
||||
WHERE tenant_id IS NULL;
|
||||
|
||||
-- 3. 添加索引优化查询性能
|
||||
ALTER TABLE t_biz_contest_notice
|
||||
ADD INDEX idx_tenant_id (tenant_id);
|
||||
|
||||
-- 4. 添加联合索引优化按租户和赛事查询
|
||||
ALTER TABLE t_biz_contest_notice
|
||||
ADD INDEX idx_tenant_contest (tenant_id, contest_id);
|
||||
Loading…
Reference in New Issue
Block a user