MiddleRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. using System.Linq;
  18. namespace GxPress.Repository.Implement
  19. {
  20. public class MiddleRepository : IMiddleRepository
  21. {
  22. private readonly Repository<Middle> _repository;
  23. private readonly IMapper _mapper;
  24. private readonly string _connectionString;
  25. private readonly string _databaseTypeStr;
  26. public MiddleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  29. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  30. var databaseType =
  31. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  32. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  33. _repository = new Repository<Middle>(database);
  34. _mapper = mapper;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. /// <summary>
  40. /// 添加
  41. /// </summary>
  42. /// <param name="middle"></param>
  43. /// <returns></returns>
  44. public async Task<int> InsertAsync(Middle middle)
  45. {
  46. middle.IsTop = false;
  47. middle.IsDelete = false;
  48. middle.IsRead = false;
  49. return await _repository.InsertAsync(middle);
  50. }
  51. public async Task<bool> ExistsAsync(SqlKata.Query query)
  52. {
  53. return await _repository.ExistsAsync(query);
  54. }
  55. /// <summary>
  56. /// 添加
  57. /// </summary>
  58. /// <param name="middle"></param>
  59. /// <returns></returns>
  60. public async Task<bool> InsertAsync(List<Middle> middles)
  61. {
  62. if (middles.Count == 0)
  63. return false;
  64. 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";
  65. foreach (var item in middles)
  66. {
  67. var guId = Guid.NewGuid().ToString();
  68. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  69. var isRead = item.IsRead == true ? 1 : 0;
  70. var isDefault = item.IsDefault == true ? 1 : 0;
  71. var isDelete = item.IsDelete == true ? 1 : 0;
  72. var isAdmin = item.IsAdmin == true ? 1 : 0;
  73. var isRecall = item.IsRecall == true ? 1 : 0;
  74. 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}),";
  75. }
  76. sql = sql.Remove(sql.Length - 1, 1);
  77. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  78. var database = new Database(databaseType, _connectionString);
  79. var connection = database.GetConnection();
  80. var items = await connection.ExecuteScalarAsync<int>(sql);
  81. return items > 0;
  82. }
  83. /// <summary>
  84. /// 修改
  85. /// </summary>
  86. /// <param name="query"></param>
  87. /// <returns></returns>
  88. public async Task<bool> UpdateAsync(Query query)
  89. {
  90. return await _repository.UpdateAsync(query) > 0;
  91. }
  92. public async Task<bool> UpdateAsync(Middle middle)
  93. {
  94. return await _repository.UpdateAsync(middle);
  95. }
  96. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  97. {
  98. return await _repository.GetAllAsync(query);
  99. }
  100. public async Task<bool> DeleteAsync(Query query)
  101. {
  102. return await _repository.DeleteAsync(query) > 0;
  103. }
  104. public async Task<bool> DeleteAsync(int middleId, int folderTypeId)
  105. {
  106. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0;
  107. }
  108. public async Task<Middle> GetMiddleAsync(Query query)
  109. {
  110. return await _repository.GetAsync(query);
  111. }
  112. public async Task<Middle> GetMiddleAsync(int id)
  113. {
  114. return await _repository.GetAsync(id);
  115. }
  116. /// <summary>
  117. /// 获取下级数据
  118. /// </summary>
  119. /// <param name="id"></param>
  120. /// <returns></returns>
  121. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  122. {
  123. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  124. }
  125. /// <summary>
  126. /// 设置排序
  127. /// </summary>
  128. /// <param name="request"></param>
  129. /// <returns></returns>
  130. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  131. {
  132. //获取第一个
  133. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  134. //获取第二个数据
  135. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  136. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  137. var database = new Database(databaseType, _connectionString);
  138. var connection = database.GetConnection();
  139. if (middleFirst.Sort < middleSecond.Sort)
  140. {
  141. //修改第一个排序
  142. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  143. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  144. int sort = middleSecond.Sort;
  145. var sql =
  146. $"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";
  147. return await connection.ExecuteAsync(sql) > 0;
  148. }
  149. else
  150. {
  151. //修改第一个排序
  152. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  153. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  154. int sort = middleSecond.Sort;
  155. var sql =
  156. $"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";
  157. return await connection.ExecuteAsync(sql) > 0;
  158. }
  159. }
  160. /// <summary>
  161. /// 设置置顶
  162. /// </summary>
  163. /// <param name="id"></param>
  164. /// <returns></returns>
  165. public async Task<bool> SetTopAsync(int id)
  166. {
  167. var middle = await _repository.GetAsync(id);
  168. if (middle == null)
  169. throw new BusinessException("数据不存在");
  170. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  171. var database = new Database(databaseType, _connectionString);
  172. var connection = database.GetConnection();
  173. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  174. // var isTop = middle.IsTop == true ? 0 : 1;
  175. // var sql = $"update tede_middle set IsTop={isTop} where id={id}";
  176. // return await connection.ExecuteAsync(sql) > 0;
  177. }
  178. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  179. {
  180. if (request.IsDelete)
  181. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  182. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  183. }
  184. /// <summary>
  185. /// 恢复删除
  186. /// </summary>
  187. /// <param name="request"></param>
  188. /// <returns></returns>
  189. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  190. {
  191. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  192. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  193. }
  194. /// <summary>
  195. /// 阅读
  196. /// </summary>
  197. /// <param name="id"></param>
  198. /// <returns></returns>
  199. public async Task<bool> ReadAsync(int id)
  200. {
  201. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  202. .Where(nameof(Middle.Id), id)) > 0;
  203. }
  204. /// <summary>
  205. /// 修改文件夹名称
  206. /// </summary>
  207. /// <returns></returns>
  208. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  209. {
  210. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  211. .Where(nameof(Middle.Id), request.Id)) > 0;
  212. }
  213. public async Task<bool> IsFolderAsync(int parentId)
  214. {
  215. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  216. }
  217. /// <summary>
  218. /// 根据用户ID文件夹ID查询小组数量
  219. /// </summary>
  220. /// <param name="userId"></param>
  221. /// <param name="groupFolderId"></param>
  222. /// <param name="folderTypeId"></param>
  223. /// <returns></returns>
  224. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  225. {
  226. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  227. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  228. }
  229. public async Task<int> CountAsync(Query query)
  230. {
  231. return await _repository.CountAsync(query);
  232. }
  233. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  234. {
  235. return await _repository.GetAllAsync(query);
  236. }
  237. public async Task<int> GetSytemFolderAsync(int userId)
  238. {
  239. var query = Q.NewQuery();
  240. query.Select(nameof(Entity.Middle.Middle.Id));
  241. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  242. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  243. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  244. query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.Note.GetHashCode());
  245. return await _repository.GetAsync<int>(query);
  246. }
  247. public async Task<IEnumerable<Middle>> FindAsync(InboxSearchRequest request)
  248. {
  249. var query = new SqlKata.Query();
  250. if (request.Type > 0)
  251. {
  252. query.Where(nameof(Middle.FolderType), request.Type);
  253. }
  254. else
  255. {
  256. 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() };
  257. query.WhereIn(nameof(Middle.FolderType), listType);
  258. }
  259. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  260. {
  261. query.OrWhere(nameof(Middle.TitleLong), request.KeyWord);
  262. query.OrWhere(nameof(Middle.AddressUserComplete), request.KeyWord);
  263. query.OrWhere(nameof(Middle.CcUserComplete), request.KeyWord);
  264. query.OrWhere(nameof(Middle.Content), request.KeyWord);
  265. }
  266. query.Where(nameof(Middle.UserId), request.UserId);
  267. 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"));
  268. }
  269. /// <summary>
  270. /// 排除不是文件夹的ID
  271. /// </summary>
  272. /// <param name="ids"></param>
  273. /// <returns></returns>
  274. public async Task<IEnumerable<int>> GetMiddleIsFolderAsync(IEnumerable<int> ids)
  275. {
  276. var query = Q.NewQuery();
  277. query.Select(nameof(Entity.Middle.Middle.Id));
  278. query.WhereIn(nameof(Entity.Middle.Middle.Id), ids);
  279. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  280. return await _repository.GetAllAsync<int>(query);
  281. }
  282. /// <summary>
  283. /// 递归获取父级下面的所有子集
  284. /// </summary>
  285. /// <param name="id"></param>
  286. /// <param name="ids"></param>
  287. /// <returns></returns>
  288. public async Task<IEnumerable<int>> GetAllMiddleChildrenByParentIdAsync(List<int> ids,List<int> returnIds)
  289. {
  290. var middles = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  291. if (middles == null || middles.Count == 0)
  292. return returnIds;
  293. foreach (var item in middles)
  294. {
  295. middles = await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  296. if (middles == null || middles.Count == 0)
  297. continue;
  298. returnIds.AddRange(middles.Select(n => n.Id).ToList());
  299. foreach (var middle in middles)
  300. {
  301. await GetAllMiddleChildrenByParentIdAsync(new List<int>{middle.Id},returnIds);
  302. }
  303. }
  304. return returnIds;
  305. }
  306. }
  307. }