Investigation2Service.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.Extensions.Logging;
  2. using FMSAdmin.Data;
  3. using System.Linq;
  4. using FMSAdmin.Helpers;
  5. using FMSAdmin.Entities;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Transactions;
  9. using System.Collections.Generic;
  10. namespace FMSAdmin.Services {
  11. public class Investigation2Service {
  12. private readonly ILogger<Investigation2Service> _logger;
  13. private readonly FMSContext _context;
  14. private readonly StorageHelper _storage;
  15. public Investigation2Service(
  16. ILogger<Investigation2Service> logger,
  17. FMSContext context,
  18. StorageHelper storage) {
  19. _logger = logger;
  20. _context = context;
  21. _storage = storage;
  22. }
  23. public void Create(CmInvestigation2 data) {
  24. using (var trans = new TransactionScope()) {
  25. if (data.CmFile != null && data.CmFile.IsUpload) {
  26. var category = _context.CmFileCategory.First(x => x.Name == "investigation2");
  27. data.CmFile.FileCategory = category;
  28. data.CmFile.CreatedDate = DateTime.Now;
  29. data.CmFile.SiteId = data.SiteId;
  30. } else {
  31. data.CmFile = null;
  32. }
  33. data.CmInvestigation2History.Add(new CmInvestigation2History {
  34. SiteId = data.SiteId,
  35. RevisionNo = 0,
  36. Description = data.Description,
  37. CmFile = data.CmFile,
  38. UpdatedDate = DateTime.Now,
  39. UpdatedUserId = data.CreateUserId
  40. });
  41. _context.CmInvestigation2.Add(data);
  42. _context.SaveChanges();
  43. if (data.CmFile != null && data.CmFile.IsUpload) {
  44. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  45. }
  46. trans.Complete();
  47. }
  48. }
  49. public void Edit(int id, int siteId, CmInvestigation2 data) {
  50. using (var trans = new TransactionScope()) {
  51. var persist = _context.CmInvestigation2
  52. .Where(x => x.InvestigationId == id && x.SiteId == siteId).FirstOrDefault();
  53. if (persist == null) {
  54. throw new ServiceException("정보를 찾을 수 없습니다.");
  55. }
  56. persist.SiteId = data.SiteId;
  57. persist.InvestigationGroupId = data.InvestigationGroupId;
  58. persist.InvestigationTypeId = data.InvestigationTypeId;
  59. persist.InvestigationNo = data.InvestigationNo;
  60. persist.Name = data.Name;
  61. persist.Description = data.Description;
  62. _context.CmInvestigation2.Update(persist);
  63. _context.SaveChanges();
  64. trans.Complete();
  65. }
  66. }
  67. public void Delete(int id, int siteId) {
  68. var data = _context.CmInvestigation2.First(x => x.InvestigationId == id && x.SiteId == siteId);
  69. _context.CmInvestigation2.Remove(data);
  70. _context.SaveChanges();
  71. }
  72. public IQueryable<CmInvestigation2> GetAll() {
  73. var query = _context.CmInvestigation2;
  74. return query;
  75. }
  76. public IQueryable<CmInvestigation2> Get(int id, int siteId) {
  77. var data = _context.CmInvestigation2.Where(x => x.InvestigationId == id && x.SiteId == siteId);
  78. return data;
  79. }
  80. }
  81. }