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

70 lines
2.7 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<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;
}
}
}