MiddleRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. using GxPress.EnumConst;
  19. namespace GxPress.Repository.Implement
  20. {
  21. public class MiddleRepository : IMiddleRepository
  22. {
  23. private readonly Repository<Middle> _repository;
  24. private readonly IMapper _mapper;
  25. private readonly string _connectionString;
  26. private readonly string _databaseTypeStr;
  27. public MiddleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  28. {
  29. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  30. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  31. var databaseType =
  32. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  33. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  34. _repository = new Repository<Middle>(database);
  35. _mapper = mapper;
  36. }
  37. public IDatabase Database => _repository.Database;
  38. public string TableName => _repository.TableName;
  39. public List<TableColumn> TableColumns => _repository.TableColumns;
  40. /// <summary>
  41. /// 添加
  42. /// </summary>
  43. /// <param name="middle"></param>
  44. /// <returns></returns>
  45. public async Task<int> InsertAsync(Middle middle)
  46. {
  47. middle.IsTop = false;
  48. middle.IsDelete = false;
  49. middle.IsRead = false;
  50. return await _repository.InsertAsync(middle);
  51. }
  52. public async Task<bool> ExistsAsync(SqlKata.Query query)
  53. {
  54. return await _repository.ExistsAsync(query);
  55. }
  56. /// <summary>
  57. /// 添加
  58. /// </summary>
  59. /// <param name="middle"></param>
  60. /// <returns></returns>
  61. public async Task<bool> InsertAsync(List<Middle> middles)
  62. {
  63. if (middles.Count == 0)
  64. return false;
  65. 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`,`IsRecall`)VALUES";
  66. foreach (var item in middles)
  67. {
  68. var guId = Guid.NewGuid().ToString();
  69. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  70. var isRead = item.IsRead == true ? 1 : 0;
  71. var isDefault = item.IsDefault == true ? 1 : 0;
  72. var isDelete = item.IsDelete == true ? 1 : 0;
  73. var isAdmin = item.IsAdmin == true ? 1 : 0;
  74. var isRecall = item.IsRecall == true ? 1 : 0;
  75. 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,{isRecall}),";
  76. }
  77. sql = sql.Remove(sql.Length - 1, 1);
  78. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  79. var database = new Database(databaseType, _connectionString);
  80. var connection = database.GetConnection();
  81. var items = await connection.ExecuteScalarAsync<int>(sql);
  82. return items > 0;
  83. }
  84. /// <summary>
  85. /// 修改
  86. /// </summary>
  87. /// <param name="query"></param>
  88. /// <returns></returns>
  89. public async Task<bool> UpdateAsync(Query query)
  90. {
  91. return await _repository.UpdateAsync(query) > 0;
  92. }
  93. public async Task<bool> UpdateAsync(Middle middle)
  94. {
  95. return await _repository.UpdateAsync(middle);
  96. }
  97. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  98. {
  99. return await _repository.GetAllAsync(query);
  100. }
  101. public async Task<bool> DeleteAsync(Query query)
  102. {
  103. return await _repository.DeleteAsync(query) > 0;
  104. }
  105. public async Task<bool> DeleteAsync(int middleId, int folderTypeId)
  106. {
  107. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0;
  108. }
  109. public async Task<Middle> GetMiddleAsync(Query query)
  110. {
  111. return await _repository.GetAsync(query);
  112. }
  113. public async Task<Middle> GetMiddleAsync(int id)
  114. {
  115. return await _repository.GetAsync(id);
  116. }
  117. /// <summary>
  118. /// 获取下级数据
  119. /// </summary>
  120. /// <param name="id"></param>
  121. /// <returns></returns>
  122. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  123. {
  124. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  125. }
  126. /// <summary>
  127. /// 设置排序
  128. /// </summary>
  129. /// <param name="request"></param>
  130. /// <returns></returns>
  131. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  132. {
  133. //获取第一个
  134. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  135. //获取第二个数据
  136. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  137. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  138. var database = new Database(databaseType, _connectionString);
  139. var connection = database.GetConnection();
  140. var lastModifiedDate = Convert.ToDateTime(middleSecond.LastModifiedDate);
  141. var sql = string.Empty;
  142. if (middleFirst.LastModifiedDate < middleSecond.LastModifiedDate)
  143. {
  144. lastModifiedDate = lastModifiedDate.AddTicks(10000000);
  145. sql =
  146. $"update tede_middle set LastModifiedDate='{lastModifiedDate.ToString()}' where Id={middleFirst.Id}";
  147. }
  148. else
  149. {
  150. lastModifiedDate = lastModifiedDate.AddTicks(-10000000);
  151. sql =
  152. $"update tede_middle set LastModifiedDate='{lastModifiedDate.ToString()}' where Id={middleFirst.Id}";
  153. }
  154. return await connection.ExecuteAsync(sql) > 0;
  155. }
  156. /// <summary>
  157. /// 设置置顶
  158. /// </summary>
  159. /// <param name="id"></param>
  160. /// <returns></returns>
  161. public async Task<bool> SetTopAsync(int id)
  162. {
  163. var middle = await _repository.GetAsync(id);
  164. if (middle == null)
  165. throw new BusinessException("数据不存在");
  166. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  167. var database = new Database(databaseType, _connectionString);
  168. var connection = database.GetConnection();
  169. var isTop = middle.IsTop == true ? 0 : 1;
  170. var sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{middle.CreatedDate.ToString()}' where id={id}";
  171. if (middle.IsTop == false)
  172. {
  173. sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{DateTime.Now.ToString()}' where id={id}";
  174. return await connection.ExecuteAsync(sql) > 0;
  175. }
  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.TopicNote.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. query.Where(nameof(Middle.UserId), request.UserId);
  260. 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"));
  261. }
  262. /// <summary>
  263. /// 排除不是文件夹的ID
  264. /// </summary>
  265. /// <param name="ids"></param>
  266. /// <returns></returns>
  267. public async Task<IEnumerable<int>> GetMiddleIsFolderAsync(IEnumerable<int> ids)
  268. {
  269. var query = Q.NewQuery();
  270. query.Select(nameof(Entity.Middle.Middle.Id));
  271. query.WhereIn(nameof(Entity.Middle.Middle.Id), ids);
  272. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  273. return await _repository.GetAllAsync<int>(query);
  274. }
  275. /// <summary>
  276. /// 递归获取父级下面的所有子集
  277. /// </summary>
  278. /// <param name="id"></param>
  279. /// <param name="ids"></param>
  280. /// <returns></returns>
  281. public async Task<IEnumerable<int>> GetAllMiddleChildrenByParentIdAsync(List<int> ids, List<int> returnIds)
  282. {
  283. var middles = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  284. if (middles == null || middles.Count == 0)
  285. return returnIds;
  286. foreach (var item in middles)
  287. {
  288. middles = await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  289. if (middles == null || middles.Count == 0)
  290. continue;
  291. returnIds.AddRange(middles.Select(n => n.Id).ToList());
  292. foreach (var middle in middles)
  293. {
  294. await GetAllMiddleChildrenByParentIdAsync(new List<int> { middle.Id }, returnIds);
  295. }
  296. }
  297. return returnIds;
  298. }
  299. /// <summary>
  300. /// 清空草稿箱
  301. /// </summary>
  302. /// <param name="userId"></param>
  303. /// <param name="typeValue"></param>
  304. /// <returns></returns>
  305. public async Task<bool> ClearDraftAsync(int userId, int typeValue)
  306. {
  307. //获取用户草稿
  308. var sql = $"select Id from tede_note where UserId={userId} and IsDraft=1";
  309. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  310. var database = new Database(databaseType, _connectionString);
  311. var connection = database.GetConnection();
  312. var noteIds = await connection.QueryAsync<int>(sql);
  313. var query = Q.NewQuery();
  314. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  315. query.WhereIn(nameof(Entity.Middle.Middle.MiddleId), noteIds);
  316. query.Where(nameof(Entity.Middle.Middle.FolderType), typeValue == 1 ? AllTypeConst.TopicNote.GetHashCode() : AllTypeConst.Note.GetHashCode());
  317. query.Set(nameof(Entity.Middle.Middle.IsDelete), true);
  318. return await UpdateAsync(query);
  319. }
  320. }
  321. }