using CH.CQRS.Command.Energy; using CH.CQRS.Service.Energy.Options; using CH.Dal; using CH.Dal.DbEntity; using CH.Enum; using Microsoft.EntityFrameworkCore; namespace CH.CQRS.Service.Energy; public class EnergyService(CHDbContext dbContext) { public async Task CreateEnergyProviderAsync(CreateEnergyProviderCommandOptions options, CancellationToken cancellationToken) { var energyProvider = new EnergyProvider { Name = options.Name, CreatedAt = DateTime.UtcNow, Active = options.Active }; await dbContext.AddAsync(energyProvider, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } public async Task CreateEnergyRateAsync(CreateEnergyRateCommandOptions options, CancellationToken cancellationToken) { var provider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken); var energyRate = new EnergyRate { Name = options.Name, Currency = (Currency)System.Enum.Parse(typeof(Currency), options.Currency?.ToUpper()), Price = options.Price, CreatedAt = DateTime.UtcNow, Provider = provider, Active = options.Active }; await dbContext.AddAsync(energyRate, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } public async Task CreateEnergyRateExceptionAsync(CreateEnergyRateExceptionCommandOptions options, CancellationToken cancellationToken) { var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(energyRate => energyRate.Id == options.RateId,cancellationToken); var energyRateException = new EnergyRateException { Name = options.Name, CreatedAt = DateTime.UtcNow, Rate = energyRate, StartedAt = options.StartedAt, EndedAt = options.EndedAt, EnergyThreshold = options.EnergyThreshold, ResetType = (EnergyRateExceptionThresholdResetType)System.Enum .Parse(typeof(EnergyRateExceptionThresholdResetType), options.ResetType.ToLower() ?? ""), }; await dbContext.AddAsync(energyRateException, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } public async Task UpdateEnergyProviderAsync(UpdateEnergyProviderCommandOptions options, CancellationToken cancellationToken) { var provider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken); if (null != provider) { provider.Name = options.Name; provider.UpdatedAt = DateTime.UtcNow; await dbContext.SaveChangesAsync(cancellationToken); } } public async Task UpdateEnergyRateAsync(UpdateEnergyRateCommandOptions options, CancellationToken cancellationToken) { var energyRateUpdate = new EnergyRateUpdate { RateId = options.RateId, Rate = options.Rate, StartedAt = options.StartedAt, CreatedAt = DateTime.UtcNow, UpdatedAt = options.UpdatedAt, AppliedAt = options.AppliedAt, SendAlert = options.SendAlert, }; await dbContext.AddAsync(energyRateUpdate, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } public async Task UpdateEnergyRateExceptionAsync(UpdateEnergyRateExceptionCommandOptions options, CancellationToken cancellationToken) { var energyRateException = await dbContext.EnergyRateExceptions .FirstOrDefaultAsync(rateException => rateException.Id == options.EnergyRateExceptionId, cancellationToken); if (null != energyRateException) { energyRateException.EnergyThreshold = options.EnergyThreshold; energyRateException.StartedAt = options.StartedAt; energyRateException.EndedAt = options.EndedAt; energyRateException.UpdatedAt = DateTime.UtcNow; if (String.IsNullOrWhiteSpace(options.ResetType)) { energyRateException.ResetType = (EnergyRateExceptionThresholdResetType)System.Enum.Parse(typeof(EnergyRateExceptionThresholdResetType),options.ResetType) ; } await dbContext.SaveChangesAsync(cancellationToken); } } public async Task DisableEnergyProviderAsync(DisableEnergyProviderCommandOptions options, CancellationToken cancellationToken) { var energyProvider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken); if (null != energyProvider) { energyProvider.Active = false; energyProvider.DisabledAt = options.DisabledAt; await dbContext.SaveChangesAsync(cancellationToken); } } public async Task DisableEnergyRateAsync(DisableEnergyRateCommandOptions options, CancellationToken cancellationToken) { var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(rate => rate.Id == options.RateId, cancellationToken); if (null != energyRate) { energyRate.Active = false; energyRate.DisabledAt = options.DisabledAt; await dbContext.SaveChangesAsync(cancellationToken); } } public async Task EnableEnergyProviderAsync(EnableEnergyProviderCommandOptions options, CancellationToken cancellationToken) { var energyProvider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken); if (null != energyProvider) { energyProvider.Active = true; energyProvider.DisabledAt = null; await dbContext.SaveChangesAsync(cancellationToken); } } public async Task EnableEnergyRateAsync(EnableEnergyRateCommandOptions options, CancellationToken cancellationToken) { var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(rate => rate.Id == options.RateId, cancellationToken); if (null != energyRate) { energyRate.Active = true; energyRate.DisabledAt = null; await dbContext.SaveChangesAsync(cancellationToken); } } }