fix: 修复定时任务

This commit is contained in:
橙子
2024-11-02 13:31:18 +08:00
parent 31dceec787
commit 76d94c0bc9
8 changed files with 188 additions and 22 deletions

View File

@@ -27,7 +27,7 @@ public class AccessLogCacheJob : QuartzBackgroundWorkerBase
//每10秒执行一次将本地缓存转入redis防止丢数据 //每10秒执行一次将本地缓存转入redis防止丢数据
Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogCacheJob)) Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogCacheJob))
.WithSimpleSchedule((schedule) => { schedule.WithInterval(TimeSpan.FromSeconds(10)).RepeatForever();; }) .WithSimpleSchedule((schedule) => { schedule.WithInterval(TimeSpan.FromSeconds(10)).RepeatForever(); })
.Build(); .Build();
} }

View File

@@ -1,4 +1,5 @@
using Quartz; using Microsoft.Extensions.Logging;
using Quartz;
using Volo.Abp.BackgroundWorkers.Quartz; using Volo.Abp.BackgroundWorkers.Quartz;
using Yi.Framework.DigitalCollectibles.Domain.Entities; using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.DigitalCollectibles.Domain.Managers; using Yi.Framework.DigitalCollectibles.Domain.Managers;
@@ -12,15 +13,18 @@ namespace Yi.Framework.DigitalCollectibles.Application.Jobs;
public class OnHookAutoMiningJob : QuartzBackgroundWorkerBase public class OnHookAutoMiningJob : QuartzBackgroundWorkerBase
{ {
private readonly MiningPoolManager _miningPoolManager; private readonly MiningPoolManager _miningPoolManager;
private readonly ILogger<OnHookAutoMiningJob> _logger;
public OnHookAutoMiningJob(MiningPoolManager miningPoolManager) public OnHookAutoMiningJob(MiningPoolManager miningPoolManager, ILogger<OnHookAutoMiningJob> logger)
{ {
_miningPoolManager = miningPoolManager; _miningPoolManager = miningPoolManager;
JobDetail = JobBuilder.Create<AutoRefreshMiningPoolJob>().WithIdentity(nameof(AutoRefreshMiningPoolJob)) _logger = logger;
JobDetail = JobBuilder.Create<OnHookAutoMiningJob>().WithIdentity(nameof(OnHookAutoMiningJob))
.Build(); .Build();
//每小时执行一次 //每小时执行一次
Trigger = TriggerBuilder.Create().WithIdentity(nameof(AutoRefreshMiningPoolJob)) Trigger = TriggerBuilder.Create().WithIdentity(nameof(OnHookAutoMiningJob))
// .WithCronSchedule("10 * * * * ?")
.WithCronSchedule("0 0 * * * ?") .WithCronSchedule("0 0 * * * ?")
.Build(); .Build();
} }

View File

@@ -99,7 +99,7 @@ public class CollectiblesService : ApplicationService
input.StartTime is not null && input.EndTime is not null, input.StartTime is not null && input.EndTime is not null,
u => u.CreationTime >= input.StartTime && u.CreationTime <= input.EndTime) u => u.CreationTime >= input.StartTime && u.CreationTime <= input.EndTime)
.LeftJoin<CollectiblesAggregateRoot>((u, c) => u.CollectiblesId == c.Id) .LeftJoin<CollectiblesAggregateRoot>((u, c) => u.CollectiblesId == c.Id)
.OrderBy((u, c) => c.OrderNum) .OrderBy((u, c) => c.CreationTime)
.GroupBy((u, c) => u.CollectiblesId) .GroupBy((u, c) => u.CollectiblesId)
.Select((u, c) => .Select((u, c) =>
new CollectiblesUserGetOutputDto new CollectiblesUserGetOutputDto

View File

@@ -0,0 +1,89 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.DigitalCollectibles.Application.Services;
/// <summary>
/// 邀请码应用服务
/// </summary>
public class InvitationCodeService : ApplicationService
{
private readonly ISqlSugarRepository<InvitationCodeAggregateRoot> _repository;
public InvitationCodeService(ISqlSugarRepository<InvitationCodeAggregateRoot> repository)
{
_repository = repository;
}
/// <summary>
/// 查询当前登录用户的邀请码数据
/// </summary>
/// <returns></returns>
[Authorize]
public async Task<object> GetAsync()
{
var userId = CurrentUser.GetId();
var entity = await _repository.GetFirstAsync(x => x.UserId == userId);
if (entity is null)
{
return new { IsInvited=false, PointsNumber=0 };
}
return new { entity.IsInvited, entity.PointsNumber };
}
/// <summary>
/// 当前用户填写邀请码
/// </summary>
/// <param name="invitedUserId"></param>
/// <exception cref="UserFriendlyException"></exception>
[Authorize]
public async Task SetAsync([FromQuery] Guid invitedUserId)
{
var userId = CurrentUser.GetId();
var entity = await _repository.GetFirstAsync(x => x.UserId == userId);
if (entity is null)
{
await _repository.InsertAsync(new InvitationCodeAggregateRoot
{
UserId = userId,
IsInvited = true,
PointsNumber = 0
});
}
else
{
if (entity.IsInvited)
{
throw new UserFriendlyException("你已填写过邀请码,无法再次填写");
}
else
{
entity.IsInvited = false;
await _repository.UpdateAsync(entity);
}
}
var invitedEntity = await _repository.GetFirstAsync(x => x.UserId == invitedUserId);
if (entity is null)
{
await _repository.InsertAsync(new InvitationCodeAggregateRoot
{
UserId = invitedUserId,
IsInvited = false,
PointsNumber = 1
});
}
else
{
invitedEntity.PointsNumber += 1;
await _repository.UpdateAsync(invitedEntity);
}
}
}

View File

@@ -36,7 +36,6 @@ public class CollectiblesRecordService : ApplicationService
var output = await _miningPoolRecordRepository._DbQueryable.WhereIF(input.StartTime is not null && input.EndTime is not null, var output = await _miningPoolRecordRepository._DbQueryable.WhereIF(input.StartTime is not null && input.EndTime is not null,
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime) x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
.Where(x => x.UserId == userId) .Where(x => x.UserId == userId)
.OrderByDescending(x => x.CreationTime)
.LeftJoin<CollectiblesAggregateRoot>((x, c) => x.CollectiblesId==c.Id) .LeftJoin<CollectiblesAggregateRoot>((x, c) => x.CollectiblesId==c.Id)
.Select((x, c) => .Select((x, c) =>
new MiningPoolRecordDto new MiningPoolRecordDto
@@ -58,6 +57,7 @@ public class CollectiblesRecordService : ApplicationService
} }
) )
.OrderByDescending(x => x.CreationTime)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<MiningPoolRecordDto>(total,output); return new PagedResultDto<MiningPoolRecordDto>(total,output);
@@ -78,7 +78,7 @@ public class CollectiblesRecordService : ApplicationService
//交易:是购买和出售,都需要展示 //交易:是购买和出售,都需要展示
.Where(x => x.SellUserId == userId||x.BuyId ==userId) .Where(x => x.SellUserId == userId||x.BuyId ==userId)
.OrderByDescending(x => x.CreationTime)
.LeftJoin<CollectiblesAggregateRoot>((x, c) => x.CollectiblesId==c.Id) .LeftJoin<CollectiblesAggregateRoot>((x, c) => x.CollectiblesId==c.Id)
.Select((x, c) => .Select((x, c) =>
new MarketRecordDto new MarketRecordDto
@@ -104,6 +104,7 @@ public class CollectiblesRecordService : ApplicationService
}, },
} }
) )
.OrderByDescending(x => x.CreationTime)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
foreach (var dto in output) foreach (var dto in output)

View File

@@ -1,6 +1,9 @@
using System.IO.Compression; using System.IO.Compression;
using FreeRedis;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Yi.Framework.DigitalCollectibles.Domain.Entities; using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.DigitalCollectibles.Domain.Entities.Record;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts; using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -9,10 +12,15 @@ namespace Yi.Framework.DigitalCollectibles.Application.Services.Tool;
public class CollectiblesToolService : ApplicationService public class CollectiblesToolService : ApplicationService
{ {
private ISqlSugarRepository<CollectiblesAggregateRoot> _repository; private ISqlSugarRepository<CollectiblesAggregateRoot> _repository;
private ISqlSugarRepository<MiningPoolRecordAggregateRoot> _recordRepository;
public CollectiblesToolService(ISqlSugarRepository<CollectiblesAggregateRoot> repository) private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
public CollectiblesToolService(ISqlSugarRepository<CollectiblesAggregateRoot> repository,
ISqlSugarRepository<MiningPoolRecordAggregateRoot> recordRepository)
{ {
_repository = repository; _repository = repository;
_recordRepository = recordRepository;
} }
public async Task<object> DeleteInitAsync() public async Task<object> DeleteInitAsync()
@@ -49,6 +57,7 @@ public class CollectiblesToolService : ApplicationService
{ {
continue; continue;
} }
var items = line.Split(",").ToList(); var items = line.Split(",").ToList();
@@ -83,8 +92,6 @@ public class CollectiblesToolService : ApplicationService
{ {
errData.Add($"文件不存在:{Path.Combine(directoryPath, fileName)}"); errData.Add($"文件不存在:{Path.Combine(directoryPath, fileName)}");
} }
} }
if (errData.Any()) if (errData.Any())
@@ -104,4 +111,41 @@ public class CollectiblesToolService : ApplicationService
await _repository.InsertRangeAsync(entities); await _repository.InsertRangeAsync(entities);
return new { ok = true, entities }; return new { ok = true, entities };
} }
/// <summary>
/// 跑马灯内容
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetNoticeAsync()
{
//获取最新的3条挖矿记录,高级
var record = await _recordRepository._DbQueryable
.LeftJoin<CollectiblesAggregateRoot>((x, c) => x.CollectiblesId == c.Id)
.Where((x,c)=>c.Rarity>=RarityEnum.Senior)
.OrderByDescending((x, c) => x.CreationTime)
.Select((x, c) => new
{
c.Name,
c.ValueNumber,
c.Rarity
})
.ToPageListAsync(1, 3);
var output = new List<string>();
var firstNoticeOrNull = await RedisClient.GetAsync("Yi:Notice");
if (firstNoticeOrNull is not null)
{
output.Add(firstNoticeOrNull);
}
foreach (var item in record)
{
output.Add($"恭喜神秘用户,挖到【{item.Rarity.GetRarityName()}-{item.Name}】,价值【{(int)(item.ValueNumber)}】");
}
return output;
}
} }

View File

@@ -18,6 +18,23 @@ public enum RarityEnum
public static class RarityEnumExtensions public static class RarityEnumExtensions
{ {
public static string GetRarityName(this RarityEnum enumValue)
{
// 获取枚举类型
Type type = enumValue.GetType();
// 获取当前枚举值的 FieldInfo
FieldInfo fieldInfo = type.GetField(enumValue.ToString());
// 获取 Display 特性
DisplayAttribute displayAttribute = fieldInfo
.GetCustomAttributes(typeof(DisplayAttribute), false)
.FirstOrDefault() as DisplayAttribute;
// 返回名称,如果没有找到,则返回枚举值的名称
return displayAttribute?.Name ?? enumValue.ToString();
}
private static T GetAttribute<T>(RarityEnum rarity) where T : Attribute private static T GetAttribute<T>(RarityEnum rarity) where T : Attribute
{ {
var fieldInfo = typeof(RarityEnum).GetField(rarity.ToString()); var fieldInfo = typeof(RarityEnum).GetField(rarity.ToString());

View File

@@ -1,5 +1,6 @@
using FreeRedis; using FreeRedis;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local; using Volo.Abp.EventBus.Local;
@@ -27,6 +28,7 @@ public class MiningPoolManager : DomainService
private readonly IDistributedCache<MiningPoolContent?> _miningPoolCache; private readonly IDistributedCache<MiningPoolContent?> _miningPoolCache;
private readonly IDistributedCache<UserMiningLimitCacheDto?> _userMiningLimitCache; private readonly IDistributedCache<UserMiningLimitCacheDto?> _userMiningLimitCache;
private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _userStoreRepository; private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _userStoreRepository;
private readonly ILogger<MiningPoolManager> _logger;
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>(); private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
private ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>(); private ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();
@@ -34,7 +36,7 @@ public class MiningPoolManager : DomainService
ISqlSugarRepository<CollectiblesAggregateRoot> collectiblesRepository, ISqlSugarRepository<CollectiblesAggregateRoot> collectiblesRepository,
ISqlSugarRepository<OnHookAggregateRoot> onHookRepository, ISqlSugarRepository<OnHookAggregateRoot> onHookRepository,
ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> userStoreRepository, ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> userStoreRepository,
IDistributedCache<UserMiningLimitCacheDto> userMiningLimitCache) IDistributedCache<UserMiningLimitCacheDto> userMiningLimitCache, ILogger<MiningPoolManager> logger)
{ {
_settingProvider = settingProvider; _settingProvider = settingProvider;
this._miningPoolCache = miningPoolCache; this._miningPoolCache = miningPoolCache;
@@ -42,6 +44,7 @@ public class MiningPoolManager : DomainService
_onHookRepository = onHookRepository; _onHookRepository = onHookRepository;
_userStoreRepository = userStoreRepository; _userStoreRepository = userStoreRepository;
_userMiningLimitCache = userMiningLimitCache; _userMiningLimitCache = userMiningLimitCache;
_logger = logger;
} }
/// <summary> /// <summary>
@@ -269,9 +272,17 @@ public class MiningPoolManager : DomainService
//根据用户对挂机卡hash关系 //根据用户对挂机卡hash关系
var userOnHookDic = currentOnHook.GroupBy(x => x.UserId).ToDictionary(x => x.Key, y => y.First()); var userOnHookDic = currentOnHook.GroupBy(x => x.UserId).ToDictionary(x => x.Key, y => y.First());
foreach (var onHookItem in userOnHookDic) foreach (var onHookItem in userOnHookDic)
{
try
{ {
await MiningAsync(onHookItem.Value.UserId); await MiningAsync(onHookItem.Value.UserId);
} }
catch (UserFriendlyException e)
{
_logger.LogInformation($"自动挖矿-{onHookItem.Value.UserId},{e.Message}");
}
}
} }
private int GetRandomIndex(decimal[] probabilities) private int GetRandomIndex(decimal[] probabilities)