大量后端代码格式化

This commit is contained in:
SunCheng
2026-01-18 22:04:56 +08:00
parent 298ce03aa6
commit 4e2bf0da6c
36 changed files with 278 additions and 200 deletions

View File

@@ -1,6 +1,6 @@
namespace Service.AppSettingModel;
public class AISettings
public class AiSettings
{
public string Endpoint { get; set; } = string.Empty;
public string Key { get; set; } = string.Empty;

View File

@@ -1,4 +1,6 @@
namespace Service;
using JetBrains.Annotations;
namespace Service;
public interface IBudgetService
{
@@ -24,6 +26,7 @@ public interface IBudgetService
Task<BudgetResult?> GetSavingsBudgetAsync(int year, int month, BudgetPeriodType type);
}
[UsedImplicitly]
public class BudgetService(
IBudgetRepository budgetRepository,
IBudgetArchiveRepository budgetArchiveRepository,
@@ -79,20 +82,22 @@ public class BudgetService(
}
// 创造虚拟的存款预算
dtos.Add(await GetVirtualSavingsDtoAsync(
dtos.Add(await GetSavingsDtoAsync(
BudgetPeriodType.Month,
referenceDate,
budgets));
dtos.Add(await GetVirtualSavingsDtoAsync(
dtos.Add(await GetSavingsDtoAsync(
BudgetPeriodType.Year,
referenceDate,
budgets));
dtos = dtos
.Where(x => x != null)
.Cast<BudgetResult>()
.OrderByDescending(x => x.IsMandatoryExpense)
.ThenBy(x => x.Type)
.ThenByDescending(x => x.Current)
.ToList();
.ToList()!;
return [.. dtos.Where(dto => dto != null).Cast<BudgetResult>()];
}
@@ -100,7 +105,7 @@ public class BudgetService(
public async Task<BudgetResult?> GetSavingsBudgetAsync(int year, int month, BudgetPeriodType type)
{
var referenceDate = new DateTime(year, month, 1);
return await GetVirtualSavingsDtoAsync(type, referenceDate);
return await GetSavingsDtoAsync(type, referenceDate);
}
public async Task<BudgetCategoryStats> GetCategoryStatsAsync(BudgetCategory category, DateTime referenceDate)
@@ -128,7 +133,7 @@ public class BudgetService(
_ => TransactionType.None
};
if (transactionType == TransactionType.None) return new List<UncoveredCategoryDetail>();
if (transactionType == TransactionType.None) return [];
// 1. 获取所有预算
var budgets = (await budgetRepository.GetAllAsync()).ToList();
@@ -205,7 +210,7 @@ public class BudgetService(
totalLimit += itemLimit;
// 当前值累加
var selectedCategories = budget.SelectedCategories != null ? string.Join(',', budget.SelectedCategories) : string.Empty;
var selectedCategories = string.Join(',', budget.SelectedCategories);
var currentAmount = await CalculateCurrentAmountAsync(new()
{
Name = budget.Name,
@@ -246,7 +251,7 @@ public class BudgetService(
if (transactionType != TransactionType.None)
{
var hasGlobalBudget = relevant.Any(b => b.SelectedCategories == null || b.SelectedCategories.Length == 0);
var hasGlobalBudget = relevant.Any(b => b.SelectedCategories.Length == 0);
var allClassifies = hasGlobalBudget
? []
@@ -256,7 +261,7 @@ public class BudgetService(
.ToList();
DateTime startDate, endDate;
bool groupByMonth = false;
bool groupByMonth;
if (statType == BudgetPeriodType.Month)
{
@@ -445,7 +450,7 @@ public class BudgetService(
.Where(t =>
{
var dict = (IDictionary<string, object>)t;
var classify = dict["Classify"]?.ToString() ?? "";
var classify = dict["Classify"].ToString() ?? "";
var type = Convert.ToInt32(dict["Type"]);
return type == 0 && !budgetedCategories.Contains(classify);
})
@@ -551,7 +556,8 @@ public class BudgetService(
// 返回实际消费和硬性消费累加中的较大值
return mandatoryAccumulation;
}
else if (budget.Type == BudgetPeriodType.Year)
if (budget.Type == BudgetPeriodType.Year)
{
// 计算本年的天数(考虑闰年)
var daysInYear = DateTime.IsLeapYear(referenceDate.Year) ? 366 : 365;
@@ -592,7 +598,7 @@ public class BudgetService(
return (start, end);
}
private async Task<BudgetResult?> GetVirtualSavingsDtoAsync(
private async Task<BudgetResult?> GetSavingsDtoAsync(
BudgetPeriodType periodType,
DateTime? referenceDate = null,
IEnumerable<BudgetRecord>? existingBudgets = null)
@@ -657,7 +663,7 @@ public class BudgetService(
if (b.Category == BudgetCategory.Savings) continue;
processedIds.Add(b.Id);
decimal factor = 1.0m;
decimal factor;
decimal historicalAmount = 0m;
var historicalMonths = new List<int>();
@@ -759,7 +765,6 @@ public class BudgetService(
foreach (var group in deletedBudgets)
{
var budgetId = group.Key;
var months = group.Select(g => g.Key.Month).OrderBy(m => m).ToList();
var totalLimit = group.Sum(g => g.Value.HistoricalLimit);
var (_, category, name) = group.First().Value;
@@ -862,12 +867,12 @@ public class BudgetService(
</thead>
<tbody>
""");
foreach (var (Name, Amount) in noLimitIncomeItems)
foreach (var (name, amount) in noLimitIncomeItems)
{
description.Append($"""
<tr>
<td>{Name}</td>
<td><span class='income-value'>{Amount:N0}</span></td>
<td>{name}</td>
<td><span class='income-value'>{amount:N0}</span></td>
</tr>
""");
}
@@ -953,12 +958,12 @@ public class BudgetService(
</thead>
<tbody>
""");
foreach (var (Name, Amount) in noLimitExpenseItems)
foreach (var (name, amount) in noLimitExpenseItems)
{
description.Append($"""
<tr>
<td>{Name}</td>
<td><span class='expense-value'>{Amount:N0}</span></td>
<td>{name}</td>
<td><span class='expense-value'>{amount:N0}</span></td>
</tr>
""");
}
@@ -1080,13 +1085,13 @@ public record BudgetResult
public decimal Limit { get; set; }
public decimal Current { get; set; }
public BudgetCategory Category { get; set; }
public string[] SelectedCategories { get; set; } = Array.Empty<string>();
public string[] SelectedCategories { get; set; } = [];
public string StartDate { get; set; } = string.Empty;
public string Period { get; set; } = string.Empty;
public DateTime? PeriodStart { get; set; }
public DateTime? PeriodEnd { get; set; }
public bool NoLimit { get; set; } = false;
public bool IsMandatoryExpense { get; set; } = false;
public bool NoLimit { get; set; }
public bool IsMandatoryExpense { get; set; }
public string Description { get; set; } = string.Empty;
public static BudgetResult FromEntity(
@@ -1107,7 +1112,7 @@ public record BudgetResult
Current = currentAmount,
Category = entity.Category,
SelectedCategories = string.IsNullOrEmpty(entity.SelectedCategories)
? Array.Empty<string>()
? []
: entity.SelectedCategories.Split(','),
StartDate = entity.StartDate.ToString("yyyy-MM-dd"),
Period = entity.Type switch
@@ -1157,7 +1162,7 @@ public class BudgetStatsDto
/// <summary>
/// 每日/每月累计金额趋势(对应当前周期内的实际发生额累计值)
/// </summary>
public List<decimal?> Trend { get; set; } = new();
public List<decimal?> Trend { get; set; } = [];
}
/// <summary>
@@ -1175,6 +1180,7 @@ public class BudgetCategoryStats
/// </summary>
public BudgetStatsDto Year { get; set; } = new();
}
public class UncoveredCategoryDetail
{
public string Category { get; set; } = string.Empty;

View File

@@ -65,7 +65,7 @@ public class EmailHandleService(
await messageService.AddAsync(
"邮件解析失败",
$"来自 {from} 发送给 {to} 的邮件(主题:{subject})未能成功解析内容,可能格式已变更或不受支持。",
url: $"/balance?tab=email"
url: "/balance?tab=email"
);
logger.LogWarning("未能成功解析邮件内容,跳过账单处理");
return true;

View File

@@ -2,8 +2,8 @@
namespace Service.EmailParseServices;
public class EmailParseFormCCSVC(
ILogger<EmailParseFormCCSVC> logger,
public class EmailParseFormCcsvc(
ILogger<EmailParseFormCcsvc> logger,
IOpenAiService openAiService
) : EmailParseServicesBase(logger, openAiService)
{
@@ -47,7 +47,7 @@ public class EmailParseFormCCSVC(
if (dateNode == null)
{
logger.LogWarning("Date node not found");
return Array.Empty<(string, string, decimal, decimal, TransactionType, DateTime?)>();
return [];
}
var dateText = dateNode.InnerText.Trim();
@@ -56,7 +56,7 @@ public class EmailParseFormCCSVC(
if (!dateMatch.Success || !DateTime.TryParse(dateMatch.Value, out var date))
{
logger.LogWarning("Failed to parse date from: {DateText}", dateText);
return Array.Empty<(string, string, decimal, decimal, TransactionType, DateTime?)>();
return [];
}
// 2. Get Balance (Available Limit)
@@ -122,7 +122,7 @@ public class EmailParseFormCCSVC(
descText = HtmlEntity.DeEntitize(descText).Replace((char)160, ' ').Trim();
// Parse Description: "尾号4390 消费 财付通-luckincoffee瑞幸咖啡"
var parts = descText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var parts = descText.Split([' '], StringSplitOptions.RemoveEmptyEntries);
string card = "";
string reason = descText;

View File

@@ -201,7 +201,7 @@ public abstract class EmailParseServicesBase(
// 收入关键词
string[] incomeKeywords =
{
[
"工资", "奖金", "退款",
"返现", "收入", "转入",
"存入", "利息", "分红",
@@ -233,13 +233,13 @@ public abstract class EmailParseServicesBase(
// 存取类
"现金存入", "柜台存入", "ATM存入",
"他人转入", "他人汇入"
};
];
if (incomeKeywords.Any(k => lowerReason.Contains(k)))
return TransactionType.Income;
// 支出关键词
string[] expenseKeywords =
{
[
"消费", "支付", "购买",
"转出", "取款", "支出",
"扣款", "缴费", "付款",
@@ -269,7 +269,7 @@ public abstract class EmailParseServicesBase(
// 信用卡/花呗等场景
"信用卡还款", "花呗还款", "白条还款",
"分期还款", "账单还款", "自动还款"
};
];
if (expenseKeywords.Any(k => lowerReason.Contains(k)))
return TransactionType.Expense;

View File

@@ -7,10 +7,11 @@ global using System.Globalization;
global using System.Text;
global using System.Text.Json;
global using Entity;
global using FreeSql;
global using System.Linq;
global using Service.AppSettingModel;
global using System.Text.Json.Serialization;
global using System.Text.Json.Nodes;
global using Microsoft.Extensions.Configuration;
global using Common;
global using Common;
global using System.Net;
global using System.Text.Encodings.Web;

View File

@@ -133,7 +133,7 @@ public class ImportService(
return DateTime.MinValue;
}
foreach (var format in DateTimeFormats)
foreach (var format in _dateTimeFormats)
{
if (DateTime.TryParseExact(
row[key],
@@ -288,7 +288,7 @@ public class ImportService(
return DateTime.MinValue;
}
foreach (var format in DateTimeFormats)
foreach (var format in _dateTimeFormats)
{
if (DateTime.TryParseExact(
row[key],
@@ -358,14 +358,13 @@ public class ImportService(
{
return await ParseCsvAsync(file);
}
else if (fileExtension == ".xlsx" || fileExtension == ".xls")
if (fileExtension == ".xlsx" || fileExtension == ".xls")
{
return await ParseExcelAsync(file);
}
else
{
throw new NotSupportedException("不支持的文件格式");
}
throw new NotSupportedException("不支持的文件格式");
}
private async Task<IDictionary<string, string>[]> ParseCsvAsync(MemoryStream file)
@@ -388,7 +387,7 @@ public class ImportService(
if (headers == null || headers.Length == 0)
{
return Array.Empty<IDictionary<string, string>>();
return [];
}
var result = new List<IDictionary<string, string>>();
@@ -420,7 +419,7 @@ public class ImportService(
if (worksheet == null || worksheet.Dimension == null)
{
return Array.Empty<IDictionary<string, string>>();
return [];
}
var rowCount = worksheet.Dimension.End.Row;
@@ -428,7 +427,7 @@ public class ImportService(
if (rowCount < 2)
{
return Array.Empty<IDictionary<string, string>>();
return [];
}
// 读取表头(第一行)
@@ -458,7 +457,7 @@ public class ImportService(
return await Task.FromResult(result.ToArray());
}
private static string[] DateTimeFormats =
private static string[] _dateTimeFormats =
[
"yyyy-MM-dd",
"yyyy-MM-dd HH",

View File

@@ -68,8 +68,8 @@ public class LogCleanupService(ILogger<LogCleanupService> logger) : BackgroundSe
// 尝试解析日期 (格式: yyyyMMdd)
if (DateTime.TryParseExact(dateStr, "yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var logDate))
{
if (logDate < cutoffDate)

View File

@@ -1,11 +1,13 @@
using WebPush;
using System.Net;
using WebPush;
using PushSubscription = Entity.PushSubscription;
namespace Service;
public interface INotificationService
{
Task<string> GetVapidPublicKeyAsync();
Task SubscribeAsync(Entity.PushSubscription subscription);
Task SubscribeAsync(PushSubscription subscription);
Task SendNotificationAsync(string message, string? url = null);
}
@@ -32,7 +34,7 @@ public class NotificationService(
return Task.FromResult(GetSettings().PublicKey);
}
public async Task SubscribeAsync(Entity.PushSubscription subscription)
public async Task SubscribeAsync(PushSubscription subscription)
{
var existing = await subscriptionRepo.GetByEndpointAsync(subscription.Endpoint);
if (existing != null)
@@ -61,7 +63,7 @@ public class NotificationService(
var webPushClient = new WebPushClient();
var subscriptions = await subscriptionRepo.GetAllAsync();
var payload = System.Text.Json.JsonSerializer.Serialize(new
var payload = JsonSerializer.Serialize(new
{
title = "System Notification",
body = message,
@@ -78,7 +80,7 @@ public class NotificationService(
}
catch (WebPushException ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.Gone || ex.StatusCode == System.Net.HttpStatusCode.NotFound)
if (ex.StatusCode == HttpStatusCode.Gone || ex.StatusCode == HttpStatusCode.NotFound)
{
await subscriptionRepo.DeleteAsync(sub.Id);
}

View File

@@ -11,7 +11,7 @@ public interface IOpenAiService
}
public class OpenAiService(
IOptions<AISettings> aiSettings,
IOptions<AiSettings> aiSettings,
ILogger<OpenAiService> logger
) : IOpenAiService
{

View File

@@ -5,6 +5,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="MailKit" />
<PackageReference Include="MimeKit" />
<PackageReference Include="Microsoft.Extensions.Configuration" />

View File

@@ -143,7 +143,7 @@ public class SmartHandleService(
chunkAction(("start", $"开始分类,共 {sampleRecords.Length} 条账单"));
var classifyResults = new List<(string Reason, string Classify, TransactionType Type)>();
var sendedIds = new HashSet<long>();
var sentIds = new HashSet<long>();
// 将流解析逻辑提取为本地函数以减少嵌套
void HandleResult(GroupClassifyResult? result)
@@ -154,16 +154,18 @@ public class SmartHandleService(
if (group == null) return;
foreach (var id in group.Ids)
{
if (sendedIds.Add(id))
if (!sentIds.Add(id))
{
var resultJson = JsonSerializer.Serialize(new
{
id,
result.Classify,
result.Type
});
chunkAction(("data", resultJson));
continue;
}
var resultJson = JsonSerializer.Serialize(new
{
id,
result.Classify,
result.Type
});
chunkAction(("data", resultJson));
}
}
@@ -193,7 +195,7 @@ public class SmartHandleService(
}
catch (Exception exArr)
{
logger.LogDebug(exArr, "按数组解析AI返回失败回退到逐对象解析。预览: {Preview}", arrJson?.Length > 200 ? arrJson.Substring(0, 200) + "..." : arrJson);
logger.LogDebug(exArr, "按数组解析AI返回失败回退到逐对象解析。预览: {Preview}", arrJson.Length > 200 ? arrJson.Substring(0, 200) + "..." : arrJson);
}
}
}
@@ -336,7 +338,7 @@ public class SmartHandleService(
{
content = $"""
<pre style="max-height: 80px; font-size: 8px; overflow-y: auto; padding: 8px; border: 1px solid #3c3c3c">
{System.Net.WebUtility.HtmlEncode(sqlText)}
{WebUtility.HtmlEncode(sqlText)}
</pre>
"""
})
@@ -361,7 +363,7 @@ public class SmartHandleService(
var dataJson = JsonSerializer.Serialize(queryResults, new JsonSerializerOptions
{
WriteIndented = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
var userPromptExtra = await configService.GetConfigByKeyAsync<string>("BillAnalysisPrompt");
@@ -429,7 +431,6 @@ public class SmartHandleService(
{
// 获取所有分类
var categories = await categoryRepository.GetAllAsync();
var categoryList = string.Join("、", categories.Select(c => $"{GetTypeName(c.Type)}-{c.Name}"));
// 构建分类信息
var categoryInfo = new StringBuilder();
@@ -542,13 +543,13 @@ public class SmartHandleService(
public record GroupClassifyResult
{
[JsonPropertyName("reason")]
public string Reason { get; set; } = string.Empty;
public string Reason { get; init; } = string.Empty;
[JsonPropertyName("classify")]
public string? Classify { get; set; }
public string? Classify { get; init; }
[JsonPropertyName("type")]
public TransactionType Type { get; set; }
public TransactionType Type { get; init; }
}
public record TransactionParseResult(string OccurredAt, string Classify, decimal Amount, string Reason, TransactionType Type);

View File

@@ -1,8 +1,7 @@
namespace Service;
using JiebaNet.Analyser;
using JiebaNet.Segmenter;
using JiebaNet.Analyser;
using Microsoft.Extensions.Logging;
namespace Service;
/// <summary>
/// 文本分词服务接口
@@ -78,7 +77,7 @@ public class TextSegmentService : ITextSegmentService
{
if (string.IsNullOrWhiteSpace(text))
{
return new List<string>();
return [];
}
try
@@ -119,7 +118,7 @@ public class TextSegmentService : ITextSegmentService
{
_logger.LogError(ex, "提取关键词失败,文本: {Text}", text);
// 降级处理:返回原文
return new List<string> { text.Length > 10 ? text.Substring(0, 10) : text };
return [text.Length > 10 ? text.Substring(0, 10) : text];
}
}
@@ -127,7 +126,7 @@ public class TextSegmentService : ITextSegmentService
{
if (string.IsNullOrWhiteSpace(text))
{
return new List<string>();
return [];
}
try
@@ -146,7 +145,7 @@ public class TextSegmentService : ITextSegmentService
catch (Exception ex)
{
_logger.LogError(ex, "分词失败,文本: {Text}", text);
return new List<string> { text };
return [text];
}
}
}

View File

@@ -144,7 +144,7 @@ public class TransactionPeriodicService(
var dayOfWeek = (int)today.DayOfWeek; // 0=Sunday, 1=Monday, ..., 6=Saturday
var executeDays = config.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => int.TryParse(d.Trim(), out var day) ? day : -1)
.Where(d => d >= 0 && d <= 6)
.Where(d => d is >= 0 and <= 6)
.ToList();
return executeDays.Contains(dayOfWeek);
@@ -160,7 +160,7 @@ public class TransactionPeriodicService(
var executeDays = config.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => int.TryParse(d.Trim(), out var day) ? day : -1)
.Where(d => d >= 1 && d <= 31)
.Where(d => d is >= 1 and <= 31)
.ToList();
// 如果当前为月末,且配置中有大于当月天数的日期,则也执行
@@ -223,7 +223,7 @@ public class TransactionPeriodicService(
var executeDays = config.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => int.TryParse(d.Trim(), out var day) ? day : -1)
.Where(d => d >= 0 && d <= 6)
.Where(d => d is >= 0 and <= 6)
.OrderBy(d => d)
.ToList();
@@ -253,7 +253,7 @@ public class TransactionPeriodicService(
var executeDays = config.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => int.TryParse(d.Trim(), out var day) ? day : -1)
.Where(d => d >= 1 && d <= 31)
.Where(d => d is >= 1 and <= 31)
.OrderBy(d => d)
.ToList();