123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Linq;
- using System.Transactions;
- using FMSAdmin.Data;
- using FMSAdmin.Entities;
- using FMSAdmin.Helpers;
- using Microsoft.Extensions.Logging;
- namespace FMSAdmin.Services {
- public class Investigation3HistoryService {
- private readonly ILogger<Investigation3HistoryService> _logger;
- private readonly FMSContext _context;
- private readonly StorageHelper _storage;
- public Investigation3HistoryService(
- ILogger<Investigation3HistoryService> logger,
- FMSContext context,
- StorageHelper storage
- ) {
- _logger = logger;
- _context = context;
- _storage = storage;
- }
- public void Edit(CmInvestigation3History data) {
- using (var trans = new TransactionScope()) {
- if (data.CmFile != null && data.CmFile.IsUpload) {
- var category = _context.CmFileCategory.First(x => x.Name == "investigation3");
- data.CmFile.FileCategory = category;
- data.CmFile.CreatedDate = DateTime.Now;
- data.CmFile.SiteId = data.SiteId;
- }
- var persist = _context.CmInvestigation3History
- .Where(x => x.HistoryId == data.HistoryId && x.SiteId == data.SiteId).FirstOrDefault();
- if (persist == null) {
- throw new ServiceException("정보를 찾을 수 없습니다.");
- }
- //persist.SiteId = data.SiteId;
- //persist.InvestigationId = data.InvestigationId;
- persist.Description = data.Description;
- persist.UpdatedDate = DateTime.Now;
- persist.UpdatedUserId = data.UpdatedUserId;
- if (data.CmFile != null && data.CmFile.IsUpload) {
- persist.CmFile = data.CmFile;
- persist.CmFile.SiteId = persist.SiteId;
- }
- _context.CmInvestigation3History.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();
- }
- }
- public void Delete(int siteId, int InvestigationId, int historyId) {
- var data = _context.CmInvestigation3History.First(x => x.SiteId == siteId && x.InvestigationId == InvestigationId && x.HistoryId == historyId);
- _context.CmInvestigation3History.Remove(data);
- _context.SaveChanges();
- }
- public IQueryable<CmInvestigation3History> GetAll() {
- var query = _context.CmInvestigation3History;
- return query;
- }
- public IQueryable<CmInvestigation3History> Get(int siteId, int InvestigationId, int historyId) {
- var data = _context.CmInvestigation3History.Where(x => x.SiteId == siteId && x.InvestigationId == InvestigationId && x.HistoryId == historyId);
- return data;
- }
- public int GetRevisionNo(int siteId, int InvestigationId) {
- var data = _context.CmInvestigation3History.Where(x => x.SiteId == siteId && x.InvestigationId == InvestigationId).Max(x => x.RevisionNo);
- data += 1;
- return data.GetValueOrDefault();
- }
- public void Create(CmInvestigation3History data) {
- using (var trans = new TransactionScope()) {
- if (data.CmFile != null && data.CmFile.IsUpload) {
- data.CmFile.SiteId = data.SiteId;
- var category = _context.CmFileCategory.First(x => x.Name == "investigation3");
- data.CmFile.FileCategory = category;
- data.CmFile.CreatedDate = DateTime.Now;
- } else {
- data.CmFile = null;
- }
- data.UpdatedDate = DateTime.Now;
- _context.CmInvestigation3History.Add(data);
- _context.SaveChanges();
- if (data.CmFile != null && data.CmFile.IsUpload) {
- // 카피
- _storage.CopyEntity(data.CmFile.Path, data.CmFile);
- }
- trans.Complete();
- }
- }
- }
- }
|