MiddleRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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 `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`)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,'{item.Title}','{item.TitleLong}','{item.Content}','{item.AddressUser}','{item.CcUser}','{item.AddressUserComplete}','{item.CcUserComplete}'),";
  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. public async Task<bool> DeleteAsync(Query query)
  95. {
  96. return await _repository.DeleteAsync(query) > 0;
  97. }
  98. public async Task<bool> DeleteAsync(int middleId, int folderTypeId)
  99. {
  100. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0;
  101. }
  102. public async Task<Middle> GetMiddleAsync(Query query)
  103. {
  104. return await _repository.GetAsync(query);
  105. }
  106. public async Task<Middle> GetMiddleAsync(int id)
  107. {
  108. return await _repository.GetAsync(id);
  109. }
  110. /// <summary>
  111. /// 获取下级数据
  112. /// </summary>
  113. /// <param name="id"></param>
  114. /// <returns></returns>
  115. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  116. {
  117. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  118. }
  119. /// <summary>
  120. /// 设置排序
  121. /// </summary>
  122. /// <param name="request"></param>
  123. /// <returns></returns>
  124. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  125. {
  126. //获取第一个
  127. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  128. //获取第二个数据
  129. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  130. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  131. var database = new Database(databaseType, _connectionString);
  132. var connection = database.GetConnection();
  133. if (middleFirst.Sort < middleSecond.Sort)
  134. {
  135. //修改第一个排序
  136. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  137. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  138. int sort = middleSecond.Sort;
  139. var sql =
  140. $"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";
  141. return await connection.ExecuteAsync(sql) > 0;
  142. }
  143. else
  144. {
  145. //修改第一个排序
  146. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  147. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  148. int sort = middleSecond.Sort;
  149. var sql =
  150. $"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";
  151. return await connection.ExecuteAsync(sql) > 0;
  152. }
  153. }
  154. /// <summary>
  155. /// 设置置顶
  156. /// </summary>
  157. /// <param name="id"></param>
  158. /// <returns></returns>
  159. public async Task<bool> SetTopAsync(int id)
  160. {
  161. var middle = await _repository.GetAsync(id);
  162. if (middle == null)
  163. throw new BusinessException("数据不存在");
  164. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  165. var database = new Database(databaseType, _connectionString);
  166. var connection = database.GetConnection();
  167. //return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  168. var isTop = middle.IsTop == true ? 0 : 1;
  169. var sql = $"update ccpph.tede_middle set IsTop={isTop} where id={id}";
  170. return await connection.ExecuteAsync(sql) > 0;
  171. }
  172. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  173. {
  174. if (request.IsDelete)
  175. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  176. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  177. }
  178. /// <summary>
  179. /// 恢复删除
  180. /// </summary>
  181. /// <param name="request"></param>
  182. /// <returns></returns>
  183. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  184. {
  185. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  186. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  187. }
  188. /// <summary>
  189. /// 阅读
  190. /// </summary>
  191. /// <param name="id"></param>
  192. /// <returns></returns>
  193. public async Task<bool> ReadAsync(int id)
  194. {
  195. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  196. .Where(nameof(Middle.Id), id)) > 0;
  197. }
  198. /// <summary>
  199. /// 修改文件夹名称
  200. /// </summary>
  201. /// <returns></returns>
  202. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  203. {
  204. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  205. .Where(nameof(Middle.Id), request.Id)) > 0;
  206. }
  207. public async Task<bool> IsFolderAsync(int parentId)
  208. {
  209. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  210. }
  211. /// <summary>
  212. /// 根据用户ID文件夹ID查询小组数量
  213. /// </summary>
  214. /// <param name="userId"></param>
  215. /// <param name="groupFolderId"></param>
  216. /// <param name="folderTypeId"></param>
  217. /// <returns></returns>
  218. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  219. {
  220. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  221. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  222. }
  223. public async Task<int> CountAsync(Query query)
  224. {
  225. return await _repository.CountAsync(query);
  226. }
  227. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  228. {
  229. return await _repository.GetAllAsync(query);
  230. }
  231. public async Task<IEnumerable<Middle>> FindAsync(InboxSearchRequest request)
  232. {
  233. var query = new SqlKata.Query();
  234. if (request.Type > 0)
  235. {
  236. query.Where(nameof(Middle.FolderType), request.Type);
  237. }
  238. else
  239. {
  240. 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() };
  241. query.WhereIn(nameof(Middle.FolderType), listType);
  242. }
  243. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  244. {
  245. query.OrWhere(nameof(Middle.TitleLong), request.KeyWord);
  246. query.OrWhere(nameof(Middle.AddressUserComplete), request.KeyWord);
  247. query.OrWhere(nameof(Middle.CcUserComplete), request.KeyWord);
  248. query.OrWhere(nameof(Middle.Content), request.KeyWord);
  249. }
  250. query.Where(nameof(Middle.UserId), request.UserId);
  251. 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"));
  252. }
  253. }
  254. }