GroupRepository.cs 36 KB

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