MiddleRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 System.Linq;
  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 `ccpph`.`tede_middle`(`Guid`,`CreatedDate`,`LastModifiedDate`,`FolderName`,`FolderType`,`IsTop`,`RoleId`,`Sort`,`MiddleId`,`ParentId`,`UserId`,`MiddleSonId`,`IsRead`,`NoticeAddresseeType`,`IsAdmin`,`IsDelete`,`AttributeValue`,`IsDefault`,`IsUpload`)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. 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),";
  69. }
  70. sql = sql.Remove(sql.Length - 1, 1);
  71. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  72. var database = new Database(databaseType, _connectionString);
  73. var connection = database.GetConnection();
  74. var items = await connection.ExecuteScalarAsync<int>(sql);
  75. return items > 0;
  76. }
  77. /// <summary>
  78. /// 修改
  79. /// </summary>
  80. /// <param name="query"></param>
  81. /// <returns></returns>
  82. public async Task<bool> UpdateAsync(Query query)
  83. {
  84. return await _repository.UpdateAsync(query) > 0;
  85. }
  86. public async Task<bool> UpdateAsync(Middle middle)
  87. {
  88. return await _repository.UpdateAsync(middle);
  89. }
  90. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  91. {
  92. return await _repository.GetAllAsync(query);
  93. }
  94. /// <summary>
  95. /// 递归获取父级下面的所有子集
  96. /// </summary>
  97. /// <param name="id"></param>
  98. /// <param name="ids"></param>
  99. /// <returns></returns>
  100. public async Task<IEnumerable<int>> GetAllMiddleChildrenByParentIdAsync(List<int> ids, List<int> returnIds)
  101. {
  102. var middles = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  103. if (middles == null || middles.Count == 0)
  104. return returnIds;
  105. foreach (var item in middles)
  106. {
  107. middles = await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  108. if (middles == null || middles.Count == 0)
  109. continue;
  110. returnIds.AddRange(middles.Select(n => n.Id).ToList());
  111. foreach (var middle in middles)
  112. {
  113. await GetAllMiddleChildrenByParentIdAsync(new List<int> { middle.Id }, returnIds);
  114. }
  115. }
  116. return returnIds;
  117. }
  118. public async Task<bool> DeleteAsync(Query query)
  119. {
  120. return await _repository.DeleteAsync(query) > 0;
  121. }
  122. public async Task<Middle> GetMiddleAsync(Query query)
  123. {
  124. return await _repository.GetAsync(query);
  125. }
  126. public async Task<Middle> GetMiddleAsync(int id)
  127. {
  128. return await _repository.GetAsync(id);
  129. }
  130. /// <summary>
  131. /// 获取下级数据
  132. /// </summary>
  133. /// <param name="id"></param>
  134. /// <returns></returns>
  135. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  136. {
  137. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  138. }
  139. /// <summary>
  140. /// 设置排序
  141. /// </summary>
  142. /// <param name="request"></param>
  143. /// <returns></returns>
  144. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  145. {
  146. //获取第一个
  147. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  148. //获取第二个数据
  149. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  150. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  151. var database = new Database(databaseType, _connectionString);
  152. var connection = database.GetConnection();
  153. if (middleFirst.Sort < middleSecond.Sort)
  154. {
  155. //修改第一个排序
  156. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  157. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  158. int sort = middleSecond.Sort;
  159. var sql =
  160. $"update ccpph.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";
  161. return await connection.ExecuteAsync(sql) > 0;
  162. }
  163. else
  164. {
  165. //修改第一个排序
  166. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  167. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  168. int sort = middleSecond.Sort;
  169. var sql =
  170. $"update ccpph.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";
  171. return await connection.ExecuteAsync(sql) > 0;
  172. }
  173. }
  174. /// <summary>
  175. /// 设置置顶
  176. /// </summary>
  177. /// <param name="id"></param>
  178. /// <returns></returns>
  179. public async Task<bool> SetTopAsync(int id)
  180. {
  181. var middle = await _repository.GetAsync(id);
  182. if (middle == null)
  183. throw new BusinessException("数据不存在");
  184. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  185. var database = new Database(databaseType, _connectionString);
  186. var connection = database.GetConnection();
  187. //return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  188. var isTop = middle.IsTop == true ? 0 : 1;
  189. var sql = $"update ccpph.tede_middle set IsTop={isTop} where id={id}";
  190. return await connection.ExecuteAsync(sql) > 0;
  191. }
  192. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  193. {
  194. if (request.IsDelete)
  195. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  196. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  197. }
  198. /// <summary>
  199. /// 恢复删除
  200. /// </summary>
  201. /// <param name="request"></param>
  202. /// <returns></returns>
  203. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  204. {
  205. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  206. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  207. }
  208. /// <summary>
  209. /// 阅读
  210. /// </summary>
  211. /// <param name="id"></param>
  212. /// <returns></returns>
  213. public async Task<bool> ReadAsync(int id)
  214. {
  215. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  216. .Where(nameof(Middle.Id), id)) > 0;
  217. }
  218. /// <summary>
  219. /// 修改文件夹名称
  220. /// </summary>
  221. /// <returns></returns>
  222. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  223. {
  224. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  225. .Where(nameof(Middle.Id), request.Id)) > 0;
  226. }
  227. public async Task<bool> IsFolderAsync(int parentId)
  228. {
  229. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  230. }
  231. /// <summary>
  232. /// 根据用户ID文件夹ID查询小组数量
  233. /// </summary>
  234. /// <param name="userId"></param>
  235. /// <param name="groupFolderId"></param>
  236. /// <param name="folderTypeId"></param>
  237. /// <returns></returns>
  238. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  239. {
  240. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  241. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  242. }
  243. public async Task<int> CountAsync(Query query)
  244. {
  245. return await _repository.CountAsync(query);
  246. }
  247. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  248. {
  249. return await _repository.GetAllAsync(query);
  250. }
  251. }
  252. }