2026-06-04 15:54:30 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2026-07-01 14:50:37 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.RabbitMQ;
|
2026-06-04 15:54:30 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.IService;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Entity;
|
2026-06-08 16:57:44 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
2026-06-04 15:54:30 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Repository;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
namespace QYZH.InteractiveMagazine.Service;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用户期刊关联服务实现
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class UserJournalService(
|
|
|
|
|
|
BaseRepository<UserJournal> userJournalRepository,
|
|
|
|
|
|
BaseRepository<Users> usersRepository,
|
|
|
|
|
|
BaseRepository<Journal> journalRepository,
|
|
|
|
|
|
ILogger<UserJournalService> logger,
|
2026-07-01 14:50:37 +08:00
|
|
|
|
IRabbitMQService rabbitMqService,
|
2026-06-04 15:54:30 +08:00
|
|
|
|
IPetService petService)
|
|
|
|
|
|
: BaseRepository<UserJournal>, IUserJournalService
|
|
|
|
|
|
{
|
2026-07-01 14:50:37 +08:00
|
|
|
|
private const string JournalExchange = "ex.journal";
|
|
|
|
|
|
private const string BindJournalQueue = "mq.journal.bindUser";
|
|
|
|
|
|
private const string BindJournalRoutingKey = "rk.journal.bindUser";
|
|
|
|
|
|
|
2026-06-04 15:54:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用户绑定期刊(扫码绑定)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<BindJournalOutput> BindJournalAsync(long userId, BindJournalInput input)
|
|
|
|
|
|
{
|
2026-06-05 15:52:11 +08:00
|
|
|
|
logger.LogInformation("用户绑定期刊,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}, Type: {Type}",
|
|
|
|
|
|
userId, input.JournalId, input.Id, input.Type);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 校验参数
|
2026-06-05 15:52:11 +08:00
|
|
|
|
if (input.JournalId <= 0|| input.Id <= 0)
|
2026-06-04 15:54:30 +08:00
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("参数错误,未获取到期刊", ResultCode.BAD_REQUEST);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 校验用户是否存在
|
|
|
|
|
|
var user = await usersRepository.GetByIdAsync(userId);
|
|
|
|
|
|
if (user == null || user.IsDeleted)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("绑定期刊失败,用户不存在,UserId: {UserId}", userId);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验期刊是否存在
|
|
|
|
|
|
var journal = await journalRepository.GetByIdAsync(input.JournalId);
|
|
|
|
|
|
if (journal == null || journal.IsDeleted)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("绑定期刊失败,期刊不存在,JournalId: {JournalId}", input.JournalId);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("期刊不存在", ResultCode.NOT_FOUND);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验期刊状态
|
2026-06-08 17:54:36 +08:00
|
|
|
|
if (journal.Status != (int)JournalStatusEnum.Published)
|
2026-06-04 15:54:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("绑定期刊失败,期刊未发布,JournalId: {JournalId}, Status: {Status}", input.JournalId, journal.Status);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("该期刊暂未发布,无法绑定", ResultCode.UNPROCESSABLE_ENTITY);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 防重复绑定:同一用户 + 期刊 + 实例 + 类型
|
|
|
|
|
|
var isExist = userJournalRepository.Any(uj =>
|
2026-06-05 15:52:11 +08:00
|
|
|
|
uj.Id == input.Id);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
|
|
|
|
|
|
if (isExist)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("重复绑定期刊,UserId: {UserId}, JournalId: {JournalId}, Type: {Type}", userId, input.JournalId, input.Type);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("该期刊已被绑定", ResultCode.BAD_REQUEST);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否为首次绑定期刊(用于激活宠物)
|
|
|
|
|
|
var isFirstBind = !userJournalRepository.Context.Queryable<UserJournal>()
|
|
|
|
|
|
.Any(uj => uj.UserId == userId);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建绑定记录
|
|
|
|
|
|
var userJournal = new UserJournal
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
JournalId = input.JournalId,
|
2026-06-05 15:52:11 +08:00
|
|
|
|
Id = input.Id,
|
2026-06-08 16:57:44 +08:00
|
|
|
|
Type = Enum.Parse<UserJournalTypeEnum>(input.Type, true),
|
2026-06-08 17:54:36 +08:00
|
|
|
|
Status = (int)UserJournalStatusEnum.Active,
|
2026-06-04 15:54:30 +08:00
|
|
|
|
IsDeleted = false,
|
|
|
|
|
|
CreatedBy = userId.ToString(),
|
|
|
|
|
|
CreatedAt = DateTime.Now,
|
|
|
|
|
|
UpdatedBy = userId.ToString(),
|
|
|
|
|
|
UpdatedAt = DateTime.Now
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var result = await userJournalRepository.InsertAsync(userJournal);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("绑定期刊失败,写入数据库失败,UserId: {UserId}, JournalId: {JournalId}", userId, input.JournalId);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("绑定期刊失败,请稍后重试", ResultCode.GLOBAL_ERROR);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("用户绑定期刊成功,UserId: {UserId}, JournalId: {JournalId}, Id: {Id}", userId, input.JournalId, userJournal.Id);
|
2026-07-01 14:50:37 +08:00
|
|
|
|
await SendBindJournalMessageAsync(user, journal);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 首次绑定期刊时激活宠物
|
|
|
|
|
|
if (isFirstBind)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await petService.ActivatePetAsync(userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "首次绑定期刊激活宠物失败,UserId: {UserId}", userId);
|
|
|
|
|
|
// 宠物激活失败不阻断绑定流程
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new BindJournalOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = userJournal.Id,
|
|
|
|
|
|
UserId = userJournal.UserId,
|
|
|
|
|
|
JournalId = userJournal.JournalId,
|
2026-06-08 16:57:44 +08:00
|
|
|
|
Type = userJournal.Type.ToString(),
|
|
|
|
|
|
Status = userJournal.Status.ToString(),
|
2026-06-04 15:54:30 +08:00
|
|
|
|
CreatedAt = userJournal.CreatedAt
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-01 14:50:37 +08:00
|
|
|
|
private async Task SendBindJournalMessageAsync(Users user, Journal journal)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var messageSent = await rabbitMqService.SendAsync(new RabbitMQSendParam
|
|
|
|
|
|
{
|
|
|
|
|
|
Exchange = JournalExchange,
|
|
|
|
|
|
Queue = BindJournalQueue,
|
|
|
|
|
|
RoutingKey = BindJournalRoutingKey,
|
|
|
|
|
|
Data = new BindJournalMessage
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
|
JournalId = journal.Id,
|
|
|
|
|
|
StartTime = journal.StartTime,
|
|
|
|
|
|
EndTime = journal.EndTime,
|
|
|
|
|
|
UploadDomain = user.UploadDomain
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!messageSent)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("发送绑定期刊消息失败,UserId: {UserId}, JournalId: {JournalId}", user.Id, journal.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "发送绑定期刊消息异常,UserId: {UserId}, JournalId: {JournalId}", user.Id, journal.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-04 15:54:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取用户的期刊绑定列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<PageListModel<BindJournalOutput>> GetUserJournalsAsync(long userId, UserJournalQueryInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("查询用户期刊绑定列表,UserId: {UserId}, PageIndex: {PageIndex}, PageSize: {PageSize}",
|
|
|
|
|
|
userId, input.PageIndex, input.PageSize);
|
|
|
|
|
|
|
|
|
|
|
|
if (input.PageIndex <= 0)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("页码必须大于0", ResultCode.BAD_REQUEST);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (input.PageSize <= 0 || input.PageSize > 100)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("每页条数必须在1-100之间", ResultCode.BAD_REQUEST);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RefAsync<int> totalNumber = 0;
|
|
|
|
|
|
var pageResult = await userJournalRepository.Queryable()
|
|
|
|
|
|
.Where(uj => uj.UserId == userId)
|
2026-06-08 16:57:44 +08:00
|
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Type), uj => uj.Type.ToString() == input.Type)
|
2026-06-04 15:54:30 +08:00
|
|
|
|
.OrderByDescending(uj => uj.CreatedAt)
|
|
|
|
|
|
.Select(uj => new BindJournalOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = uj.Id,
|
|
|
|
|
|
UserId = uj.UserId,
|
|
|
|
|
|
JournalId = uj.JournalId,
|
2026-06-08 16:57:44 +08:00
|
|
|
|
Type = uj.Type.ToString(),
|
|
|
|
|
|
Status = uj.Status.ToString(),
|
2026-06-04 15:54:30 +08:00
|
|
|
|
CreatedAt = uj.CreatedAt
|
|
|
|
|
|
}, true)
|
|
|
|
|
|
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
|
|
|
|
|
|
|
|
|
|
|
return new PageListModel<BindJournalOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 取消期刊绑定
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task UnbindJournalAsync(long userId, long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("取消期刊绑定,UserId: {UserId}, Id: {Id}", userId, id);
|
|
|
|
|
|
|
|
|
|
|
|
var userJournal = await userJournalRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (userJournal == null || userJournal.IsDeleted)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("取消绑定失败,记录不存在,Id: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("绑定记录不存在", ResultCode.NOT_FOUND);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验归属权:只能取消自己的绑定
|
|
|
|
|
|
if (userJournal.UserId != userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("取消绑定失败,无权操作,UserId: {UserId}, RecordUserId: {RecordUserId}", userId, userJournal.UserId);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("无权取消该绑定", ResultCode.FORBIDDEN);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await userJournalRepository.DeleteByIdAsync(id);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("取消绑定失败,Id: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("取消绑定失败,请稍后重试", ResultCode.GLOBAL_ERROR);
|
2026-06-04 15:54:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("取消期刊绑定成功,UserId: {UserId}, Id: {Id}", userId, id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|