fix
This commit is contained in:
170
Application/TransactionPeriodicApplication.cs
Normal file
170
Application/TransactionPeriodicApplication.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using Application.Dto.Periodic;
|
||||
using Service.Transaction;
|
||||
|
||||
namespace Application;
|
||||
|
||||
/// <summary>
|
||||
/// 周期性账单应用服务接口
|
||||
/// </summary>
|
||||
public interface ITransactionPeriodicApplication
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取周期性账单列表(分页)
|
||||
/// </summary>
|
||||
Task<PeriodicPagedResult> GetListAsync(int pageIndex = 1, int pageSize = 20, string? searchKeyword = null);
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取周期性账单详情
|
||||
/// </summary>
|
||||
Task<PeriodicResponse> GetByIdAsync(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 创建周期性账单
|
||||
/// </summary>
|
||||
Task<PeriodicResponse> CreateAsync(CreatePeriodicRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 更新周期性账单
|
||||
/// </summary>
|
||||
Task UpdateAsync(UpdatePeriodicRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 删除周期性账单
|
||||
/// </summary>
|
||||
Task DeleteByIdAsync(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 启用/禁用周期性账单
|
||||
/// </summary>
|
||||
Task ToggleEnabledAsync(long id, bool enabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 周期性账单应用服务实现
|
||||
/// </summary>
|
||||
public class TransactionPeriodicApplication(
|
||||
ITransactionPeriodicRepository periodicRepository,
|
||||
ITransactionPeriodicService periodicService,
|
||||
ILogger<TransactionPeriodicApplication> logger
|
||||
) : ITransactionPeriodicApplication
|
||||
{
|
||||
public async Task<PeriodicPagedResult> GetListAsync(int pageIndex = 1, int pageSize = 20, string? searchKeyword = null)
|
||||
{
|
||||
var list = await periodicRepository.GetPagedListAsync(pageIndex, pageSize, searchKeyword);
|
||||
var total = await periodicRepository.GetTotalCountAsync(searchKeyword);
|
||||
|
||||
return new PeriodicPagedResult
|
||||
{
|
||||
Data = list.Select(MapToResponse).ToArray(),
|
||||
Total = (int)total
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<PeriodicResponse> GetByIdAsync(long id)
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(id);
|
||||
if (periodic == null)
|
||||
{
|
||||
throw new NotFoundException("周期性账单不存在");
|
||||
}
|
||||
|
||||
return MapToResponse(periodic);
|
||||
}
|
||||
|
||||
public async Task<PeriodicResponse> CreateAsync(CreatePeriodicRequest request)
|
||||
{
|
||||
var periodic = new TransactionPeriodic
|
||||
{
|
||||
PeriodicType = request.PeriodicType,
|
||||
PeriodicConfig = request.PeriodicConfig ?? string.Empty,
|
||||
Amount = request.Amount,
|
||||
Type = request.Type,
|
||||
Classify = request.Classify ?? string.Empty,
|
||||
Reason = request.Reason ?? string.Empty,
|
||||
IsEnabled = true
|
||||
};
|
||||
|
||||
// 计算下次执行时间
|
||||
periodic.NextExecuteTime = periodicService.CalculateNextExecuteTime(periodic, DateTime.Now);
|
||||
|
||||
var success = await periodicRepository.AddAsync(periodic);
|
||||
if (!success)
|
||||
{
|
||||
throw new BusinessException("创建周期性账单失败");
|
||||
}
|
||||
|
||||
return MapToResponse(periodic);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(UpdatePeriodicRequest request)
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(request.Id);
|
||||
if (periodic == null)
|
||||
{
|
||||
throw new NotFoundException("周期性账单不存在");
|
||||
}
|
||||
|
||||
periodic.PeriodicType = request.PeriodicType;
|
||||
periodic.PeriodicConfig = request.PeriodicConfig ?? string.Empty;
|
||||
periodic.Amount = request.Amount;
|
||||
periodic.Type = request.Type;
|
||||
periodic.Classify = request.Classify ?? string.Empty;
|
||||
periodic.Reason = request.Reason ?? string.Empty;
|
||||
periodic.IsEnabled = request.IsEnabled;
|
||||
periodic.UpdateTime = DateTime.Now;
|
||||
|
||||
// 重新计算下次执行时间
|
||||
periodic.NextExecuteTime = periodicService.CalculateNextExecuteTime(periodic, DateTime.Now);
|
||||
|
||||
var success = await periodicRepository.UpdateAsync(periodic);
|
||||
if (!success)
|
||||
{
|
||||
throw new BusinessException("更新周期性账单失败");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteByIdAsync(long id)
|
||||
{
|
||||
var success = await periodicRepository.DeleteAsync(id);
|
||||
if (!success)
|
||||
{
|
||||
throw new BusinessException("删除周期性账单失败");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ToggleEnabledAsync(long id, bool enabled)
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(id);
|
||||
if (periodic == null)
|
||||
{
|
||||
throw new NotFoundException("周期性账单不存在");
|
||||
}
|
||||
|
||||
periodic.IsEnabled = enabled;
|
||||
periodic.UpdateTime = DateTime.Now;
|
||||
|
||||
var success = await periodicRepository.UpdateAsync(periodic);
|
||||
if (!success)
|
||||
{
|
||||
throw new BusinessException("操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
private static PeriodicResponse MapToResponse(TransactionPeriodic periodic)
|
||||
{
|
||||
return new PeriodicResponse
|
||||
{
|
||||
Id = periodic.Id,
|
||||
PeriodicType = periodic.PeriodicType,
|
||||
PeriodicConfig = periodic.PeriodicConfig,
|
||||
Amount = periodic.Amount,
|
||||
Type = periodic.Type,
|
||||
Classify = periodic.Classify,
|
||||
Reason = periodic.Reason,
|
||||
IsEnabled = periodic.IsEnabled,
|
||||
NextExecuteTime = periodic.NextExecuteTime,
|
||||
CreateTime = periodic.CreateTime,
|
||||
UpdateTime = periodic.UpdateTime
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user