GroupRepository.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.EnumConst;
  11. using GxPress.Repository.Interface;
  12. using GxPress.Request.App.Group;
  13. using GxPress.Result.App.Group;
  14. using GxPress.Result.App.GroupUser;
  15. using Microsoft.AspNetCore.Hosting;
  16. using Microsoft.Extensions.Options;
  17. using Newtonsoft.Json;
  18. using Datory;
  19. using GxPress.Request.App.Topic;
  20. using SqlKata;
  21. using GxPress.Result.Job;
  22. using System.Linq;
  23. namespace GxPress.Repository.Implement
  24. {
  25. public class GroupRepository : IGroupRepository
  26. {
  27. private readonly Repository<Entity.Group> _repository;
  28. private readonly IMapper _mapper;
  29. private readonly IWebHostEnvironment _environment;
  30. private readonly string _connectionString;
  31. private readonly string _databaseTypeStr;
  32. private readonly Repository<GroupUser> _groupUserRepository;
  33. public GroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
  34. IWebHostEnvironment environment)
  35. {
  36. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  37. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  38. var databaseType =
  39. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  40. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  41. _repository = new Repository<Entity.Group>(database);
  42. _groupUserRepository = new Repository<GroupUser>(database);
  43. _environment = environment;
  44. _mapper = mapper;
  45. }
  46. public IDatabase Database => _repository.Database;
  47. public string TableName => _repository.TableName;
  48. public List<TableColumn> TableColumns => _repository.TableColumns;
  49. public async Task<int> InsertAsync(GroupInRequest request)
  50. {
  51. if (string.IsNullOrEmpty(request.Name))
  52. throw new BusinessException("小组名称不能为空");
  53. var group = new Entity.Group
  54. {
  55. Name = request.Name,
  56. AvatarUrl = request.AvatarUrl,
  57. Introduce = request.Introduce,
  58. IsAdmin = false,
  59. IsUDisturb = false,
  60. QRCoder = request.QRCoder,
  61. IsWords = false,
  62. UserId = request.UserId,
  63. GroupCategroyId = request.GroupCategroyId,
  64. LabelName = request.LabelName,
  65. IsShow = request.IsShow
  66. };
  67. if (!string.IsNullOrEmpty(group.AvatarUrl))
  68. group.AvatarUrl = StringUtils.RemoveDomain(group.AvatarUrl);
  69. var id = await _repository.InsertAsync(group);
  70. //生成二维码
  71. var groupDto = await _repository.GetAsync(id);
  72. var qrCodeData = new { key = QRCodeTypeConst.GroupInfo, value = groupDto.Guid };
  73. var resultPath =
  74. FileHelper.SaveQRCodeFile(StringUtils.GetWebRootPath(_environment.WebRootPath),
  75. JsonConvert.SerializeObject(qrCodeData));
  76. groupDto.QRCoder = resultPath.RelativePath;
  77. await _repository.UpdateAsync(groupDto);
  78. return id;
  79. }
  80. public async Task<IEnumerable<GroupUser>> GetAllAsync(SqlKata.Query query)
  81. {
  82. return await _groupUserRepository.GetAllAsync(query);
  83. }
  84. /// <summary>
  85. /// 修改小组
  86. /// </summary>
  87. /// <param name="request"></param>
  88. /// <returns></returns>
  89. public async Task<bool> UpdateAsync(GroupUpRequest request)
  90. {
  91. var group = await _repository.GetAsync(request.Id);
  92. if (!string.IsNullOrEmpty(request.Name))
  93. group.Name = request.Name;
  94. if (!string.IsNullOrEmpty(request.AvatarUrl))
  95. group.AvatarUrl = StringUtils.RemoveDomain(request.AvatarUrl);
  96. if (!string.IsNullOrEmpty(request.Introduce))
  97. group.Introduce = request.Introduce;
  98. if (!string.IsNullOrEmpty(request.LabelName))
  99. group.LabelName = request.LabelName;
  100. if (request.IsShow > 0)
  101. group.IsShow = request.IsShow == 1;
  102. if (request.IsAdmin > 0)
  103. group.IsAdmin = request.IsAdmin == 1;
  104. if (request.IsWords > 0)
  105. group.IsWords = request.IsWords == 1;
  106. return await _repository.UpdateAsync(group);
  107. }
  108. /// <summary>
  109. /// 删除小组
  110. /// </summary>
  111. /// <param name="id"></param>
  112. /// <returns></returns>
  113. public async Task<bool> DeleteAsync(int id)
  114. {
  115. return await _repository.DeleteAsync(id);
  116. }
  117. public async Task<bool> DeleteAsync(Query query)
  118. {
  119. return await _repository.DeleteAsync(query) > 0;
  120. }
  121. /// <summary>
  122. /// 查询小组
  123. /// </summary>
  124. /// <param name="request"></param>
  125. /// <returns></returns>
  126. public async Task<PagedList<GroupUserFindResult>> FindGroupByGroupFolderId(GroupSearchRequest request)
  127. {
  128. var pagedList = new PagedList<GroupUserFindResult>();
  129. //根据文件夹ID查询成员
  130. var sqlValue = "";
  131. // if (!string.IsNullOrEmpty(request.Key))
  132. // sqlValue += $"and b.Name like '%{request.Key}%'";
  133. //共享数量
  134. var topicSharingCount = "(select count(1) from tede_group_user where GroupId=a.MiddleId) as TopicSharingCount";
  135. //总话题数量
  136. var topicAllCount = "(select count(1) from tede_topic where GroupId=a.MiddleId) as TopicAllCount";
  137. //未读话题数量
  138. var uReadTopicCount = $"(select count(1) from tede_topic_addressee where TopicId in (select Id from tede_topic where GroupId=a.MiddleId) and UserId={request.UserId} and IsRead=0) as UReadTopicCount";
  139. var categoryName = "(select FolderName from tede_middle where id=a.ParentId) as CategoryName";
  140. var sql =
  141. $@"SELECT a.*,{topicSharingCount},{topicAllCount},{uReadTopicCount},{categoryName},
  142. (SELECT
  143. COUNT(1)
  144. FROM
  145. tede_group
  146. WHERE
  147. id IN (SELECT
  148. MiddleId
  149. FROM
  150. tede_middle
  151. WHERE
  152. ParentId = a.Id)) AS FileCount,b.Id, b.AvatarUrl, b.Name FROM tede_Middle a LEFT JOIN tede_group b ON a.MiddleId = b.Id WHERE 1=1 ";
  153. if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key))
  154. sql += $"and a.ParentId = @groupFolderId ";
  155. else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key))
  156. sql += $"and a.ParentId = @groupFolderId and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  157. else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key))
  158. sql += $"and a.ParentId = @groupFolderId ";
  159. else
  160. sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  161. sql +=
  162. $@" and a.UserId=@userId and a.FolderType=7 and a.IsDelete=0 {sqlValue} and (b.id>0 or a.AttributeValue=2) ORDER BY a.IsTop DESC, a.LastModifiedDate DESC ,a.AttributeValue DESC LIMIT @page , @pageSize";
  163. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  164. var database = new Database(databaseType, _connectionString);
  165. var connection = database.GetConnection();
  166. var result =
  167. await connection
  168. .QueryAsync<GroupUserFindResult, Entity.Group, GroupUserFindResult>(sql,
  169. (groupUserFindResult, group) =>
  170. {
  171. groupUserFindResult.GroupName = group == null ? "" : group.Name;
  172. groupUserFindResult.AvatarUrl = group == null ? "" : StringUtils.AddDomainMin(group.AvatarUrl);
  173. groupUserFindResult.GroupId = group?.Id ?? 0;
  174. return groupUserFindResult;
  175. },
  176. new
  177. {
  178. groupFolderId = request.GroupFolderId,
  179. userId = request.UserId,
  180. page = (request.Page - 1) * request.PerPage,
  181. pageSize = request.PerPage * request.Page
  182. }, splitOn: "Id");
  183. pagedList.Items = result;
  184. var sqlCount =
  185. @"SELECT count(1) FROM tede_Middle a LEFT JOIN tede_group b ON a.MiddleId = b.Id WHERE 1=1 ";
  186. if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key))
  187. sqlCount += $"and a.ParentId = {request.GroupFolderId} ";
  188. else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key))
  189. sqlCount += $"and a.ParentId = {request.GroupFolderId} and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  190. else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key))
  191. sqlCount += $"and a.ParentId = {request.GroupFolderId} ";
  192. else
  193. sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  194. sqlCount +=
  195. $" and a.UserId={request.UserId} and a.FolderType=7 and a.IsDelete=0 {sqlValue} and (b.id>0 or a.AttributeValue=2)";
  196. pagedList.Total = await FindGroupByGroupFolderIdCountAsync(sqlCount);
  197. return pagedList;
  198. }
  199. /// <summary>
  200. /// 条数
  201. /// </summary>
  202. /// <param name="sql"></param>
  203. /// <returns></returns>
  204. public async Task<int> FindGroupByGroupFolderIdCountAsync(string sql)
  205. {
  206. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  207. var database = new Database(databaseType, _connectionString);
  208. var connection = database.GetConnection();
  209. var result = await connection.ExecuteScalarAsync<int>(sql);
  210. return result;
  211. }
  212. /// <summary>
  213. /// 获取小组详情
  214. /// </summary>
  215. /// <param name="request"></param>
  216. /// <returns></returns>
  217. public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
  218. {
  219. var groupDetail = await _repository.GetAsync(request.GroupId);
  220. if (groupDetail == null)
  221. throw new BusinessException("小组不存在");
  222. if (!string.IsNullOrEmpty(groupDetail.AvatarUrl))
  223. groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl);
  224. if (!string.IsNullOrEmpty(groupDetail.QRCoder))
  225. groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
  226. var sql =
  227. "SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM tede_group_user a inner join tede_user b on a.UserId=b.Id inner join tede_department c on b.DepartmentId=c.Id where a.GroupId=@groupId order by a.GroupUserRoleId";
  228. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  229. var database = new Database(databaseType, _connectionString);
  230. var connection = database.GetConnection();
  231. var result =
  232. await connection
  233. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  234. (groupUserListResult, user, department) =>
  235. {
  236. groupUserListResult.DepartmentName = department.Name;
  237. groupUserListResult.Name = user.Name;
  238. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  239. return groupUserListResult;
  240. }, new { groupId = request.GroupId }, splitOn: "Name");
  241. var groupDetailResult = _mapper.Map<GroupDetailResult>(groupDetail);
  242. groupDetailResult.GroupUserListResult = result;
  243. var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), request.UserId)
  244. .Where(nameof(GroupUser.GroupId), request.GroupId));
  245. //获取当前用户的权限
  246. groupDetailResult.GroupUserRoleId = groupUser != null ? groupUser.GroupUserRoleId : 0;
  247. groupDetailResult.IsUDisturb = groupUser != null ? groupUser.IsUDisturb : false;
  248. groupDetailResult.IsAdmin = groupUser != null ? groupUser.UserId == groupDetailResult.UserId : false;
  249. groupDetailResult.IsUser = groupUser != null ? groupUser.UserId == request.UserId : false;
  250. groupDetailResult.CreatedDate = groupDetail.CreatedDate;
  251. return groupDetailResult;
  252. }
  253. /// <summary>
  254. /// 搜索小组成员
  255. /// </summary>
  256. /// <param name="request"></param>
  257. /// <returns></returns>
  258. public async Task<IEnumerable<GroupUserListResult>> SearchGroupUserAsync(TopicDetailListRequest request)
  259. {
  260. string sql =
  261. $@"SELECT a.*, b.Name, b.AvatarUrl, c.Name FROM tede_group_user a INNER JOIN tede_user b ON a.UserId = b.Id INNER JOIN
  262. tede_department c ON b.DepartmentId = c.Id
  263. WHERE a.GroupId = {request.GroupId} and b.Name like '%{request.Key}%'";
  264. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  265. var database = new Database(databaseType, _connectionString);
  266. var connection = database.GetConnection();
  267. var result =
  268. await connection
  269. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  270. (groupUserListResult, user, department) =>
  271. {
  272. groupUserListResult.DepartmentName = department.Name;
  273. groupUserListResult.Name = user.Name;
  274. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  275. return groupUserListResult;
  276. }, splitOn: "Name");
  277. return result;
  278. }
  279. /// <summary>
  280. /// 获取小组详情
  281. /// </summary>
  282. /// <param name="guId"></param>
  283. /// <param name="userId"></param>
  284. /// <returns></returns>
  285. public async Task<GroupDetailResult> GroupDetailByGuIdAsync(string guId, int userId)
  286. {
  287. var groupDetail = await _repository.GetAsync(Q.Where(nameof(Entity.Group.Guid), guId));
  288. if (groupDetail == null)
  289. throw new BusinessException("小组不存在");
  290. if (!string.IsNullOrEmpty(groupDetail.AvatarUrl))
  291. groupDetail.AvatarUrl = StringUtils.AddDomainMin(groupDetail.AvatarUrl);
  292. if (!string.IsNullOrEmpty(groupDetail.QRCoder))
  293. groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
  294. var sql =
  295. "SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM tede_group_user a inner join tede_user b on a.UserId=b.Id inner join tede_department c on b.DepartmentId=c.Id where a.GroupId=@groupId";
  296. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  297. var database = new Database(databaseType, _connectionString);
  298. var connection = database.GetConnection();
  299. var result =
  300. await connection
  301. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  302. (groupUserListResult, user, department) =>
  303. {
  304. groupUserListResult.DepartmentName = department.Name;
  305. groupUserListResult.Name = user.Name;
  306. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  307. return groupUserListResult;
  308. }, new { groupId = groupDetail.Id }, splitOn: "Name");
  309. var groupDetailResult = _mapper.Map<GroupDetailResult>(groupDetail);
  310. groupDetailResult.GroupUserListResult = result;
  311. var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), userId)
  312. .Where(nameof(GroupUser.Guid), guId));
  313. groupDetailResult.GroupUserRoleId = groupUser?.GroupUserRoleId ?? GroupUserRoleTyeConst.General;
  314. //获取当前用户的权限
  315. return groupDetailResult;
  316. }
  317. /// <summary>
  318. /// 获取用户未读取话题数量
  319. /// </summary>
  320. /// <param name="userId"></param>
  321. /// <param name="groupId"></param>
  322. /// <returns></returns>
  323. public async Task<int> GetUReadCountAsync(int userId, int groupId)
  324. {
  325. string sql =
  326. @"select count(1) from tede_topic a inner join tede_topic_addressee b on a.Id=b.TopicId where b.UserId=@userId and b.IsRead=0 and a.GroupId=@groupId";
  327. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  328. var database = new Database(databaseType, _connectionString);
  329. var connection = database.GetConnection();
  330. var result = await connection.ExecuteScalarAsync<int>(sql, new { userId, groupId });
  331. return result;
  332. }
  333. /// <summary>
  334. /// 获取小组的共享话题数量
  335. /// </summary>
  336. /// <param name="groupId"></param>
  337. /// <returns></returns>
  338. public async Task<int> SharingCountAsync(int groupId)
  339. {
  340. var sql =
  341. $"select count(1) from tede_group_user where GroupId={groupId}";
  342. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  343. var database = new Database(databaseType, _connectionString);
  344. var connection = database.GetConnection();
  345. var result = await connection.ExecuteScalarAsync<int>(sql, new { GroupId = groupId });
  346. return result;
  347. }
  348. public async Task<Entity.Group> GetAsync(int id)
  349. {
  350. return await _repository.GetAsync(id);
  351. }
  352. /// <summary>
  353. /// 是开启管理员同意加入
  354. /// </summary>
  355. /// <param name="groupId"></param>
  356. /// <returns></returns>
  357. public async Task<bool> SetGroupIsAdminAsync(int groupId)
  358. {
  359. var group = await GetAsync(groupId);
  360. return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsAdmin), !group.IsAdmin)
  361. .Where(nameof(Entity.Group.Id), groupId)) > 0;
  362. }
  363. /// <summary>
  364. /// 是否禁言
  365. /// </summary>
  366. /// <param name="groupId"></param>
  367. /// <returns></returns>
  368. public async Task<bool> SetGroupIsWordsAsync(int groupId)
  369. {
  370. var group = await GetAsync(groupId);
  371. return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsWords), !group.IsWords)
  372. .Where(nameof(Entity.Group.Id), groupId)) > 0;
  373. }
  374. public async Task<List<JobGroupResult>> ElasticSearchGroup()
  375. {
  376. var sql = "select a.Id,a.GuId,a.UserId,b.Id,b.Name,b.AvatarUrl,b.CreatedDate,c.Name from tede_middle a inner join tede_group b on a.MiddleId=b.Id inner join tede_user c on c.Id=b.UserId where a.FolderType=7 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  377. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  378. var database = new Database(databaseType, _connectionString);
  379. var connection = database.GetConnection();
  380. var items = await connection
  381. .QueryAsync<JobGroupResult, Entity.Group, User, JobGroupResult>(sql,
  382. (jobGroupResult, group, user) =>
  383. {
  384. jobGroupResult.MiddleId = jobGroupResult.Id;
  385. jobGroupResult.Id = group.Id;
  386. jobGroupResult.Name = group != null ? group.Name : "";
  387. jobGroupResult.AvatarUrl = group != null ? group.AvatarUrl : "";
  388. return jobGroupResult;
  389. },
  390. splitOn: "Id,Id,Name");
  391. return items.ToList();
  392. }
  393. /// <summary>
  394. /// 根据广场类别查询小组
  395. /// </summary>
  396. /// <param name="categoryId"></param>
  397. /// <returns></returns>
  398. public async Task<IEnumerable<Entity.Group>> GetAllAsync(int categoryId)
  399. {
  400. var sql = $@"SELECT
  401. *,
  402. (SELECT
  403. COUNT(1)
  404. FROM
  405. tede_group_user
  406. WHERE
  407. GroupId = a.Id) AS UserCount,
  408. (SELECT
  409. COUNT(1)
  410. FROM
  411. tede_topic
  412. WHERE
  413. GroupId = a.Id) AS GroupArticleCount
  414. FROM
  415. tede_group a
  416. WHERE
  417. a.GroupCategroyId = {categoryId} and a.IsShow=1";
  418. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  419. var database = new Database(databaseType, _connectionString);
  420. var connection = database.GetConnection();
  421. var result = await connection.QueryAsync<Entity.Group>(sql);
  422. //return await _repository.GetAllAsync(Q.Where(nameof(Entity.Group.GroupCategroyId), categoryId));
  423. return result;
  424. }
  425. /// <summary>
  426. /// 用户小组列表
  427. /// </summary>
  428. /// <param name="userId"></param>
  429. /// <returns></returns>
  430. public async Task<IEnumerable<UserGroupList>> GetAllByUserIdAsync(int userId)
  431. {
  432. string sql = $@"SELECT
  433. Id,
  434. Name,
  435. AvatarUrl,
  436. (SELECT
  437. COUNT(1)
  438. FROM
  439. tede_group_user
  440. WHERE
  441. GroupId = a.Id) AS UserCount
  442. FROM
  443. tede_group a
  444. WHERE
  445. a.id IN (SELECT
  446. GroupId
  447. FROM
  448. ccpph_tede.tede_group_user
  449. WHERE
  450. UserId = {userId})";
  451. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  452. var database = new Database(databaseType, _connectionString);
  453. var connection = database.GetConnection();
  454. var items = await connection
  455. .QueryAsync<UserGroupList>(sql);
  456. foreach (var item in items)
  457. {
  458. item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl);
  459. }
  460. return items;
  461. }
  462. }
  463. }