using Dapr;
using Hotline.Application.TimeLimits;
using Hotline.CallCenter.BlackLists;
using Hotline.CallCenter.Devices;
using Hotline.CallCenter.Ivrs;
using Hotline.Identity.Roles;
using Hotline.Realtimes;
using Hotline.Repository.SqlSugar;
using Hotline.Share.Dtos.Realtime;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Enums.Settings;
using Hotline.Users;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using NewRock.Sdk;
using NewRock.Sdk.Security;
using SqlSugar;
using Wex.Sdk;
using Wex.Sdk.Tel;
using XC.RSAUtil;
using XF.Domain.Authentications;
using XF.Domain.Cache;
using XF.Domain.Exceptions;
using XF.Domain.Locks;
namespace Hotline.Api.Controllers;
///
///
///
[AllowAnonymous]
public class TestController : BaseController
{
private readonly ILogger _logger;
private readonly IAuthorizeGenerator _authorizeGenerator;
private readonly IOptionsSnapshot _options;
private readonly ISessionContext _sessionContext;
private readonly IUserRepository _userRepository;
private readonly ITypedCache _cache;
private readonly IRealtimeService _realtimeService;
private readonly IBlacklistDomainService _blacklistDomainService;
private readonly IIvrDomainService _ivrDomainService;
private readonly ISugarUnitOfWork _uow;
private readonly IRoleRepository _roleRepository;
private readonly IMediator _mediator;
private readonly ITimeLimitApplication _timeLimitApplication;
private readonly IDistributedLock _distributedLock;
private readonly IWexClient _wexClient;
private readonly IGroupManager _goupManager;
//private readonly ITypedCache> _cache;
//private readonly ICacheManager _cache;
public TestController(
INewRockClient client,
ILogger logger,
IAuthorizeGenerator authorizeGenerator,
IOptionsSnapshot options,
ISessionContext sessionContext,
IUserRepository userRepository,
//ICacheManager cache
//ITypedCache> cache
ITypedCache cache,
IRealtimeService realtimeService,
IBlacklistDomainService blacklistDomainService,
IIvrDomainService ivrDomainService,
ISugarUnitOfWork uow,
IRoleRepository roleRepository,
IMediator mediator,
ITimeLimitApplication timeLimitApplication,
IDistributedLock distributedLock,
IWexClient wexClient
)
{
_logger = logger;
_authorizeGenerator = authorizeGenerator;
_options = options;
_sessionContext = sessionContext;
_userRepository = userRepository;
_cache = cache;
_realtimeService = realtimeService;
_blacklistDomainService = blacklistDomainService;
_ivrDomainService = ivrDomainService;
_uow = uow;
_roleRepository = roleRepository;
_mediator = mediator;
_timeLimitApplication = timeLimitApplication;
_distributedLock = distributedLock;
_wexClient = wexClient;
}
[HttpGet("time")]
public async Task GetTime()
{
//var rsp = await _wexClient.QueryTelsAsync(new QueryTelRequest { StaffNo = "123" }, HttpContext.RequestAborted);
return DateTime.Now.ToString("F");
}
[HttpGet("pgsql")]
public async Task Pgsql()
{
var role = new Role
{
Name = $"test_role_{TimeOnly.FromDateTime(DateTime.Now)}",
DisplayName = "test_role_display",
ClientId = "test"
};
var roleId = await _roleRepository.AddAsync(role, HttpContext.RequestAborted);
role.Description = "Description";
await _roleRepository.UpdateAsync(role, HttpContext.RequestAborted);
return roleId;
}
[AllowAnonymous]
[HttpGet("roles")]
public async Task> GetRoles()
{
using var lockManager = new LockManager(_distributedLock, $"{nameof(TestController)}.{nameof(GetRoles)}");
lockManager.InvokeInLock(() =>
{
Console.WriteLine("do something");
}, TimeSpan.FromSeconds(10));
await lockManager.InvokeInLockAsync(
d => _roleRepository.Queryable(includeDeleted: true).ToListAsync(),
TimeSpan.FromSeconds(10),
HttpContext.RequestAborted);
var a = await _roleRepository.Queryable()
//.Where(d => !d.IsDeleted)
.ToListAsync();
//var a = await db.Queryable().ToListAsync();
var b = await _roleRepository.Queryable(includeDeleted: true).ToListAsync();
return a;
}
[AllowAnonymous]
[HttpGet("testtime")]
public async Task TestTime(DateTime beginTime, ETimeType timeType, int timeValue)
{
return _timeLimitApplication.CalcEndTime(beginTime, timeType, timeValue);
}
[AllowAnonymous]
[HttpGet("timeend")]
public async Task GetWorkDayEnd(DateTime date, int days)
{
return _timeLimitApplication.GetEndDateWork(date, days);
}
//[AllowAnonymous]
[HttpGet("hash")]
public async Task Hash()
{
var s = _sessionContext;
}
///
/// signalR测试(method: Ring)
///
///
[HttpGet("ring")]
public async Task RingTest()
{
await _realtimeService.RingAsync(_sessionContext.RequiredUserId, new RingDto { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
}
///
/// signalR测试(method: Answered)
///
///
[HttpGet("answered")]
public async Task AnsweredTest()
{
await _realtimeService.AnsweredAsync(_sessionContext.RequiredUserId, new AnsweredDto() { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
}
///
/// signalR测试(method: Bye)
///
///
[HttpGet("bye")]
public async Task ByeTest()
{
await _realtimeService.ByeAsync(_sessionContext.RequiredUserId, new ByeDto() { Id = new Guid().ToString() }, HttpContext.RequestAborted);
}
///
///
///
///
[AllowAnonymous]
[HttpGet("t2")]
public async Task GetVoiceEndAnswerAsyncTest()
{
//var answer = await _ivrDomainService.GetVoiceEndAnswerAsync("3", HttpContext.RequestAborted);
//Console.WriteLine(answer.Content);
throw new UserFriendlyException(2001, "test");
}
[AllowAnonymous]
[HttpGet("wfdefine")]
public async Task GetWorkflowDefine()
{
}
[AllowAnonymous]
[Topic("pubsub", "test")]
[HttpPost("t3")]
public Task TestDaprPubsub(int data)
{
_logger.LogDebug("receive dapr event, params: {0}", data);
return Task.FromResult(data);
}
[HttpGet("rsa")]
public async Task Rsa()
{
var keyList = RsaKeyGenerator.Pkcs1Key(2048, true);
var privateKey = keyList[0];
var publicKey = keyList[1];
return $"{publicKey} \r\n {privateKey}";
}
}