MiddleRepository.cs 16 KB

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