refactor(user-journal): 重构用户期刊关联逻辑,移除枚举依赖并新增二维码功能
1. 删除不再使用的UserJournalTypeEnum枚举 2. 将UserJournal实体的Type字段改为int类型并移除枚举依赖 3. 移除BindJournal相关接口的类型参数校验和赋值逻辑 4. 新增二维码生成和上传功能,为用户期刊绑定QrCodeUrl字段 5. 修复UserAnswerTaskController的用户ID获取逻辑 6. 调整UserJournalQrCodeOutput的字段映射,替换QrCodeContent为QrCodeUrl
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QYZH.InteractiveMagazine.Common.Helpers;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.OSS;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.RabbitMQ;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
@ -20,6 +22,7 @@ public class UserJournalService(
|
||||
BaseRepository<Journal> journalRepository,
|
||||
ILogger<UserJournalService> logger,
|
||||
IRabbitMQService rabbitMqService,
|
||||
OssService ossService,
|
||||
IPetService petService)
|
||||
: BaseRepository<UserJournal>, IUserJournalService
|
||||
{
|
||||
@ -32,8 +35,6 @@ public class UserJournalService(
|
||||
/// </summary>
|
||||
public async Task<BindJournalOutput> BindJournalAsync(long userId, BindJournalInput input)
|
||||
{
|
||||
logger.LogInformation("用户绑定期刊,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}, Type: {Type}",
|
||||
userId, input.JournalId, input.Id, input.Type);
|
||||
|
||||
// 校验参数
|
||||
if (input.JournalId <= 0|| input.Id <= 0)
|
||||
@ -63,17 +64,10 @@ public class UserJournalService(
|
||||
throw new BusinessException("该期刊暂未发布,无法绑定", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
// 防重复绑定:同一用户 + 期刊 + 实例 + 类型
|
||||
if (!Enum.TryParse<UserJournalTypeEnum>(input.Type, true, out var bindType))
|
||||
{
|
||||
throw new BusinessException("关联类型不正确", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var userJournal = await userJournalRepository.Queryable()
|
||||
.Where(uj => uj.Id == input.Id && uj.JournalId == input.JournalId && !uj.IsDeleted)
|
||||
.FirstAsync();
|
||||
|
||||
// 检查是否为首次绑定期刊(用于激活宠物)
|
||||
if (userJournal == null)
|
||||
{
|
||||
logger.LogWarning("绑定期刊失败,二维码记录不存在,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, input.Id);
|
||||
@ -97,7 +91,6 @@ public class UserJournalService(
|
||||
// 创建绑定记录
|
||||
var updateCount = await userJournalRepository.Updateable()
|
||||
.SetColumns(uj => uj.UserId == userId)
|
||||
.SetColumns(uj => uj.Type == bindType)
|
||||
.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))
|
||||
@ -111,7 +104,6 @@ public class UserJournalService(
|
||||
|
||||
logger.LogInformation("用户绑定期刊成功,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, userJournal.Id);
|
||||
userJournal.UserId = userId;
|
||||
userJournal.Type = bindType;
|
||||
userJournal.UpdatedBy = userId.ToString();
|
||||
userJournal.UpdatedAt = DateTime.Now;
|
||||
|
||||
@ -152,11 +144,6 @@ public class UserJournalService(
|
||||
throw new BusinessException("期刊Id不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (!Enum.TryParse<UserJournalTypeEnum>(input.Type, true, out var type))
|
||||
{
|
||||
throw new BusinessException("关联类型不正确", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var journal = await journalRepository.GetByIdAsync(input.JournalId);
|
||||
if (journal == null || journal.IsDeleted)
|
||||
{
|
||||
@ -172,7 +159,7 @@ public class UserJournalService(
|
||||
{
|
||||
UserId = null,
|
||||
JournalId = input.JournalId,
|
||||
Type = type,
|
||||
Type = 0,
|
||||
Status = (int)UserJournalStatusEnum.Active,
|
||||
IsDeleted = false,
|
||||
CreatedBy = operatorId.ToString(),
|
||||
@ -181,6 +168,17 @@ public class UserJournalService(
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
|
||||
var qrCodeContent = BuildQrCodeContent(record.JournalId, record.Id);
|
||||
var qrCodeKey = $"journal/qrcode/{record.JournalId}/{record.Id}.png";
|
||||
using var qrCodeStream = new MemoryStream(QrCodeHelper.GeneratePng(qrCodeContent));
|
||||
var uploadedKey = ossService.PutObject(qrCodeKey, qrCodeStream);
|
||||
if (string.IsNullOrWhiteSpace(uploadedKey))
|
||||
{
|
||||
throw new BusinessException("二维码图片上传失败,请稍后重试", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
record.QrCodeUrl = uploadedKey;
|
||||
|
||||
var result = await userJournalRepository.InsertAsync(record);
|
||||
if (!result)
|
||||
{
|
||||
@ -322,7 +320,7 @@ public class UserJournalService(
|
||||
Type = record.Type.ToString(),
|
||||
Status = record.Status.ToString(),
|
||||
IsBound = record.UserId.HasValue && record.UserId.Value > 0,
|
||||
QrCodeContent = BuildQrCodeContent(record.JournalId, record.Id),
|
||||
QrCodeUrl = DomainHelper.OssFullUrl(record.QrCodeUrl ?? string.Empty),
|
||||
CreatedAt = record.CreatedAt,
|
||||
BoundAt = record.UserId.HasValue && record.UserId.Value > 0 ? record.UpdatedAt : null
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user