GroupRepository.cs 35 KB

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