GroupRepository.cs 36 KB

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