优化代码
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 23s
Docker Build & Deploy / Deploy to Production (push) Successful in 6s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s

This commit is contained in:
孙诚
2026-01-04 16:43:32 +08:00
parent ab22325ca7
commit 557d44aed8
11 changed files with 200 additions and 460 deletions

View File

@@ -72,19 +72,15 @@ public class TransactionRecordController(
var transaction = await transactionRepository.GetByIdAsync(id);
if (transaction == null)
{
return BaseResponse<TransactionRecord>.Fail("交易记录不存在");
return "交易记录不存在".Fail<TransactionRecord>();
}
return new BaseResponse<TransactionRecord>
{
Success = true,
Data = transaction
};
return transaction.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取交易记录详情失败交易ID: {TransactionId}", id);
return BaseResponse<TransactionRecord>.Fail($"获取交易记录详情失败: {ex.Message}");
return $"获取交易记录详情失败: {ex.Message}".Fail<TransactionRecord>();
}
}
@@ -97,16 +93,12 @@ public class TransactionRecordController(
try
{
var transactions = await transactionRepository.GetByEmailIdAsync(emailId);
return new BaseResponse<List<TransactionRecord>>
{
Success = true,
Data = transactions
};
return transactions.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取邮件交易记录失败邮件ID: {EmailId}", emailId);
return BaseResponse<List<TransactionRecord>>.Fail($"获取邮件交易记录失败: {ex.Message}");
return $"获取邮件交易记录失败: {ex.Message}".Fail<List<TransactionRecord>>();
}
}
@@ -121,7 +113,7 @@ public class TransactionRecordController(
// 解析日期字符串
if (!DateTime.TryParse(dto.OccurredAt, out var occurredAt))
{
return BaseResponse.Fail("交易时间格式不正确");
return "交易时间格式不正确".Fail();
}
var transaction = new TransactionRecord
@@ -140,20 +132,17 @@ public class TransactionRecordController(
var result = await transactionRepository.AddAsync(transaction);
if (result)
{
return new BaseResponse
{
Success = true
};
return BaseResponse.Done();
}
else
{
return BaseResponse.Fail("创建交易记录失败");
return "创建交易记录失败".Fail();
}
}
catch (Exception ex)
{
logger.LogError(ex, "创建交易记录失败,交易信息: {@TransactionDto}", dto);
return BaseResponse.Fail($"创建交易记录失败: {ex.Message}");
return $"创建交易记录失败: {ex.Message}".Fail();
}
}
@@ -168,7 +157,7 @@ public class TransactionRecordController(
var transaction = await transactionRepository.GetByIdAsync(dto.Id);
if (transaction == null)
{
return BaseResponse.Fail("交易记录不存在");
return "交易记录不存在".Fail();
}
// 更新可编辑字段
@@ -181,20 +170,17 @@ public class TransactionRecordController(
var success = await transactionRepository.UpdateAsync(transaction);
if (success)
{
return new BaseResponse
{
Success = true
};
return BaseResponse.Done();
}
else
{
return BaseResponse.Fail("更新交易记录失败");
return "更新交易记录失败".Fail();
}
}
catch (Exception ex)
{
logger.LogError(ex, "更新交易记录失败交易ID: {TransactionId}, 交易信息: {@TransactionDto}", dto.Id, dto);
return BaseResponse.Fail($"更新交易记录失败: {ex.Message}");
return $"更新交易记录失败: {ex.Message}".Fail();
}
}
@@ -209,20 +195,17 @@ public class TransactionRecordController(
var success = await transactionRepository.DeleteAsync(id);
if (success)
{
return new BaseResponse
{
Success = true
};
return BaseResponse.Done();
}
else
{
return BaseResponse.Fail("删除交易记录失败,记录不存在");
return "删除交易记录失败,记录不存在".Fail();
}
}
catch (Exception ex)
{
logger.LogError(ex, "删除交易记录失败交易ID: {TransactionId}", id);
return BaseResponse.Fail($"删除交易记录失败: {ex.Message}");
return $"删除交易记录失败: {ex.Message}".Fail();
}
}
@@ -240,16 +223,12 @@ public class TransactionRecordController(
var statistics = await transactionRepository.GetDailyStatisticsAsync(year, month);
var result = statistics.Select(s => new DailyStatisticsDto(s.Key, s.Value.count, s.Value.amount)).ToList();
return new BaseResponse<List<DailyStatisticsDto>>
{
Success = true,
Data = result
};
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取日历统计数据失败,年份: {Year}, 月份: {Month}", year, month);
return BaseResponse<List<DailyStatisticsDto>>.Fail($"获取日历统计数据失败: {ex.Message}");
return $"获取日历统计数据失败: {ex.Message}".Fail<List<DailyStatisticsDto>>();
}
}
@@ -265,16 +244,12 @@ public class TransactionRecordController(
try
{
var statistics = await transactionRepository.GetMonthlyStatisticsAsync(year, month);
return new BaseResponse<MonthlyStatistics>
{
Success = true,
Data = statistics
};
return statistics.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取月度统计数据失败,年份: {Year}, 月份: {Month}", year, month);
return BaseResponse<MonthlyStatistics>.Fail($"获取月度统计数据失败: {ex.Message}");
return $"获取月度统计数据失败: {ex.Message}".Fail<MonthlyStatistics>();
}
}
@@ -291,16 +266,12 @@ public class TransactionRecordController(
try
{
var statistics = await transactionRepository.GetCategoryStatisticsAsync(year, month, type);
return new BaseResponse<List<CategoryStatistics>>
{
Success = true,
Data = statistics
};
return statistics.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取分类统计数据失败,年份: {Year}, 月份: {Month}, 类型: {Type}", year, month, type);
return BaseResponse<List<CategoryStatistics>>.Fail($"获取分类统计数据失败: {ex.Message}");
return $"获取分类统计数据失败: {ex.Message}".Fail<List<CategoryStatistics>>();
}
}
@@ -317,16 +288,12 @@ public class TransactionRecordController(
try
{
var statistics = await transactionRepository.GetTrendStatisticsAsync(startYear, startMonth, monthCount);
return new BaseResponse<List<TrendStatistics>>
{
Success = true,
Data = statistics
};
return statistics.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取趋势统计数据失败,开始年份: {Year}, 开始月份: {Month}, 月份数: {Count}", startYear, startMonth, monthCount);
return BaseResponse<List<TrendStatistics>>.Fail($"获取趋势统计数据失败: {ex.Message}");
return $"获取趋势统计数据失败: {ex.Message}".Fail<List<TrendStatistics>>();
}
}
@@ -361,7 +328,7 @@ public class TransactionRecordController(
{
if (!DateTime.TryParse(date, out var targetDate))
{
return BaseResponse<List<TransactionRecord>>.Fail("日期格式不正确");
return "日期格式不正确".Fail<List<TransactionRecord>>();
}
// 获取当天的开始和结束时间
@@ -370,16 +337,12 @@ public class TransactionRecordController(
var records = await transactionRepository.GetByDateRangeAsync(startDate, endDate);
return new BaseResponse<List<TransactionRecord>>
{
Success = true,
Data = records
};
return records.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取指定日期的交易记录失败,日期: {Date}", date);
return BaseResponse<List<TransactionRecord>>.Fail($"获取指定日期的交易记录失败: {ex.Message}");
return $"获取指定日期的交易记录失败: {ex.Message}".Fail<List<TransactionRecord>>();
}
}
@@ -392,16 +355,12 @@ public class TransactionRecordController(
try
{
var count = await transactionRepository.GetUnclassifiedCountAsync();
return new BaseResponse<int>
{
Success = true,
Data = count
};
return count.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取未分类账单数量失败");
return BaseResponse<int>.Fail($"获取未分类账单数量失败: {ex.Message}");
return $"获取未分类账单数量失败: {ex.Message}".Fail<int>();
}
}
@@ -414,16 +373,12 @@ public class TransactionRecordController(
try
{
var records = await transactionRepository.GetUnclassifiedAsync(pageSize);
return new BaseResponse<List<TransactionRecord>>
{
Success = true,
Data = records
};
return records.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取未分类账单列表失败");
return BaseResponse<List<TransactionRecord>>.Fail($"获取未分类账单列表失败: {ex.Message}");
return $"获取未分类账单列表失败: {ex.Message}".Fail<List<TransactionRecord>>();
}
}
@@ -487,16 +442,12 @@ public class TransactionRecordController(
}
}
return new BaseResponse
{
Success = true,
Message = $"批量更新完成,成功 {successCount} 条,失败 {failCount} 条"
};
return $"批量更新完成,成功 {successCount} 条,失败 {failCount} 条".Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "批量更新分类失败");
return BaseResponse.Fail($"批量更新分类失败: {ex.Message}");
return $"批量更新分类失败: {ex.Message}".Fail();
}
}
@@ -534,17 +485,12 @@ public class TransactionRecordController(
try
{
var count = await transactionRepository.BatchUpdateByReasonAsync(dto.Reason, dto.Type, dto.Classify);
return new BaseResponse<int>
{
Success = true,
Data = count,
Message = $"成功更新 {count} 条记录"
};
return count.Ok($"成功更新 {count} 条记录");
}
catch (Exception ex)
{
logger.LogError(ex, "按摘要批量更新分类失败,摘要: {Reason}", dto.Reason);
return BaseResponse<int>.Fail($"按摘要批量更新分类失败: {ex.Message}");
return $"按摘要批量更新分类失败: {ex.Message}".Fail<int>();
}
}
@@ -556,7 +502,7 @@ public class TransactionRecordController(
{
if (string.IsNullOrEmpty(request.Text))
{
return BaseResponse<TransactionParseResult>.Fail("请求参数缺失text");
return "请求参数缺失text".Fail<TransactionParseResult>();
}
try
@@ -565,15 +511,15 @@ public class TransactionRecordController(
if (result == null)
{
return BaseResponse<TransactionParseResult>.Fail("AI解析失败");
return "AI解析失败".Fail<TransactionParseResult>();
}
return BaseResponse<TransactionParseResult>.Done(result);
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "一句话录账解析失败,文本: {Text}", request.Text);
return BaseResponse<TransactionParseResult>.Fail("AI解析失败: " + ex.Message);
return ("AI解析失败: " + ex.Message).Fail<TransactionParseResult>();
}
}
@@ -588,17 +534,17 @@ public class TransactionRecordController(
var current = await transactionRepository.GetByIdAsync(id);
if (current == null)
{
return BaseResponse<TransactionRecord[]>.Done([]);
return ((TransactionRecord[])[]).Ok();
}
var list = await transactionRepository.GetCandidatesForOffsetAsync(id, current.Amount, current.Type);
return BaseResponse<TransactionRecord[]>.Done(list.ToArray());
return list.ToArray().Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取抵账候选列表失败交易ID: {TransactionId}", id);
return BaseResponse<TransactionRecord[]>.Fail($"获取抵账候选列表失败: {ex.Message}");
return $"获取抵账候选列表失败: {ex.Message}".Fail<TransactionRecord[]>();
}
}