From 72c7a38f1d3b872b3105f2576242c0217f827fc7 Mon Sep 17 00:00:00 2001 From: glz <694770232@qq.com> Date: Thu, 2 Jul 2026 11:25:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(UserJournal):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=9C=9F=E5=88=8A=E7=BB=91=E5=AE=9A=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 重构用户查询逻辑,增加未删除过滤 2. 将枚举常量提取复用,统一状态判断 3. 新增数据库事务包裹绑定操作 4. 增加重复绑定同一期刊的校验逻辑 5. 统一绑定时间变量复用 6. 完善更新条件,增加期刊ID和状态校验 --- .../UserJournalService.cs | 61 +++++++++++++------ ...YZH.InteractiveMagazine.WorkService.csproj | 6 ++ .../dotnet-tools.json | 13 ++++ 3 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 QYZH.InteractiveMagazine.WorkService/dotnet-tools.json diff --git a/QYZH.InteractiveMagazine.Service/UserJournalService.cs b/QYZH.InteractiveMagazine.Service/UserJournalService.cs index e5af33c..74494e8 100644 --- a/QYZH.InteractiveMagazine.Service/UserJournalService.cs +++ b/QYZH.InteractiveMagazine.Service/UserJournalService.cs @@ -45,7 +45,12 @@ public class UserJournalService( 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) { logger.LogWarning("绑定期刊失败,用户不存在,UserId: {UserId}", userId); @@ -77,7 +82,7 @@ public class UserJournalService( throw new BusinessException("二维码不存在或已失效", ResultCode.NOT_FOUND); } - if (userJournal.Status != (int)UserJournalStatusEnum.Active) + if (userJournal.Status != activeStatus) { throw new BusinessException("二维码已失效", ResultCode.UNPROCESSABLE_ENTITY); } @@ -88,27 +93,47 @@ public class UserJournalService( throw new BusinessException("该期刊已被绑定", ResultCode.BAD_REQUEST); } - var isFirstBind = !userJournalRepository.Context.Queryable() - .Any(uj => uj.UserId == userId); - - // 创建绑定记录 - var updateCount = await userJournalRepository.Updateable() - .SetColumns(uj => uj.UserId == userId) - .SetColumns(uj => uj.UpdatedBy == userId.ToString()) - .SetColumns(uj => uj.UpdatedAt == DateTime.Now) - .Where(uj => uj.Id == input.Id && !uj.IsDeleted && (uj.UserId == null || uj.UserId == 0)) - .ExecuteCommandAsync(); - - if (updateCount <= 0) + var isFirstBind = false; + await userJournalRepository.UseTranAsync(async () => { - logger.LogError("绑定期刊失败,写入数据库失败,UserId: {UserId}, JournalId: {JournalId}", userId, input.JournalId); - throw new BusinessException("绑定期刊失败,请稍后重试", ResultCode.GLOBAL_ERROR); - } + 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() + .SetColumns(uj => uj.UserId == userId) + .SetColumns(uj => uj.UpdatedBy == userId.ToString()) + .SetColumns(uj => uj.UpdatedAt == boundAt) + .Where(uj => uj.Id == input.Id + && uj.JournalId == input.JournalId + && !uj.IsDeleted + && uj.Status == activeStatus + && (uj.UserId == null || uj.UserId == 0)) + .ExecuteCommandAsync(); + + if (updateCount <= 0) + { + logger.LogError("绑定期刊失败,写入数据库失败,UserId: {UserId}, JournalId: {JournalId}", userId, input.JournalId); + throw new BusinessException("绑定期刊失败,请稍后重试", ResultCode.GLOBAL_ERROR); + } + + }); logger.LogInformation("用户绑定期刊成功,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, userJournal.Id); userJournal.UserId = userId; userJournal.UpdatedBy = userId.ToString(); - userJournal.UpdatedAt = DateTime.Now; + userJournal.UpdatedAt = boundAt; await SendBindJournalMessageAsync(user, journal); diff --git a/QYZH.InteractiveMagazine.WorkService/QYZH.InteractiveMagazine.WorkService.csproj b/QYZH.InteractiveMagazine.WorkService/QYZH.InteractiveMagazine.WorkService.csproj index b931f2a..d427715 100644 --- a/QYZH.InteractiveMagazine.WorkService/QYZH.InteractiveMagazine.WorkService.csproj +++ b/QYZH.InteractiveMagazine.WorkService/QYZH.InteractiveMagazine.WorkService.csproj @@ -22,4 +22,10 @@ + + + Always + + + diff --git a/QYZH.InteractiveMagazine.WorkService/dotnet-tools.json b/QYZH.InteractiveMagazine.WorkService/dotnet-tools.json new file mode 100644 index 0000000..807729e --- /dev/null +++ b/QYZH.InteractiveMagazine.WorkService/dotnet-tools.json @@ -0,0 +1,13 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "10.0.9", + "commands": [ + "dotnet-ef" + ], + "rollForward": false + } + } +} \ No newline at end of file