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 = options.Currency, 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 = options.ResetType, }; await dbContext.AddAsync(energyRateException, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } public async Task UpdateEnergyProviderAsync(UpdateEnergyProviderCommandOptions options, CancellationToken cancellationToken) { var provider = await dbContext.EnergyProviders .AsTracking() .FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken); if (provider is null) return; provider.Name = options.Name; provider.UpdatedAt = DateTime.UtcNow; await dbContext.SaveChangesAsync(cancellationToken); } public async Task UpdateEnergyRateValueAsync(UpdateEnergyRateCommandOptions options, CancellationToken cancellationToken) { var energyRate = await dbContext.EnergyRates .AsTracking() .FirstOrDefaultAsync(energyRate => energyRate.Id == options.RateId, cancellationToken); if (energyRate is null) return; if (options.Rate.HasValue) energyRate.Price = options.Rate.Value; if (false == string.IsNullOrWhiteSpace(options.Name)) energyRate.Name = options.Name; energyRate.UpdatedAt = DateTime.UtcNow; } public async Task UpdateEnergyRateAsync(UpdateEnergyRateCommandOptions options, CancellationToken cancellationToken) { if (options.StartedAt.HasValue) { var energyRateUpdate = new EnergyRateUpdate { RateId = options.RateId, Rate = options.Rate, Name = options.Name ?? "", StartedAt = options.StartedAt.Value, }; await dbContext.AddAsync(energyRateUpdate, cancellationToken); } else { await UpdateEnergyRateValueAsync(options, cancellationToken); } await dbContext.SaveChangesAsync(cancellationToken); } public async Task UpdateEnergyRateExceptionAsync(UpdateEnergyRateExceptionCommandOptions options, CancellationToken cancellationToken) { var energyRateException = await dbContext.EnergyRateExceptions .AsTracking() .FirstOrDefaultAsync(rateException => rateException.Id == options.EnergyRateExceptionId, cancellationToken); if (energyRateException is null) return; if (options.EnergyThreshold.HasValue) energyRateException.EnergyThreshold = options.EnergyThreshold; if (options.StartedAt.HasValue) energyRateException.StartedAt = options.StartedAt; if (options.EndedAt.HasValue) energyRateException.EndedAt = options.EndedAt; energyRateException.UpdatedAt = DateTime.UtcNow; if (energyRateException.ResetType != options.ResetType) { energyRateException.ResetType = 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 ?? DateTime.UtcNow; 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 ?? DateTime.UtcNow; 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); } } }