GroupRepository.cs 35 KB

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