123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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 FacilityTimeService {
- private readonly ILogger<FacilityTimeService> _logger;
- private readonly FMSContext _context;
- private readonly AppSettings _appSettings;
- private readonly StorageHelper _storage;
- public FacilityTimeService(
- ILogger<FacilityTimeService> logger,
- FMSContext context,
- IOptions<AppSettings> appSettings,
- StorageHelper storage
- ) {
- _logger = logger;
- _context = context;
- _appSettings = appSettings.Value;
- _storage = storage;
- }
- public void Save(CmFacility data) {
- using (var trans = new TransactionScope()) {
- var persist = _context.CmFacility
- .Where(x => x.SiteId == data.SiteId
- && x.FacilityCode == data.FacilityCode).FirstOrDefault();
- if (persist == null) {
- var check = _context.CmFacility.Where(x => x.SiteId == data.SiteId
- && x.FacilityCode == data.FacilityCode).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 == "facility");
- data.CmFile.FileCategory = category;
- data.CmFile.CreatedDate = DateTime.Now;
- } else {
- data.CmFile = null;
- }
- _context.CmFacility.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.IsDelete) {
- persist.CmFile = null;
- } else if (data.CmFile != null && data.CmFile.IsUpload) {
- var category = _context.CmFileCategory.First(x => x.Name == "facility");
- persist.CmFile = new CmFile {
- SiteId = data.SiteId,
- FileCategory = category,
- CreatedDate = DateTime.Now,
- Name = data.CmFile.Name,
- FileSize = data.CmFile.FileSize,
- ContentType = data.CmFile.ContentType,
- };
- }
- persist.FacilityTypeId = data.FacilityTypeId;
- persist.DeviceType = data.DeviceType;
- persist.Name = data.Name;
- persist.Nickname = data.Nickname;
- persist.FacilityUsage = data.FacilityUsage;
- persist.IsUse = data.IsUse;
- persist.FirstClassId = data.FirstClassId;
- persist.SecondClassId = data.SecondClassId;
- persist.ThirdClassId = data.ThirdClassId;
- persist.FacilityCapacity = data.FacilityCapacity;
- persist.Rfid = data.Rfid;
- persist.RatedPowerConsumption = data.RatedPowerConsumption;
- persist.RatedCop = data.RatedCop;
- persist.Manufacturer = data.Manufacturer;
- persist.ManufactureDate = data.ManufactureDate;
- persist.ManufactureSerial = data.ManufactureSerial;
- persist.ManufactureModel = data.ManufactureModel;
- persist.Supplier = data.Supplier;
- persist.SupplierPhoneNo = data.SupplierPhoneNo;
- persist.BuildingId = data.BuildingId;
- persist.FloorId = data.FloorId;
- persist.ZoneId = data.ZoneId;
- persist.InstallDate = data.InstallDate;
- persist.OperationStartDate = data.OperationStartDate;
- persist.MainContactorUserId = data.MainContactorUserId;
- persist.SubContactorUserId = data.SubContactorUserId;
- persist.FacilityCost = data.FacilityCost;
- persist.FacilityCount = data.FacilityCount;
- persist.FuelTypeId = data.FuelTypeId;
- persist.ContractType = data.ContractType;
- persist.Note = data.Note;
- _context.CmFacility.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 IQueryable<BemsMonitoringPointHistoryDaily> GetAll() {
- var query = _context.BemsMonitoringPointHistoryDaily;
- return query;
- }
- }
- }
|