ReportFormatService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 System.Transactions;
  8. using FMSAdmin.Data;
  9. using FMSAdmin.Helpers;
  10. using FMSAdmin.Entities;
  11. using Microsoft.EntityFrameworkCore;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using Microsoft.IdentityModel.Tokens;
  15. using FMSAdmin.Models;
  16. namespace FMSAdmin.Services {
  17. public class ReportFormatService {
  18. private readonly ILogger<ReportFormatService> _logger;
  19. private readonly FMSContext _context;
  20. public ReportFormatService(
  21. ILogger<ReportFormatService> logger,
  22. FMSContext context) {
  23. _logger = logger;
  24. _context = context;
  25. }
  26. public void Save(BemsReportFormat data) {
  27. using (var trans = new TransactionScope()) {
  28. var persist_1 = 0;
  29. if (data.ReportFormatId == 0) {
  30. var iCnt = _context.BemsReportFormat.Where(x => x.SiteId == data.SiteId).Count();
  31. if (iCnt > 0) persist_1 = _context.BemsReportFormat.Where(x => x.SiteId == data.SiteId).Max(x => x.ReportFormatId);
  32. else persist_1 = 0;
  33. data.ReportFormatId = persist_1 + 1;
  34. }
  35. var persist = _context.BemsReportFormat
  36. .Where(x => x.SiteId == data.SiteId && x.ReportFormatId == data.ReportFormatId).FirstOrDefault();
  37. if (persist == null) {
  38. // 설비 기본정보 생성되지 않도록 처리
  39. foreach (var facility in data.BemsReportFormatToFacility) {
  40. facility.CmFacility = null;
  41. }
  42. _context.BemsReportFormat.Add(data);
  43. _context.SaveChanges();
  44. } else {
  45. persist.BusinessFieldId = data.BusinessFieldId;
  46. persist.FormatName = data.FormatName;
  47. persist.BemsReportFormatToFacility.Clear();
  48. persist.BemsReportFormatContent.Clear();
  49. // 추가된 설비 생성
  50. foreach (var facility in data.BemsReportFormatToFacility) {
  51. persist.BemsReportFormatToFacility.Add(new BemsReportFormatToFacility() {
  52. SiteId = persist.SiteId,
  53. ReportFormatId = persist.ReportFormatId,
  54. FacilityCode = facility.FacilityCode,
  55. });
  56. }
  57. // 추가된 일지 생성
  58. foreach (var info in data.BemsReportFormatContent) {
  59. persist.BemsReportFormatContent.Add(new BemsReportFormatContent() {
  60. SiteId = persist.SiteId,
  61. ReportFormatId = persist.ReportFormatId,
  62. ContentId = info.ContentId,
  63. Content = info.Content,
  64. });
  65. }
  66. _context.BemsReportFormat.Update(persist);
  67. _context.SaveChanges();
  68. }
  69. trans.Complete();
  70. }
  71. }
  72. public IQueryable<BemsReportFormat> GetAll() {
  73. var query = _context.BemsReportFormat;//.AsNoTracking();
  74. return query;
  75. }
  76. public IQueryable<BemsReportFormat> Get(int reportFormatId, int siteId) {
  77. var data = _context.BemsReportFormat.Where(x => x.SiteId == siteId && x.ReportFormatId == reportFormatId);
  78. return data;
  79. }
  80. public void Delete(int reportFormatId, int siteId) {
  81. using (var trans = new TransactionScope()) {
  82. var data = _context.BemsReportFormat.First(x => x.ReportFormatId == reportFormatId && x.SiteId == siteId);
  83. var detail = _context.BemsReportFormatContent.Where(x => x.ReportFormatId == reportFormatId && x.SiteId == siteId);
  84. var facility = _context.BemsReportFormatToFacility.Where(x => x.ReportFormatId == reportFormatId);
  85. _context.BemsReportFormatToFacility.RemoveRange(facility);
  86. _context.BemsReportFormatContent.RemoveRange(detail);
  87. _context.BemsReportFormat.Remove(data);
  88. _context.SaveChanges();
  89. trans.Complete();
  90. }
  91. }
  92. }
  93. }