using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Transactions; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.App.OftenContact; using GxPress.Result.App.OftenContact; using Microsoft.Extensions.Options; using Datory; using SqlKata; using Dapper; using GxPress.Result.Job; using System.Linq; namespace GxPress.Repository.Implement { public class OftenContactRepository : IOftenContactRepository { private readonly Repository _repository; private readonly Repository _userRepository; private readonly Repository _chatRecordRepository; private readonly Repository _groupChatRepository; private readonly Repository _middleRepository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public OftenContactRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _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); _userRepository = new Repository(database); _chatRecordRepository = new Repository(database); _groupChatRepository = new Repository(database); _middleRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } /// /// 添加好友关系 /// /// /// /// public async Task InsertAsync(OftenContactInRequest request, int userId) { if (string.IsNullOrEmpty(request.ContactUserId)) throw new BusinessException("缺少必要字段"); if (request.ChatType == 0) throw new BusinessException("类型必须"); var count = await _repository.CountAsync(Q.Where(nameof(OftenContact.UserId), userId) .Where(nameof(OftenContact.ContactUserId), request.ContactUserId)); if (count > 0) { //修改好友关系 await _repository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), false) .Where(nameof(OftenContact.UserId), userId) .Where(nameof(OftenContact.ContactUserId), request.ContactUserId)); if (request.ChatType == 1) await _repository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), false) .Where(nameof(OftenContact.UserId), int.Parse(request.ContactUserId)) .Where(nameof(OftenContact.ContactUserId), userId)); return true; } try { using (TransactionScope transactionScope = new TransactionScope()) { if (request.ChatType == 1) { var oftenContact = new OftenContact { IndividualGroupId = request.IndividualGroupId, ContactUserId = request.ContactUserId, UserId = userId, ChatType = request.ChatType, IsBlack = false, IsDelete = false }; await _repository.InsertAsync(oftenContact); oftenContact = new OftenContact { IndividualGroupId = 0, ContactUserId = userId.ToString(), UserId = int.Parse(request.ContactUserId), ChatType = request.ChatType, IsBlack = false, IsDelete = false }; await _repository.InsertAsync(oftenContact); } else { var oftenContact = new OftenContact { IndividualGroupId = request.IndividualGroupId, ContactUserId = request.ContactUserId, UserId = userId, ChatType = request.ChatType, IsBlack = false, IsDelete = false }; await _repository.InsertAsync(oftenContact); } transactionScope.Complete(); } } catch (Exception e) { Console.WriteLine(e); throw; } return true; } /// /// 修改黑名单 /// /// /// /// public async Task SetBlackUserAsync(int userId, int blackUserId) { //获取 var oftenContact = await _repository.GetAsync(Q.Where(nameof(OftenContact.UserId), userId) .Where(nameof(OftenContact.ContactUserId), blackUserId.ToString())); if (oftenContact != null) { oftenContact.IsBlack = !oftenContact.IsBlack; return await _repository.UpdateAsync(oftenContact); } return true; } public async Task FindOftenContactAsync(int userId, string groupChatImId) { return await _repository.GetAsync(Q.Where(nameof(OftenContact.UserId), userId) .Where(nameof(OftenContact.ContactUserId), groupChatImId)); } /// /// 移除当前文件夹关系 /// /// /// public async Task RemoveOftenContactAsync(OftenContactDeRequest request) { var oftenContact = await _repository.GetAsync(request.OftenContactId); oftenContact.IndividualGroupId = 0; return await _repository.UpdateAsync(oftenContact); } /// /// 设置置顶 /// /// public async Task SetOftenContactIsTopAsync(OftenContactDeRequest request) { var oftenContact = await _repository.GetAsync(request.OftenContactId); if (oftenContact == null) throw new BusinessException("聊天不存在"); return await _repository.UpdateAsync(Q.Set(nameof(OftenContact.IsTop), !oftenContact.IsTop) .Where(nameof(oftenContact.Id), request.OftenContactId)) > 0; } /// /// 移动到文件夹 /// /// /// public async Task MoveOftenContactToIndividualGroupAsync(OftenContactUpRequest request) { return await _repository.UpdateAsync(Q .Set(nameof(OftenContact.IndividualGroupId), request.IndividualGroupId) .WhereIn(nameof(OftenContact.Id), request.OftenContactIds)) > 0; } /// /// 删除聊天界面 /// /// /// public async Task DeleteAsync(List oftenContactIds) { return await _repository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), true) .WhereIn(nameof(OftenContact.Id), oftenContactIds)) > 0; } /// /// 获取是否置顶 /// /// /// /// public async Task GetIsTopAsync(int userId, string imId) { var oftenContact = await _repository.GetAsync(Q.Where(nameof(Entity.OftenContact.UserId), userId) .Where(nameof(Entity.OftenContact.ContactUserId), imId)); if (oftenContact == null) return false; var midel = await _middleRepository.GetAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), 10).Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id)); return midel.IsTop; } public async Task CountAsync(Query query) { return await _repository.CountAsync(query); } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } public async Task InsertAsync(OftenContact oftenContact) { return await _repository.InsertAsync(oftenContact); } /// /// 插入多个 /// /// /// public async Task InsertAsync(List oftenContacts) { if (oftenContacts.Count == 0 || oftenContacts == null) return false; string sql = @"INSERT INTO `tede_often_contact`(`Guid`,`CreatedDate`,`LastModifiedDate`,`UserId`,`ContactUserId`,`IndividualGroupId`,`UpdateTime`,`ChatType`,`IsBlack`,`IsTop`,`IsDelete`)VALUES"; foreach (var item in oftenContacts) { var guId = Guid.NewGuid().ToString(); var nowTime = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); var isBlack = item.IsBlack ? 1 : 0; var isTop = item.IsTop ? 1 : 0; var isDelete = item.IsDelete ? 1 : 0; sql += $@"('{guId}','{nowTime}','{nowTime}',{item.UserId},'{item.ContactUserId}',{item.IndividualGroupId},'{nowTime}',{item.ChatType},{isBlack},{isTop},{isDelete}),"; } sql = sql.Remove(sql.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); return await connection.ExecuteAsync(sql) > 0; } public async Task GetAsync(SqlKata.Query query) { return await _repository.GetAsync(query); } public async Task> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query); } /// /// 根据用户ID获取最近聊天用户 /// /// /// public async Task> GetAllAsync(int userId) { return await _repository.GetAllAsync(Q.Select(nameof(OftenContact.ContactUserId)).Where(nameof(OftenContact.UserId), userId).Where(nameof(OftenContact.ChatType), 1)); } public async Task> GetContactInfosAsync(OftenContactSearchRequest request) { var sql = $"select a.*,b.* from tede_middle a left join tede_often_contact b on a.MiddleId=b.Id where a.FolderType=10 and a.UserId={request.UserId} and a.ParentId={request.IndividualGroupId} and a.IsDelete=0 order by a. IsTop desc,a.LastModifiedDate desc"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (oftenContactInfo, oftenContact) => { oftenContactInfo.ChatType = oftenContact == null ? 0 : oftenContact.ChatType; oftenContactInfo.ContactUserId = oftenContact == null ? "" : oftenContact.ContactUserId; return oftenContactInfo; }, splitOn: "Id,Id"); return result.ToList(); } public async Task> GetContactInfoAllAsync(OftenContactSearchRequest request) { var sql = $"select a.*,b.* from tede_middle a inner join tede_often_contact b on a.MiddleId=b.Id where a.FolderType=10 and a.UserId={request.UserId} and a.IsDelete=0 order by a. IsTop desc,a.LastModifiedDate desc"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (oftenContactInfo, oftenContact) => { oftenContactInfo.ChatType = oftenContact == null ? 0 : oftenContact.ChatType; oftenContactInfo.ContactUserId = oftenContact == null ? "" : oftenContact.ContactUserId; return oftenContactInfo; }, splitOn: "Id,Id"); return result; } /// /// 获取全局索引聊天群 /// /// public async Task> ElasticSearchGroupChat() { string sql = "select a.Id,a.GuId,a.MiddleId,a.UserId,a.CreatedDate,b.ContactUserId,c.AvatarUrl,c.Name from tede_middle a inner join tede_often_contact b on a.MiddleId=b.Id inner join tede_group_chat c on c.GroupChatImId=b.ContactUserId where a.FolderType=10 and b.ChatType=2 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 result = await connection .QueryAsync(sql, (jobChatResult, oftenContact, groupChat) => { jobChatResult.ChatType = 2; jobChatResult.Name = groupChat != null ? groupChat.Name : ""; jobChatResult.AvatarUrl = groupChat != null ? groupChat.AvatarUrl : ""; jobChatResult.ImId = oftenContact != null ? oftenContact.ContactUserId : ""; return jobChatResult; }, splitOn: "Id,ContactUserId,AvatarUrl"); return result.ToList(); } /// /// 获取全局索引聊天 /// /// public async Task> ElasticSearchChat() { string sql = "select a.Id,a.GuId,a.MiddleId,a.UserId,a.CreatedDate,b.ContactUserId,c.AvatarUrl,c.Name,c.ImId from tede_middle a inner join tede_often_contact b on a.MiddleId=b.Id inner join tede_user c on c.Id=b.ContactUserId where a.FolderType=10 and b.ChatType=1 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 result = await connection .QueryAsync(sql, (jobChatResult, oftenContact, user) => { jobChatResult.ChatType = 1; jobChatResult.Name = user != null ? user.Name : ""; jobChatResult.AvatarUrl = user != null ? user.AvatarUrl : ""; jobChatResult.ImId = user != null ? user.ImId : ""; jobChatResult.ToUserId = oftenContact != null ? Convert.ToInt32(oftenContact.ContactUserId) : 0; return jobChatResult; }, splitOn: "Id,ContactUserId,AvatarUrl"); return result.ToList(); } } }