From d339b42b6691943d26966819829d2aa4c49f41e9 Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 5 Feb 2026 20:11:31 +0800 Subject: [PATCH] 21 --- .../src/HoneyBox.Core/Services/TaskService.cs | 23 +++++++++- .../src/HoneyBox.Core/Services/UserService.cs | 42 +++++++++++-------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/TaskService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/TaskService.cs index 5bd07585..2d878d1e 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/TaskService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/TaskService.cs @@ -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 /// /// 当前欧气值 /// 欧气等级 - private static int CalculateOuQiLevel(int ouQi) + private async Task 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; diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs index 1da6163e..33202330 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs @@ -169,29 +169,37 @@ public class UserService : BaseService, IUserService /// 用户欧气值 /// 用户欧气等级 /// 权益等级DTO - private Task CalculateQuanYiLevelAsync(int ouQi, int ouQiLevel) + private async Task CalculateQuanYiLevelAsync(int ouQi, int ouQiLevel) { - // 等级阈值配置 - var levelThresholds = new Dictionary - { - { 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 + { + { 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, 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 - }); + }; } ///