248 lines
7.7 KiB
C#
248 lines
7.7 KiB
C#
using MiAssessment.Admin.Business.Attributes;
|
||
using MiAssessment.Admin.Business.Constants;
|
||
using MiAssessment.Admin.Business.Models;
|
||
using MiAssessment.Admin.Business.Models.BusinessPage;
|
||
using MiAssessment.Admin.Business.Models.Common;
|
||
using MiAssessment.Admin.Business.Services.Interfaces;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace MiAssessment.Admin.Business.Controllers;
|
||
|
||
/// <summary>
|
||
/// 业务介绍页管理控制器
|
||
/// </summary>
|
||
[Route("api/admin/businessPage")]
|
||
public class BusinessPageController : BusinessControllerBase
|
||
{
|
||
private readonly IBusinessPageService _businessPageService;
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="businessPageService">业务介绍页服务</param>
|
||
public BusinessPageController(IBusinessPageService businessPageService)
|
||
{
|
||
_businessPageService = businessPageService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取业务介绍页列表
|
||
/// </summary>
|
||
/// <param name="request">查询参数</param>
|
||
/// <returns>分页业务介绍页列表</returns>
|
||
[HttpGet("getList")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.View)]
|
||
public async Task<IActionResult> GetList([FromQuery] BusinessPageQueryRequest request)
|
||
{
|
||
try
|
||
{
|
||
var result = await _businessPageService.GetPageListAsync(request);
|
||
return Ok(result);
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "获取业务介绍页列表失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取业务介绍页详情
|
||
/// </summary>
|
||
/// <param name="id">业务介绍页ID</param>
|
||
/// <returns>业务介绍页详情</returns>
|
||
[HttpGet("getDetail")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.View)]
|
||
public async Task<IActionResult> GetDetail([FromQuery] long id)
|
||
{
|
||
if (id <= 0)
|
||
{
|
||
return ValidationError("业务介绍页ID无效");
|
||
}
|
||
|
||
try
|
||
{
|
||
var result = await _businessPageService.GetPageByIdAsync(id);
|
||
if (result == null)
|
||
{
|
||
return Error(ErrorCodes.BusinessPageNotFound, "业务介绍页不存在");
|
||
}
|
||
return Ok(result);
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "获取业务介绍页详情失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建业务介绍页
|
||
/// </summary>
|
||
/// <param name="request">创建请求</param>
|
||
/// <returns>新创建的业务介绍页ID</returns>
|
||
[HttpPost("create")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.Create)]
|
||
public async Task<IActionResult> Create([FromBody] CreateBusinessPageRequest request)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
var errorMessage = ModelState.Values
|
||
.SelectMany(v => v.Errors)
|
||
.Select(e => e.ErrorMessage)
|
||
.FirstOrDefault() ?? "参数验证失败";
|
||
return ValidationError(errorMessage);
|
||
}
|
||
|
||
try
|
||
{
|
||
var id = await _businessPageService.CreatePageAsync(request);
|
||
return Ok(new { Id = id });
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "创建业务介绍页失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新业务介绍页
|
||
/// </summary>
|
||
/// <param name="request">更新请求</param>
|
||
/// <returns>是否更新成功</returns>
|
||
[HttpPost("update")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.Update)]
|
||
public async Task<IActionResult> Update([FromBody] UpdateBusinessPageRequest request)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
var errorMessage = ModelState.Values
|
||
.SelectMany(v => v.Errors)
|
||
.Select(e => e.ErrorMessage)
|
||
.FirstOrDefault() ?? "参数验证失败";
|
||
return ValidationError(errorMessage);
|
||
}
|
||
|
||
if (request.Id <= 0)
|
||
{
|
||
return ValidationError("业务介绍页ID无效");
|
||
}
|
||
|
||
try
|
||
{
|
||
var success = await _businessPageService.UpdatePageAsync(request);
|
||
return success ? Ok() : Error(ErrorCodes.BusinessPageNotFound, "业务介绍页不存在");
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "更新业务介绍页失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除业务介绍页(软删除)
|
||
/// </summary>
|
||
/// <param name="request">删除请求</param>
|
||
/// <returns>是否删除成功</returns>
|
||
[HttpPost("delete")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.Delete)]
|
||
public async Task<IActionResult> Delete([FromBody] DeleteRequest request)
|
||
{
|
||
if (request.Id <= 0)
|
||
{
|
||
return ValidationError("业务介绍页ID无效");
|
||
}
|
||
|
||
try
|
||
{
|
||
var success = await _businessPageService.DeletePageAsync(request.Id);
|
||
return success ? Ok() : Error(ErrorCodes.BusinessPageNotFound, "业务介绍页不存在");
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "删除业务介绍页失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新业务介绍页状态
|
||
/// </summary>
|
||
/// <param name="request">状态更新请求</param>
|
||
/// <returns>是否更新成功</returns>
|
||
[HttpPost("updateStatus")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.Update)]
|
||
public async Task<IActionResult> UpdateStatus([FromBody] UpdateStatusRequest request)
|
||
{
|
||
if (request.Id <= 0)
|
||
{
|
||
return ValidationError("业务介绍页ID无效");
|
||
}
|
||
|
||
if (request.Status != 0 && request.Status != 1)
|
||
{
|
||
return ValidationError("状态值无效,只能为0(禁用)或1(启用)");
|
||
}
|
||
|
||
try
|
||
{
|
||
var success = await _businessPageService.UpdatePageStatusAsync(request.Id, request.Status);
|
||
return success ? Ok() : Error(ErrorCodes.BusinessPageNotFound, "业务介绍页不存在");
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "更新业务介绍页状态失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新业务介绍页排序
|
||
/// </summary>
|
||
/// <param name="request">排序更新请求</param>
|
||
/// <returns>是否更新成功</returns>
|
||
[HttpPost("updateSort")]
|
||
[BusinessPermission(BusinessPermissions.BusinessPage.Update)]
|
||
public async Task<IActionResult> UpdateSort([FromBody] SingleSortRequest request)
|
||
{
|
||
if (request.Id <= 0)
|
||
{
|
||
return ValidationError("业务介绍页ID无效");
|
||
}
|
||
|
||
try
|
||
{
|
||
var success = await _businessPageService.UpdatePageSortAsync(request.Id, request.Sort);
|
||
return success ? Ok() : Error(ErrorCodes.BusinessPageNotFound, "业务介绍页不存在");
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
return Error(ex.Code, ex.Message);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return Error(ErrorCodes.SystemError, "更新业务介绍页排序失败");
|
||
}
|
||
}
|
||
}
|