refactor(UserJournal): 优化期刊绑定逻辑并完善校验

1. 重构用户查询逻辑,增加未删除过滤
2. 将枚举常量提取复用,统一状态判断
3. 新增数据库事务包裹绑定操作
4. 增加重复绑定同一期刊的校验逻辑
5. 统一绑定时间变量复用
6. 完善更新条件,增加期刊ID和状态校验
This commit is contained in:
glz
2026-07-02 11:25:24 +08:00
parent 5700da58d9
commit 72c7a38f1d
3 changed files with 62 additions and 18 deletions

View File

@ -45,7 +45,12 @@ public class UserJournalService(
throw new BusinessException("参数错误,未获取到期刊", ResultCode.BAD_REQUEST); throw new BusinessException("参数错误,未获取到期刊", ResultCode.BAD_REQUEST);
} }
// 校验用户是否存在 // 校验用户是否存在
var user = await usersRepository.GetByIdAsync(userId); var activeStatus = (int)UserJournalStatusEnum.Active;
var boundAt = DateTime.Now;
var user = await usersRepository.Queryable()
.Where(u => u.Id == userId && !u.IsDeleted)
.FirstAsync();
if (user == null || user.IsDeleted) if (user == null || user.IsDeleted)
{ {
logger.LogWarning("绑定期刊失败用户不存在UserId: {UserId}", userId); logger.LogWarning("绑定期刊失败用户不存在UserId: {UserId}", userId);
@ -77,7 +82,7 @@ public class UserJournalService(
throw new BusinessException("二维码不存在或已失效", ResultCode.NOT_FOUND); throw new BusinessException("二维码不存在或已失效", ResultCode.NOT_FOUND);
} }
if (userJournal.Status != (int)UserJournalStatusEnum.Active) if (userJournal.Status != activeStatus)
{ {
throw new BusinessException("二维码已失效", ResultCode.UNPROCESSABLE_ENTITY); throw new BusinessException("二维码已失效", ResultCode.UNPROCESSABLE_ENTITY);
} }
@ -88,15 +93,33 @@ public class UserJournalService(
throw new BusinessException("该期刊已被绑定", ResultCode.BAD_REQUEST); throw new BusinessException("该期刊已被绑定", ResultCode.BAD_REQUEST);
} }
var isFirstBind = !userJournalRepository.Context.Queryable<UserJournal>() var isFirstBind = false;
.Any(uj => uj.UserId == userId); await userJournalRepository.UseTranAsync(async () =>
{
var boundJournalIds = await userJournalRepository.Queryable()
.Where(uj => uj.UserId == userId && !uj.IsDeleted && uj.Status == activeStatus)
.Select(uj => uj.JournalId)
.ToListAsync();
if (boundJournalIds.Contains(input.JournalId))
{
logger.LogWarning("用户重复绑定同一期刊UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, input.Id);
throw new BusinessException("该用户已绑定过该期刊", ResultCode.BAD_REQUEST);
}
isFirstBind = boundJournalIds.Count == 0;
boundAt = DateTime.Now;
// 创建绑定记录 // 创建绑定记录
var updateCount = await userJournalRepository.Updateable() var updateCount = await userJournalRepository.Updateable()
.SetColumns(uj => uj.UserId == userId) .SetColumns(uj => uj.UserId == userId)
.SetColumns(uj => uj.UpdatedBy == userId.ToString()) .SetColumns(uj => uj.UpdatedBy == userId.ToString())
.SetColumns(uj => uj.UpdatedAt == DateTime.Now) .SetColumns(uj => uj.UpdatedAt == boundAt)
.Where(uj => uj.Id == input.Id && !uj.IsDeleted && (uj.UserId == null || uj.UserId == 0)) .Where(uj => uj.Id == input.Id
&& uj.JournalId == input.JournalId
&& !uj.IsDeleted
&& uj.Status == activeStatus
&& (uj.UserId == null || uj.UserId == 0))
.ExecuteCommandAsync(); .ExecuteCommandAsync();
if (updateCount <= 0) if (updateCount <= 0)
@ -105,10 +128,12 @@ public class UserJournalService(
throw new BusinessException("绑定期刊失败,请稍后重试", ResultCode.GLOBAL_ERROR); throw new BusinessException("绑定期刊失败,请稍后重试", ResultCode.GLOBAL_ERROR);
} }
});
logger.LogInformation("用户绑定期刊成功UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, userJournal.Id); logger.LogInformation("用户绑定期刊成功UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, userJournal.Id);
userJournal.UserId = userId; userJournal.UserId = userId;
userJournal.UpdatedBy = userId.ToString(); userJournal.UpdatedBy = userId.ToString();
userJournal.UpdatedAt = DateTime.Now; userJournal.UpdatedAt = boundAt;
await SendBindJournalMessageAsync(user, journal); await SendBindJournalMessageAsync(user, journal);

View File

@ -22,4 +22,10 @@
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" /> <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="Dockerfile">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "10.0.9",
"commands": [
"dotnet-ef"
],
"rollForward": false
}
}
}