259 lines
11 KiB
C#
259 lines
11 KiB
C#
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using TakeoutSaaS.Application.App.Merchants.Commands;
|
|
using TakeoutSaaS.Application.App.Merchants.Dto;
|
|
using TakeoutSaaS.Application.App.Merchants.Queries;
|
|
using TakeoutSaaS.Domain.Merchants.Enums;
|
|
using TakeoutSaaS.Module.Authorization.Attributes;
|
|
using TakeoutSaaS.Shared.Abstractions.Constants;
|
|
using TakeoutSaaS.Shared.Abstractions.Results;
|
|
using TakeoutSaaS.Shared.Web.Api;
|
|
|
|
namespace TakeoutSaaS.AdminApi.Controllers;
|
|
|
|
/// <summary>
|
|
/// 商户管理。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 初始化控制器。
|
|
/// </remarks>
|
|
[ApiVersion("1.0")]
|
|
[Authorize]
|
|
[Route("api/admin/v{version:apiVersion}/merchants")]
|
|
public sealed class MerchantsController(IMediator mediator) : BaseApiController
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// 创建商户。
|
|
/// </summary>
|
|
[HttpPost]
|
|
[PermissionAuthorize("merchant:create")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantDto>> Create([FromBody] CreateMerchantCommand command, CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询商户列表。
|
|
/// </summary>
|
|
[HttpGet]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<MerchantDto>>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<PagedResult<MerchantDto>>> List(
|
|
[FromQuery] MerchantStatus? status,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20,
|
|
[FromQuery] string? sortBy = null,
|
|
[FromQuery] bool sortDesc = true,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var result = await mediator.Send(new SearchMerchantsQuery
|
|
{
|
|
Status = status,
|
|
Page = page,
|
|
PageSize = pageSize,
|
|
SortBy = sortBy,
|
|
SortDescending = sortDesc
|
|
}, cancellationToken);
|
|
return ApiResponse<PagedResult<MerchantDto>>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新商户。
|
|
/// </summary>
|
|
[HttpPut("{merchantId:long}")]
|
|
[PermissionAuthorize("merchant:update")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
|
public async Task<ApiResponse<MerchantDto>> Update(long merchantId, [FromBody] UpdateMerchantCommand command, CancellationToken cancellationToken)
|
|
{
|
|
if (command.MerchantId == 0)
|
|
{
|
|
command = command with { MerchantId = merchantId };
|
|
}
|
|
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return result == null
|
|
? ApiResponse<MerchantDto>.Error(ErrorCodes.NotFound, "商户不存在")
|
|
: ApiResponse<MerchantDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除商户。
|
|
/// </summary>
|
|
[HttpDelete("{merchantId:long}")]
|
|
[PermissionAuthorize("merchant:delete")]
|
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
|
public async Task<ApiResponse<object>> Delete(long merchantId, CancellationToken cancellationToken)
|
|
{
|
|
var success = await mediator.Send(new DeleteMerchantCommand { MerchantId = merchantId }, cancellationToken);
|
|
return success
|
|
? ApiResponse<object>.Ok(null)
|
|
: ApiResponse<object>.Error(ErrorCodes.NotFound, "商户不存在");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取商户概览。
|
|
/// </summary>
|
|
[HttpGet("{merchantId:long}")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
|
public async Task<ApiResponse<MerchantDto>> Detail(long merchantId, CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantByIdQuery { MerchantId = merchantId }, cancellationToken);
|
|
return result == null
|
|
? ApiResponse<MerchantDto>.Error(ErrorCodes.NotFound, "商户不存在")
|
|
: ApiResponse<MerchantDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取商户详细资料(含证照、合同)。
|
|
/// </summary>
|
|
[HttpGet("{merchantId:long}/detail")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDetailDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantDetailDto>> FullDetail(long merchantId, CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantDetailQuery(merchantId), cancellationToken);
|
|
return ApiResponse<MerchantDetailDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上传商户证照信息(先通过文件上传接口获取 COS 地址)。
|
|
/// </summary>
|
|
[HttpPost("{merchantId:long}/documents")]
|
|
[PermissionAuthorize("merchant:update")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDocumentDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantDocumentDto>> CreateDocument(
|
|
long merchantId,
|
|
[FromBody] AddMerchantDocumentCommand body,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var command = body with { MerchantId = merchantId };
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantDocumentDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商户证照列表。
|
|
/// </summary>
|
|
[HttpGet("{merchantId:long}/documents")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<MerchantDocumentDto>>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<IReadOnlyList<MerchantDocumentDto>>> Documents(long merchantId, CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantDocumentsQuery(merchantId), cancellationToken);
|
|
return ApiResponse<IReadOnlyList<MerchantDocumentDto>>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 审核指定证照。
|
|
/// </summary>
|
|
[HttpPost("{merchantId:long}/documents/{documentId:long}/review")]
|
|
[PermissionAuthorize("merchant:review")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDocumentDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantDocumentDto>> ReviewDocument(
|
|
long merchantId,
|
|
long documentId,
|
|
[FromBody] ReviewMerchantDocumentCommand body,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var command = body with { MerchantId = merchantId, DocumentId = documentId };
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantDocumentDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增商户合同。
|
|
/// </summary>
|
|
[HttpPost("{merchantId:long}/contracts")]
|
|
[PermissionAuthorize("merchant:update")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantContractDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantContractDto>> CreateContract(
|
|
long merchantId,
|
|
[FromBody] CreateMerchantContractCommand body,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var command = body with { MerchantId = merchantId };
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantContractDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 合同列表。
|
|
/// </summary>
|
|
[HttpGet("{merchantId:long}/contracts")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<MerchantContractDto>>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<IReadOnlyList<MerchantContractDto>>> Contracts(long merchantId, CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantContractsQuery(merchantId), cancellationToken);
|
|
return ApiResponse<IReadOnlyList<MerchantContractDto>>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新合同状态(生效/终止等)。
|
|
/// </summary>
|
|
[HttpPut("{merchantId:long}/contracts/{contractId:long}/status")]
|
|
[PermissionAuthorize("merchant:update")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantContractDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantContractDto>> UpdateContractStatus(
|
|
long merchantId,
|
|
long contractId,
|
|
[FromBody] UpdateMerchantContractStatusCommand body,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var command = body with { MerchantId = merchantId, ContractId = contractId };
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantContractDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 审核商户(通过/驳回)。
|
|
/// </summary>
|
|
[HttpPost("{merchantId:long}/review")]
|
|
[PermissionAuthorize("merchant:review")]
|
|
[ProducesResponseType(typeof(ApiResponse<MerchantDto>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<MerchantDto>> Review(long merchantId, [FromBody] ReviewMerchantCommand body, CancellationToken cancellationToken)
|
|
{
|
|
var command = body with { MerchantId = merchantId };
|
|
var result = await mediator.Send(command, cancellationToken);
|
|
return ApiResponse<MerchantDto>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 审核日志。
|
|
/// </summary>
|
|
[HttpGet("{merchantId:long}/audits")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<PagedResult<MerchantAuditLogDto>>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<PagedResult<MerchantAuditLogDto>>> AuditLogs(
|
|
long merchantId,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantAuditLogsQuery(merchantId, page, pageSize), cancellationToken);
|
|
return ApiResponse<PagedResult<MerchantAuditLogDto>>.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 可选商户类目列表。
|
|
/// </summary>
|
|
[HttpGet("categories")]
|
|
[PermissionAuthorize("merchant:read")]
|
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<string>>), StatusCodes.Status200OK)]
|
|
public async Task<ApiResponse<IReadOnlyList<string>>> Categories(CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetMerchantCategoriesQuery(), cancellationToken);
|
|
return ApiResponse<IReadOnlyList<string>>.Ok(result);
|
|
}
|
|
}
|