Investigation2HistoryService.cs 4.3 KB

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