MiddleRepository.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. public async Task<bool> ExistsAsync(SqlKata.Query query)
  51. {
  52. return await _repository.ExistsAsync(query);
  53. }
  54. /// <summary>
  55. /// 添加
  56. /// </summary>
  57. /// <param name="middle"></param>
  58. /// <returns></returns>
  59. public async Task<bool> InsertAsync(List<Middle> middles)
  60. {
  61. if (middles.Count == 0)
  62. return false;
  63. 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";
  64. foreach (var item in middles)
  65. {
  66. var guId = Guid.NewGuid().ToString();
  67. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  68. var isRead = item.IsRead == true ? 1 : 0;
  69. var isDefault = item.IsDefault == true ? 1 : 0;
  70. var isDelete = item.IsDelete == true ? 1 : 0;
  71. var isAdmin = item.IsAdmin == true ? 1 : 0;
  72. var isRecall = item.IsRecall == true ? 1 : 0;
  73. 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}),";
  74. }
  75. sql = sql.Remove(sql.Length - 1, 1);
  76. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  77. var database = new Database(databaseType, _connectionString);
  78. var connection = database.GetConnection();
  79. var items = await connection.ExecuteScalarAsync<int>(sql);
  80. return items > 0;
  81. }
  82. /// <summary>
  83. /// 修改
  84. /// </summary>
  85. /// <param name="query"></param>
  86. /// <returns></returns>
  87. public async Task<bool> UpdateAsync(Query query)
  88. {
  89. return await _repository.UpdateAsync(query) > 0;
  90. }
  91. public async Task<bool> UpdateAsync(Middle middle)
  92. {
  93. return await _repository.UpdateAsync(middle);
  94. }
  95. public async Task<IEnumerable<Middle>> FindAsync(Query query)
  96. {
  97. return await _repository.GetAllAsync(query);
  98. }
  99. public async Task<bool> DeleteAsync(Query query)
  100. {
  101. return await _repository.DeleteAsync(query) > 0;
  102. }
  103. public async Task<bool> DeleteAsync(int middleId, int folderTypeId)
  104. {
  105. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0;
  106. }
  107. public async Task<Middle> GetMiddleAsync(Query query)
  108. {
  109. return await _repository.GetAsync(query);
  110. }
  111. public async Task<Middle> GetMiddleAsync(int id)
  112. {
  113. return await _repository.GetAsync(id);
  114. }
  115. /// <summary>
  116. /// 获取下级数据
  117. /// </summary>
  118. /// <param name="id"></param>
  119. /// <returns></returns>
  120. public async Task<IEnumerable<Middle>> GetMiddleChildrenAsync(int id)
  121. {
  122. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id));
  123. }
  124. /// <summary>
  125. /// 设置排序
  126. /// </summary>
  127. /// <param name="request"></param>
  128. /// <returns></returns>
  129. public async Task<bool> SetSortAsync(MiddleSortRequest request)
  130. {
  131. //获取第一个
  132. var middleFirst = await _repository.GetAsync(request.MiddleIdFirst);
  133. //获取第二个数据
  134. var middleSecond = await _repository.GetAsync(request.MiddleIdSecond);
  135. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  136. var database = new Database(databaseType, _connectionString);
  137. var connection = database.GetConnection();
  138. if (middleFirst.Sort < middleSecond.Sort)
  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 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. else
  149. {
  150. //修改第一个排序
  151. await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort)
  152. .Where(nameof(Middle.Id), request.MiddleIdFirst));
  153. int sort = middleSecond.Sort;
  154. var sql =
  155. $"update 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";
  156. return await connection.ExecuteAsync(sql) > 0;
  157. }
  158. }
  159. /// <summary>
  160. /// 设置置顶
  161. /// </summary>
  162. /// <param name="id"></param>
  163. /// <returns></returns>
  164. public async Task<bool> SetTopAsync(int id)
  165. {
  166. var middle = await _repository.GetAsync(id);
  167. if (middle == null)
  168. throw new BusinessException("数据不存在");
  169. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  170. var database = new Database(databaseType, _connectionString);
  171. var connection = database.GetConnection();
  172. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0;
  173. // var isTop = middle.IsTop == true ? 0 : 1;
  174. // var sql = $"update tede_middle set IsTop={isTop} where id={id}";
  175. // return await connection.ExecuteAsync(sql) > 0;
  176. }
  177. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  178. {
  179. if (request.IsDelete)
  180. return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0;
  181. return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0;
  182. }
  183. /// <summary>
  184. /// 恢复删除
  185. /// </summary>
  186. /// <param name="request"></param>
  187. /// <returns></returns>
  188. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  189. {
  190. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false)
  191. .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0;
  192. }
  193. /// <summary>
  194. /// 阅读
  195. /// </summary>
  196. /// <param name="id"></param>
  197. /// <returns></returns>
  198. public async Task<bool> ReadAsync(int id)
  199. {
  200. return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true)
  201. .Where(nameof(Middle.Id), id)) > 0;
  202. }
  203. /// <summary>
  204. /// 修改文件夹名称
  205. /// </summary>
  206. /// <returns></returns>
  207. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  208. {
  209. return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName)
  210. .Where(nameof(Middle.Id), request.Id)) > 0;
  211. }
  212. public async Task<bool> IsFolderAsync(int parentId)
  213. {
  214. return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0;
  215. }
  216. /// <summary>
  217. /// 根据用户ID文件夹ID查询小组数量
  218. /// </summary>
  219. /// <param name="userId"></param>
  220. /// <param name="groupFolderId"></param>
  221. /// <param name="folderTypeId"></param>
  222. /// <returns></returns>
  223. public async Task<int> FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId)
  224. {
  225. return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId)
  226. .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId));
  227. }
  228. public async Task<int> CountAsync(Query query)
  229. {
  230. return await _repository.CountAsync(query);
  231. }
  232. public async Task<IEnumerable<Middle>> GetAllAsync(SqlKata.Query query)
  233. {
  234. return await _repository.GetAllAsync(query);
  235. }
  236. public async Task<int> GetSytemFolderAsync(int userId)
  237. {
  238. var query = Q.NewQuery();
  239. query.Select(nameof(Entity.Middle.Middle.Id));
  240. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  241. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  242. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  243. query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.Note.GetHashCode());
  244. return await _repository.GetAsync<int>(query);
  245. }
  246. public async Task<IEnumerable<Middle>> FindAsync(InboxSearchRequest request)
  247. {
  248. var query = new SqlKata.Query();
  249. if (request.Type > 0)
  250. {
  251. query.Where(nameof(Middle.FolderType), request.Type);
  252. }
  253. else
  254. {
  255. 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() };
  256. query.WhereIn(nameof(Middle.FolderType), listType);
  257. }
  258. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  259. {
  260. query.OrWhere(nameof(Middle.TitleLong), request.KeyWord);
  261. query.OrWhere(nameof(Middle.AddressUserComplete), request.KeyWord);
  262. query.OrWhere(nameof(Middle.CcUserComplete), request.KeyWord);
  263. query.OrWhere(nameof(Middle.Content), request.KeyWord);
  264. }
  265. query.Where(nameof(Middle.UserId), request.UserId);
  266. 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"));
  267. }
  268. }
  269. }