using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using FMSAdmin.Data; using FMSAdmin.Helpers; using FMSAdmin.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using FMSAdmin.Models; using System.Transactions; namespace FMSApp.Services { public class AnnouncementService { private readonly ILogger _logger; private readonly FMSContext _context; private readonly StorageHelper _storage; private readonly AppSettings _appSettings; private readonly PushHelper _push; public AnnouncementService( ILogger logger, FMSContext context, IOptions appSettings, StorageHelper storage, PushHelper push) { _logger = logger; _context = context; _appSettings = appSettings.Value; _storage = storage; _push = push; } public void Save(CmAnnouncement data) { bool bCreate = false; using (var trans = new TransactionScope()) { var persist = _context.CmAnnouncement .Where(x => x.SiteId == data.SiteId && x.AnnouncementId == data.AnnouncementId).FirstOrDefault(); if (persist == null) { bCreate = true; var check = _context.CmAnnouncement.Where(x => x.SiteId == data.SiteId && x.AnnouncementId == data.AnnouncementId).Count(); if (check > 0) { throw new ServiceException("아이디가 중복됩니다."); } if (data.CmFile != null && data.CmFile.IsUpload) { data.CmFile.SiteId = data.SiteId; var category = _context.CmFileCategory.First(x => x.Name == "announcement"); data.CmFile.FileCategory = category; data.CmFile.CreatedDate = DateTime.Now; } else { data.CmFile = null; } _context.CmAnnouncement.Add(data); _context.SaveChanges(); if (data.CmFile != null && data.CmFile.IsUpload) { // 카피 _storage.CopyEntity(data.CmFile.Path, data.CmFile); } } else { if (data.CmFile != null && data.CmFile.IsUpload) { var category = _context.CmFileCategory.First(x => x.Name == "announcement"); persist.CmFile = new CmFile { SiteId = data.SiteId, FileCategory = category, CreatedDate = DateTime.Now, Name = data.CmFile.Name, FileSize = data.CmFile.FileSize, ContentType = data.CmFile.ContentType, }; _logger.LogInformation("upload file1"); } else if (data.CmFile != null && data.CmFile.IsDelete) { persist.FileId = null; persist.CmFile = null; _logger.LogInformation("delete file1"); } persist.Title = data.Title; persist.BusinessFieldId = data.BusinessFieldId; persist.Contents = data.Contents; persist.AddDate = data.AddDate; persist.UpdateDate = data.UpdateDate; persist.IsAllView = data.IsAllView; if (Util.S(persist.RegisterUserId) == "") { persist.RegisterUserId = data.RegisterUserId; } _context.CmAnnouncement.Update(persist); _context.SaveChanges(); if (data.CmFile != null && data.CmFile.IsUpload) { // 카피 _storage.CopyEntity(data.CmFile.Path, persist.CmFile); if (persist.CmFile != null) { // 파일 삭제시 진짜 DB삭제? } } } trans.Complete(); } if (bCreate) { if (data.IsAllView != null && data.IsAllView == true) { _push.SendToAllUsers("공지사항", data.Title, new { type = "announcement", id = data.AnnouncementId }); } else { _push.SendToSiteUsers(data.SiteId, "공지사항", data.Title, new { type = "announcement", id = data.AnnouncementId }); } } } public void AddCount(int id) { var announcement = _context.CmAnnouncement.FirstOrDefault(x => x.AnnouncementId == id); if (announcement != null) { announcement.ReadCount++; _context.CmAnnouncement.Update(announcement); _context.SaveChanges(); } } public void Delete(int siteId, int annId) { var data = _context.CmAnnouncement.First(x => x.SiteId == siteId && x.AnnouncementId == annId); _context.CmAnnouncement.Remove(data); _context.SaveChanges(); } public IQueryable GetAll() { var query = _context.CmAnnouncement; return query; } } }