DrawingService.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. namespace FMSAdmin.Services {
  10. public class DrawingService {
  11. private readonly ILogger<DrawingService> _logger;
  12. private readonly FMSContext _context;
  13. private readonly StorageHelper _storage;
  14. public DrawingService(
  15. ILogger<DrawingService> logger,
  16. FMSContext context,
  17. StorageHelper storage) {
  18. _logger = logger;
  19. _context = context;
  20. _storage = storage;
  21. }
  22. public void Create(FmsDrawing data) {
  23. using (var trans = new TransactionScope()) {
  24. if (data.CmFile != null && data.CmFile.IsUpload) {
  25. var category = _context.CmFileCategory.First(x => x.Name == "drawing");
  26. data.CmFile.FileCategory = category;
  27. data.CmFile.CreatedDate = DateTime.Now;
  28. data.CmFile.SiteId = data.SiteId;
  29. } else {
  30. data.CmFile = null;
  31. }
  32. data.FmsDrawingHistory.Add(new FmsDrawingHistory {
  33. SiteId = data.SiteId,
  34. RevisionNo = 0,
  35. Description = data.Description,
  36. CmFile = data.CmFile,
  37. UpdatedDate = DateTime.Now,
  38. UpdatedUserId = data.CreateUserId
  39. });
  40. _context.FmsDrawing.Add(data);
  41. _context.SaveChanges();
  42. if (data.CmFile != null && data.CmFile.IsUpload) {
  43. // 카피
  44. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  45. }
  46. trans.Complete();
  47. }
  48. }
  49. public void Edit(int id, int siteId, FmsDrawing data) {
  50. using (var trans = new TransactionScope()) {
  51. var persist = _context.FmsDrawing
  52. .Where(x => x.DrawingId == id && x.SiteId == siteId).FirstOrDefault();
  53. if (persist == null) {
  54. throw new ServiceException("정보를 찾을 수 없습니다.");
  55. }
  56. persist.SiteId = data.SiteId;
  57. persist.DrawingGroupId = data.DrawingGroupId;
  58. persist.DrawingTypeId = data.DrawingTypeId;
  59. persist.DrawingNo = data.DrawingNo;
  60. persist.Name = data.Name;
  61. persist.Description = data.Description;
  62. _context.FmsDrawing.Update(persist);
  63. _context.SaveChanges();
  64. trans.Complete();
  65. }
  66. }
  67. public void Delete(int id, int siteId) {
  68. var data = _context.FmsDrawing.First(x => x.DrawingId == id && x.SiteId == siteId);
  69. _context.FmsDrawing.Remove(data);
  70. _context.SaveChanges();
  71. }
  72. public IQueryable<FmsDrawing> GetAll() {
  73. var query = _context.FmsDrawing;
  74. return query;
  75. }
  76. public IQueryable<FmsDrawing> Get(int id, int siteId) {
  77. var data = _context.FmsDrawing.Where(x => x.DrawingId == id && x.SiteId == siteId);
  78. return data;
  79. }
  80. }
  81. }