using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Page; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.EnumConst; using GxPress.Repository.Interface; using GxPress.Request.App.Group; using GxPress.Result.App.Group; using GxPress.Result.App.GroupUser; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Datory; using GxPress.Request.App.Topic; using SqlKata; using GxPress.Result.Job; using System.Linq; namespace GxPress.Repository.Implement { public class GroupRepository : IGroupRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly IWebHostEnvironment _environment; private readonly string _connectionString; private readonly string _databaseTypeStr; private readonly Repository _groupUserRepository; public GroupRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper, IWebHostEnvironment environment) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _groupUserRepository = new Repository(database); _environment = environment; _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(GroupInRequest request) { if (string.IsNullOrEmpty(request.Name)) throw new BusinessException("小组名称不能为空"); var group = new Entity.Group { Name = request.Name, AvatarUrl = request.AvatarUrl, Introduce = request.Introduce, IsAdmin = false, IsUDisturb = false, QRCoder = request.QRCoder, IsWords = false, UserId = request.UserId, GroupCategroyId = request.GroupCategroyId, LabelName = request.LabelName, IsShow = request.IsShow }; if (!string.IsNullOrEmpty(group.AvatarUrl)) group.AvatarUrl = StringUtils.RemoveDomain(group.AvatarUrl); var id = await _repository.InsertAsync(group); //生成二维码 var groupDto = await _repository.GetAsync(id); var qrCodeData = new { key = QRCodeTypeConst.GroupInfo, value = groupDto.Guid }; var resultPath = FileHelper.SaveQRCodeFile(StringUtils.GetWebRootPath(_environment.WebRootPath), JsonConvert.SerializeObject(qrCodeData)); groupDto.QRCoder = resultPath.RelativePath; groupDto.Sort = id; await _repository.UpdateAsync(groupDto); return id; } public async Task> GetAllAsync(SqlKata.Query query) { return await _groupUserRepository.GetAllAsync(query); } /// /// 修改小组 /// /// /// public async Task UpdateAsync(GroupUpRequest request) { var group = await _repository.GetAsync(request.Id); if (!string.IsNullOrEmpty(request.Name)) group.Name = request.Name; if (request.GroupCategroyId > 0) group.GroupCategroyId = request.GroupCategroyId; if (!string.IsNullOrEmpty(request.AvatarUrl)) { group.HistoryAvatarUrl = group.AvatarUrl; group.AvatarUrl = StringUtils.RemoveDomain(request.AvatarUrl); } if (!string.IsNullOrEmpty(request.Introduce)) group.Introduce = request.Introduce; if (!string.IsNullOrEmpty(request.LabelName)) group.LabelName = request.LabelName; if (request.IsShow > 0) group.IsShow = request.IsShow == 1; if (request.IsAdmin > 0) group.IsAdmin = request.IsAdmin == 1; if (request.IsWords > 0) group.IsWords = request.IsWords == 1; if (request.Sort > 0) group.Sort = request.Sort; return await _repository.UpdateAsync(group); } /// /// 修改小组 /// /// /// public async Task UpdateAsync(Entity.Group group) { return await _repository.UpdateAsync(group); } /// /// 删除小组 /// /// /// public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } /// /// 查询小组 /// /// /// public async Task> FindGroupByGroupFolderId(GroupSearchRequest request) { var pagedList = new PagedList(); var groupConst = AllTypeConst.Group.GetHashCode(); //根据文件夹ID查询成员 var sqlValue = ""; // if (!string.IsNullOrEmpty(request.Key)) // sqlValue += $"and b.Name like '%{request.Key}%'"; //共享数量 var topicSharingCount = "(select count(1) from tede_group_user where GroupId=a.MiddleId) as TopicSharingCount"; //总话题数量 var topicAllCount = "(select count(1) from tede_topic where GroupId=a.MiddleId) as TopicAllCount"; //未读话题数量 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"; var categoryName = "(select FolderName from tede_middle where id=a.ParentId) as CategoryName"; var sql = $@"SELECT a.*,{topicSharingCount},{topicAllCount},{uReadTopicCount},{categoryName}, (SELECT COUNT(1) FROM tede_group WHERE id IN (SELECT MiddleId FROM tede_middle WHERE 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 "; if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key)) sql += $"and a.ParentId = @groupFolderId "; else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key)) sql += $"and a.ParentId = @groupFolderId and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')"; else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key)) sql += $"and a.ParentId = @groupFolderId "; else sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')"; sql += $@" 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"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (groupUserFindResult, group) => { groupUserFindResult.GroupName = group == null ? "" : group.Name; groupUserFindResult.AvatarUrl = group == null ? "" : StringUtils.AddDomainMin(group.AvatarUrl); groupUserFindResult.GroupId = group?.Id ?? 0; return groupUserFindResult; }, new { groupFolderId = request.GroupFolderId, userId = request.UserId, page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage * request.Page }, splitOn: "Id"); pagedList.Items = result; var sqlCount = @"SELECT count(1) FROM tede_Middle a LEFT JOIN tede_group b ON a.MiddleId = b.Id WHERE 1=1 "; if (request.GroupFolderId == 0 && string.IsNullOrEmpty(request.Key)) sqlCount += $"and a.ParentId = {request.GroupFolderId} "; else if (request.GroupFolderId > 0 && !string.IsNullOrEmpty(request.Key)) sqlCount += $"and a.ParentId = {request.GroupFolderId} and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')"; else if (request.GroupFolderId > 0 && string.IsNullOrEmpty(request.Key)) sqlCount += $"and a.ParentId = {request.GroupFolderId} "; else sql += $" and (a.FolderName like '%{request.Key}%' or b.Name like '%{request.Key}%')"; sqlCount += $" and a.UserId={request.UserId} and a.FolderType={groupConst} and a.IsDelete=0 {sqlValue} and (b.id>0 or a.AttributeValue=2)"; pagedList.Total = await FindGroupByGroupFolderIdCountAsync(sqlCount); return pagedList; } /// /// 条数 /// /// /// public async Task FindGroupByGroupFolderIdCountAsync(string sql) { var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.ExecuteScalarAsync(sql); return result; } /// /// 获取小组详情 /// /// /// public async Task GroupDetailAsync(GroupDetailRequest request) { var groupDetail = await _repository.GetAsync(request.GroupId); if (groupDetail == null) throw new BusinessException("小组不存在"); if (!string.IsNullOrEmpty(groupDetail.AvatarUrl)) groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl); if (!string.IsNullOrEmpty(groupDetail.AvatarUrl)) groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl); if (!string.IsNullOrEmpty(groupDetail.HistoryAvatarUrl)) groupDetail.HistoryAvatarUrl = StringUtils.AddDomain(groupDetail.HistoryAvatarUrl); var sql = $@"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 order by a.GroupUserRoleId"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (groupUserListResult, user, department) => { groupUserListResult.DepartmentName = department.Name; groupUserListResult.Name = user.Name; groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl); return groupUserListResult; }, new { groupId = request.GroupId }, splitOn: "Name"); var groupDetailResult = _mapper.Map(groupDetail); groupDetailResult.GroupUserListResult = result; var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), request.UserId) .Where(nameof(GroupUser.GroupId), request.GroupId)); //获取当前用户的权限 groupDetailResult.GroupUserRoleId = groupUser != null ? groupUser.GroupUserRoleId : 0; groupDetailResult.IsUDisturb = groupUser != null ? groupUser.IsUDisturb : false; //groupDetailResult.IsAdmin = groupDetail != null && groupUser != null ? groupDetail.UserId == groupUser.UserId : false; groupDetailResult.IsUser = groupUser != null ? groupUser.UserId == request.UserId : false; groupDetailResult.CreatedDate = groupDetail.CreatedDate; groupDetailResult.QRCoder = StringUtils.AddDomain(groupDetailResult.QRCoder); return groupDetailResult; } /// /// 搜索小组成员 /// /// /// public async Task> SearchGroupUserAsync(TopicDetailListRequest request) { string sql = $@"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 = {request.GroupId} and b.Name like '%{request.Key}%'"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (groupUserListResult, user, department) => { groupUserListResult.DepartmentName = department.Name; groupUserListResult.Name = user.Name; groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl); return groupUserListResult; }, splitOn: "Name"); return result; } /// /// 获取小组详情 /// /// /// /// public async Task GroupDetailByGuIdAsync(string guId, int userId) { var groupDetail = await _repository.GetAsync(Q.Where(nameof(Entity.Group.Guid), guId)); if (groupDetail == null) throw new BusinessException("小组不存在"); if (!string.IsNullOrEmpty(groupDetail.AvatarUrl)) groupDetail.AvatarUrl = StringUtils.AddDomainMin(groupDetail.AvatarUrl); if (!string.IsNullOrEmpty(groupDetail.QRCoder)) groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder); var sql = "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"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (groupUserListResult, user, department) => { groupUserListResult.DepartmentName = department.Name; groupUserListResult.Name = user.Name; groupUserListResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl); return groupUserListResult; }, new { groupId = groupDetail.Id }, splitOn: "Name"); var groupDetailResult = _mapper.Map(groupDetail); groupDetailResult.GroupUserListResult = result; var groupUser = await _groupUserRepository.GetAsync(Q.Where(nameof(GroupUser.UserId), userId) .Where(nameof(GroupUser.Guid), guId)); groupDetailResult.GroupUserRoleId = groupUser?.GroupUserRoleId ?? GroupUserRoleTyeConst.General; //获取当前用户的权限 return groupDetailResult; } /// /// 获取用户未读取话题数量 /// /// /// /// public async Task GetUReadCountAsync(int userId, int groupId) { string sql = @"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"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.ExecuteScalarAsync(sql, new { userId, groupId }); return result; } /// /// 获取小组的共享话题数量 /// /// /// public async Task SharingCountAsync(int groupId) { var sql = $"select count(1) from tede_group_user where GroupId={groupId}"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.ExecuteScalarAsync(sql, new { GroupId = groupId }); return result; } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } /// /// 是开启管理员同意加入 /// /// /// public async Task SetGroupIsAdminAsync(int groupId) { var group = await GetAsync(groupId); return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsAdmin), !group.IsAdmin) .Where(nameof(Entity.Group.Id), groupId)) > 0; } /// /// 是否禁言 /// /// /// public async Task SetGroupIsWordsAsync(int groupId) { var group = await GetAsync(groupId); return await _repository.UpdateAsync(Q.Set(nameof(Entity.Group.IsWords), !group.IsWords) .Where(nameof(Entity.Group.Id), groupId)) > 0; } public async Task> ElasticSearchGroup() { 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"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql, (jobGroupResult, group, user) => { jobGroupResult.MiddleId = jobGroupResult.Id; jobGroupResult.Id = group.Id; jobGroupResult.Name = group != null ? group.Name : ""; jobGroupResult.AvatarUrl = group != null ? group.AvatarUrl : ""; return jobGroupResult; }, splitOn: "Id,Id,Name"); return items.ToList(); } /// /// 根据广场类别查询小组 /// /// /// public async Task> GetAllAsync(int categoryId, int userId) { var sql = $@"SELECT a.Id, a.Name, a.AvatarUrl, a.Introduce, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.Id) AS UserCount, (SELECT COUNT(1) FROM tede_topic WHERE GroupId = a.Id) AS TopicCount FROM tede_group a WHERE a.GroupCategroyId = {categoryId} and a.IsShow=1 order by a.Sort desc"; if (userId > 0) { sql = $@"SELECT a.Id, a.Name, a.AvatarUrl, a.Introduce, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.Id AND UserId = {userId} LIMIT 1) AS IsUser, (SELECT count(1) FROM tede_admin_verify WHERE SourceType = 1 AND VerifyType = 3 AND UserId = {userId} AND DisposeType = 0 AND SourceId = a.Id limit 1) as IsApply FROM tede_group a where a.GroupCategroyId = {categoryId} and a.IsShow=1 order by a.Sort desc"; } var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); //return await _repository.GetAllAsync(Q.Where(nameof(Entity.Group.GroupCategroyId), categoryId)); return result; } /// /// 用户小组列表 /// /// /// public async Task> GetAllByUserIdAsync(int userId) { var groupConstValue = AllTypeConst.Group.GetHashCode(); string sql = $@"SELECT b.*, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.MiddleId) AS UserCount FROM tede_middle a INNER JOIN tede_group b ON a.MiddleId = b.Id WHERE a.FolderType ={groupConstValue} AND a.UserId ={userId} AND a.IsDelete = 0"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql); foreach (var item in items) item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl); return items; } /// /// 或者最近使用的小组 /// /// /// public async Task> GetLatelyGroupDetailResultAsync(int userId) { string sql = $@" SELECT a.Id, a.Name, a.AvatarUrl, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.Id) AS UserCount FROM tede_group a INNER JOIN tede_middle b ON a.Id = b.MiddleId WHERE b.UserId = {userId} AND b.FolderType = {AllTypeConst.Group.GetHashCode()} AND b.IsDelete = 0 AND a.Id IN (SELECT GroupId FROM tede_topic WHERE GroupId > 0 AND UserId = {userId} ORDER BY CreatedDate DESC)"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql); foreach (var item in items) { item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl); item.TypeValue = UserlinkConst.MyGroup.GetHashCode(); item.TypeId = UserlinkConst.System.GetHashCode(); } return items; } /// /// 小组列表 搜索专用 /// /// /// public async Task> GetUserGroupListAsync(int userId) { var result = new List(); string sql = $@"SELECT b.* FROM tede_group_user a INNER JOIN tede_group b ON a.GroupId = b.Id WHERE a.UserId = {userId} ORDER BY b.CreatedDate DESC"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql); result.Add(new UserGroupResult { Id = -1, Name = "全网公开" }); result.Add(new UserGroupResult { Id = -2, Name = "我的小组" }); foreach (var item in items) { result.Add(new UserGroupResult { Id = item.Id, Name = item.Name }); } return result; } /// /// 获取小组列表 /// /// public async Task> GetGroupDetailResults(string keyWord) { string sqlStr = string.Empty; if (!string.IsNullOrWhiteSpace(keyWord)) sqlStr += $" and a.Name like '%{keyWord}%'"; string sql = $@"SELECT a.*, b.PiazzaName AS CategroyName, c.Name AS UserName FROM tede_group a INNER JOIN tede_user c ON a.UserId = c.Id LEFT JOIN tede_group_piazza b ON a.GroupCategroyId = b.Id Where 1=1 {sqlStr} ORDER BY a.Sort DESC , a.CreatedDate DESC"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.QRCoder = StringUtils.AddDomainMin(item.QRCoder); } return result; } /// /// 小组广场搜索 /// /// /// /// public async Task> GetSearchGroupAsync(string keyWord, int userId) { string sqlStr = string.Empty; if (!string.IsNullOrWhiteSpace(keyWord)) sqlStr += $" and a.Name like '%{keyWord}%'"; string sql = $@"SELECT a.Id, a.Name, a.AvatarUrl, a.Introduce, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.Id) AS UserCount, (SELECT COUNT(1) FROM tede_topic WHERE GroupId = a.Id) as TopicCount, (SELECT COUNT(1) FROM tede_group_user WHERE GroupId = a.Id AND UserId = {userId} LIMIT 1) AS IsUser, (SELECT count(1) FROM tede_admin_verify WHERE SourceType = 1 AND VerifyType = 3 AND UserId = {userId} AND DisposeType = 0 AND SourceId = a.Id limit 1) as IsApply FROM tede_group a WHERE a.IsShow = 1 {sqlStr} ORDER BY a.Sort DESC"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.Introduce = string.IsNullOrWhiteSpace(item.Introduce) ? string.Empty : item.Introduce; } return result; } } }