using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity.Middle; using GxPress.Repository.Interface; using GxPress.Request.Notice; using Microsoft.Extensions.Options; using SqlKata; using Datory; using GxPress.Request.App.Middle; using System; using GxPress.Request.Inbox; using System.Linq; using GxPress.EnumConst; namespace GxPress.Repository.Implement { public class MiddleRepository : IMiddleRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public MiddleRepository(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); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加 /// /// /// public async Task InsertAsync(Middle middle) { middle.IsTop = false; middle.IsDelete = false; middle.IsRead = false; return await _repository.InsertAsync(middle); } public async Task ExistsAsync(SqlKata.Query query) { return await _repository.ExistsAsync(query); } /// /// 添加 /// /// /// public async Task InsertAsync(List middles) { if (middles.Count == 0) return false; var sql = "INSERT INTO `tede_middle`(`Guid`,`CreatedDate`,`LastModifiedDate`,`FolderName`,`FolderType`,`IsTop`,`RoleId`,`Sort`,`MiddleId`,`ParentId`,`UserId`,`MiddleSonId`,`IsRead`,`NoticeAddresseeType`,`IsAdmin`,`IsDelete`,`AttributeValue`,`IsDefault`,`IsUpload`,`IsRecall`)VALUES"; foreach (var item in middles) { var guId = Guid.NewGuid().ToString(); var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var isRead = item.IsRead == true ? 1 : 0; var isDefault = item.IsDefault == true ? 1 : 0; var isDelete = item.IsDelete == true ? 1 : 0; var isAdmin = item.IsAdmin == true ? 1 : 0; var isRecall = item.IsRecall == true ? 1 : 0; sql += $"('{guId}','{createdDate}','{createdDate}','{item.FolderName}',{item.FolderType},{item.IsTop},{item.RoleId},{item.Sort},{item.MiddleId},{item.ParentId},{item.UserId},{item.MiddleSonId},{isRead},{item.NoticeAddresseeType.GetHashCode()},{isAdmin},{isDelete},{item.AttributeValue},{isDefault},0,{isRecall}),"; } sql = sql.Remove(sql.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection.ExecuteScalarAsync(sql); return items > 0; } /// /// 修改 /// /// /// public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } public async Task UpdateAsync(Middle middle) { return await _repository.UpdateAsync(middle); } public async Task> FindAsync(Query query) { return await _repository.GetAllAsync(query); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } public async Task DeleteAsync(int middleId, int folderTypeId) { return await _repository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), middleId).Where(nameof(Entity.Middle.Middle.FolderType), folderTypeId)) > 0; } public async Task GetMiddleAsync(Query query) { return await _repository.GetAsync(query); } public async Task GetMiddleAsync(int id) { return await _repository.GetAsync(id); } /// /// 获取下级数据 /// /// /// public async Task> GetMiddleChildrenAsync(int id) { return await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), id)); } /// /// 设置排序 /// /// /// public async Task SetSortAsync(MiddleSortRequest request) { //获取第一个 var middleFirst = await _repository.GetAsync(request.MiddleIdFirst); //获取第二个数据 var middleSecond = await _repository.GetAsync(request.MiddleIdSecond); var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var lastModifiedDate = Convert.ToDateTime(middleSecond.LastModifiedDate).AddTicks(1); // if (middleFirst.Sort < middleSecond.Sort) // { // //修改第一个排序 // await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort) // .Where(nameof(Middle.Id), request.MiddleIdFirst)); // int sort = middleSecond.Sort; // var sql = // $"update tede_middle set Sort=Sort-1,LastModifiedDate='{lastModifiedDate}' where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort<={sort} and Id not in({middleFirst.Id}) and id>0"; // return await connection.ExecuteAsync(sql) > 0; // } // else // { // //修改第一个排序 // await _repository.UpdateAsync(Q.Set(nameof(Middle.Sort), middleSecond.Sort) // .Where(nameof(Middle.Id), request.MiddleIdFirst)); // int sort = middleSecond.Sort; // var sql = // $"update tede_middle set Sort=Sort+1,LastModifiedDate='{lastModifiedDate}' where UserId={middleFirst.UserId} and FolderType={middleFirst.FolderType} and Sort>={sort} and Id not in({middleFirst.Id}) and id>0"; // return await connection.ExecuteAsync(sql) > 0; // } var sql = $"update tede_middle set LastModifiedDate='{lastModifiedDate.ToString()}' where Id={middleFirst.Id}"; return await connection.ExecuteAsync(sql) > 0; } /// /// 设置置顶 /// /// /// public async Task SetTopAsync(int id) { var middle = await _repository.GetAsync(id); if (middle == null) throw new BusinessException("数据不存在"); var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var isTop = middle.IsTop == true ? 0 : 1; var sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{middle.CreatedDate.ToString()}' where id={id}"; if (middle.IsTop == false) { sql = $"update tede_middle set IsTop={isTop},LastModifiedDate='{DateTime.Now.ToString()}' where id={id}"; return await connection.ExecuteAsync(sql) > 0; } return await connection.ExecuteAsync(sql) > 0; } public async Task DeleteAsync(NoticeDeRequest request) { if (request.IsDelete) return await _repository.DeleteAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType)) > 0; return await _repository.UpdateAsync(Q.WhereIn(nameof(Middle.Id), request.MiddleIds).Where(nameof(Middle.FolderType), request.FolderType).Set(nameof(Middle.IsDelete), true)) > 0; } /// /// 恢复删除 /// /// /// public async Task RecoverDeleteAsync(NoticeDeRequest request) { return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsDelete), false) .WhereIn(nameof(Middle.Id), request.MiddleIds)) > 0; } /// /// 阅读 /// /// /// public async Task ReadAsync(int id) { return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsRead), true) .Where(nameof(Middle.Id), id)) > 0; } /// /// 修改文件夹名称 /// /// public async Task UpdateFolderNameAsync(MiddleFolderNameRequest request) { return await _repository.UpdateAsync(Q.Set(nameof(Middle.FolderName), request.FolderName) .Where(nameof(Middle.Id), request.Id)) > 0; } public async Task IsFolderAsync(int parentId) { return await _repository.CountAsync(Q.Where(nameof(Middle.ParentId), parentId).Where(nameof(Middle.IsDelete), false).Where(nameof(Middle.AttributeValue), 2)) > 0; } /// /// 根据用户ID文件夹ID查询小组数量 /// /// /// /// /// public async Task FindGroupCountAsync(int userId, int groupFolderId, int folderTypeId) { return await _repository.CountAsync(Q.Where(nameof(Middle.UserId), userId) .Where(nameof(Middle.Id), groupFolderId).Where(nameof(Middle.FolderType), folderTypeId)); } public async Task CountAsync(Query query) { return await _repository.CountAsync(query); } public async Task> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query); } public async Task GetSytemFolderAsync(int userId) { var query = Q.NewQuery(); query.Select(nameof(Entity.Middle.Middle.Id)); query.Where(nameof(Entity.Middle.Middle.UserId), userId); query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2); query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true); query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode()); return await _repository.GetAsync(query); } public async Task> FindAsync(InboxSearchRequest request) { var query = new SqlKata.Query(); if (request.Type > 0) { query.Where(nameof(Middle.FolderType), request.Type); } else { var listType = new List { GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Submitted.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Approval.GetHashCode(), GxPress.EnumConst.FolderTypeConst.Cc.GetHashCode() }; query.WhereIn(nameof(Middle.FolderType), listType); } query.Where(nameof(Middle.UserId), request.UserId); return await _repository.GetAllAsync(query.Select("Id", "Guid", "CreatedDate", "LastModifiedDate", "FolderName", "FolderType", "IsTop", "RoleId", "Sort", "MiddleId", "ParentId", "UserId", "MiddleSonId", "IsRead", "NoticeAddresseeType", "IsAdmin", "IsDelete", "AttributeValue", "IsDefault", "IsUpload", "Title", "AddressUser", "CcUser")); } /// /// 排除不是文件夹的ID /// /// /// public async Task> GetMiddleIsFolderAsync(IEnumerable ids) { var query = Q.NewQuery(); query.Select(nameof(Entity.Middle.Middle.Id)); query.WhereIn(nameof(Entity.Middle.Middle.Id), ids); query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2); return await _repository.GetAllAsync(query); } /// /// 递归获取父级下面的所有子集 /// /// /// /// public async Task> GetAllMiddleChildrenByParentIdAsync(List ids, List returnIds) { var middles = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), ids)); if (middles == null || middles.Count == 0) return returnIds; foreach (var item in middles) { middles = await _repository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id)); if (middles == null || middles.Count == 0) continue; returnIds.AddRange(middles.Select(n => n.Id).ToList()); foreach (var middle in middles) { await GetAllMiddleChildrenByParentIdAsync(new List { middle.Id }, returnIds); } } return returnIds; } /// /// 清空草稿箱 /// /// /// /// public async Task ClearDraftAsync(int userId, int typeValue) { //获取用户草稿 var sql = $"select Id from tede_note where UserId={userId} and IsDraft=1"; var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var noteIds = await connection.QueryAsync(sql); var query = Q.NewQuery(); query.Where(nameof(Entity.Middle.Middle.UserId), userId); query.WhereIn(nameof(Entity.Middle.Middle.MiddleId), noteIds); query.Where(nameof(Entity.Middle.Middle.FolderType), typeValue == 1 ? AllTypeConst.TopicNote.GetHashCode() : AllTypeConst.Note.GetHashCode()); query.Set(nameof(Entity.Middle.Middle.IsDelete), true); return await UpdateAsync(query); } } }