115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using System.Threading.Tasks;
|
|
using Common;
|
|
using CommonDomain;
|
|
using CommonInfrastructure;
|
|
using CommonInfrastructure.Response;
|
|
using CommonService;
|
|
using CommonService.BackgroundOperation;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using UserDomain.Entity;
|
|
|
|
namespace AdminApi.BackgroundOperation
|
|
{
|
|
[Authorize]
|
|
[Route("adminapi/[controller]")]
|
|
[ApiController]
|
|
public class BackgroundOperationController : BaseController
|
|
{
|
|
private readonly IBackgroundOperationService _operationService;
|
|
|
|
public BackgroundOperationController(IBackgroundOperationService operationService)
|
|
{
|
|
_operationService = operationService;
|
|
}
|
|
|
|
[HttpPost("charge")]
|
|
public async Task<BaseResponse<string>> Charge([FromBody] BackgroundOperationRequest request)
|
|
{
|
|
if (request == null || string.IsNullOrWhiteSpace(request.TargetUserId))
|
|
return this.ToFailResult<string>("目标用户ID不能为空");
|
|
|
|
if (request.Amount <= 0)
|
|
return this.ToFailResult<string>("操作数量必须大于0");
|
|
|
|
var (success, message) = await _operationService.Charge(
|
|
loginUser.UserId, request.TargetUserId, request.CurrencyType, request.Amount, request.Remark);
|
|
|
|
return success
|
|
? this.ToResult("充值成功")
|
|
: this.ToFailResult<string>(message);
|
|
}
|
|
|
|
[HttpPost("deduct")]
|
|
public async Task<BaseResponse<string>> Deduct([FromBody] BackgroundOperationRequest request)
|
|
{
|
|
if (request == null || string.IsNullOrWhiteSpace(request.TargetUserId))
|
|
return this.ToFailResult<string>("目标用户ID不能为空");
|
|
|
|
if (request.Amount <= 0)
|
|
return this.ToFailResult<string>("操作数量必须大于0");
|
|
|
|
var (success, message) = await _operationService.Deduct(
|
|
loginUser.UserId, request.TargetUserId, request.CurrencyType, request.Amount, request.Remark);
|
|
|
|
return success
|
|
? this.ToResult("扣除成功")
|
|
: this.ToFailResult<string>(message);
|
|
}
|
|
|
|
[HttpGet("logs")]
|
|
public async Task<PageResponse<BackgroundOperationLog>> GetLogs(
|
|
string? adminUserId = null,
|
|
string? targetUserId = null,
|
|
int? operationType = null,
|
|
int pageIndex = 1,
|
|
int pageSize = 20)
|
|
{
|
|
var (data, total) = await _operationService.QueryLogs(
|
|
adminUserId, targetUserId, operationType, pageIndex, pageSize);
|
|
|
|
return this.ToPageResult(data, total);
|
|
}
|
|
|
|
[HttpGet("search-user")]
|
|
public async Task<BaseResponse<SearchUserResponse>> SearchUser(string keyword)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(keyword))
|
|
{
|
|
|
|
return this.ToFailResult<SearchUserResponse>("搜索关键词不能为空");
|
|
}
|
|
|
|
var user = await _operationService.SearchUser(keyword);
|
|
|
|
if (user == null)
|
|
{
|
|
return this.ToFailResult<SearchUserResponse>("用户不存在");
|
|
}
|
|
|
|
var dto = new SearchUserResponse
|
|
{
|
|
UserId = user.Id,
|
|
UserName = user.BaseInfo?.UserName ?? "",
|
|
UserPhone = user.BaseInfo?.Mobile ?? "",
|
|
Gold = user.Assets?.Gold ?? 0,
|
|
Diamond = user.Assets?.Diamond ?? 0,
|
|
RegisterTime = user.CreateTime / 1000
|
|
};
|
|
|
|
return this.ToResult(dto);
|
|
}
|
|
|
|
}
|
|
|
|
public class SearchUserResponse
|
|
{
|
|
public string UserId { get; set; }
|
|
public string UserName { get; set; }
|
|
public string UserPhone { get; set; }
|
|
public long Gold { get; set; }
|
|
public int Diamond { get; set; }
|
|
public long RegisterTime { get; set; }
|
|
}
|
|
}
|