ManualHistoryService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Linq;
  3. using System.Transactions;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Entities;
  6. using FMSAdmin.Helpers;
  7. using Microsoft.Extensions.Logging;
  8. namespace FMSAdmin.Services {
  9. public class ManualHistoryService {
  10. private readonly ILogger<ManualHistoryService> _logger;
  11. private readonly FMSContext _context;
  12. private readonly StorageHelper _storage;
  13. public ManualHistoryService(
  14. ILogger<ManualHistoryService> logger,
  15. FMSContext context,
  16. StorageHelper storage
  17. ) {
  18. _logger = logger;
  19. _context = context;
  20. _storage = storage;
  21. }
  22. public void Edit(FmsManualHistory data) {
  23. using (var trans = new TransactionScope()) {
  24. if (data.CmFile != null && data.CmFile.IsUpload) {
  25. var category = _context.CmFileCategory.First(x => x.Name == "manual");
  26. data.CmFile.FileCategory = category;
  27. data.CmFile.CreatedDate = DateTime.Now;
  28. }
  29. var persist = _context.FmsManualHistory
  30. .Where(x => x.HistoryId == data.HistoryId && x.SiteId == data.SiteId).FirstOrDefault();
  31. if (persist == null) {
  32. throw new ServiceException("정보를 찾을 수 없습니다.");
  33. }
  34. persist.SiteId = data.SiteId;
  35. persist.ManualId = data.ManualId;
  36. persist.Description = data.Description;
  37. persist.UpdatedDate = DateTime.Now;
  38. persist.UpdatedUserId = data.UpdatedUserId;
  39. if (data.CmFile != null && data.CmFile.IsUpload) {
  40. persist.CmFile = data.CmFile;
  41. }
  42. _context.FmsManualHistory.Update(persist);
  43. _context.SaveChanges();
  44. if (data.CmFile != null && data.CmFile.IsUpload) {
  45. // 카피
  46. _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
  47. if (persist.CmFile != null) {
  48. // 파일 삭제시 진짜 DB삭제?
  49. }
  50. }
  51. trans.Complete();
  52. }
  53. }
  54. public void Delete(int siteId, int ManualId, int historyId) {
  55. var data = _context.FmsManualHistory.First(x => x.SiteId == siteId && x.ManualId == ManualId && x.HistoryId == historyId);
  56. _context.FmsManualHistory.Remove(data);
  57. _context.SaveChanges();
  58. }
  59. public IQueryable<FmsManualHistory> GetAll() {
  60. var query = _context.FmsManualHistory;
  61. return query;
  62. }
  63. public IQueryable<FmsManualHistory> Get(int siteId, int ManualId, int historyId) {
  64. var data = _context.FmsManualHistory.Where(x => x.SiteId == siteId && x.ManualId == ManualId && x.HistoryId == historyId);
  65. return data;
  66. }
  67. public int GetRevisionNo(int siteId, int ManualId) {
  68. var data = _context.FmsManualHistory.Where(x => x.SiteId == siteId && x.ManualId == ManualId).Max(x => x.RevisionNo);
  69. data += 1;
  70. return data.GetValueOrDefault();
  71. }
  72. public void Create(FmsManualHistory data) {
  73. using (var trans = new TransactionScope()) {
  74. if (data.CmFile != null && data.CmFile.IsUpload) {
  75. data.CmFile.SiteId = data.SiteId;
  76. var category = _context.CmFileCategory.First(x => x.Name == "manual");
  77. data.CmFile.FileCategory = category;
  78. data.CmFile.CreatedDate = DateTime.Now;
  79. } else {
  80. data.CmFile = null;
  81. }
  82. data.UpdatedDate = DateTime.Now;
  83. _context.FmsManualHistory.Add(data);
  84. _context.SaveChanges();
  85. if (data.CmFile != null && data.CmFile.IsUpload) {
  86. // 카피
  87. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  88. }
  89. trans.Complete();
  90. }
  91. }
  92. }
  93. }