MiddleRepository.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. namespace GxPress.Repository.Implement
  17. {
  18. public class MiddleRepository : IMiddleRepository
  19. {
  20. private readonly Repository<Middle> _repository;
  21. private readonly IMapper _mapper;
  22. private readonly string _connectionString;
  23. private readonly string _databaseTypeStr;
  24. public MiddleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  25. {
  26. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  27. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  28. var databaseType =
  29. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<Middle>(database);
  32. _mapper = mapper;
  33. }
  34. public IDatabase Database => _repository.Database;
  35. public string TableName => _repository.TableName;
  36. public List<TableColumn> TableColumns => _repository.TableColumns;
  37. /// <summary>
  38. /// 添加
  39. /// </summary>
  40. /// <param name="middle"></param>
  41. /// <returns></returns>
  42. public async Task<int> InsertAsync(Middle middle)
  43. {
  44. middle.IsTop = false;
  45. middle.IsDelete = false;
  46. middle.IsRead = false;
  47. return await _repository.InsertAsync(middle);
  48. }
  49. /// <summary>
  50. /// 添加
  51. /// </summary>
  52. /// <param name="middle"></param>
  53. /// <returns></returns>
  54. public async Task<bool> InsertAsync(List<Middle> middles)
  55. {
  56. if (middles.Count == 0)
  57. return false;
  58. 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";
  59. foreach (var item in middles)
  60. {
  61. var guId = Guid.NewGuid().ToString();
  62. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  63. var isRead = item.IsRead == true ? 1 : 0;
  64. var isDefault = item.IsDefault == true ? 1 : 0;
  65. var isDelete = item.IsDelete == true ? 1 : 0;
  66. var isAdmin = item.IsAdmin == true ? 1 : 0;
  67. 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),";
  68. }
  69. sql = sql.Remove(sql.Length - 1, 1);
  70. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  71. var database = new Database(databaseType, _connectionString);
  72. var connection = database.GetConnection();
  73. var items = await connection.ExecuteScalarAsync<int>(sql);
  74. return items > 0;
  75. }
  76. /// <summary>
  77. /// 修改
  78. /// </summary>
  79. /// <param name="query"></param>
  80. /// <returns></returns>
  81. public async Task<bool> UpdateAsync(Query query)
  82. {
  83. return await _repository.UpdateAsync(query) > 0;
  84. }
  85. public async Task<bool> UpdateAsync(Middle middle)
  86. {
  87. return await _repository.UpdateAsync(middle);
  88. }
  89. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  90. {
  91. return await _repository.GetAllAsync(query);
  92. }
  93. public async Task<bool> DeleteAsync(Query query)
  94. {
  95. return await _repository.DeleteAsync(query) > 0;
  96. }
  97. public async Task<Middle> GetMiddleAsync(Query query)
  98. {
  99. return await _repository.GetAsync(query);
  100. }
  101. public async Task<Middle> GetMiddleAsync(int id)
  102. {
  103. return await _repository.GetAsync(id);
  104. }
  105. /// <summary>
  106. /// 获取下级数据
  107. /// </summary>
  108. /// <param name="id"></param>
  109. /// <returns></returns>
  110. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  111. {
  112. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  113. }
  114. /// <summary>
  115. /// 设置排序
  116. /// </summary>
  117. /// <param name="request"></param>
  118. /// <returns></returns>
  119. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  120. {
  121. //获取第一个
  122. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  123. //获取第二个数据
  124. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  125. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  126. var database = new Database(databaseType, _connectionString);
  127. var connection = database.GetConnection();
  128. if (middleFirst.Sort < middleSecond.Sort)
  129. {
  130. //修改第一个排序
  131. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  132. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  133. int sort = middleSecond.Sort;
  134. var sql =
  135. $"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";
  136. return await connection.ExecuteAsync(sql) > 0;
  137. }
  138. else
  139. {
  140. //修改第一个排序
  141. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  142. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  143. int sort = middleSecond.Sort;
  144. var sql =
  145. $"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";
  146. return await connection.ExecuteAsync(sql) > 0;
  147. }
  148. }
  149. /// <summary>
  150. /// 设置置顶
  151. /// </summary>
  152. /// <param name="id"></param>
  153. /// <returns></returns>
  154. public async Task<bool> SetTopAsync(int id)
  155. {
  156. var middle = await _repository.GetAsync(id);
  157. if (middle == null)
  158. throw new BusinessException("数据不存在");
  159. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  160. var database = new Database(databaseType, _connectionString);
  161. var connection = database.GetConnection();
  162. //return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  163. var isTop = middle.IsTop == true ? 0 : 1;
  164. var sql = $"update ccpph.tede_middle set IsTop={isTop} where id={id}";
  165. return await connection.ExecuteAsync(sql) > 0;
  166. }
  167. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  168. {
  169. if (request.IsDelete)
  170. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  171. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  172. }
  173. /// <summary>
  174. /// 恢复删除
  175. /// </summary>
  176. /// <param name="request"></param>
  177. /// <returns></returns>
  178. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  179. {
  180. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  181. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  182. }
  183. /// <summary>
  184. /// 阅读
  185. /// </summary>
  186. /// <param name="id"></param>
  187. /// <returns></returns>
  188. public async Task<bool> ReadAsync(int id)
  189. {
  190. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  191. .Where(nameof(Middle.Id), id)) > 0;
  192. }
  193. /// <summary>
  194. /// 修改文件夹名称
  195. /// </summary>
  196. /// <returns></returns>
  197. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  198. {
  199. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  200. .Where(nameof(Middle.Id), request.Id)) > 0;
  201. }
  202. public async Task<bool> IsFolderAsync(int parentId)
  203. {
  204. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  205. }
  206. /// <summary>
  207. /// 根据用户ID文件夹ID查询小组数量
  208. /// </summary>
  209. /// <param name="userId"></param>
  210. /// <param name="groupFolderId"></param>
  211. /// <param name="folderTypeId"></param>
  212. /// <returns></returns>
  213. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  214. {
  215. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  216. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  217. }
  218. public async Task<int> CountAsync(Query query)
  219. {
  220. return await _repository.CountAsync(query);
  221. }
  222. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  223. {
  224. return await _repository.GetAllAsync(query);
  225. }
  226. }
  227. }