feat: 权益等级支持新增和删除功能
This commit is contained in:
parent
008b44dc12
commit
ae25832ee1
|
|
@ -87,6 +87,46 @@ public class QyLevelController : BusinessControllerBase
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增权益等级
|
||||
/// </summary>
|
||||
/// <param name="request">创建请求</param>
|
||||
/// <returns>新创建的权益等级ID</returns>
|
||||
[HttpPost]
|
||||
[BusinessPermission("qylevel:add")]
|
||||
public async Task<IActionResult> CreateQyLevel([FromBody] QyLevelCreateRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = await _qyLevelService.CreateQyLevelAsync(request);
|
||||
return Ok(new { id }, "创建成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
return Error(ex.Code, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除权益等级(软删除)
|
||||
/// </summary>
|
||||
/// <param name="id">权益等级ID</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpDelete("{id}")]
|
||||
[BusinessPermission("qylevel:delete")]
|
||||
public async Task<IActionResult> DeleteQyLevel(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _qyLevelService.DeleteQyLevelAsync(id);
|
||||
return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
return Error(ex.Code, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 权益等级奖品接口
|
||||
|
|
|
|||
|
|
@ -172,6 +172,27 @@ public class QyLevelUpdateRequest
|
|||
public int RequiredPoints { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 权益等级创建请求
|
||||
/// </summary>
|
||||
public class QyLevelCreateRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 等级
|
||||
/// </summary>
|
||||
public int Level { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 等级名称
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 所需欧气值
|
||||
/// </summary>
|
||||
public int RequiredPoints { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 权益等级奖品列表查询请求
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,20 @@ public interface IQyLevelService
|
|||
/// <returns>是否成功</returns>
|
||||
Task<bool> UpdateQyLevelAsync(int id, QyLevelUpdateRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 创建权益等级
|
||||
/// </summary>
|
||||
/// <param name="request">创建请求</param>
|
||||
/// <returns>新创建的权益等级ID</returns>
|
||||
Task<int> CreateQyLevelAsync(QyLevelCreateRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 删除权益等级(软删除)
|
||||
/// </summary>
|
||||
/// <param name="id">权益等级ID</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> DeleteQyLevelAsync(int id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 权益等级奖品操作
|
||||
|
|
|
|||
|
|
@ -100,6 +100,73 @@ public class QyLevelService : IQyLevelService
|
|||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<int> CreateQyLevelAsync(QyLevelCreateRequest request)
|
||||
{
|
||||
// 验证请求
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "等级名称不能为空");
|
||||
}
|
||||
|
||||
if (request.Level < 0)
|
||||
{
|
||||
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "等级不能为负数");
|
||||
}
|
||||
|
||||
if (request.RequiredPoints < 0)
|
||||
{
|
||||
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "所需欧气值不能为负数");
|
||||
}
|
||||
|
||||
// 检查等级是否已存在
|
||||
var existingLevel = await _dbContext.EquityLevels
|
||||
.FirstOrDefaultAsync(e => e.Level == request.Level && e.DeletedAt == null);
|
||||
|
||||
if (existingLevel != null)
|
||||
{
|
||||
throw new BusinessException(BusinessErrorCodes.ValidationFailed, $"等级 {request.Level} 已存在");
|
||||
}
|
||||
|
||||
var entity = new EquityLevel
|
||||
{
|
||||
Level = request.Level,
|
||||
Title = request.Title,
|
||||
Number = request.RequiredPoints,
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
|
||||
_dbContext.EquityLevels.Add(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("创建权益等级成功: Id={Id}, Level={Level}, Title={Title}", entity.Id, entity.Level, entity.Title);
|
||||
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> DeleteQyLevelAsync(int id)
|
||||
{
|
||||
var entity = await _dbContext.EquityLevels
|
||||
.FirstOrDefaultAsync(e => e.Id == id && e.DeletedAt == null);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new BusinessException(BusinessErrorCodes.NotFound, "权益等级不存在");
|
||||
}
|
||||
|
||||
// 软删除
|
||||
entity.DeletedAt = DateTime.Now;
|
||||
entity.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await _dbContext.SaveChangesAsync() > 0;
|
||||
|
||||
_logger.LogInformation("删除权益等级成功: Id={Id}, Level={Level}", id, entity.Level);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 权益等级奖品操作
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user