GroupRepository.cs 31 KB

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