123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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<AnnouncementService> _logger;
- private readonly FMSContext _context;
- private readonly StorageHelper _storage;
- private readonly AppSettings _appSettings;
- private readonly PushHelper _push;
- public AnnouncementService(
- ILogger<AnnouncementService> logger,
- FMSContext context,
- IOptions<AppSettings> 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<CmAnnouncement> GetAll() {
- var query = _context.CmAnnouncement;
- return query;
- }
- }
- }
|