fix
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Service.Transaction;
|
||||
using Application.Dto.Periodic;
|
||||
using Application;
|
||||
|
||||
namespace WebApi.Controllers;
|
||||
|
||||
@@ -8,98 +9,46 @@ namespace WebApi.Controllers;
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class TransactionPeriodicController(
|
||||
ITransactionPeriodicRepository periodicRepository,
|
||||
ITransactionPeriodicService periodicService,
|
||||
ILogger<TransactionPeriodicController> logger
|
||||
ITransactionPeriodicApplication periodicApplication
|
||||
) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取周期性账单列表(分页)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<PagedResponse<TransactionPeriodic>> GetListAsync(
|
||||
public async Task<PagedResponse<PeriodicResponse>> GetListAsync(
|
||||
[FromQuery] int pageIndex = 1,
|
||||
[FromQuery] int pageSize = 20,
|
||||
[FromQuery] string? searchKeyword = null
|
||||
)
|
||||
{
|
||||
try
|
||||
var result = await periodicApplication.GetListAsync(pageIndex, pageSize, searchKeyword);
|
||||
return new PagedResponse<PeriodicResponse>
|
||||
{
|
||||
var list = await periodicRepository.GetPagedListAsync(pageIndex, pageSize, searchKeyword);
|
||||
var total = await periodicRepository.GetTotalCountAsync(searchKeyword);
|
||||
|
||||
return new PagedResponse<TransactionPeriodic>
|
||||
{
|
||||
Success = true,
|
||||
Data = list.ToArray(),
|
||||
Total = (int)total
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取周期性账单列表失败");
|
||||
return PagedResponse<TransactionPeriodic>.Fail($"获取列表失败: {ex.Message}");
|
||||
}
|
||||
Success = true,
|
||||
Data = result.Data,
|
||||
Total = result.Total
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取周期性账单详情
|
||||
/// </summary>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<BaseResponse<TransactionPeriodic>> GetByIdAsync(long id)
|
||||
public async Task<BaseResponse<PeriodicResponse>> GetByIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(id);
|
||||
if (periodic == null)
|
||||
{
|
||||
return "周期性账单不存在".Fail<TransactionPeriodic>();
|
||||
}
|
||||
|
||||
return periodic.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取周期性账单详情失败,ID: {Id}", id);
|
||||
return $"获取详情失败: {ex.Message}".Fail<TransactionPeriodic>();
|
||||
}
|
||||
var periodic = await periodicApplication.GetByIdAsync(id);
|
||||
return periodic.Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建周期性账单
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<TransactionPeriodic>> CreateAsync([FromBody] CreatePeriodicRequest request)
|
||||
public async Task<BaseResponse<PeriodicResponse>> CreateAsync([FromBody] CreatePeriodicRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
return "创建周期性账单失败".Fail<TransactionPeriodic>();
|
||||
}
|
||||
|
||||
return periodic.Ok("创建成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "创建周期性账单失败");
|
||||
return $"创建失败: {ex.Message}".Fail<TransactionPeriodic>();
|
||||
}
|
||||
var periodic = await periodicApplication.CreateAsync(request);
|
||||
return periodic.Ok("创建成功");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,39 +57,8 @@ public class TransactionPeriodicController(
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse> UpdateAsync([FromBody] UpdatePeriodicRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(request.Id);
|
||||
if (periodic == null)
|
||||
{
|
||||
return "周期性账单不存在".Fail();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return "更新周期性账单失败".Fail();
|
||||
}
|
||||
|
||||
return "更新成功".Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "更新周期性账单失败,ID: {Id}", request.Id);
|
||||
return $"更新失败: {ex.Message}".Fail();
|
||||
}
|
||||
await periodicApplication.UpdateAsync(request);
|
||||
return "更新成功".Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -149,21 +67,8 @@ public class TransactionPeriodicController(
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse> DeleteByIdAsync([FromQuery] long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var success = await periodicRepository.DeleteAsync(id);
|
||||
if (!success)
|
||||
{
|
||||
return "删除周期性账单失败".Fail();
|
||||
}
|
||||
|
||||
return "删除成功".Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "删除周期性账单失败,ID: {Id}", id);
|
||||
return $"删除失败: {ex.Message}".Fail();
|
||||
}
|
||||
await periodicApplication.DeleteByIdAsync(id);
|
||||
return "删除成功".Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -172,57 +77,7 @@ public class TransactionPeriodicController(
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse> ToggleEnabledAsync([FromQuery] long id, [FromQuery] bool enabled)
|
||||
{
|
||||
try
|
||||
{
|
||||
var periodic = await periodicRepository.GetByIdAsync(id);
|
||||
if (periodic == null)
|
||||
{
|
||||
return "周期性账单不存在".Fail();
|
||||
}
|
||||
|
||||
periodic.IsEnabled = enabled;
|
||||
periodic.UpdateTime = DateTime.Now;
|
||||
|
||||
var success = await periodicRepository.UpdateAsync(periodic);
|
||||
if (!success)
|
||||
{
|
||||
return "操作失败".Fail();
|
||||
}
|
||||
|
||||
return (enabled ? "已启用" : "已禁用").Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "启用/禁用周期性账单失败,ID: {Id}", id);
|
||||
return $"操作失败: {ex.Message}".Fail();
|
||||
}
|
||||
await periodicApplication.ToggleEnabledAsync(id, enabled);
|
||||
return (enabled ? "已启用" : "已禁用").Ok();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建周期性账单请求
|
||||
/// </summary>
|
||||
public class CreatePeriodicRequest
|
||||
{
|
||||
public PeriodicType PeriodicType { get; set; }
|
||||
public string? PeriodicConfig { get; set; }
|
||||
public decimal Amount { get; set; }
|
||||
public TransactionType Type { get; set; }
|
||||
public string? Classify { get; set; }
|
||||
public string? Reason { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新周期性账单请求
|
||||
/// </summary>
|
||||
public class UpdatePeriodicRequest
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public PeriodicType PeriodicType { get; set; }
|
||||
public string? PeriodicConfig { get; set; }
|
||||
public decimal Amount { get; set; }
|
||||
public TransactionType Type { get; set; }
|
||||
public string? Classify { get; set; }
|
||||
public string? Reason { get; set; }
|
||||
public bool IsEnabled { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user