using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; using System.Linq.Expressions; using System.Linq.Dynamic.Core; namespace YLShipBuildLandMap.Services.Common { public class FileService : IFileService { private YLShipBuildLandMapContext DbContext { get; set; } public FileService(YLShipBuildLandMapContext dbContext) { this.DbContext = dbContext; } public Task AddFile(List fileList) { foreach (var file in fileList) { DbContext.SysAttachment.Add(file); } return DbContext.SaveChangesAsync(); } private IQueryable GetFileQuery(Expression> exp, int? fileType) { var sql = from f in DbContext.SysAttachment.Where(exp).WhereIf(fileType.HasValue, f => f.Type == fileType) join disFileType in DbContext.SysDictionaryItem.Where(dis => dis.DictionaryCode == "FileType") on f.Type equals disFileType.Value into dbFileType from disFileType in dbFileType.DefaultIfEmpty() select new AttachmentView { AttachmentId = f.AttachmentId, Name = f.Name, Suffix = f.Suffix, Url = f.Url, ReferenceId = f.ReferenceId, Type = f.Type, FileTypeName = disFileType.Name, CreateTime = f.CreateTime, CreateUserId = f.CreateUserId }; return sql; } public Task> GetFileList(int pageIndex, int pageSize, Guid fileRefId, int? fileType) { var list = GetFileQuery(x => x.ReferenceId == fileRefId, fileType); //var list = from f in DbContext.SysAttachment.Where(f => f.ReferenceId == fileRefId).WhereIf(fileType.HasValue, f => f.Type == fileType) // join disFileType in DbContext.SysDictionaryItem.Where(dis => dis.DictionaryCode == "FileType") on f.Type equals disFileType.Value into dbFileType // from disFileType in dbFileType.DefaultIfEmpty() // select new AttachmentView // { // AttachmentId = f.AttachmentId, // Name = f.Name, // Suffix = f.Suffix, // Url = f.Url, // ReferenceId = f.ReferenceId, // Type = f.Type, // FileTypeName = disFileType.Name, // CreateTime = f.CreateTime, // CreateUserId = f.CreateUserId // }; return Task.FromResult(list.OrderBy(f => f.CreateTime).ToGridResultSet(pageIndex, pageSize)); } public Task> GetFileList(int pageIndex, int pageSize, int? fileType, List ids) { var list = GetFileQuery(x => ids.Contains(x.ReferenceId.Value), fileType); return Task.FromResult(list.OrderBy(f => f.CreateTime).ToGridResultSet(pageIndex, pageSize)); } public Task GetFileById(Guid fileId) { return Task.FromResult(DbContext.SysAttachment.Where(f => f.AttachmentId == fileId).FirstOrDefault()); } public Task DeleteFileById(Guid fileId) { return Task.FromResult(DbContext.SysAttachment.Where(f => f.AttachmentId == fileId).Delete()); } public Task DelfileByRefID(Guid noDeleteFileId, Guid fileRefId, int? fileType) { return Task.FromResult(DbContext.SysAttachment.Where(f => f.ReferenceId == fileRefId && f.Type == fileType && f.AttachmentId != noDeleteFileId).Delete()); } } }