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 System.Linq; 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 InsertAsync(List middles) { if (middles.Count == 0) return false; var sql = "INSERT INTO `ccpph`.`tede_middle`(`Guid`,`CreatedDate`,`LastModifiedDate`,`FolderName`,`FolderType`,`IsTop`,`RoleId`,`Sort`,`MiddleId`,`ParentId`,`UserId`,`MiddleSonId`,`IsRead`,`NoticeAddresseeType`,`IsAdmin`,`IsDelete`,`AttributeValue`,`IsDefault`,`IsUpload`)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; 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),"; } 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> 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 DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 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(); 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 ccpph.tede_middle set Sort=Sort-1 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 ccpph.tede_middle set Sort=Sort+1 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; } } /// /// 设置置顶 /// /// /// 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(); //return await _repository.UpdateAsync(Q.Set(nameof(Middle.IsTop), !middle.IsTop).Where(nameof(Middle.Id), id)) > 0; var isTop = middle.IsTop == true ? 0 : 1; var sql = $"update ccpph.tede_middle set IsTop={isTop} where id={id}"; 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); } } }