GroupRepository.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. (SELECT
  430. COUNT(1)
  431. FROM
  432. tede_group_user
  433. WHERE
  434. GroupId = a.Id) AS UserCount,
  435. (SELECT
  436. COUNT(1)
  437. FROM
  438. tede_topic
  439. WHERE
  440. GroupId = a.Id) AS TopicCount
  441. FROM
  442. tede_group a
  443. WHERE
  444. a.GroupCategroyId = {categoryId} and a.IsShow=1 order by a.Sort desc";
  445. if (userId > 0)
  446. {
  447. sql = $@"SELECT
  448. a.Id,
  449. a.Name,
  450. a.AvatarUrl,
  451. a.Introduce,
  452. (SELECT
  453. COUNT(1)
  454. FROM
  455. tede_group_user
  456. WHERE
  457. GroupId = a.Id AND UserId = {userId}
  458. LIMIT 1) AS IsUser,
  459. (SELECT
  460. count(1)
  461. FROM
  462. tede_admin_verify
  463. WHERE
  464. SourceType = 1 AND VerifyType = 3
  465. AND UserId = {userId}
  466. AND DisposeType = 0
  467. AND SourceId = a.Id limit 1) as IsApply
  468. FROM
  469. tede_group a where a.GroupCategroyId = {categoryId} and a.IsShow=1 order by a.Sort desc";
  470. }
  471. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  472. var database = new Database(databaseType, _connectionString);
  473. var connection = database.GetConnection();
  474. var result = await connection.QueryAsync<SearchGroupDetailResult>(sql);
  475. //return await _repository.GetAllAsync(Q.Where(nameof(Entity.Group.GroupCategroyId), categoryId));
  476. return result;
  477. }
  478. /// <summary>
  479. /// 用户小组列表
  480. /// </summary>
  481. /// <param name="userId"></param>
  482. /// <returns></returns>
  483. public async Task<IEnumerable<UserGroupList>> GetAllByUserIdAsync(int userId)
  484. {
  485. var groupConstValue = AllTypeConst.Group.GetHashCode();
  486. string sql = $@"SELECT
  487. b.*,
  488. (SELECT
  489. COUNT(1)
  490. FROM
  491. tede_group_user
  492. WHERE
  493. GroupId = a.MiddleId) AS UserCount
  494. FROM
  495. tede_middle a
  496. INNER JOIN
  497. tede_group b ON a.MiddleId = b.Id
  498. WHERE
  499. a.FolderType ={groupConstValue} AND a.UserId ={userId}
  500. AND a.IsDelete = 0";
  501. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  502. var database = new Database(databaseType, _connectionString);
  503. var connection = database.GetConnection();
  504. var items = await connection
  505. .QueryAsync<UserGroupList>(sql);
  506. foreach (var item in items)
  507. item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl);
  508. return items;
  509. }
  510. /// <summary>
  511. /// 或者最近使用的小组
  512. /// </summary>
  513. /// <param name="userId"></param>
  514. /// <returns></returns>
  515. public async Task<IEnumerable<LatelyGroupDetailResult>> GetLatelyGroupDetailResultAsync(int userId)
  516. {
  517. string sql = $@"
  518. SELECT
  519. a.Id,
  520. a.Name,
  521. a.AvatarUrl,
  522. (SELECT
  523. COUNT(1)
  524. FROM
  525. tede_group_user
  526. WHERE
  527. GroupId = a.Id) AS UserCount
  528. FROM
  529. tede_group a
  530. INNER JOIN
  531. tede_middle b ON a.Id = b.MiddleId
  532. WHERE
  533. b.UserId = {userId} AND b.FolderType = {AllTypeConst.Group.GetHashCode()}
  534. AND b.IsDelete = 0
  535. AND a.Id IN (SELECT
  536. GroupId
  537. FROM
  538. tede_topic
  539. WHERE
  540. GroupId > 0 AND UserId = {userId}
  541. ORDER BY CreatedDate DESC)";
  542. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  543. var database = new Database(databaseType, _connectionString);
  544. var connection = database.GetConnection();
  545. var items = await connection
  546. .QueryAsync<LatelyGroupDetailResult>(sql);
  547. foreach (var item in items)
  548. {
  549. item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl);
  550. item.TypeValue = UserlinkConst.MyGroup.GetHashCode();
  551. item.TypeId = UserlinkConst.System.GetHashCode();
  552. }
  553. return items;
  554. }
  555. /// <summary>
  556. /// 小组列表 搜索专用
  557. /// </summary>
  558. /// <param name="userId"></param>
  559. /// <returns></returns>
  560. public async Task<IEnumerable<UserGroupResult>> GetUserGroupListAsync(int userId)
  561. {
  562. var result = new List<UserGroupResult>();
  563. string sql = $@"SELECT
  564. b.*
  565. FROM
  566. tede_group_user a
  567. INNER JOIN
  568. tede_group b ON a.GroupId = b.Id
  569. WHERE
  570. a.UserId = {userId}
  571. ORDER BY b.CreatedDate DESC";
  572. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  573. var database = new Database(databaseType, _connectionString);
  574. var connection = database.GetConnection();
  575. var items = await connection
  576. .QueryAsync<Entity.Group>(sql);
  577. result.Add(new UserGroupResult
  578. {
  579. Id = -1,
  580. Name = "全网公开"
  581. });
  582. result.Add(new UserGroupResult
  583. {
  584. Id = -2,
  585. Name = "我的小组"
  586. });
  587. foreach (var item in items)
  588. {
  589. result.Add(new UserGroupResult
  590. {
  591. Id = item.Id,
  592. Name = item.Name
  593. });
  594. }
  595. return result;
  596. }
  597. /// <summary>
  598. /// 获取小组列表
  599. /// </summary>
  600. /// <returns></returns>
  601. public async Task<IEnumerable<GroupDetailResult>> GetGroupDetailResults(string keyWord)
  602. {
  603. string sqlStr = string.Empty;
  604. if (!string.IsNullOrWhiteSpace(keyWord))
  605. sqlStr += $" and a.Name like '%{keyWord}%'";
  606. string sql = $@"SELECT
  607. a.*, b.PiazzaName AS CategroyName, c.Name AS UserName
  608. FROM
  609. tede_group a
  610. INNER JOIN
  611. tede_user c ON a.UserId = c.Id
  612. LEFT JOIN
  613. tede_group_piazza b ON a.GroupCategroyId = b.Id Where 1=1 {sqlStr}
  614. ORDER BY a.Sort DESC , a.CreatedDate DESC";
  615. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  616. var database = new Database(databaseType, _connectionString);
  617. var connection = database.GetConnection();
  618. var result = await connection
  619. .QueryAsync<GroupDetailResult>(sql);
  620. foreach (var item in result)
  621. {
  622. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  623. item.QRCoder = StringUtils.AddDomainMin(item.QRCoder);
  624. }
  625. return result;
  626. }
  627. /// <summary>
  628. /// 小组广场搜索
  629. /// </summary>
  630. /// <param name="keyWord"></param>
  631. /// <param name="userId"></param>
  632. /// <returns></returns>
  633. public async Task<IEnumerable<SearchGroupDetailResult>> GetSearchGroupAsync(string keyWord, int userId)
  634. {
  635. string sqlStr = string.Empty;
  636. if (!string.IsNullOrWhiteSpace(keyWord))
  637. sqlStr += $" and a.Name like '%{keyWord}%'";
  638. string sql = $@"SELECT
  639. a.Id,
  640. a.Name,
  641. a.AvatarUrl,
  642. a.Introduce,
  643. (SELECT
  644. COUNT(1)
  645. FROM
  646. tede_group_user
  647. WHERE
  648. GroupId = a.Id) AS UserCount,
  649. (SELECT
  650. COUNT(1)
  651. FROM
  652. tede_topic
  653. WHERE
  654. GroupId = a.Id) as TopicCount,
  655. (SELECT
  656. COUNT(1)
  657. FROM
  658. tede_group_user
  659. WHERE
  660. GroupId = a.Id AND UserId = {userId}
  661. LIMIT 1) AS IsUser,
  662. (SELECT
  663. count(1)
  664. FROM
  665. tede_admin_verify
  666. WHERE
  667. SourceType = 1 AND VerifyType = 3
  668. AND UserId = {userId}
  669. AND DisposeType = 0
  670. AND SourceId = a.Id limit 1) as IsApply
  671. FROM
  672. tede_group a
  673. WHERE
  674. a.IsShow = 1 {sqlStr}
  675. ORDER BY a.Sort DESC";
  676. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  677. var database = new Database(databaseType, _connectionString);
  678. var connection = database.GetConnection();
  679. var result = await connection
  680. .QueryAsync<SearchGroupDetailResult>(sql);
  681. foreach (var item in result)
  682. {
  683. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  684. item.Introduce = string.IsNullOrWhiteSpace(item.Introduce) ? string.Empty : item.Introduce;
  685. }
  686. return result;
  687. }
  688. }
  689. }