250 lines
7.7 KiB
C#
250 lines
7.7 KiB
C#
|
|
namespace WebApi.Controllers;
|
|||
|
|
|
|||
|
|
using Repository;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 周期性账单控制器
|
|||
|
|
/// </summary>
|
|||
|
|
[ApiController]
|
|||
|
|
[Route("api/[controller]/[action]")]
|
|||
|
|
public class TransactionPeriodicController(
|
|||
|
|
ITransactionPeriodicRepository periodicRepository,
|
|||
|
|
ITransactionPeriodicService periodicService,
|
|||
|
|
ILogger<TransactionPeriodicController> logger
|
|||
|
|
) : ControllerBase
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取周期性账单列表(分页)
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpGet]
|
|||
|
|
public async Task<PagedResponse<TransactionPeriodic>> GetListAsync(
|
|||
|
|
[FromQuery] int pageIndex = 1,
|
|||
|
|
[FromQuery] int pageSize = 20,
|
|||
|
|
[FromQuery] string? searchKeyword = null
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
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}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 根据ID获取周期性账单详情
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpGet("{id}")]
|
|||
|
|
public async Task<BaseResponse<TransactionPeriodic>> GetByIdAsync(long id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var periodic = await periodicRepository.GetByIdAsync(id);
|
|||
|
|
if (periodic == null)
|
|||
|
|
{
|
|||
|
|
return BaseResponse<TransactionPeriodic>.Fail("周期性账单不存在");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new BaseResponse<TransactionPeriodic>
|
|||
|
|
{
|
|||
|
|
Success = true,
|
|||
|
|
Data = periodic
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
logger.LogError(ex, "获取周期性账单详情失败,ID: {Id}", id);
|
|||
|
|
return BaseResponse<TransactionPeriodic>.Fail($"获取详情失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 创建周期性账单
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<BaseResponse<TransactionPeriodic>> 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 BaseResponse<TransactionPeriodic>.Fail("创建周期性账单失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new BaseResponse<TransactionPeriodic>
|
|||
|
|
{
|
|||
|
|
Success = true,
|
|||
|
|
Data = periodic,
|
|||
|
|
Message = "创建成功"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
logger.LogError(ex, "创建周期性账单失败");
|
|||
|
|
return BaseResponse<TransactionPeriodic>.Fail($"创建失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 更新周期性账单
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<BaseResponse<object>> UpdateAsync([FromBody] UpdatePeriodicRequest request)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var periodic = await periodicRepository.GetByIdAsync(request.Id);
|
|||
|
|
if (periodic == null)
|
|||
|
|
{
|
|||
|
|
return BaseResponse<object>.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 BaseResponse<object>.Fail("更新周期性账单失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new BaseResponse<object>
|
|||
|
|
{
|
|||
|
|
Success = true,
|
|||
|
|
Message = "更新成功"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
logger.LogError(ex, "更新周期性账单失败,ID: {Id}", request.Id);
|
|||
|
|
return BaseResponse<object>.Fail($"更新失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除周期性账单
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<BaseResponse<object>> DeleteByIdAsync([FromQuery] long id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var success = await periodicRepository.DeleteAsync(id);
|
|||
|
|
if (!success)
|
|||
|
|
{
|
|||
|
|
return BaseResponse<object>.Fail("删除周期性账单失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new BaseResponse<object>
|
|||
|
|
{
|
|||
|
|
Success = true,
|
|||
|
|
Message = "删除成功"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
logger.LogError(ex, "删除周期性账单失败,ID: {Id}", id);
|
|||
|
|
return BaseResponse<object>.Fail($"删除失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 启用/禁用周期性账单
|
|||
|
|
/// </summary>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<BaseResponse<object>> ToggleEnabledAsync([FromQuery] long id, [FromQuery] bool enabled)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var periodic = await periodicRepository.GetByIdAsync(id);
|
|||
|
|
if (periodic == null)
|
|||
|
|
{
|
|||
|
|
return BaseResponse<object>.Fail("周期性账单不存在");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
periodic.IsEnabled = enabled;
|
|||
|
|
periodic.UpdateTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
var success = await periodicRepository.UpdateAsync(periodic);
|
|||
|
|
if (!success)
|
|||
|
|
{
|
|||
|
|
return BaseResponse<object>.Fail("操作失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new BaseResponse<object>
|
|||
|
|
{
|
|||
|
|
Success = true,
|
|||
|
|
Message = enabled ? "已启用" : "已禁用"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
logger.LogError(ex, "启用/禁用周期性账单失败,ID: {Id}", id);
|
|||
|
|
return BaseResponse<object>.Fail($"操作失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <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; }
|
|||
|
|
}
|