GroupRepository.cs 27 KB

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