public IList <StorageFileRelation> GetFileRelations(Guid sourceId, string moduleKey, string sourceType = "") { using (var dbContext = new SystemSettingsDbContext()) { var sourceTypeIsNull = string.IsNullOrWhiteSpace(sourceType); return(dbContext.StorageFileRelations.AsExpandable().Where( r => r.SourceId == sourceId && r.ModuleKey.Equals(moduleKey, StringComparison.OrdinalIgnoreCase) && (sourceTypeIsNull || r.SourceType.Equals(sourceType, StringComparison.OrdinalIgnoreCase))).OrderBy(r => r.Sort) .ToList()); } }
public bool DisassociateFile(Guid sourceId, string moduleKey, string sourceType = "") { using (var dbContext = new SystemSettingsDbContext()) { var sourceTypeIsNull = string.IsNullOrWhiteSpace(sourceType); var entitys = dbContext.StorageFileRelations.Where(r => r.SourceId == sourceId && r.ModuleKey.Equals(moduleKey, StringComparison.OrdinalIgnoreCase) && (sourceTypeIsNull || r.SourceType.Equals(sourceType, StringComparison.OrdinalIgnoreCase))).ToList(); entitys.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); return(dbContext.SaveChanges() > 0); } }
public bool DeleteBySourceType(string sourceType) { if (string.IsNullOrWhiteSpace(sourceType)) { return(false); } using (var dbContext = new SystemSettingsDbContext()) { var entitys = dbContext.StorageFileRelations.Where(r => r.SourceType.Equals(sourceType, StringComparison.OrdinalIgnoreCase)).ToList(); entitys.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); return(dbContext.SaveChanges() > 0); } }
public bool SetFileSort(Guid sourceId, string moduleKey, Guid fileId, int sort, string sourceType = "") { if (fileId.Equals(Guid.Empty)) { return(false); } using (var dbContext = new SystemSettingsDbContext()) { var sourceTypeIsNull = string.IsNullOrWhiteSpace(sourceType); var entitys = dbContext.StorageFileRelations.Where(r => r.SourceId == sourceId && r.ModuleKey.Equals(moduleKey, StringComparison.OrdinalIgnoreCase) && r.FileId == fileId && (sourceTypeIsNull || r.SourceType.Equals(sourceType, StringComparison.OrdinalIgnoreCase))).ToList(); entitys.ForEach(m => m.Sort = sort); return(dbContext.SaveChanges() > 0); } }
public int DeleteFilesForTask(DateTime outTime) { var count = 0; List <StorageFile> files; using (var dbContext = new SystemSettingsDbContext()) { //取出超期未使用的私有文件,并删除 var query = from f in dbContext.StorageFiles where dbContext.StorageFileRelations.All(me => me.FileId != f.Id) && f.IsPublic == false && f.CreateTime < outTime select f; //sql语句写法 //var sql = "select f.* from bnt_system_files f where not EXISTS(select FileId from bnt_system_file_relations r where r.FileId = f.Id) and f.IsPublic=0"; //files = dbContext.Database.SqlQuery<StorageFile>(sql).ToList(); files = query.ToList(); if (files.Count > 0) { //循环删除本地文件 foreach (var file in files) { var filePath = AppDomain.CurrentDomain.BaseDirectory + "/" + file.RelativePath; if (File.Exists(filePath)) { File.Delete(filePath); } Logger.Operation($"删除文件:{file.FileName}", SystemSettingsModule.Instance); count++; } //一次性批量删除无效的file数据, 提升性能 var sqlDelete = @"delete f from bnt_system_files f where not EXISTS(select FileId from bnt_system_file_relations r where r.FileId = f.Id) and f.IsPublic=0 and f.CreateTime<'" + outTime.ToString("yyyy-MM-dd HH:mm:ss") + "'"; dbContext.Database.ExecuteSqlCommand(sqlDelete); } } return(count); }
public bool AssociateFile(Guid sourceId, string moduleKey, string moduleName, Guid fileId, string sourceType = "", int sort = 0) { if (fileId.Equals(Guid.Empty)) { return(false); } using (var dbContext = new SystemSettingsDbContext()) { dbContext.StorageFileRelations.Add(new StorageFileRelation { Id = KeyGenerator.GetGuidKey(), SourceId = sourceId, FileId = fileId, ModuleKey = moduleKey, ModuleName = moduleName, SourceType = sourceType, CreateTime = DateTime.Now, Sort = sort }); return(dbContext.SaveChanges() > 0); } }