ManualHistoryService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Microsoft.IdentityModel.Tokens;
  14. using FMSAdmin.Models;
  15. using System.Transactions;
  16. namespace FMSApp.Services {
  17. public class ManualHistoryService {
  18. private readonly ILogger<ManualHistoryService> _logger;
  19. private readonly FMSContext _context;
  20. private readonly AppSettings _appSettings;
  21. private readonly StorageHelper _storage;
  22. public ManualHistoryService(
  23. ILogger<ManualHistoryService> logger,
  24. FMSContext context,
  25. IOptions<AppSettings> appSettings,
  26. StorageHelper storage
  27. ) {
  28. _logger = logger;
  29. _context = context;
  30. _appSettings = appSettings.Value;
  31. _storage = storage;
  32. }
  33. public void Save(FmsManualHistory data) {
  34. using (var trans = new TransactionScope()) {
  35. var persist = _context.FmsManualHistory
  36. .Where(x => x.SiteId == data.SiteId
  37. && x.ManualId == data.ManualId
  38. && x.HistoryId == data.HistoryId).FirstOrDefault();
  39. if (persist == null) {
  40. var check = _context.FmsManualHistory.Where(x => x.SiteId == data.SiteId
  41. && x.ManualId == data.ManualId
  42. && x.HistoryId == data.HistoryId).Count();
  43. if (check > 0) {
  44. throw new ServiceException("아이디가 중복됩니다.");
  45. }
  46. if (data.CmFile != null && data.CmFile.IsUpload) {
  47. data.CmFile.SiteId = data.SiteId;
  48. var category = _context.CmFileCategory.First(x => x.Name == "manual");
  49. data.CmFile.FileCategory = category;
  50. data.CmFile.CreatedDate = DateTime.Now;
  51. } else {
  52. data.CmFile = null;
  53. }
  54. data.UpdatedDate = DateTime.Now;
  55. _context.FmsManualHistory.Add(data);
  56. _context.SaveChanges();
  57. if (data.CmFile != null && data.CmFile.IsUpload) {
  58. // 카피
  59. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  60. }
  61. } else {
  62. if (data.CmFile != null && data.CmFile.IsUpload) {
  63. var category = _context.CmFileCategory.First(x => x.Name == "manual");
  64. persist.CmFile = new CmFile {
  65. SiteId = data.SiteId,
  66. FileCategory = category,
  67. CreatedDate = DateTime.Now,
  68. Name = data.CmFile.Name,
  69. FileSize = data.CmFile.FileSize,
  70. ContentType = data.CmFile.ContentType,
  71. };
  72. _logger.LogInformation("upload file");
  73. } else if (data.CmFile != null && data.CmFile.IsDelete) {
  74. persist.FileId = null;
  75. persist.CmFile = null;
  76. _logger.LogInformation("delete file");
  77. }
  78. persist.UpdatedUserId = data.UpdatedUserId;
  79. persist.UpdatedDate = DateTime.Now;
  80. persist.RevisionNo = data.RevisionNo;
  81. persist.Description = data.Description;
  82. _context.FmsManualHistory.Update(persist);
  83. _context.SaveChanges();
  84. if (data.CmFile != null && data.CmFile.IsUpload) {
  85. // 카피
  86. _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
  87. }
  88. }
  89. trans.Complete();
  90. }
  91. }
  92. public void Delete(int siteId, int manualId, int historyId) {
  93. var data = _context.FmsManualHistory.First(x => x.SiteId == siteId && x.ManualId == manualId && x.HistoryId == historyId);
  94. _context.FmsManualHistory.Remove(data);
  95. _context.SaveChanges();
  96. }
  97. public IQueryable<FmsManualHistory> GetAll() {
  98. var query = _context.FmsManualHistory;
  99. return query;
  100. }
  101. }
  102. }