MiddleRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Dapper;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Exceptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity.Middle;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Request.Notice;
  11. using Microsoft.Extensions.Options;
  12. using SqlKata;
  13. using Datory;
  14. using GxPress.Request.App.Middle;
  15. using System;
  16. using GxPress.Request.Inbox;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class MiddleRepository : IMiddleRepository
  20. {
  21. private readonly Repository<Middle> _repository;
  22. private readonly IMapper _mapper;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypeStr;
  25. public MiddleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType =
  30. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<Middle>(database);
  33. _mapper = mapper;
  34. }
  35. public IDatabase Database => _repository.Database;
  36. public string TableName => _repository.TableName;
  37. public List<TableColumn> TableColumns => _repository.TableColumns;
  38. /// <summary>
  39. /// 添加
  40. /// </summary>
  41. /// <param name="middle"></param>
  42. /// <returns></returns>
  43. public async Task<int> InsertAsync(Middle middle)
  44. {
  45. middle.IsTop = false;
  46. middle.IsDelete = false;
  47. middle.IsRead = false;
  48. return await _repository.InsertAsync(middle);
  49. }
  50. /// <summary>
  51. /// 添加
  52. /// </summary>
  53. /// <param name="middle"></param>
  54. /// <returns></returns>
  55. public async Task<bool> InsertAsync(List<Middle> middles)
  56. {
  57. if (middles.Count == 0)
  58. return false;
  59. var sql = "INSERT INTO `tede_middle`(`Guid`,`CreatedDate`,`LastModifiedDate`,`FolderName`,`FolderType`,`IsTop`,`RoleId`,`Sort`,`MiddleId`,`ParentId`,`UserId`,`MiddleSonId`,`IsRead`,`NoticeAddresseeType`,`IsAdmin`,`IsDelete`,`AttributeValue`,`IsDefault`,`IsUpload`,`Title`,`TitleLong`,`Content`,`AddressUser`,`CcUser`,`AddressUserComplete`,`CcUserComplete`,`IsRecall`)VALUES";
  60. foreach (var item in middles)
  61. {
  62. var guId = Guid.NewGuid().ToString();
  63. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  64. var isRead = item.IsRead == true ? 1 : 0;
  65. var isDefault = item.IsDefault == true ? 1 : 0;
  66. var isDelete = item.IsDelete == true ? 1 : 0;
  67. var isAdmin = item.IsAdmin == true ? 1 : 0;
  68. var isRecall = item.IsRecall == true ? 1 : 0;
  69. sql += $"('{guId}','{createdDate}','{createdDate}','{item.FolderName}',{item.FolderType},{item.IsTop},{item.RoleId},{item.Sort},{item.MiddleId},{item.ParentId},{item.UserId},{item.MiddleSonId},{isRead},{item.NoticeAddresseeType.GetHashCode()},{isAdmin},{isDelete},{item.AttributeValue},{isDefault},0,'{item.Title}','{item.TitleLong}','{item.Content}','{item.AddressUser}','{item.CcUser}','{item.AddressUserComplete}','{item.CcUserComplete}',{isRecall}),";
  70. }
  71. sql = sql.Remove(sql.Length - 1, 1);
  72. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  73. var database = new Database(databaseType, _connectionString);
  74. var connection = database.GetConnection();
  75. var items = await connection.ExecuteScalarAsync<int>(sql);
  76. return items > 0;
  77. }
  78. /// <summary>
  79. /// 修改
  80. /// </summary>
  81. /// <param name="query"></param>
  82. /// <returns></returns>
  83. public async Task<bool> UpdateAsync(Query query)
  84. {
  85. return await _repository.UpdateAsync(query) > 0;
  86. }
  87. public async Task<bool> UpdateAsync(Middle middle)
  88. {
  89. return await _repository.UpdateAsync(middle);
  90. }
  91. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  92. {
  93. return await _repository.GetAllAsync(query);
  94. }
  95. public async Task<bool> DeleteAsync(Query query)
  96. {
  97. return await _repository.DeleteAsync(query) > 0;
  98. }
  99. public async Task<bool> DeleteAsync(int middleId, int folderTypeId)
  100. {
  101. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0;
  102. }
  103. public async Task<Middle> GetMiddleAsync(Query query)
  104. {
  105. return await _repository.GetAsync(query);
  106. }
  107. public async Task<Middle> GetMiddleAsync(int id)
  108. {
  109. return await _repository.GetAsync(id);
  110. }
  111. /// <summary>
  112. /// 获取下级数据
  113. /// </summary>
  114. /// <param name="id"></param>
  115. /// <returns></returns>
  116. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  117. {
  118. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  119. }
  120. /// <summary>
  121. /// 设置排序
  122. /// </summary>
  123. /// <param name="request"></param>
  124. /// <returns></returns>
  125. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  126. {
  127. //获取第一个
  128. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  129. //获取第二个数据
  130. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  131. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  132. var database = new Database(databaseType, _connectionString);
  133. var connection = database.GetConnection();
  134. if (middleFirst.Sort < middleSecond.Sort)
  135. {
  136. //修改第一个排序
  137. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  138. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  139. int sort = middleSecond.Sort;
  140. var sql =
  141. $"update tede_middle set Sort=Sort-1 where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort<={sort} and Id not in({middleFirst.Id}) and id>0";
  142. return await connection.ExecuteAsync(sql) > 0;
  143. }
  144. else
  145. {
  146. //修改第一个排序
  147. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  148. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  149. int sort = middleSecond.Sort;
  150. var sql =
  151. $"update tede_middle set Sort=Sort+1 where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort<={sort} and Id not in({middleFirst.Id}) and id>0";
  152. return await connection.ExecuteAsync(sql) > 0;
  153. }
  154. }
  155. /// <summary>
  156. /// 设置置顶
  157. /// </summary>
  158. /// <param name="id"></param>
  159. /// <returns></returns>
  160. public async Task<bool> SetTopAsync(int id)
  161. {
  162. var middle = await _repository.GetAsync(id);
  163. if (middle == null)
  164. throw new BusinessException("数据不存在");
  165. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  166. var database = new Database(databaseType, _connectionString);
  167. var connection = database.GetConnection();
  168. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  169. // var isTop = middle.IsTop == true ? 0 : 1;
  170. // var sql = $"update tede_middle set IsTop={isTop} where id={id}";
  171. // return await connection.ExecuteAsync(sql) > 0;
  172. }
  173. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  174. {
  175. if (request.IsDelete)
  176. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  177. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  178. }
  179. /// <summary>
  180. /// 恢复删除
  181. /// </summary>
  182. /// <param name="request"></param>
  183. /// <returns></returns>
  184. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  185. {
  186. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  187. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  188. }
  189. /// <summary>
  190. /// 阅读
  191. /// </summary>
  192. /// <param name="id"></param>
  193. /// <returns></returns>
  194. public async Task<bool> ReadAsync(int id)
  195. {
  196. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  197. .Where(nameof(Middle.Id), id)) > 0;
  198. }
  199. /// <summary>
  200. /// 修改文件夹名称
  201. /// </summary>
  202. /// <returns></returns>
  203. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  204. {
  205. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  206. .Where(nameof(Middle.Id), request.Id)) > 0;
  207. }
  208. public async Task<bool> IsFolderAsync(int parentId)
  209. {
  210. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  211. }
  212. /// <summary>
  213. /// 根据用户ID文件夹ID查询小组数量
  214. /// </summary>
  215. /// <param name="userId"></param>
  216. /// <param name="groupFolderId"></param>
  217. /// <param name="folderTypeId"></param>
  218. /// <returns></returns>
  219. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  220. {
  221. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  222. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  223. }
  224. public async Task<int> CountAsync(Query query)
  225. {
  226. return await _repository.CountAsync(query);
  227. }
  228. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  229. {
  230. return await _repository.GetAllAsync(query);
  231. }
  232. public async Task<IEnumerable<Middle>> FindAsync(InboxSearchRequest request)
  233. {
  234. var query = new SqlKata.Query();
  235. if (request.Type > 0)
  236. {
  237. query.Where(nameof(Middle.FolderType), request.Type);
  238. }
  239. else
  240. {
  241. var listType = new List<int> { GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Submitted.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Approval.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Cc.GetHashCode() };
  242. query.WhereIn(nameof(Middle.FolderType), listType);
  243. }
  244. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  245. {
  246. query.OrWhere(nameof(Middle.TitleLong), request.KeyWord);
  247. query.OrWhere(nameof(Middle.AddressUserComplete), request.KeyWord);
  248. query.OrWhere(nameof(Middle.CcUserComplete), request.KeyWord);
  249. query.OrWhere(nameof(Middle.Content), request.KeyWord);
  250. }
  251. query.Where(nameof(Middle.UserId), request.UserId);
  252. return await _repository.GetAllAsync(query.Select("Id", "Guid", "CreatedDate", "LastModifiedDate", "FolderName", "FolderType", "IsTop", "RoleId", "Sort", "MiddleId", "ParentId", "UserId", "MiddleSonId", "IsRead", "NoticeAddresseeType", "IsAdmin", "IsDelete", "AttributeValue", "IsDefault", "IsUpload", "Title", "AddressUser", "CcUser"));
  253. }
  254. }
  255. }