FileService.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Bowin.Common.Linq.Entity;
  2. using YLShipBuildLandMap.Entity;
  3. using YLShipBuildLandMap.Entity.ViewModel;
  4. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Z.EntityFramework.Plus;
  11. using System.Linq.Expressions;
  12. using System.Linq.Dynamic.Core;
  13. namespace YLShipBuildLandMap.Services.Common
  14. {
  15. public class FileService : IFileService
  16. {
  17. private YLShipBuildLandMapContext DbContext { get; set; }
  18. public FileService(YLShipBuildLandMapContext dbContext)
  19. {
  20. this.DbContext = dbContext;
  21. }
  22. public Task<int> AddFile(List<SysAttachment> fileList)
  23. {
  24. foreach (var file in fileList)
  25. {
  26. DbContext.SysAttachment.Add(file);
  27. }
  28. return DbContext.SaveChangesAsync();
  29. }
  30. private IQueryable<AttachmentView> GetFileQuery(Expression<Func<SysAttachment, bool>> exp, int? fileType)
  31. {
  32. var sql = from f in DbContext.SysAttachment.Where(exp).WhereIf(fileType.HasValue, f => f.Type == fileType)
  33. join disFileType in DbContext.SysDictionaryItem.Where(dis => dis.DictionaryCode == "FileType") on f.Type equals disFileType.Value into dbFileType
  34. from disFileType in dbFileType.DefaultIfEmpty()
  35. select new AttachmentView
  36. {
  37. AttachmentId = f.AttachmentId,
  38. Name = f.Name,
  39. Suffix = f.Suffix,
  40. Url = f.Url,
  41. ReferenceId = f.ReferenceId,
  42. Type = f.Type,
  43. FileTypeName = disFileType.Name,
  44. CreateTime = f.CreateTime,
  45. CreateUserId = f.CreateUserId
  46. };
  47. return sql;
  48. }
  49. public Task<IGridResultSet<AttachmentView>> GetFileList(int pageIndex, int pageSize, Guid fileRefId, int? fileType)
  50. {
  51. var list = GetFileQuery(x => x.ReferenceId == fileRefId, fileType);
  52. //var list = from f in DbContext.SysAttachment.Where(f => f.ReferenceId == fileRefId).WhereIf(fileType.HasValue, f => f.Type == fileType)
  53. // join disFileType in DbContext.SysDictionaryItem.Where(dis => dis.DictionaryCode == "FileType") on f.Type equals disFileType.Value into dbFileType
  54. // from disFileType in dbFileType.DefaultIfEmpty()
  55. // select new AttachmentView
  56. // {
  57. // AttachmentId = f.AttachmentId,
  58. // Name = f.Name,
  59. // Suffix = f.Suffix,
  60. // Url = f.Url,
  61. // ReferenceId = f.ReferenceId,
  62. // Type = f.Type,
  63. // FileTypeName = disFileType.Name,
  64. // CreateTime = f.CreateTime,
  65. // CreateUserId = f.CreateUserId
  66. // };
  67. return Task.FromResult(list.OrderBy(f => f.CreateTime).ToGridResultSet(pageIndex, pageSize));
  68. }
  69. public Task<IGridResultSet<AttachmentView>> GetFileList(int pageIndex, int pageSize, int? fileType, List<Guid> ids)
  70. {
  71. var list = GetFileQuery(x => ids.Contains(x.ReferenceId.Value), fileType);
  72. return Task.FromResult(list.OrderBy(f => f.CreateTime).ToGridResultSet(pageIndex, pageSize));
  73. }
  74. public Task<SysAttachment> GetFileById(Guid fileId)
  75. {
  76. return Task.FromResult(DbContext.SysAttachment.Where(f => f.AttachmentId == fileId).FirstOrDefault());
  77. }
  78. public Task<int> DeleteFileById(Guid fileId)
  79. {
  80. return Task.FromResult(DbContext.SysAttachment.Where(f => f.AttachmentId == fileId).Delete());
  81. }
  82. public Task<int> DelfileByRefID(Guid noDeleteFileId, Guid fileRefId, int? fileType)
  83. {
  84. return Task.FromResult(DbContext.SysAttachment.Where(f => f.ReferenceId == fileRefId && f.Type == fileType && f.AttachmentId != noDeleteFileId).Delete());
  85. }
  86. }
  87. }