2024-08-12 17:16:30 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
|
|
|
|
|
using Volo.Abp.EventBus.Local;
|
|
|
|
|
|
using Volo.Abp.Users;
|
2024-08-11 23:23:38 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Assignment;
|
2024-08-12 17:16:30 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Managers.AssignmentProviders;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2024-08-11 23:23:38 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.Bbs.Domain.Managers;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 任务领域,任务相关核心逻辑
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AssignmentManager : DomainService
|
|
|
|
|
|
{
|
2024-08-12 17:16:30 +08:00
|
|
|
|
private readonly IEnumerable<IAssignmentProvider> _assignmentProviders;
|
2024-08-12 23:23:29 +08:00
|
|
|
|
public readonly ISqlSugarRepository<AssignmentAggregateRoot> _assignmentRepository;
|
|
|
|
|
|
public readonly ISqlSugarRepository<AssignmentDefineAggregateRoot> _assignmentDefineRepository;
|
2024-08-12 17:16:30 +08:00
|
|
|
|
private readonly ILocalEventBus _localEventBus;
|
|
|
|
|
|
|
|
|
|
|
|
public AssignmentManager(IEnumerable<IAssignmentProvider> assignmentProviders,
|
|
|
|
|
|
ISqlSugarRepository<AssignmentAggregateRoot> assignmentRepository,
|
|
|
|
|
|
ISqlSugarRepository<AssignmentDefineAggregateRoot> assignmentDefineRepository, ILocalEventBus localEventBus)
|
|
|
|
|
|
{
|
|
|
|
|
|
this._assignmentProviders = assignmentProviders;
|
|
|
|
|
|
_assignmentRepository = assignmentRepository;
|
|
|
|
|
|
_assignmentDefineRepository = assignmentDefineRepository;
|
|
|
|
|
|
_localEventBus = localEventBus;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-11 23:23:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接受任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId">领取用户</param>
|
|
|
|
|
|
/// <param name="asignmentDefineId">任务定义id</param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-08-12 17:16:30 +08:00
|
|
|
|
public async Task AcceptAsync(Guid userId, Guid asignmentDefineId)
|
2024-08-11 23:23:38 +08:00
|
|
|
|
{
|
2024-08-12 17:16:30 +08:00
|
|
|
|
var canReceiveList = await GetCanReceiveListAsync(userId);
|
|
|
|
|
|
|
|
|
|
|
|
//如果要接收的任务id在可领取的任务列表中,就可以接收任务
|
|
|
|
|
|
if (canReceiveList.Select(x => x.Id).Contains(asignmentDefineId))
|
|
|
|
|
|
{
|
|
|
|
|
|
var assignmentDefine = await _assignmentDefineRepository.GetByIdAsync(asignmentDefineId);
|
|
|
|
|
|
|
|
|
|
|
|
var entity = new AssignmentAggregateRoot();
|
|
|
|
|
|
entity.AssignmentDefineId = asignmentDefineId;
|
|
|
|
|
|
entity.UserId = userId;
|
|
|
|
|
|
entity.AssignmentState = AssignmentStateEnum.Progress;
|
|
|
|
|
|
entity.CurrentStepNumber = 0;
|
|
|
|
|
|
entity.TotalStepNumber = assignmentDefine.TotalStepNumber;
|
|
|
|
|
|
entity.RewardsMoneyNumber = assignmentDefine.RewardsMoneyNumber;
|
2024-08-13 16:45:56 +08:00
|
|
|
|
entity.AssignmentRequirementType = assignmentDefine.AssignmentRequirementType;
|
2024-08-12 17:16:30 +08:00
|
|
|
|
entity.ExpireTime = assignmentDefine.AssignmentType.GetExpireTime();
|
|
|
|
|
|
await _assignmentRepository.InsertAsync(entity);
|
|
|
|
|
|
}
|
2024-08-11 23:23:38 +08:00
|
|
|
|
}
|
2024-08-12 17:16:30 +08:00
|
|
|
|
|
2024-08-11 23:23:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 领取任务奖励
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="asignmentId">任务id</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2024-08-12 17:16:30 +08:00
|
|
|
|
public async Task ReceiveRewardsAsync(Guid asignmentId)
|
2024-08-11 23:23:38 +08:00
|
|
|
|
{
|
2024-08-12 17:16:30 +08:00
|
|
|
|
var assignment = await _assignmentRepository.GetByIdAsync(asignmentId);
|
|
|
|
|
|
if (assignment.IsAllowCompleted())
|
|
|
|
|
|
{
|
|
|
|
|
|
//加钱加钱
|
|
|
|
|
|
await _localEventBus.PublishAsync(
|
|
|
|
|
|
new MoneyChangeEventArgs { UserId = assignment.UserId, Number = assignment.RewardsMoneyNumber }, false);
|
2024-08-13 16:45:56 +08:00
|
|
|
|
|
2024-08-13 15:33:03 +08:00
|
|
|
|
//设置已完成,并领取奖励,钱钱
|
2024-08-14 18:31:37 +08:00
|
|
|
|
assignment.SetEnd();
|
2024-08-13 15:33:03 +08:00
|
|
|
|
await _assignmentRepository.UpdateAsync(assignment);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//不能领取
|
2024-08-13 16:45:56 +08:00
|
|
|
|
throw new UserFriendlyException("该任务没有满足领取条件,请检查任务详情");
|
2024-08-12 17:16:30 +08:00
|
|
|
|
}
|
2024-08-11 23:23:38 +08:00
|
|
|
|
}
|
2024-08-12 17:16:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-08-11 23:23:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据用户id获取能够领取的任务列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId">用户id</param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-08-12 17:16:30 +08:00
|
|
|
|
public async Task<List<AssignmentDefineAggregateRoot>> GetCanReceiveListAsync(Guid userId)
|
2024-08-11 23:23:38 +08:00
|
|
|
|
{
|
2024-08-12 17:16:30 +08:00
|
|
|
|
var context = await GetAssignmentContext(userId);
|
|
|
|
|
|
var output = new List<AssignmentDefineAggregateRoot>();
|
|
|
|
|
|
foreach (var assignmentProvider in _assignmentProviders)
|
|
|
|
|
|
{
|
|
|
|
|
|
output.AddRange(await assignmentProvider.GetCanReceiveListAsync(context));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-13 16:45:56 +08:00
|
|
|
|
output = output.DistinctBy(x => x.Id).OrderBy(x => x.OrderNum).ToList();
|
2024-08-12 23:23:29 +08:00
|
|
|
|
return output;
|
2024-08-11 23:23:38 +08:00
|
|
|
|
}
|
2024-08-12 17:16:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取任务的上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private async Task<AssignmentContext> GetAssignmentContext(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var allAssignmentDefine = await _assignmentDefineRepository.GetListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var currentUserAssignment = await _assignmentRepository.GetListAsync(x => x.UserId == userId);
|
|
|
|
|
|
|
|
|
|
|
|
var context = new AssignmentContext(userId, allAssignmentDefine, currentUserAssignment);
|
|
|
|
|
|
return context;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 过期超时的任务,定时任务去处理即可
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task ExpireTimeoutAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var progressEntities = await _assignmentRepository._DbQueryable
|
|
|
|
|
|
.Where(x => x.AssignmentState == AssignmentStateEnum.Progress)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var needUpdateEntities = new List<AssignmentAggregateRoot>();
|
|
|
|
|
|
foreach (var progressEntity in progressEntities)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (progressEntity.TrySetExpire())
|
|
|
|
|
|
{
|
|
|
|
|
|
needUpdateEntities.Add(progressEntity);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (needUpdateEntities.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
await _assignmentRepository._Db.Updateable(needUpdateEntities).ExecuteCommandAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-11 23:23:38 +08:00
|
|
|
|
}
|