FacilityTimeService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 FacilityTimeService {
  18. private readonly ILogger<FacilityTimeService> _logger;
  19. private readonly FMSContext _context;
  20. private readonly AppSettings _appSettings;
  21. private readonly StorageHelper _storage;
  22. public FacilityTimeService(
  23. ILogger<FacilityTimeService> 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(CmFacility data) {
  34. using (var trans = new TransactionScope()) {
  35. var persist = _context.CmFacility
  36. .Where(x => x.SiteId == data.SiteId
  37. && x.FacilityCode == data.FacilityCode).FirstOrDefault();
  38. if (persist == null) {
  39. var check = _context.CmFacility.Where(x => x.SiteId == data.SiteId
  40. && x.FacilityCode == data.FacilityCode).Count();
  41. if (check > 0) {
  42. throw new ServiceException("아이디가 중복됩니다.");
  43. }
  44. if (data.CmFile != null && data.CmFile.IsUpload) {
  45. data.CmFile.SiteId = data.SiteId;
  46. var category = _context.CmFileCategory.First(x => x.Name == "facility");
  47. data.CmFile.FileCategory = category;
  48. data.CmFile.CreatedDate = DateTime.Now;
  49. } else {
  50. data.CmFile = null;
  51. }
  52. _context.CmFacility.Add(data);
  53. _context.SaveChanges();
  54. if (data.CmFile != null && data.CmFile.IsUpload) {
  55. // 카피
  56. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  57. }
  58. } else {
  59. if (data.CmFile != null && data.CmFile.IsDelete) {
  60. persist.CmFile = null;
  61. } else if (data.CmFile != null && data.CmFile.IsUpload) {
  62. var category = _context.CmFileCategory.First(x => x.Name == "facility");
  63. persist.CmFile = new CmFile {
  64. SiteId = data.SiteId,
  65. FileCategory = category,
  66. CreatedDate = DateTime.Now,
  67. Name = data.CmFile.Name,
  68. FileSize = data.CmFile.FileSize,
  69. ContentType = data.CmFile.ContentType,
  70. };
  71. }
  72. persist.FacilityTypeId = data.FacilityTypeId;
  73. persist.DeviceType = data.DeviceType;
  74. persist.Name = data.Name;
  75. persist.Nickname = data.Nickname;
  76. persist.FacilityUsage = data.FacilityUsage;
  77. persist.IsUse = data.IsUse;
  78. persist.FirstClassId = data.FirstClassId;
  79. persist.SecondClassId = data.SecondClassId;
  80. persist.ThirdClassId = data.ThirdClassId;
  81. persist.FacilityCapacity = data.FacilityCapacity;
  82. persist.Rfid = data.Rfid;
  83. persist.RatedPowerConsumption = data.RatedPowerConsumption;
  84. persist.RatedCop = data.RatedCop;
  85. persist.Manufacturer = data.Manufacturer;
  86. persist.ManufactureDate = data.ManufactureDate;
  87. persist.ManufactureSerial = data.ManufactureSerial;
  88. persist.ManufactureModel = data.ManufactureModel;
  89. persist.Supplier = data.Supplier;
  90. persist.SupplierPhoneNo = data.SupplierPhoneNo;
  91. persist.BuildingId = data.BuildingId;
  92. persist.FloorId = data.FloorId;
  93. persist.ZoneId = data.ZoneId;
  94. persist.InstallDate = data.InstallDate;
  95. persist.OperationStartDate = data.OperationStartDate;
  96. persist.MainContactorUserId = data.MainContactorUserId;
  97. persist.SubContactorUserId = data.SubContactorUserId;
  98. persist.FacilityCost = data.FacilityCost;
  99. persist.FacilityCount = data.FacilityCount;
  100. persist.FuelTypeId = data.FuelTypeId;
  101. persist.ContractType = data.ContractType;
  102. persist.Note = data.Note;
  103. _context.CmFacility.Update(persist);
  104. _context.SaveChanges();
  105. if (data.CmFile != null && data.CmFile.IsUpload) {
  106. // 카피
  107. _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
  108. if (persist.CmFile != null) {
  109. // 파일 삭제시 진짜 DB삭제?
  110. }
  111. }
  112. }
  113. trans.Complete();
  114. }
  115. }
  116. public IQueryable<BemsMonitoringPointHistoryDaily> GetAll() {
  117. var query = _context.BemsMonitoringPointHistoryDaily;
  118. return query;
  119. }
  120. }
  121. }