TestController.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Hotline.Application.TimeLimits;
  2. using Hotline.CallCenter.BlackLists;
  3. using Hotline.CallCenter.Devices;
  4. using Hotline.CallCenter.Ivrs;
  5. using Hotline.Identity.Roles;
  6. using Hotline.Orders;
  7. using Hotline.Realtimes;
  8. using Hotline.Repository.SqlSugar;
  9. using Hotline.Repository.SqlSugar.Ts;
  10. using Hotline.Settings.TimeLimits;
  11. using Hotline.Share.Dtos.Realtime;
  12. using Hotline.Share.Dtos.Settings;
  13. using Hotline.Share.Enums.Settings;
  14. using Hotline.Users;
  15. using MediatR;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Microsoft.AspNetCore.SignalR;
  19. using Microsoft.Extensions.Options;
  20. using NewRock.Sdk;
  21. using NewRock.Sdk.Security;
  22. using NpgsqlTypes;
  23. using SqlSugar;
  24. using Wex.Sdk;
  25. using Wex.Sdk.Tel;
  26. using XC.RSAUtil;
  27. using XF.Domain.Authentications;
  28. using XF.Domain.Cache;
  29. using XF.Domain.Exceptions;
  30. using XF.Domain.Filters;
  31. using XF.Domain.Locks;
  32. using XF.Domain.Repository;
  33. namespace Hotline.Api.Controllers;
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. [AllowAnonymous]
  38. public class TestController : BaseController
  39. {
  40. private readonly ILogger<TestController> _logger;
  41. private readonly IAuthorizeGenerator _authorizeGenerator;
  42. private readonly IOptionsSnapshot<CallCenterConfiguration> _options;
  43. private readonly ISessionContext _sessionContext;
  44. private readonly IRepository<User> _userRepository;
  45. private readonly ITypedCache<User> _cache;
  46. private readonly IRealtimeService _realtimeService;
  47. private readonly IBlacklistDomainService _blacklistDomainService;
  48. private readonly IIvrDomainService _ivrDomainService;
  49. private readonly ISugarUnitOfWork<HotlineDbContext> _uow;
  50. private readonly IRepository<Role> _roleRepository;
  51. private readonly IMediator _mediator;
  52. private readonly ITimeLimitApplication _timeLimitApplication;
  53. private readonly IDistributedLock _distributedLock;
  54. private readonly IWexClient _wexClient;
  55. private readonly ISugarUnitOfWork<WexDbContext> _uowWex;
  56. private readonly IRepository<OrderUrge> _orderUrgeRepository;
  57. private readonly IRepositoryTextSearch<OrderTs> _repositoryts;
  58. private readonly IGroupManager _goupManager;
  59. private readonly ITimeLimitDomainService _timeLimitDomainService;
  60. //private readonly ITypedCache<List<User>> _cache;
  61. //private readonly ICacheManager<User> _cache;
  62. public TestController(
  63. INewRockClient client,
  64. ILogger<TestController> logger,
  65. IAuthorizeGenerator authorizeGenerator,
  66. IOptionsSnapshot<CallCenterConfiguration> options,
  67. ISessionContext sessionContext,
  68. IRepository<User> userRepository,
  69. //ICacheManager<User> cache
  70. //ITypedCache<List<User>> cache
  71. ITypedCache<User> cache,
  72. IRealtimeService realtimeService,
  73. IBlacklistDomainService blacklistDomainService,
  74. IIvrDomainService ivrDomainService,
  75. ISugarUnitOfWork<HotlineDbContext> uow,
  76. IRepository<Role> roleRepository,
  77. IMediator mediator,
  78. ITimeLimitApplication timeLimitApplication,
  79. IDistributedLock distributedLock,
  80. IWexClient wexClient,
  81. ISugarUnitOfWork<WexDbContext> uowWex,
  82. IRepository<OrderUrge> orderUrgeRepository,
  83. IRepositoryTextSearch<OrderTs> repositoryts,
  84. ITimeLimitDomainService timeLimitDomainService)
  85. {
  86. _logger = logger;
  87. _authorizeGenerator = authorizeGenerator;
  88. _options = options;
  89. _sessionContext = sessionContext;
  90. _userRepository = userRepository;
  91. _cache = cache;
  92. _realtimeService = realtimeService;
  93. _blacklistDomainService = blacklistDomainService;
  94. _ivrDomainService = ivrDomainService;
  95. _uow = uow;
  96. _roleRepository = roleRepository;
  97. _mediator = mediator;
  98. _timeLimitApplication = timeLimitApplication;
  99. _distributedLock = distributedLock;
  100. _wexClient = wexClient;
  101. _uowWex = uowWex;
  102. _orderUrgeRepository = orderUrgeRepository;
  103. _repositoryts = repositoryts;
  104. _timeLimitDomainService = timeLimitDomainService;
  105. }
  106. [HttpGet("time")]
  107. public async Task<OpenResponse> GetTime()
  108. {
  109. //await _repositoryts.AddVectorAsync("f595e730-909a-45e4-9138-a84bf15f4662", DateTime.Now,
  110. // new List<string> { "xx", "bb" }, HttpContext.RequestAborted);
  111. //var result = await _repositoryts.SearchAsync(new List<string> { "bb" }, HttpContext.RequestAborted);
  112. //await _repositoryts.UpdateVectorAsync("f595e730-909a-45e4-9138-a84bf15f4662",
  113. // new List<string> { "ss", "bb" }, HttpContext.RequestAborted);
  114. //_uow.Db.Ado.SqlQueryAsync<OrderUrge>("SELECT to_tsvector('fat cats ate fat rats') @@ to_tsquery('fat & rat')")
  115. //var tests = _uowWex.Db.Ado.SqlQuery<Test>("select * from view_telinfo_full where IsSynch=@IsSynch", new { IsSynch = 0 });
  116. //var a = _uowWex.Db.Ado.ExecuteCommand("update telinfo set IsSynch=1 where GUID=@CallId", new { CallId = "b1f97f3c-88b6-4f42-b8de-83ed448854b0" });
  117. //var rsp = await _wexClient.QueryTelsAsync(new QueryTelRequest { }, HttpContext.RequestAborted);
  118. //int a = _timeLimitDomainService.CalcWorkTime(DateTime.Now, DateTime.Parse("2023-09-11 16:21:00"));
  119. int m = _timeLimitDomainService.CalcWorkTime(DateTime.Parse("2023-09-19 12:00:00"), DateTime.Parse("2023-09-20 10:00:00"), true);
  120. return OpenResponse.Ok(DateTime.Now.ToString("F"));
  121. //var rsp = await _daprClient.InvokeMethodAsync<ApiResponse<string>>(HttpMethod.Get, "identity", "api/v1/Test/time", HttpContext.RequestAborted);
  122. //var rsp1 = await _daprClient.InvokeMethodAsync<int, ApiResponse<string>>(HttpMethod.Post, "identity", "api/v1/Test/time1", 222, HttpContext.RequestAborted);
  123. }
  124. [HttpGet("pgsql")]
  125. public async Task<string> Pgsql()
  126. {
  127. var role = new Role
  128. {
  129. Name = $"test_role_{TimeOnly.FromDateTime(DateTime.Now)}",
  130. DisplayName = "test_role_display",
  131. ClientId = "test"
  132. };
  133. var roleId = await _roleRepository.AddAsync(role, HttpContext.RequestAborted);
  134. role.Description = "Description";
  135. await _roleRepository.UpdateAsync(role, HttpContext.RequestAborted);
  136. return roleId;
  137. }
  138. [AllowAnonymous]
  139. [HttpGet("roles")]
  140. public async Task<List<Role>> GetRoles()
  141. {
  142. using var lockManager = new LockManager(_distributedLock, $"{nameof(TestController)}.{nameof(GetRoles)}");
  143. lockManager.InvokeInLock(() =>
  144. {
  145. Console.WriteLine("do something");
  146. }, TimeSpan.FromSeconds(10));
  147. await lockManager.InvokeInLockAsync(
  148. d => _roleRepository.Queryable(includeDeleted: true).ToListAsync(),
  149. TimeSpan.FromSeconds(10),
  150. HttpContext.RequestAborted);
  151. var a = await _roleRepository.Queryable()
  152. //.Where(d => !d.IsDeleted)
  153. .ToListAsync();
  154. //var a = await db.Queryable<Role>().ToListAsync();
  155. var b = await _roleRepository.Queryable(includeDeleted: true).ToListAsync();
  156. return a;
  157. }
  158. [AllowAnonymous]
  159. [HttpGet("testtime")]
  160. public async Task<TimeResult> TestTime(DateTime beginTime, ETimeType timeType, int timeValue)
  161. {
  162. return _timeLimitApplication.CalcEndTime(beginTime, timeType, timeValue);
  163. }
  164. [AllowAnonymous]
  165. [HttpGet("timeend")]
  166. public async Task<DateTime> GetWorkDayEnd(DateTime date, int days)
  167. {
  168. return _timeLimitApplication.GetEndDateWork(date, days);
  169. }
  170. //[AllowAnonymous]
  171. [HttpGet("hash")]
  172. public async Task Hash()
  173. {
  174. var s = _sessionContext;
  175. }
  176. /// <summary>
  177. /// signalR测试(method: Ring)
  178. /// </summary>
  179. /// <returns></returns>
  180. [HttpGet("ring")]
  181. public async Task RingTest()
  182. {
  183. await _realtimeService.RingAsync(_sessionContext.RequiredUserId, new RingDto { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
  184. }
  185. /// <summary>
  186. /// signalR测试(method: Answered)
  187. /// </summary>
  188. /// <returns></returns>
  189. [HttpGet("answered")]
  190. public async Task AnsweredTest()
  191. {
  192. await _realtimeService.AnsweredAsync(_sessionContext.RequiredUserId, new AnsweredDto() { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
  193. }
  194. /// <summary>
  195. /// signalR测试(method: Bye)
  196. /// </summary>
  197. /// <returns></returns>
  198. [HttpGet("bye")]
  199. public async Task ByeTest()
  200. {
  201. await _realtimeService.ByeAsync(_sessionContext.RequiredUserId, new ByeDto() { Id = new Guid().ToString() }, HttpContext.RequestAborted);
  202. }
  203. /// <summary>
  204. ///
  205. /// </summary>
  206. /// <returns></returns>
  207. [AllowAnonymous]
  208. [HttpGet("t2")]
  209. public async Task GetVoiceEndAnswerAsyncTest()
  210. {
  211. //var answer = await _ivrDomainService.GetVoiceEndAnswerAsync("3", HttpContext.RequestAborted);
  212. //Console.WriteLine(answer.Content);
  213. throw new UserFriendlyException(2001, "test");
  214. }
  215. [AllowAnonymous]
  216. [HttpGet("wfdefine")]
  217. public async Task GetWorkflowDefine()
  218. {
  219. }
  220. [AllowAnonymous]
  221. [HttpPost("t3")]
  222. public Task<int> TestDaprPubsub(int data)
  223. {
  224. _logger.LogDebug("receive dapr event, params: {0}", data);
  225. return Task.FromResult(data);
  226. }
  227. [HttpGet("rsa")]
  228. public async Task<string> Rsa()
  229. {
  230. var keyList = RsaKeyGenerator.Pkcs1Key(2048, true);
  231. var privateKey = keyList[0];
  232. var publicKey = keyList[1];
  233. return $"{publicKey} \r\n {privateKey}";
  234. }
  235. }
  236. public class Test
  237. {
  238. [SugarColumn(ColumnName = "CallID")]
  239. public string CallId { get; set; }
  240. [SugarColumn(ColumnName = "IVR")]
  241. public string Ivr { get; set; }
  242. }