This commit is contained in:
zpc 2026-02-05 20:11:31 +08:00
parent 1f6d996d85
commit d339b42b66
2 changed files with 46 additions and 19 deletions

View File

@ -165,7 +165,7 @@ public class TaskService : ITaskService
currentOuQi = user.OuQi ?? 0;
// 重新计算欧气等级
user.OuQiLevel = CalculateOuQiLevel(currentOuQi);
user.OuQiLevel = await CalculateOuQiLevelAsync(currentOuQi);
user.UpdatedAt = now;
// 记录欧气值变动
@ -266,8 +266,27 @@ public class TaskService : ITaskService
/// </summary>
/// <param name="ouQi">当前欧气值</param>
/// <returns>欧气等级</returns>
private static int CalculateOuQiLevel(int ouQi)
private async Task<int> CalculateOuQiLevelAsync(int ouQi)
{
// 从数据库读取等级配置
var equityLevels = await _dbContext.EquityLevels
.Where(e => e.DeletedAt == null)
.OrderByDescending(e => e.Number)
.ToListAsync();
if (equityLevels.Any())
{
foreach (var level in equityLevels)
{
if (ouQi >= level.Number)
{
return level.Level;
}
}
return 0;
}
// 如果数据库没有配置,使用默认值
// 等级阈值: 0=普通, 1=青铜(100), 2=白银(500), 3=黄金(1000),
// 4=铂金(3000), 5=钻石(6000), 6=星耀(10000), 7=王者(20000)
if (ouQi >= 20000) return 7;

View File

@ -169,29 +169,37 @@ public class UserService : BaseService<User, int>, IUserService
/// <param name="ouQi">用户欧气值</param>
/// <param name="ouQiLevel">用户欧气等级</param>
/// <returns>权益等级DTO</returns>
private Task<QuanYiLevelDto> CalculateQuanYiLevelAsync(int ouQi, int ouQiLevel)
private async Task<QuanYiLevelDto> CalculateQuanYiLevelAsync(int ouQi, int ouQiLevel)
{
// 等级阈值配置
var levelThresholds = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 100 },
{ 2, 500 },
{ 3, 1000 },
{ 4, 3000 },
{ 5, 6000 },
{ 6, 10000 },
{ 7, 20000 }
};
// 从数据库读取等级配置
var equityLevels = await _dbContext.EquityLevels
.Where(e => e.DeletedAt == null)
.OrderBy(e => e.Level)
.ToListAsync();
var levelThresholds = equityLevels.Any()
? equityLevels.ToDictionary(e => e.Level, e => e.Number)
: new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 100 },
{ 2, 500 },
{ 3, 1000 },
{ 4, 3000 },
{ 5, 6000 },
{ 6, 10000 },
{ 7, 20000 }
};
var maxLevel = levelThresholds.Keys.DefaultIfEmpty(0).Max();
var nextLevel = ouQiLevel + 1;
var currentLevelOuQi = levelThresholds.GetValueOrDefault(ouQiLevel, 0);
var nextOuQi = levelThresholds.GetValueOrDefault(nextLevel, 99999);
var nextOuQi = levelThresholds.GetValueOrDefault(nextLevel, 0);
// 计算差值和进度
int cha;
int jindu;
if (nextLevel > 7)
if (nextLevel > maxLevel || nextOuQi == 0)
{
// 已满级
cha = -1;
@ -206,12 +214,12 @@ public class UserService : BaseService<User, int>, IUserService
jindu = levelRange > 0 ? Math.Min(100, Math.Max(0, progress * 100 / levelRange)) : 0;
}
return Task.FromResult(new QuanYiLevelDto
return new QuanYiLevelDto
{
Level = ouQiLevel,
Cha = cha,
Jindu = jindu
});
};
}
/// <inheritdoc/>