GroupRepository.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.IsWords > 0)
  103. group.IsWords = request.IsWords == 1;
  104. return await _repository.UpdateAsync(group);
  105. }
  106. /// <summary>
  107. /// 删除小组
  108. /// </summary>
  109. /// <param name="id"></param>
  110. /// <returns></returns>
  111. public async Task<bool> DeleteAsync(int id)
  112. {
  113. return await _repository.DeleteAsync(id);
  114. }
  115. public async Task<bool> DeleteAsync(Query query)
  116. {
  117. return await _repository.DeleteAsync(query) > 0;
  118. }
  119. /// <summary>
  120. /// 查询小组
  121. /// </summary>
  122. /// <param name="request"></param>
  123. /// <returns></returns>
  124. public async Task<PagedList<GroupUserFindResult>> FindGroupByGroupFolderId(GroupSearchRequest request)
  125. {
  126. var pagedList = new PagedList<GroupUserFindResult>();
  127. //根据文件夹ID查询成员
  128. var sqlValue = "";
  129. // if (!string.IsNullOrEmpty(request.Key))
  130. // sqlValue += $"and b.Name like '%{request.Key}%'";
  131. //共享数量
  132. var topicSharingCount = "(select count(1) from tede_group_user where GroupId=a.MiddleId) as TopicSharingCount";
  133. //总话题数量
  134. var topicAllCount = "(select count(1) from tede_topic where GroupId=a.MiddleId) as TopicAllCount";
  135. //未读话题数量
  136. 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";
  137. var categoryName = "(select FolderName from tede_middle where id=a.ParentId) as CategoryName";
  138. var sql =
  139. $@"SELECT a.*,{topicSharingCount},{topicAllCount},{uReadTopicCount},{categoryName},
  140. (SELECT
  141. COUNT(1)
  142. FROM
  143. tede_group
  144. WHERE
  145. id IN (SELECT
  146. MiddleId
  147. FROM
  148. tede_middle
  149. WHERE
  150. 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 ";
  151. if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key))
  152. sql += $"and a.ParentId = @groupFolderId ";
  153. else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key))
  154. sql += $"and a.ParentId = @groupFolderId and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  155. else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key))
  156. sql += $"and a.ParentId = @groupFolderId ";
  157. else
  158. sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  159. sql +=
  160. $@" 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";
  161. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  162. var database = new Database(databaseType, _connectionString);
  163. var connection = database.GetConnection();
  164. var result =
  165. await connection
  166. .QueryAsync<GroupUserFindResult, Entity.Group, GroupUserFindResult>(sql,
  167. (groupUserFindResult, group) =>
  168. {
  169. groupUserFindResult.GroupName = group == null ? "" : group.Name;
  170. groupUserFindResult.AvatarUrl = group == null ? "" : StringUtils.AddDomainMin(group.AvatarUrl);
  171. groupUserFindResult.GroupId = group?.Id ?? 0;
  172. return groupUserFindResult;
  173. },
  174. new
  175. {
  176. groupFolderId = request.GroupFolderId,
  177. userId = request.UserId,
  178. page = (request.Page - 1) * request.PerPage,
  179. pageSize = request.PerPage * request.Page
  180. }, splitOn: "Id");
  181. pagedList.Items = result;
  182. var sqlCount =
  183. @"SELECT count(1) FROM tede_Middle a LEFT JOIN tede_group b ON a.MiddleId = b.Id WHERE 1=1 ";
  184. if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key))
  185. sqlCount += $"and a.ParentId = {request.GroupFolderId} ";
  186. else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key))
  187. sqlCount += $"and a.ParentId = {request.GroupFolderId} and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  188. else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key))
  189. sqlCount += $"and a.ParentId = {request.GroupFolderId} ";
  190. else
  191. sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  192. sqlCount +=
  193. $" and a.UserId={request.UserId} and a.FolderType=7 and a.IsDelete=0 {sqlValue} and (b.id>0 or a.AttributeValue=2)";
  194. pagedList.Total = await FindGroupByGroupFolderIdCountAsync(sqlCount);
  195. return pagedList;
  196. }
  197. /// <summary>
  198. /// 条数
  199. /// </summary>
  200. /// <param name="sql"></param>
  201. /// <returns></returns>
  202. public async Task<int> FindGroupByGroupFolderIdCountAsync(string sql)
  203. {
  204. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  205. var database = new Database(databaseType, _connectionString);
  206. var connection = database.GetConnection();
  207. var result = await connection.ExecuteScalarAsync<int>(sql);
  208. return result;
  209. }
  210. /// <summary>
  211. /// 获取小组详情
  212. /// </summary>
  213. /// <param name="request"></param>
  214. /// <returns></returns>
  215. public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
  216. {
  217. var groupDetail = await _repository.GetAsync(request.GroupId);
  218. if (groupDetail == null)
  219. throw new BusinessException("小组不存在");
  220. if (!string.IsNullOrEmpty(groupDetail.AvatarUrl))
  221. groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl);
  222. if (!string.IsNullOrEmpty(groupDetail.QRCoder))
  223. groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
  224. var sql =
  225. "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";
  226. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  227. var database = new Database(databaseType, _connectionString);
  228. var connection = database.GetConnection();
  229. var result =
  230. await connection
  231. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  232. (groupUserListResult, user, department) =>
  233. {
  234. groupUserListResult.DepartmentName = department.Name;
  235. groupUserListResult.Name = user.Name;
  236. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  237. return groupUserListResult;
  238. }, new { groupId = request.GroupId }, splitOn: "Name");
  239. var groupDetailResult = _mapper.Map<GroupDetailResult>(groupDetail);
  240. groupDetailResult.GroupUserListResult = result;
  241. var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), request.UserId)
  242. .Where(nameof(GroupUser.GroupId), request.GroupId));
  243. //获取当前用户的权限
  244. groupDetailResult.GroupUserRoleId = groupUser != null ? groupUser.GroupUserRoleId : 0;
  245. groupDetailResult.IsUDisturb = groupUser != null ? groupUser.IsUDisturb : false;
  246. groupDetailResult.IsAdmin = groupUser != null ? groupUser.UserId == groupDetailResult.UserId : false;
  247. groupDetailResult.IsUser = groupUser != null ? groupUser.UserId == request.UserId : false;
  248. groupDetailResult.CreatedDate = groupDetail.CreatedDate;
  249. return groupDetailResult;
  250. }
  251. /// <summary>
  252. /// 搜索小组成员
  253. /// </summary>
  254. /// <param name="request"></param>
  255. /// <returns></returns>
  256. public async Task<IEnumerable<GroupUserListResult>> SearchGroupUserAsync(TopicDetailListRequest request)
  257. {
  258. string sql =
  259. $@"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
  260. tede_department c ON b.DepartmentId = c.Id
  261. WHERE a.GroupId = {request.GroupId} and b.Name like '%{request.Key}%'";
  262. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  263. var database = new Database(databaseType, _connectionString);
  264. var connection = database.GetConnection();
  265. var result =
  266. await connection
  267. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  268. (groupUserListResult, user, department) =>
  269. {
  270. groupUserListResult.DepartmentName = department.Name;
  271. groupUserListResult.Name = user.Name;
  272. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  273. return groupUserListResult;
  274. }, splitOn: "Name");
  275. return result;
  276. }
  277. /// <summary>
  278. /// 获取小组详情
  279. /// </summary>
  280. /// <param name="guId"></param>
  281. /// <param name="userId"></param>
  282. /// <returns></returns>
  283. public async Task<GroupDetailResult> GroupDetailByGuIdAsync(string guId, int userId)
  284. {
  285. var groupDetail = await _repository.GetAsync(Q.Where(nameof(Entity.Group.Guid), guId));
  286. if (groupDetail == null)
  287. throw new BusinessException("小组不存在");
  288. if (!string.IsNullOrEmpty(groupDetail.AvatarUrl))
  289. groupDetail.AvatarUrl = StringUtils.AddDomainMin(groupDetail.AvatarUrl);
  290. if (!string.IsNullOrEmpty(groupDetail.QRCoder))
  291. groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
  292. var sql =
  293. "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";
  294. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  295. var database = new Database(databaseType, _connectionString);
  296. var connection = database.GetConnection();
  297. var result =
  298. await connection
  299. .QueryAsync<GroupUserListResult, User, Department, GroupUserListResult>(sql,
  300. (groupUserListResult, user, department) =>
  301. {
  302. groupUserListResult.DepartmentName = department.Name;
  303. groupUserListResult.Name = user.Name;
  304. groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  305. return groupUserListResult;
  306. }, new { groupId = groupDetail.Id }, splitOn: "Name");
  307. var groupDetailResult = _mapper.Map<GroupDetailResult>(groupDetail);
  308. groupDetailResult.GroupUserListResult = result;
  309. var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), userId)
  310. .Where(nameof(GroupUser.Guid), guId));
  311. groupDetailResult.GroupUserRoleId = groupUser?.GroupUserRoleId ?? GroupUserRoleTyeConst.General;
  312. //获取当前用户的权限
  313. return groupDetailResult;
  314. }
  315. /// <summary>
  316. /// 获取用户未读取话题数量
  317. /// </summary>
  318. /// <param name="userId"></param>
  319. /// <param name="groupId"></param>
  320. /// <returns></returns>
  321. public async Task<int> GetUReadCountAsync(int userId, int groupId)
  322. {
  323. string sql =
  324. @"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";
  325. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  326. var database = new Database(databaseType, _connectionString);
  327. var connection = database.GetConnection();
  328. var result = await connection.ExecuteScalarAsync<int>(sql, new { userId, groupId });
  329. return result;
  330. }
  331. /// <summary>
  332. /// 获取小组的共享话题数量
  333. /// </summary>
  334. /// <param name="groupId"></param>
  335. /// <returns></returns>
  336. public async Task<int> SharingCountAsync(int groupId)
  337. {
  338. var sql =
  339. $"select count(1) from tede_group_user where GroupId={groupId}";
  340. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  341. var database = new Database(databaseType, _connectionString);
  342. var connection = database.GetConnection();
  343. var result = await connection.ExecuteScalarAsync<int>(sql, new { GroupId = groupId });
  344. return result;
  345. }
  346. public async Task<Entity.Group> GetAsync(int id)
  347. {
  348. return await _repository.GetAsync(id);
  349. }
  350. /// <summary>
  351. /// 是开启管理员同意加入
  352. /// </summary>
  353. /// <param name="groupId"></param>
  354. /// <returns></returns>
  355. public async Task<bool> SetGroupIsAdminAsync(int groupId)
  356. {
  357. var group = await GetAsync(groupId);
  358. return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsAdmin), !group.IsAdmin)
  359. .Where(nameof(Entity.Group.Id), groupId)) > 0;
  360. }
  361. /// <summary>
  362. /// 是否禁言
  363. /// </summary>
  364. /// <param name="groupId"></param>
  365. /// <returns></returns>
  366. public async Task<bool> SetGroupIsWordsAsync(int groupId)
  367. {
  368. var group = await GetAsync(groupId);
  369. return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsWords), !group.IsWords)
  370. .Where(nameof(Entity.Group.Id), groupId)) > 0;
  371. }
  372. public async Task<List<JobGroupResult>> ElasticSearchGroup()
  373. {
  374. 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";
  375. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  376. var database = new Database(databaseType, _connectionString);
  377. var connection = database.GetConnection();
  378. var items = await connection
  379. .QueryAsync<JobGroupResult, Entity.Group, User, JobGroupResult>(sql,
  380. (jobGroupResult, group, user) =>
  381. {
  382. jobGroupResult.MiddleId = jobGroupResult.Id;
  383. jobGroupResult.Id = group.Id;
  384. jobGroupResult.Name = group != null ? group.Name : "";
  385. jobGroupResult.AvatarUrl = group != null ? group.AvatarUrl : "";
  386. return jobGroupResult;
  387. },
  388. splitOn: "Id,Id,Name");
  389. return items.ToList();
  390. }
  391. /// <summary>
  392. /// 根据广场类别查询小组
  393. /// </summary>
  394. /// <param name="categoryId"></param>
  395. /// <returns></returns>
  396. public async Task<IEnumerable<Entity.Group>> GetAllAsync(int categoryId)
  397. {
  398. var sql = $@"SELECT
  399. *,
  400. (SELECT
  401. COUNT(1)
  402. FROM
  403. tede_group_user
  404. WHERE
  405. GroupId = a.Id) AS UserCount,
  406. (SELECT
  407. COUNT(1)
  408. FROM
  409. tede_topic
  410. WHERE
  411. GroupId = a.Id) AS GroupArticleCount
  412. FROM
  413. tede_group a
  414. WHERE
  415. a.GroupCategroyId = {categoryId} and a.IsShow=1";
  416. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  417. var database = new Database(databaseType, _connectionString);
  418. var connection = database.GetConnection();
  419. var result = await connection.QueryAsync<Entity.Group>(sql);
  420. //return await _repository.GetAllAsync(Q.Where(nameof(Entity.Group.GroupCategroyId), categoryId));
  421. return result;
  422. }
  423. /// <summary>
  424. /// 用户小组列表
  425. /// </summary>
  426. /// <param name="userId"></param>
  427. /// <returns></returns>
  428. public async Task<IEnumerable<UserGroupList>> GetAllByUserIdAsync(int userId)
  429. {
  430. string sql = $@"SELECT
  431. Id,
  432. Name,
  433. AvatarUrl,
  434. (SELECT
  435. COUNT(1)
  436. FROM
  437. tede_group_user
  438. WHERE
  439. GroupId = a.Id) AS UserCount
  440. FROM
  441. tede_group a
  442. WHERE
  443. a.id IN (SELECT
  444. GroupId
  445. FROM
  446. ccpph_tede.tede_group_user
  447. WHERE
  448. UserId = {userId})";
  449. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  450. var database = new Database(databaseType, _connectionString);
  451. var connection = database.GetConnection();
  452. var items = await connection
  453. .QueryAsync<UserGroupList>(sql);
  454. foreach (var item in items)
  455. {
  456. item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl);
  457. }
  458. return items;
  459. }
  460. }
  461. }