refactor: 重构勋章模块,拆分规则独立管理,优化期刊绑定逻辑

1.  新增MedalRule实体类,将勋章条件拆分独立为规则管理
2.  重构Medal相关服务和DTO,支持勋章规则的增删改查
3.  简化Journal绑定逻辑,移除冗余的实例化期刊校验和字段
4.  重命名部分实体类,统一命名前缀
5.  为勋章相关操作添加事务管理,优化异常处理逻辑
This commit is contained in:
glz
2026-06-05 15:52:11 +08:00
parent f9b79417a0
commit eab84747a5
9 changed files with 471 additions and 193 deletions

View File

@ -24,15 +24,14 @@ public class UserJournalService(
/// </summary>
public async Task<BindJournalOutput> BindJournalAsync(long userId, BindJournalInput input)
{
logger.LogInformation("用户绑定期刊UserId: {UserId}, JournalId: {JournalId}, JournalInstanceId: {JournalInstanceId}, Type: {Type}",
userId, input.JournalId, input.JournalInstanceId, input.Type);
logger.LogInformation("用户绑定期刊UserId: {UserId}, JournalId: {JournalId}, Id: {Id}, Type: {Type}",
userId, input.JournalId, input.Id, input.Type);
// 校验参数
if (input.JournalId <= 0)
if (input.JournalId <= 0|| input.Id <= 0)
{
throw new BusinessException("期刊Id不能为空", 400);
throw new BusinessException("参数错误,未获取到期刊", 400);
}
// 校验用户是否存在
var user = await usersRepository.GetByIdAsync(userId);
if (user == null || user.IsDeleted)
@ -56,29 +55,14 @@ public class UserJournalService(
throw new BusinessException("该期刊暂未发布,无法绑定", 400);
}
// 校验实例化期刊是否存在(如果传入了 JournalInstanceId
if (input.JournalInstanceId.HasValue && input.JournalInstanceId.Value > 0)
{
var instance = await journalRepository.GetByIdAsync(input.JournalInstanceId.Value);
if (instance == null || instance.IsDeleted)
{
logger.LogWarning("绑定期刊失败实例化期刊不存在JournalInstanceId: {JournalInstanceId}", input.JournalInstanceId);
throw new BusinessException("实例化期刊不存在", 404);
}
}
// 防重复绑定:同一用户 + 期刊 + 实例 + 类型
var isExist = userJournalRepository.Any(uj =>
uj.UserId == userId &&
uj.JournalId == input.JournalId &&
uj.JournalInstanceId == input.JournalInstanceId &&
uj.Type == input.Type &&
!uj.IsDeleted);
uj.Id == input.Id);
if (isExist)
{
logger.LogWarning("重复绑定期刊UserId: {UserId}, JournalId: {JournalId}, Type: {Type}", userId, input.JournalId, input.Type);
throw new BusinessException("您已绑定过该期刊,无需重复操作", 400);
throw new BusinessException("该期刊已被绑定", 400);
}
// 检查是否为首次绑定期刊(用于激活宠物)
@ -90,7 +74,7 @@ public class UserJournalService(
{
UserId = userId,
JournalId = input.JournalId,
JournalInstanceId = input.JournalInstanceId,
Id = input.Id,
Type = input.Type,
Status = "Active",
IsDeleted = false,
@ -128,7 +112,6 @@ public class UserJournalService(
Id = userJournal.Id,
UserId = userJournal.UserId,
JournalId = userJournal.JournalId,
JournalInstanceId = userJournal.JournalInstanceId,
Type = userJournal.Type,
Status = userJournal.Status,
CreatedAt = userJournal.CreatedAt
@ -156,8 +139,6 @@ public class UserJournalService(
RefAsync<int> totalNumber = 0;
var pageResult = await userJournalRepository.Queryable()
.Where(uj => uj.UserId == userId)
.WhereIF(input.JournalId.HasValue, uj => uj.JournalId == input.JournalId.Value)
.WhereIF(input.JournalInstanceId.HasValue, uj => uj.JournalInstanceId == input.JournalInstanceId.Value)
.WhereIF(!string.IsNullOrWhiteSpace(input.Type), uj => uj.Type == input.Type)
.OrderByDescending(uj => uj.CreatedAt)
.Select(uj => new BindJournalOutput
@ -165,7 +146,6 @@ public class UserJournalService(
Id = uj.Id,
UserId = uj.UserId,
JournalId = uj.JournalId,
JournalInstanceId = uj.JournalInstanceId,
Type = uj.Type,
Status = uj.Status,
CreatedAt = uj.CreatedAt