first commot
This commit is contained in:
301
WebApi/Controllers/TransactionCategoryController.cs
Normal file
301
WebApi/Controllers/TransactionCategoryController.cs
Normal file
@@ -0,0 +1,301 @@
|
||||
namespace WebApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class TransactionCategoryController(
|
||||
ITransactionCategoryRepository categoryRepository,
|
||||
ILogger<TransactionCategoryController> logger
|
||||
) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取分类树(支持按类型筛选)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategoryTreeDto>>> GetTreeAsync([FromQuery] TransactionType? type = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tree = await categoryRepository.GetCategoryTreeAsync(type);
|
||||
return new BaseResponse<List<TransactionCategoryTreeDto>>
|
||||
{
|
||||
Success = true,
|
||||
Data = tree
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取分类树失败");
|
||||
return BaseResponse<List<TransactionCategoryTreeDto>>.Fail($"获取分类树失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取顶级分类列表(按类型)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategory>>> GetTopLevelAsync([FromQuery] TransactionType type)
|
||||
{
|
||||
try
|
||||
{
|
||||
var categories = await categoryRepository.GetTopLevelCategoriesByTypeAsync(type);
|
||||
return new BaseResponse<List<TransactionCategory>>
|
||||
{
|
||||
Success = true,
|
||||
Data = categories
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取顶级分类失败, Type: {Type}", type);
|
||||
return BaseResponse<List<TransactionCategory>>.Fail($"获取顶级分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取子分类列表
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategory>>> GetChildrenAsync([FromQuery] long parentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var categories = await categoryRepository.GetChildCategoriesAsync(parentId);
|
||||
return new BaseResponse<List<TransactionCategory>>
|
||||
{
|
||||
Success = true,
|
||||
Data = categories
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取子分类失败, ParentId: {ParentId}", parentId);
|
||||
return BaseResponse<List<TransactionCategory>>.Fail($"获取子分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取分类详情
|
||||
/// </summary>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<BaseResponse<TransactionCategory>> GetByIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var category = await categoryRepository.GetByIdAsync(id);
|
||||
if (category == null)
|
||||
{
|
||||
return BaseResponse<TransactionCategory>.Fail("分类不存在");
|
||||
}
|
||||
|
||||
return new BaseResponse<TransactionCategory>
|
||||
{
|
||||
Success = true,
|
||||
Data = category
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取分类详情失败, Id: {Id}", id);
|
||||
return BaseResponse<TransactionCategory>.Fail($"获取分类详情失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建分类
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<long>> CreateAsync([FromBody] CreateCategoryDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查同名分类
|
||||
var existing = await categoryRepository.GetByNameAndParentAsync(dto.Name, dto.ParentId, dto.Type);
|
||||
if (existing != null)
|
||||
{
|
||||
return BaseResponse<long>.Fail("同级已存在相同名称的分类");
|
||||
}
|
||||
|
||||
var category = new TransactionCategory
|
||||
{
|
||||
Name = dto.Name,
|
||||
ParentId = dto.ParentId,
|
||||
Type = dto.Type,
|
||||
Level = dto.Level,
|
||||
SortOrder = dto.SortOrder,
|
||||
Icon = dto.Icon,
|
||||
Remark = dto.Remark
|
||||
};
|
||||
|
||||
var result = await categoryRepository.AddAsync(category);
|
||||
if (result)
|
||||
{
|
||||
return new BaseResponse<long>
|
||||
{
|
||||
Success = true,
|
||||
Data = category.Id
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return BaseResponse<long>.Fail("创建分类失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "创建分类失败, Dto: {@Dto}", dto);
|
||||
return BaseResponse<long>.Fail($"创建分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新分类
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse> UpdateAsync([FromBody] UpdateCategoryDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
var category = await categoryRepository.GetByIdAsync(dto.Id);
|
||||
if (category == null)
|
||||
{
|
||||
return BaseResponse.Fail("分类不存在");
|
||||
}
|
||||
|
||||
// 如果修改了名称,检查同名
|
||||
if (category.Name != dto.Name)
|
||||
{
|
||||
var existing = await categoryRepository.GetByNameAndParentAsync(dto.Name, category.ParentId, category.Type);
|
||||
if (existing != null && existing.Id != dto.Id)
|
||||
{
|
||||
return BaseResponse.Fail("同级已存在相同名称的分类");
|
||||
}
|
||||
}
|
||||
|
||||
category.Name = dto.Name;
|
||||
category.SortOrder = dto.SortOrder;
|
||||
category.Icon = dto.Icon;
|
||||
category.IsEnabled = dto.IsEnabled;
|
||||
category.Remark = dto.Remark;
|
||||
category.UpdateTime = DateTime.Now;
|
||||
|
||||
var success = await categoryRepository.UpdateAsync(category);
|
||||
if (success)
|
||||
{
|
||||
return new BaseResponse { Success = true };
|
||||
}
|
||||
else
|
||||
{
|
||||
return BaseResponse.Fail("更新分类失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "更新分类失败, Dto: {@Dto}", dto);
|
||||
return BaseResponse.Fail($"更新分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除分类
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse> DeleteAsync([FromQuery] long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查是否有子分类
|
||||
var children = await categoryRepository.GetChildCategoriesAsync(id);
|
||||
if (children.Any())
|
||||
{
|
||||
return BaseResponse.Fail("该分类下存在子分类,无法删除");
|
||||
}
|
||||
|
||||
// 检查是否被使用
|
||||
var inUse = await categoryRepository.IsCategoryInUseAsync(id);
|
||||
if (inUse)
|
||||
{
|
||||
return BaseResponse.Fail("该分类已被使用,无法删除");
|
||||
}
|
||||
|
||||
var success = await categoryRepository.DeleteAsync(id);
|
||||
if (success)
|
||||
{
|
||||
return new BaseResponse { Success = true };
|
||||
}
|
||||
else
|
||||
{
|
||||
return BaseResponse.Fail("删除分类失败,分类不存在");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "删除分类失败, Id: {Id}", id);
|
||||
return BaseResponse.Fail($"删除分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量创建分类(用于初始化)
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<int>> BatchCreateAsync([FromBody] List<CreateCategoryDto> dtoList)
|
||||
{
|
||||
try
|
||||
{
|
||||
var categories = dtoList.Select(dto => new TransactionCategory
|
||||
{
|
||||
Name = dto.Name,
|
||||
ParentId = dto.ParentId,
|
||||
Type = dto.Type,
|
||||
Level = dto.Level,
|
||||
SortOrder = dto.SortOrder,
|
||||
Icon = dto.Icon,
|
||||
Remark = dto.Remark
|
||||
}).ToList();
|
||||
|
||||
var result = await categoryRepository.AddRangeAsync(categories);
|
||||
if (result)
|
||||
{
|
||||
return new BaseResponse<int>
|
||||
{
|
||||
Success = true,
|
||||
Data = categories.Count
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return BaseResponse<int>.Fail("批量创建分类失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "批量创建分类失败, Count: {Count}", dtoList.Count);
|
||||
return BaseResponse<int>.Fail($"批量创建分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建分类DTO
|
||||
/// </summary>
|
||||
public record CreateCategoryDto(
|
||||
string Name,
|
||||
long ParentId,
|
||||
TransactionType Type,
|
||||
int Level,
|
||||
int SortOrder = 0,
|
||||
string? Icon = null,
|
||||
string? Remark = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 更新分类DTO
|
||||
/// </summary>
|
||||
public record UpdateCategoryDto(
|
||||
long Id,
|
||||
string Name,
|
||||
int SortOrder,
|
||||
string? Icon,
|
||||
bool IsEnabled,
|
||||
string? Remark
|
||||
);
|
||||
Reference in New Issue
Block a user