GroupRepository.cs 36 KB

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