FIC_Solar/SolarPower/Repository/Implement/OverviewRepository.cs
2021-07-06 20:25:51 +08:00

170 lines
6.6 KiB
C#

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<Overview>, IOverviewRepository
{
public OverviewRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
tableName = "power_station";
}
public async Task<List<int>> GetAllCityIdAsync()
{
List<int> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $"SELECT Id FROM city";
result = (await conn.QueryAsync<int>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task<Overview> GetOverviewByPowerStationIds(List<int> 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<Overview>(sql, new { PowerStationIds = powerStationIds });
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task<List<CapacityDataTable>> GetCapacityDataTableByPowerStationIds(List<int> powerStationIds)
{
List<CapacityDataTable> 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<CapacityDataTable>(sql, new { PowerStationIds = powerStationIds })).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task<List<PowerStation>> GetListPowerStationByPowerStationIds(List<int> powerStationIds)
{
List<PowerStation> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = @$"SELECT * FROM power_station ps WHERE ps.Id IN @PowerStationIds
";
result = (await conn.QueryAsync<PowerStation>(sql, new { PowerStationIds = powerStationIds })).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task<List<PowerStation>> GetSolarByCity(MyUser User, UseStatusCityGetPowerStation post)
{
using IDbConnection conn = _databaseHelper.GetConnection();
List<PowerStation> powerstation = new List<PowerStation>();
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<PowerStation>(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<PowerStation>(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<PowerStation>(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;
}
}
}