WaitHandleRepository.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity.WaitHandle;
  10. using GxPress.Repository.Interface.WaitHandle;
  11. using GxPress.Request.App.WaitHandle;
  12. using GxPress.Result.App.WaitHandle;
  13. using Microsoft.Extensions.Options;
  14. using SqlKata;
  15. namespace GxPress.Repository.Implement.WaitHandle
  16. {
  17. public class WaitHandleRepository : IWaitHandleRepository
  18. {
  19. private readonly Repository<Entity.WaitHandle.WaitHandle> _repository;
  20. private readonly IMapper _mapper;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypeStr;
  23. public WaitHandleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  28. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  29. _repository = new Repository<Entity.WaitHandle.WaitHandle>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. public async Task<int> InsertAsync(Entity.WaitHandle.WaitHandle waitHandle)
  36. {
  37. return await _repository.InsertAsync(waitHandle);
  38. }
  39. public async Task<bool> UpdateAsync(Entity.WaitHandle.WaitHandle waitHandle)
  40. {
  41. return await _repository.UpdateAsync(waitHandle);
  42. }
  43. public async Task<bool> UpdateAsync(Query query)
  44. {
  45. return await _repository.UpdateAsync(query) > 0;
  46. }
  47. public async Task<bool> DeleteAsync(int id)
  48. {
  49. return await _repository.DeleteAsync(id);
  50. }
  51. public async Task<bool> DeleteAsync(Query query)
  52. {
  53. return await _repository.DeleteAsync(query) > 0;
  54. }
  55. public async Task<Entity.WaitHandle.WaitHandle> GetAsync(Query query)
  56. {
  57. return await _repository.GetAsync(query);
  58. }
  59. public async Task<Entity.WaitHandle.WaitHandle> GetAsync(int id)
  60. {
  61. return await _repository.GetAsync(id);
  62. }
  63. public async Task<IEnumerable<Entity.WaitHandle.WaitHandle>> GetAllAsync(Query query)
  64. {
  65. return await _repository.GetAllAsync(query);
  66. }
  67. public async Task<int> CountAsync(Query query)
  68. {
  69. return await _repository.CountAsync(query);
  70. }
  71. /// <summary>
  72. /// 查询
  73. /// </summary>
  74. /// <param name="request"></param>
  75. /// <returns></returns>
  76. public async Task<WaitHandleListResult> GetAllAsync(ListWaitHandleRequest request)
  77. {
  78. var @dateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  79. var waitHandlesSql =
  80. $@"
  81. SELECT
  82. a.*,(SELECT
  83. COUNT(1)
  84. FROM
  85. tede_wait_handle
  86. WHERE
  87. DATE_ADD(AlarmTime,
  88. INTERVAL -Minute MINUTE)< NOW() and Id=a.Id and IsFinish=0) IsFinish, b.Title
  89. FROM
  90. tede_wait_handle a
  91. LEFT JOIN
  92. tede_wait_handle_label b ON a.LabelId = b.Id
  93. WHERE
  94. a.UserId = {request.UserId}
  95. and a.IsFinish=0";
  96. var finishWaitHandlesSql =
  97. $@"SELECT
  98. a.*, b.Title
  99. FROM
  100. tede_wait_handle a
  101. LEFT JOIN
  102. tede_wait_handle_label b ON a.LabelId = b.Id
  103. WHERE
  104. a.UserId = {request.UserId}
  105. and a.IsFinish=1";
  106. var result = new WaitHandleListResult();
  107. if (request.SearchType == 1)
  108. {
  109. waitHandlesSql += " order by a.IsFinish desc";
  110. finishWaitHandlesSql += " order by a.IsFinish desc";
  111. }
  112. else if (request.SearchType == 2)
  113. {
  114. waitHandlesSql += " order by a.AlarmTime asc";
  115. finishWaitHandlesSql += " order by a.AlarmTime desc";
  116. }
  117. else if (request.SearchType == 3)
  118. {
  119. waitHandlesSql += " order by a.LabelId desc";
  120. finishWaitHandlesSql += " order by a.LabelId desc";
  121. }
  122. else if (request.SearchType == 4)
  123. {
  124. waitHandlesSql += " order by a.Level desc";
  125. finishWaitHandlesSql += " order by a.Level desc";
  126. }
  127. else
  128. {
  129. waitHandlesSql += " order by a.AlarmTime asc";
  130. finishWaitHandlesSql += " order by a.AlarmTime desc";
  131. }
  132. var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql);
  133. var database = new Database(databaseType, _connectionString);
  134. var connection = database.GetConnection();
  135. var waitHandles =
  136. await connection
  137. .QueryAsync<WaitHandleResult, WaitHandleLabel, WaitHandleResult>(waitHandlesSql,
  138. (waitHandleResult, waitHandleLabel) =>
  139. {
  140. waitHandleResult.LabelName = waitHandleLabel != null ? waitHandleLabel.Title : "";
  141. return waitHandleResult;
  142. }, splitOn: "Title");
  143. result.WaitHandles = waitHandles;
  144. var finishWaitHandles =
  145. await connection
  146. .QueryAsync<WaitHandleResult, WaitHandleLabel, WaitHandleResult>(finishWaitHandlesSql,
  147. (waitHandleResult, waitHandleLabel) =>
  148. {
  149. waitHandleResult.LabelName = waitHandleLabel != null ? waitHandleLabel.Title : "未设置";
  150. return waitHandleResult;
  151. }, splitOn: "Title");
  152. result.FinishWaitHandles = finishWaitHandles;
  153. return result;
  154. }
  155. public async Task<int> GetUFinishAsync(int userId)
  156. {
  157. string sql = $"SELECT count(1) FROM tede_wait_handle WHERE DATE_ADD(AlarmTime,INTERVAL -Minute MINUTE) < NOW() and UserId={userId} and IsFinish=0";
  158. var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql);
  159. var database = new Database(databaseType, _connectionString);
  160. var connection = database.GetConnection();
  161. var count =
  162. await connection.ExecuteScalarAsync<int>(sql);
  163. return count;
  164. }
  165. /// <summary>
  166. /// 获取详情
  167. /// </summary>
  168. /// <param name="id"></param>
  169. /// <returns></returns>
  170. public async Task<Entity.WaitHandle.WaitHandle> GetWaitAsync(int id)
  171. {
  172. var waitHandle = await _repository.GetAsync(id);
  173. if (!string.IsNullOrEmpty(waitHandle.AlarmTime))
  174. {
  175. var AlarmTime = DateTime.Parse(waitHandle.AlarmTime);
  176. var alarmTime = AlarmTime.AddMinutes(-waitHandle.Minute);
  177. //设置
  178. if (alarmTime <= System.DateTime.Now)
  179. {
  180. waitHandle.IsFinish = true;
  181. await _repository.UpdateAsync(waitHandle);
  182. }
  183. }
  184. return waitHandle;
  185. }
  186. }
  187. }