Investigation3HistoryService.cs 4.4 KB

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