using Dapper; using SolarPower.Helper; using SolarPower.Models; using SolarPower.Models.PowerStation; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Repository.Implement { public class OverviewRepository : RepositoryBase, IOverviewRepository { public OverviewRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { tableName = "power_station"; } public async Task> GetAllCityIdAsync() { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT Id FROM city"; result = (await conn.QueryAsync(sql)).ToList(); } catch (Exception exception) { throw exception; } return result; } } public async Task GetOverviewByPowerStationIds(List powerStationIds) { Overview result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT SUM(ps.Today_kwh) AS Today_kwh, SUM(ps.Total_kwh) AS Total_kwh, AVG(ps.Today_irradiance) AS today_irradiance, AVG(ps.avg_irradiance) AS avg_irradiance, AVG(ps.today_PR) AS today_PR, AVG(ps.avg_PR) AS avg_PR, AVG(ps.today_kwhkwp) AS today_kwhkwp, AVG(ps.avg_kwhkwp) AS avg_kwhkwp, SUM(ps.today_carbon) AS today_carbon, SUM(ps.total_carbon) AS total_carbon FROM power_station ps WHERE ps.Id IN @PowerStationIds "; result = await conn.QueryFirstOrDefaultAsync(sql, new { PowerStationIds = powerStationIds }); } catch (Exception exception) { throw exception; } return result; } } public async Task> GetCapacityDataTableByPowerStationIds(List powerStationIds) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT ps.CityId, c.Name AS CityName, COUNT(*) AS SubPowerStationCount, SUM(ps.GeneratingCapacity) AS SubTotalCapacity FROM power_station ps LEFT JOIN city c ON ps.CityId = c.Id WHERE ps.Id IN @PowerStationIds GROUP BY ps.CityId "; result = (await conn.QueryAsync(sql, new { PowerStationIds = powerStationIds })).ToList(); } catch (Exception exception) { throw exception; } return result; } } public async Task> GetListPowerStationByPowerStationIds(List powerStationIds) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT * FROM power_station ps WHERE ps.Id IN @PowerStationIds "; result = (await conn.QueryAsync(sql, new { PowerStationIds = powerStationIds })).ToList(); } catch (Exception exception) { throw exception; } return result; } } public async Task> GetSolarByCity(MyUser User, UseStatusCityGetPowerStation post) { using IDbConnection conn = _databaseHelper.GetConnection(); List powerstation = new List(); conn.Open(); var trans = conn.BeginTransaction(); try { var ids = ""; foreach (var id in post.CityId) { ids = ids + id + ","; } if (ids.Length != 0) { ids = ids.Substring(0, ids.Length - 1); } if (User.Role.Layer == 0 || User.Role.Layer == 1) { var sql = "SELECT * FROM power_station WHERE CityId IN @IDs AND HealthStatus IN @Status"; powerstation = (await conn.QueryAsync(sql, new { IDs = post.CityId , Status = post.Status})).ToList(); trans.Commit(); } else if (User.Role.Layer == 2) { var sql = "SELECT * FROM power_station WHERE CityId IN @IDs AND CompanyId=@CompanyId AND HealthStatus IN @Status"; powerstation = (await conn.QueryAsync(sql, new { IDs = post.CityId, CompanyId = User.CompanyId , Status = post.Status })).ToList(); trans.Commit(); } else { var sql = "SELECT power_station.* FROM power_station LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE CityId IN @IDs AND Userid = @UserId AND HealthStatus IN @Status"; powerstation = (await conn.QueryAsync(sql, new { IDs = post.CityId, UserId = User.Id , Status = post.Status })).ToList(); trans.Commit(); } } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } return powerstation; } } }