GroupRepository.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. private readonly Repository<Entity.Middle.Middle> middleRepository;
  34. public GroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
  35. IWebHostEnvironment environment)
  36. {
  37. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  38. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  39. var databaseType =
  40. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  41. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  42. _repository = new Repository<Entity.Group>(database);
  43. _groupUserRepository = new Repository<GroupUser>(database);
  44. middleRepository = new Repository<Entity.Middle.Middle>(database);
  45. _environment = environment;
  46. _mapper = mapper;
  47. }
  48. public IDatabase Database => _repository.Database;
  49. public string TableName => _repository.TableName;
  50. public List<TableColumn> TableColumns => _repository.TableColumns;
  51. public async Task<int> InsertAsync(GroupInRequest request)
  52. {
  53. if (string.IsNullOrEmpty(request.Name))
  54. throw new BusinessException("小组名称不能为空");
  55. var group = new Entity.Group
  56. {
  57. Name = request.Name,
  58. AvatarUrl = request.AvatarUrl,
  59. Introduce = request.Introduce,
  60. IsAdmin = false,
  61. IsUDisturb = false,
  62. QRCoder = request.QRCoder,
  63. IsWords = false,
  64. UserId = request.UserId,
  65. GroupCategroyId = request.GroupCategroyId,
  66. LabelName = request.LabelName,
  67. IsShow = request.IsShow
  68. };
  69. if (!string.IsNullOrEmpty(group.AvatarUrl))
  70. group.AvatarUrl = StringUtils.RemoveDomain(group.AvatarUrl);
  71. var id = await _repository.InsertAsync(group);
  72. //生成二维码
  73. var groupDto = await _repository.GetAsync(id);
  74. var qrCodeData = new { key = QRCodeTypeConst.GroupInfo, value = groupDto.Guid };
  75. var resultPath =
  76. FileHelper.SaveQRCodeFile(StringUtils.GetWebRootPath(_environment.WebRootPath),
  77. JsonConvert.SerializeObject(qrCodeData));
  78. groupDto.QRCoder = resultPath.RelativePath;
  79. groupDto.Sort = id;
  80. await _repository.UpdateAsync(groupDto);
  81. return id;
  82. }
  83. public async Task<IEnumerable<GroupUser>> GetAllAsync(SqlKata.Query query)
  84. {
  85. return await _groupUserRepository.GetAllAsync(query);
  86. }
  87. /// <summary>
  88. /// 修改小组
  89. /// </summary>
  90. /// <param name="request"></param>
  91. /// <returns></returns>
  92. public async Task<bool> UpdateAsync(GroupUpRequest request)
  93. {
  94. var group = await _repository.GetAsync(request.Id);
  95. if (!string.IsNullOrEmpty(request.Name))
  96. group.Name = request.Name;
  97. if (request.GroupCategroyId > 0)
  98. group.GroupCategroyId = request.GroupCategroyId;
  99. if (!string.IsNullOrEmpty(request.AvatarUrl))
  100. {
  101. group.HistoryAvatarUrl = group.AvatarUrl;
  102. group.AvatarUrl = StringUtils.RemoveDomain(request.AvatarUrl);
  103. }
  104. if (!string.IsNullOrEmpty(request.Introduce))
  105. group.Introduce = request.Introduce;
  106. if (!string.IsNullOrEmpty(request.LabelName))
  107. group.LabelName = request.LabelName;
  108. if (request.IsShow > 0)
  109. group.IsShow = request.IsShow == 1;
  110. if (request.IsAdmin > 0)
  111. group.IsAdmin = request.IsAdmin == 1;
  112. if (request.IsWords > 0)
  113. group.IsWords = request.IsWords == 1;
  114. if (request.Sort > 0)
  115. group.Sort = request.Sort;
  116. return await _repository.UpdateAsync(group);
  117. }
  118. /// <summary>
  119. /// 是否添加到通讯录
  120. /// </summary>
  121. /// <param name="groupId"></param>
  122. /// <param name="userId"></param>
  123. /// <returns></returns>
  124. public async Task<bool> UpdateContactsAsync(int groupId, int userId)
  125. {
  126. var query = Q.NewQuery();
  127. query.Where(nameof(Entity.GroupUser.GroupId), groupId);
  128. query.Where(nameof(Entity.GroupUser.UserId), userId);
  129. var groupUser = await _groupUserRepository.GetAsync(query);
  130. if (groupUser == null)
  131. throw new BusinessException("小组用户不存在");
  132. groupUser.IsContacts = !groupUser.IsContacts;
  133. return await _groupUserRepository.UpdateAsync(groupUser);
  134. }
  135. /// <summary>
  136. /// /// 修改小组
  137. /// </summary>
  138. /// <param name="request"></param>
  139. /// <returns></returns>
  140. public async Task<bool> UpdateAsync(Entity.Group group)
  141. {
  142. return await _repository.UpdateAsync(group);
  143. }
  144. /// <summary>
  145. /// 删除小组
  146. /// </summary>
  147. /// <param name="id"></param>
  148. /// <returns></returns>
  149. public async Task<bool> DeleteAsync(int id)
  150. {
  151. return await _repository.DeleteAsync(id);
  152. }
  153. public async Task<bool> DeleteAsync(Query query)
  154. {
  155. return await _repository.DeleteAsync(query) > 0;
  156. }
  157. /// <summary>
  158. /// 查询小组
  159. /// </summary>
  160. /// <param name="request"></param>
  161. /// <returns></returns>
  162. public async Task<PagedList<GroupUserFindResult>> FindGroupByGroupFolderId(GroupSearchRequest request)
  163. {
  164. var result = new PagedList<GroupUserFindResult>();
  165. var data = new List<GroupUserFindResult>();
  166. data.AddRange(await GetGroupUserFindResult(true, request));
  167. data.AddRange(await GetGroupUserFindResult(false, request));
  168. for (int i = 0; i < data.Count; i++)
  169. {
  170. data[i].AvatarUrl = StringUtils.AddDomainMin(data[i].AvatarUrl);
  171. if (data[i].FileCount == 0 && request.IsFiltrationBlank && data[i].AttributeValue == AttributeValueConst.File.GetHashCode())
  172. data.Remove(data[i]);
  173. }
  174. result.Items = data;
  175. result.Total = result.Items.Count();
  176. return result;
  177. }
  178. public async Task<IEnumerable<GroupUserFindResult>> GetGroupUserFindResult(bool IsTop, GroupSearchRequest request)
  179. {
  180. var groupConst = AllTypeConst.Group.GetHashCode();
  181. var query = new SqlKata.Query("tede_Middle");
  182. query.LeftJoin("tede_group", "tede_group.Id", "tede_Middle.MiddleId");
  183. if (request.IsContacts)
  184. {
  185. query.LeftJoin("tede_group_user", "tede_group_user.GroupId", "tede_group.Id");
  186. query.Where("tede_group_user.IsContacts", request.IsContacts);
  187. query.Where("tede_group_user.UserId", request.UserId);
  188. }
  189. if (request.GroupFolderId > 0)
  190. query.Where("tede_Middle.ParentId", request.GroupFolderId);
  191. if (!string.IsNullOrEmpty(request.Key))
  192. query.Where(n => n.OrWhereLike("tede_Middle.FolderName", $"%request.Key%").OrWhereLike("tede_group.Name", $"%{request.Key}%"));
  193. if (string.IsNullOrEmpty(request.Key) && request.GroupFolderId == 0)
  194. query.Where("tede_Middle.ParentId", request.GroupFolderId);
  195. query.Where("tede_Middle.UserId", request.UserId);
  196. query.Where("tede_Middle.IsDelete", false);
  197. query.Where("tede_Middle.FolderType", groupConst);
  198. query.Where("tede_Middle.IsTop", IsTop);
  199. if (IsTop)
  200. {
  201. query.OrderByDesc("tede_Middle.IsTop");
  202. query.OrderByDesc("tede_Middle.LastModifiedDate");
  203. }
  204. else
  205. query.OrderByDesc("tede_group.LastModifiedDate");
  206. var topicSharingCountQuery = new SqlKata.Query("tede_group_user");
  207. topicSharingCountQuery.WhereColumns("tede_group_user.GroupId", "=", "tede_Middle.MiddleId");
  208. topicSharingCountQuery.AsCount();
  209. query.Select(topicSharingCountQuery, "TopicSharingCount");
  210. var topicAllCountQuery = new SqlKata.Query("tede_topic");
  211. topicAllCountQuery.WhereColumns("tede_topic.GroupId", "=", "tede_Middle.MiddleId");
  212. topicAllCountQuery.Where("tede_topic.IsDraft", false);
  213. topicAllCountQuery.AsCount();
  214. query.Select(topicAllCountQuery, "TopicAllCount");
  215. var groupCountQuery = new SqlKata.Query("tede_middle as a");
  216. groupCountQuery.WhereColumns("a.parentId", "=", "tede_Middle.Id");
  217. groupCountQuery.AsCount();
  218. query.Select(groupCountQuery, "GroupCount");
  219. var uReadTopicCountQuery = new SqlKata.Query("tede_topic");
  220. uReadTopicCountQuery.WhereColumns("tede_topic.groupId", "=", "tede_group.Id");
  221. var groupUserQuery = new SqlKata.Query("tede_group_user");
  222. groupUserQuery.WhereColumns("tede_group_user.groupId", "=", "tede_group.Id");
  223. groupUserQuery.Where("tede_group_user.userId", request.UserId);
  224. groupUserQuery.Select("ReadDate");
  225. uReadTopicCountQuery.Where("tede_topic.LastModifiedDate", ">", groupUserQuery);
  226. uReadTopicCountQuery.AsCount();
  227. query.Select(uReadTopicCountQuery, "UReadTopicCount");
  228. var categoryNameQuery = new SqlKata.Query("tede_middle as a");
  229. categoryNameQuery.WhereColumns("a.Id", "=", "tede_Middle.ParentId");
  230. categoryNameQuery.Select("a.FolderName");
  231. query.Select(categoryNameQuery, "CategoryName");
  232. var fileCountQuery = new SqlKata.Query("tede_group");
  233. var middleQuery = new SqlKata.Query("tede_Middle as a");
  234. middleQuery.WhereColumns("a.ParentId", "=", "tede_Middle.Id");
  235. middleQuery.Select("a.MiddleId");
  236. fileCountQuery.WhereIn("tede_group.Id", middleQuery);
  237. fileCountQuery.AsCount();
  238. query.Select(fileCountQuery, "FileCount");
  239. query.Select("tede_Middle.{Id,IsTop,FolderName,AttributeValue}",
  240. "tede_group.{Id as GroupId,AvatarUrl,Name as GroupName}");
  241. return await middleRepository.GetAllAsync<GroupUserFindResult>(query);
  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 a.LastModifiedDate 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. }