AnnouncementService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 AnnouncementService {
  18. private readonly ILogger<AnnouncementService> _logger;
  19. private readonly FMSContext _context;
  20. private readonly StorageHelper _storage;
  21. private readonly AppSettings _appSettings;
  22. private readonly PushHelper _push;
  23. public AnnouncementService(
  24. ILogger<AnnouncementService> logger,
  25. FMSContext context,
  26. IOptions<AppSettings> appSettings,
  27. StorageHelper storage,
  28. PushHelper push) {
  29. _logger = logger;
  30. _context = context;
  31. _appSettings = appSettings.Value;
  32. _storage = storage;
  33. _push = push;
  34. }
  35. public void Save(CmAnnouncement data) {
  36. bool bCreate = false;
  37. using (var trans = new TransactionScope()) {
  38. var persist = _context.CmAnnouncement
  39. .Where(x => x.SiteId == data.SiteId
  40. && x.AnnouncementId == data.AnnouncementId).FirstOrDefault();
  41. if (persist == null) {
  42. bCreate = true;
  43. var check = _context.CmAnnouncement.Where(x => x.SiteId == data.SiteId
  44. && x.AnnouncementId == data.AnnouncementId).Count();
  45. if (check > 0) {
  46. throw new ServiceException("아이디가 중복됩니다.");
  47. }
  48. if (data.CmFile != null && data.CmFile.IsUpload) {
  49. data.CmFile.SiteId = data.SiteId;
  50. var category = _context.CmFileCategory.First(x => x.Name == "announcement");
  51. data.CmFile.FileCategory = category;
  52. data.CmFile.CreatedDate = DateTime.Now;
  53. } else {
  54. data.CmFile = null;
  55. }
  56. _context.CmAnnouncement.Add(data);
  57. _context.SaveChanges();
  58. if (data.CmFile != null && data.CmFile.IsUpload) {
  59. // 카피
  60. _storage.CopyEntity(data.CmFile.Path, data.CmFile);
  61. }
  62. } else {
  63. if (data.CmFile != null && data.CmFile.IsUpload) {
  64. var category = _context.CmFileCategory.First(x => x.Name == "announcement");
  65. persist.CmFile = new CmFile {
  66. SiteId = data.SiteId,
  67. FileCategory = category,
  68. CreatedDate = DateTime.Now,
  69. Name = data.CmFile.Name,
  70. FileSize = data.CmFile.FileSize,
  71. ContentType = data.CmFile.ContentType,
  72. };
  73. _logger.LogInformation("upload file1");
  74. } else if (data.CmFile != null && data.CmFile.IsDelete) {
  75. persist.FileId = null;
  76. persist.CmFile = null;
  77. _logger.LogInformation("delete file1");
  78. }
  79. persist.Title = data.Title;
  80. persist.BusinessFieldId = data.BusinessFieldId;
  81. persist.Contents = data.Contents;
  82. persist.AddDate = data.AddDate;
  83. persist.UpdateDate = data.UpdateDate;
  84. persist.IsAllView = data.IsAllView;
  85. if (Util.S(persist.RegisterUserId) == "") {
  86. persist.RegisterUserId = data.RegisterUserId;
  87. }
  88. _context.CmAnnouncement.Update(persist);
  89. _context.SaveChanges();
  90. if (data.CmFile != null && data.CmFile.IsUpload) {
  91. // 카피
  92. _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
  93. if (persist.CmFile != null) {
  94. // 파일 삭제시 진짜 DB삭제?
  95. }
  96. }
  97. }
  98. trans.Complete();
  99. }
  100. if (bCreate) {
  101. if (data.IsAllView != null && data.IsAllView == true) {
  102. _push.SendToAllUsers("공지사항", data.Title, new {
  103. type = "announcement",
  104. id = data.AnnouncementId
  105. });
  106. } else {
  107. _push.SendToSiteUsers(data.SiteId, "공지사항", data.Title, new {
  108. type = "announcement",
  109. id = data.AnnouncementId
  110. });
  111. }
  112. }
  113. }
  114. public void AddCount(int id) {
  115. var announcement = _context.CmAnnouncement.FirstOrDefault(x => x.AnnouncementId == id);
  116. if (announcement != null) {
  117. announcement.ReadCount++;
  118. _context.CmAnnouncement.Update(announcement);
  119. _context.SaveChanges();
  120. }
  121. }
  122. public void Delete(int siteId, int annId) {
  123. var data = _context.CmAnnouncement.First(x => x.SiteId == siteId && x.AnnouncementId == annId);
  124. _context.CmAnnouncement.Remove(data);
  125. _context.SaveChanges();
  126. }
  127. public IQueryable<CmAnnouncement> GetAll() {
  128. var query = _context.CmAnnouncement;
  129. return query;
  130. }
  131. }
  132. }