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> Charge([FromBody] BackgroundOperationRequest request) { if (request == null || string.IsNullOrWhiteSpace(request.TargetUserId)) return this.ToFailResult("目标用户ID不能为空"); if (request.Amount <= 0) return this.ToFailResult("操作数量必须大于0"); var (success, message) = await _operationService.Charge( loginUser.UserId, request.TargetUserId, request.CurrencyType, request.Amount, request.Remark); return success ? this.ToResult("充值成功") : this.ToFailResult(message); } [HttpPost("deduct")] public async Task> Deduct([FromBody] BackgroundOperationRequest request) { if (request == null || string.IsNullOrWhiteSpace(request.TargetUserId)) return this.ToFailResult("目标用户ID不能为空"); if (request.Amount <= 0) return this.ToFailResult("操作数量必须大于0"); var (success, message) = await _operationService.Deduct( loginUser.UserId, request.TargetUserId, request.CurrencyType, request.Amount, request.Remark); return success ? this.ToResult("扣除成功") : this.ToFailResult(message); } [HttpGet("logs")] public async Task> 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> SearchUser(string keyword) { if (string.IsNullOrWhiteSpace(keyword)) { return this.ToFailResult("搜索关键词不能为空"); } var user = await _operationService.SearchUser(keyword); if (user == null) { return this.ToFailResult("用户不存在"); } 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; } } }