MiddleRepository.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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`,`Title`,`TitleLong`,`Content`,`AddressUser`,`CcUser`,`AddressUserComplete`,`CcUserComplete`,`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,'{item.Title}','{item.TitleLong}','{item.Content}','{item.AddressUser}','{item.CcUser}','{item.AddressUserComplete}','{item.CcUserComplete}',{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 = DateTime.Now.ToString();
  141. if (middleFirst.Sort < middleSecond.Sort)
  142. {
  143. //修改第一个排序
  144. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  145. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  146. int sort = middleSecond.Sort;
  147. var sql =
  148. $"update tede_middle set Sort=Sort-1,LastModifiedDate='{lastModifiedDate}' where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort<={sort} and Id not in({middleFirst.Id}) and id>0";
  149. return await connection.ExecuteAsync(sql) > 0;
  150. }
  151. else
  152. {
  153. //修改第一个排序
  154. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  155. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  156. int sort = middleSecond.Sort;
  157. var sql =
  158. $"update tede_middle set Sort=Sort+1,LastModifiedDate='{lastModifiedDate}' where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort>={sort} and Id not in({middleFirst.Id}) and id>0";
  159. return await connection.ExecuteAsync(sql) > 0;
  160. }
  161. }
  162. /// <summary>
  163. /// 设置置顶
  164. /// </summary>
  165. /// <param name="id"></param>
  166. /// <returns></returns>
  167. public async Task<bool> SetTopAsync(int id)
  168. {
  169. var middle = await _repository.GetAsync(id);
  170. if (middle == null)
  171. throw new BusinessException("数据不存在");
  172. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  173. var database = new Database(databaseType, _connectionString);
  174. var connection = database.GetConnection();
  175. var isTop = middle.IsTop == true ? 0 : 1;
  176. var sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{middle.CreatedDate.ToString()}' where id={id}";
  177. if (middle.IsTop == false)
  178. {
  179. sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{DateTime.Now.ToString()}' where id={id}";
  180. return await connection.ExecuteAsync(sql) > 0;
  181. }
  182. return await connection.ExecuteAsync(sql) > 0;
  183. }
  184. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  185. {
  186. if (request.IsDelete)
  187. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  188. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  189. }
  190. /// <summary>
  191. /// 恢复删除
  192. /// </summary>
  193. /// <param name="request"></param>
  194. /// <returns></returns>
  195. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  196. {
  197. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  198. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  199. }
  200. /// <summary>
  201. /// 阅读
  202. /// </summary>
  203. /// <param name="id"></param>
  204. /// <returns></returns>
  205. public async Task<bool> ReadAsync(int id)
  206. {
  207. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  208. .Where(nameof(Middle.Id), id)) > 0;
  209. }
  210. /// <summary>
  211. /// 修改文件夹名称
  212. /// </summary>
  213. /// <returns></returns>
  214. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  215. {
  216. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  217. .Where(nameof(Middle.Id), request.Id)) > 0;
  218. }
  219. public async Task<bool> IsFolderAsync(int parentId)
  220. {
  221. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  222. }
  223. /// <summary>
  224. /// 根据用户ID文件夹ID查询小组数量
  225. /// </summary>
  226. /// <param name="userId"></param>
  227. /// <param name="groupFolderId"></param>
  228. /// <param name="folderTypeId"></param>
  229. /// <returns></returns>
  230. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  231. {
  232. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  233. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  234. }
  235. public async Task<int> CountAsync(Query query)
  236. {
  237. return await _repository.CountAsync(query);
  238. }
  239. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  240. {
  241. return await _repository.GetAllAsync(query);
  242. }
  243. public async Task<int> GetSytemFolderAsync(int userId)
  244. {
  245. var query = Q.NewQuery();
  246. query.Select(nameof(Entity.Middle.Middle.Id));
  247. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  248. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  249. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  250. query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode());
  251. return await _repository.GetAsync<int>(query);
  252. }
  253. public async Task<IEnumerable<Middle>> FindAsync(InboxSearchRequest request)
  254. {
  255. var query = new SqlKata.Query();
  256. if (request.Type > 0)
  257. {
  258. query.Where(nameof(Middle.FolderType), request.Type);
  259. }
  260. else
  261. {
  262. 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() };
  263. query.WhereIn(nameof(Middle.FolderType), listType);
  264. }
  265. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  266. {
  267. query.OrWhere(nameof(Middle.TitleLong), request.KeyWord);
  268. query.OrWhere(nameof(Middle.AddressUserComplete), request.KeyWord);
  269. query.OrWhere(nameof(Middle.CcUserComplete), request.KeyWord);
  270. query.OrWhere(nameof(Middle.Content), request.KeyWord);
  271. }
  272. query.Where(nameof(Middle.UserId), request.UserId);
  273. 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"));
  274. }
  275. /// <summary>
  276. /// 排除不是文件夹的ID
  277. /// </summary>
  278. /// <param name="ids"></param>
  279. /// <returns></returns>
  280. public async Task<IEnumerable<int>> GetMiddleIsFolderAsync(IEnumerable<int> ids)
  281. {
  282. var query = Q.NewQuery();
  283. query.Select(nameof(Entity.Middle.Middle.Id));
  284. query.WhereIn(nameof(Entity.Middle.Middle.Id), ids);
  285. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  286. return await _repository.GetAllAsync<int>(query);
  287. }
  288. /// <summary>
  289. /// 递归获取父级下面的所有子集
  290. /// </summary>
  291. /// <param name="id"></param>
  292. /// <param name="ids"></param>
  293. /// <returns></returns>
  294. public async Task<IEnumerable<int>> GetAllMiddleChildrenByParentIdAsync(List<int> ids, List<int> returnIds)
  295. {
  296. var middles = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  297. if (middles == null || middles.Count == 0)
  298. return returnIds;
  299. foreach (var item in middles)
  300. {
  301. middles = await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  302. if (middles == null || middles.Count == 0)
  303. continue;
  304. returnIds.AddRange(middles.Select(n => n.Id).ToList());
  305. foreach (var middle in middles)
  306. {
  307. await GetAllMiddleChildrenByParentIdAsync(new List<int> { middle.Id }, returnIds);
  308. }
  309. }
  310. return returnIds;
  311. }
  312. /// <summary>
  313. /// 清空草稿箱
  314. /// </summary>
  315. /// <param name="userId"></param>
  316. /// <param name="typeValue"></param>
  317. /// <returns></returns>
  318. public async Task<bool> ClearDraftAsync(int userId, int typeValue)
  319. {
  320. //获取用户草稿
  321. var sql = $"select Id from tede_note where UserId={userId} and IsDraft=1";
  322. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  323. var database = new Database(databaseType, _connectionString);
  324. var connection = database.GetConnection();
  325. var noteIds = await connection.QueryAsync<int>(sql);
  326. var query = Q.NewQuery();
  327. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  328. query.WhereIn(nameof(Entity.Middle.Middle.MiddleId), noteIds);
  329. query.Where(nameof(Entity.Middle.Middle.FolderType), typeValue == 1 ? AllTypeConst.TopicNote.GetHashCode() : AllTypeConst.Note.GetHashCode());
  330. query.Set(nameof(Entity.Middle.Middle.IsDelete), true);
  331. return await UpdateAsync(query);
  332. }
  333. }
  334. }