854 lines
29 KiB
C#
854 lines
29 KiB
C#
using Dapper;
|
|
using SolarPower.Helper;
|
|
using SolarPower.Models.PowerStation;
|
|
using SolarPower.Models.User;
|
|
using SolarPower.Repository.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Text.Json;
|
|
using SolarPower.Models;
|
|
|
|
namespace SolarPower.Repository.Implement
|
|
{
|
|
public class PowerStationRepository : RepositoryBase<PowerStation>, IPowerStationRepository
|
|
{
|
|
public PowerStationRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
|
|
{
|
|
tableName = "power_station";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢縣市列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<CitySelectItemList>> GetCitySelectOptionListAsync()
|
|
{
|
|
List<CitySelectItemList> result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = $"SELECT Id AS Value, Name AS Text FROM city";
|
|
|
|
result = (await conn.QueryAsync<CitySelectItemList>(sql)).ToList();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢縣市列表
|
|
/// </summary>
|
|
/// <param name="cityId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<AreaSelectItemList>> GetAreaSelectOptionListAsync(int cityId)
|
|
{
|
|
List<AreaSelectItemList> result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = $"SELECT Id AS Value, Name AS Text FROM Area WHERE CityId = @CityId";
|
|
|
|
result = (await conn.QueryAsync<AreaSelectItemList>(sql, new { CityId = cityId })).ToList();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 透過編號取得,縣市資訊
|
|
/// </summary>
|
|
/// <param name="cityId"></param>
|
|
/// <returns></returns>
|
|
public async Task<City> GetOneCityByIdAsync(int cityId)
|
|
{
|
|
City result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = @$"SELECT * FROM city WHERE Id=@Id";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<City>(sql, new { Id = cityId });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 透過編號取得,地區資訊
|
|
/// </summary>
|
|
/// <param name="areaId"></param>
|
|
/// <returns></returns>
|
|
public async Task<Area> GetOneAreaByIdAsync(int areaId)
|
|
{
|
|
Area result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = @$"SELECT * FROM area WHERE Id=@Id";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<Area>(sql, new { Id = areaId });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得縣市地區代碼
|
|
/// </summary>
|
|
/// <param name="areaId"></param>
|
|
/// <returns></returns>
|
|
public async Task<Zipcode> GetCityAreaZipcodeAsync(int areaId)
|
|
{
|
|
Zipcode result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = @$"SELECT a.ZipCode AS Area, c.ZipCode AS City FROM area a
|
|
LEFT JOIN city c ON a.cityId = c.Id
|
|
WHERE a.Id = @AreaId";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<Zipcode>(sql, new { AreaId = areaId });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 透過縣市地區編號,取得該縣市地區最後的流水號
|
|
/// </summary>
|
|
/// <param name="cityId"></param>
|
|
/// <param name="areaId"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetLastSerialNumberByCityAreaIdAsync(int cityId, int areaId)
|
|
{
|
|
string result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
try
|
|
{
|
|
var sql = @$"SELECT SerialNumber FROM {tableName}
|
|
WHERE CityId = @CityId && AreaId = @AreaId ORDER BY SerialNumber DESC";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<string>(sql, new { CityId = cityId, AreaId = areaId });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 透過電站編號,取得單一電站資訊(覆寫)
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public override async Task<PowerStation> GetOneAsync(int id)
|
|
{
|
|
//base.GetOneAsync(id);
|
|
|
|
PowerStation result;
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
try
|
|
{
|
|
var sql = @$"SELECT ps.*, u.Name AS CreatorName FROM {tableName} ps
|
|
LEFT JOIN user u ON ps.CreatedBy = u.Id
|
|
WHERE ps.Deleted = 0 AND ps.Id = @Id";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<PowerStation>(sql, new { Id = id});
|
|
|
|
if(result!= null)
|
|
{
|
|
var sql_land_building = @$"SELECT lb.*, u.Name AS CreatorName FROM land_building lb
|
|
LEFT JOIN user u ON lb.CreatedBy = u.Id
|
|
WHERE lb.Deleted = 0 AND PowerStationId = @PowerStationId";
|
|
result.LandBuildings = (await conn.QueryAsync<LandBuilding>(sql_land_building, new { PowerStationId = result.Id })).ToList();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改電站基本資訊
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdatePowerStationInfo(UpdatePowerStationInfo entity, List<string> properties)
|
|
{
|
|
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
using (var trans = conn.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var sql = GenerateUpdateQuery(properties);
|
|
|
|
await conn.ExecuteAsync(sql, entity, trans);
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改能源局與台電資訊
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateBoETPCInfo(UpdateBoETPCInfo entity, List<string> properties)
|
|
{
|
|
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
using (var trans = conn.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var sql = GenerateUpdateQuery(properties);
|
|
|
|
await conn.ExecuteAsync(sql, entity, trans);
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 土地房屋資訊
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<LandBuilding> GetOneLandBuildingInfo(int id)
|
|
{
|
|
LandBuilding result;
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
try
|
|
{
|
|
var sql = @"SELECT * FROM land_building WHERE Deleted =0 AND Id = @Id";
|
|
|
|
result = await conn.QueryFirstOrDefaultAsync<LandBuilding>(sql, new { Id = id });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 土地房屋資訊
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task<int> AddOneLandBuildingInfo(LandBuilding entity, List<string> properties)
|
|
{
|
|
int id;
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = GenerateInsertQueryWithCustomTable(properties, "land_building");
|
|
|
|
sql += "SELECT LAST_INSERT_ID();";
|
|
|
|
id = (await conn.QueryAsync<int>(sql, entity)).Single();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
return id;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新 土地房屋資訊
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateLandBuildingInfo(UpdateLandBuilding entity, List<string> properties)
|
|
{
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
using (var trans = conn.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var sql = GenerateUpdateQueryWithCustomTable(properties, "land_building");
|
|
|
|
await conn.ExecuteAsync(sql, entity, trans);
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 軟刪除土地房屋資訊
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task DeleteOneLandBuildingInfo(int id)
|
|
{
|
|
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
|
{
|
|
conn.Open();
|
|
using (var trans = conn.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var sql = $"UPDATE land_building SET deleted = 1 WHERE id = @Id";
|
|
|
|
await conn.ExecuteAsync(sql, new { Id = id }, trans);
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增運維
|
|
/// </summary>
|
|
/// <param name="operation"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task<int> AddOperation(OperationInfo operation, List<string> properties)
|
|
{
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
int count;
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = GenerateInsertQueryWithCustomTable(properties, "operation_firm");
|
|
|
|
count = await conn.ExecuteAsync(sql, operation);
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
return count;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 運維DataTable
|
|
/// </summary>
|
|
/// <param name="stationId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<OperationTable>> OperationTable (int stationId)
|
|
{
|
|
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
List<OperationTable> operation = new List<OperationTable>();
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = @$"SELECT operation_firm.Name,
|
|
operation_firm.PowerStationId,
|
|
operation_firm.Id,operation_firm.ContactPerson,operation_firm.Phone,operation_firm.Email,user.Name AS CreatedName,operation_firm.CreatedAt,operation_firm.Type
|
|
FROM operation_firm LEFT JOIN user ON operation_firm.CreatedBy = user.id WHERE operation_firm.Deleted = 0 AND operation_firm.PowerStationId = @StationId";
|
|
operation = (await conn.QueryAsync<OperationTable>(sql, new { StationId = stationId })).ToList();
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return operation;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 選取單一運維
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<OperationInfo> OneOperationInfo (int id)
|
|
{
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
OperationInfo operation;
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = @$"SELECT * FROM operation_firm WHERE Id = @Id";
|
|
operation = await conn.QueryFirstOrDefaultAsync<OperationInfo>(sql, new { Id = id });
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return operation;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 更新運維
|
|
/// </summary>
|
|
/// <param name="operation"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateOperation(OperationInfo operation , List<string> properties)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
var updateQuery = new StringBuilder($"UPDATE operation_firm SET ");
|
|
properties.ForEach(property =>
|
|
{
|
|
if (!property.Equals("Id"))
|
|
{
|
|
updateQuery.Append($"{property}=@{property},");
|
|
}
|
|
});
|
|
updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma
|
|
updateQuery.Append(" WHERE id = @Id");
|
|
await conn.ExecuteAsync(updateQuery.ToString(), operation, trans);
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 裝置類型下拉選單
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<UserSelectItemList>> DeviceType()
|
|
{
|
|
List<UserSelectItemList> result = new List<UserSelectItemList>();
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
string sql = @$"SELECT * FROM variable WHERE name = @name";
|
|
var json = await conn.QueryFirstOrDefaultAsync<Variable>(sql, new { name = "Type" });
|
|
Root jsonfor = JsonSerializer.Deserialize<Root>(json.value);
|
|
foreach(Models.PowerStation.Type a in jsonfor.Type)
|
|
{
|
|
UserSelectItemList KeyValue = new UserSelectItemList
|
|
{
|
|
Value = a.EName,
|
|
Text = a.Name
|
|
};
|
|
result.Add(KeyValue);
|
|
}
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 新增裝置資料
|
|
/// </summary>
|
|
/// <param name="DeviceInfo"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task AddDevice(Device DeviceInfo, List<string> properties)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = GenerateInsertQueryWithCustomTable(properties, "device");
|
|
|
|
await conn.ExecuteAsync(sql, DeviceInfo);
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 修改裝置資料
|
|
/// </summary>
|
|
/// <param name="DeviceInfo"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateDevice(Device DeviceInfo, List<string> properties)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
var updateQuery = GenerateUpdateQueryWithCustomTable(properties, "device");
|
|
await conn.ExecuteAsync(updateQuery.ToString(), DeviceInfo, trans);
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 裝置dataTable
|
|
/// </summary>
|
|
/// <param name="stationId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<DeviceTable>> DeviceTable(int stationId)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
List<DeviceTable> Device = new List<DeviceTable>();
|
|
try
|
|
{
|
|
string sql = @$"SELECT * FROM device WHERE Deleted = 0 AND PowerStationId = @StationId";
|
|
Device = (await conn.QueryAsync<DeviceTable>(sql, new { StationId = stationId })).ToList();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return Device;
|
|
}
|
|
/// <summary>
|
|
/// 取單一筆DeviceInfo
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<DeviceInfo> OneDeviceInfo(int id)
|
|
{
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
DeviceInfo Device;
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = @$"SELECT * FROM device WHERE Id = @Id";
|
|
Device = await conn.QueryFirstOrDefaultAsync<DeviceInfo>(sql, new { Id = id });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return Device;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 新增 異常設定
|
|
/// </summary>
|
|
/// <param name="Exception"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task AddException(ExceptionModal Exception, List<string> properties)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = GenerateInsertQueryWithCustomTable(properties, "power_station_exception");
|
|
|
|
await conn.ExecuteAsync(sql, Exception);
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 異常dataTable
|
|
/// </summary>
|
|
/// <param name="stationId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ExceptionTable>> ExceptionTable(int stationId)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
List<ExceptionTable> Exception = new List<ExceptionTable>();
|
|
try
|
|
{
|
|
string sql = @$"SELECT pe.Type,pe.UpperLimit,pe.LowerLimit,pe.Alarm,ps.Code AS PowerStationCode ,ps.Name AS PowerStationName,pe.CreatedAt,pe.Id
|
|
FROM power_station_exception pe
|
|
LEFT JOIN power_station ps ON pe.PowerStationId = ps.Id
|
|
WHERE pe.Deleted = 0 AND pe.PowerStationId = @StationId";
|
|
Exception = (await conn.QueryAsync<ExceptionTable>(sql, new { StationId = stationId })).ToList();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return Exception;
|
|
}
|
|
/// <summary>
|
|
/// 取一筆異常設定
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<ExceptionModal> OneException(int id)
|
|
{
|
|
using (IDbConnection conn = _databaseHelper.GetConnection())
|
|
{
|
|
ExceptionModal Exception;
|
|
conn.Open();
|
|
try
|
|
{
|
|
string sql = @$"SELECT * FROM power_station_exception WHERE Id = @Id";
|
|
Exception = await conn.QueryFirstOrDefaultAsync<ExceptionModal>(sql, new { Id = id });
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return Exception;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 更新異常設定
|
|
/// </summary>
|
|
/// <param name="Exception"></param>
|
|
/// <param name="properties"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateException(ExceptionModal Exception, List<string> properties)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
var updateQuery = GenerateUpdateQueryWithCustomTable(properties, "power_station_exception");
|
|
await conn.ExecuteAsync(updateQuery.ToString(), Exception, trans);
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
public async Task<string> GetFinalSerialNumber(int PowerStationId, string Type)
|
|
{
|
|
string Num;
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
var sql = "SELECT SerialNumber FROM device WHERE PowerStationId = @PowerStationId AND Type = @Type ORDER BY SerialNumber DESC";
|
|
Num = await conn.QueryFirstOrDefaultAsync<string>(sql, new { PowerStationId = PowerStationId, Type = Type });
|
|
trans.Commit();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return Num;
|
|
}
|
|
|
|
public async Task<List<SolarCityAmount>> GetSolarCitySummary(MyUser User)
|
|
{
|
|
using IDbConnection conn = _databaseHelper.GetConnection();
|
|
List<SolarCityAmount> solaramount = new List<SolarCityAmount>();
|
|
conn.Open();
|
|
var trans = conn.BeginTransaction();
|
|
try
|
|
{
|
|
if (User.Role.Layer == 0 || User.Role.Layer == 1)
|
|
{
|
|
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id GROUP BY power_station.CityId";
|
|
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql)).ToList();
|
|
trans.Commit();
|
|
}
|
|
else if (User.Role.Layer == 2)
|
|
{
|
|
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id WHERE CompanyId = @CompanyId GROUP BY power_station.CityId ORDER BY power_station.CityId ";
|
|
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql, new { CompanyId = User.CompanyId })).ToList();
|
|
trans.Commit();
|
|
}
|
|
else
|
|
{
|
|
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE UserId = @UserId GROUP BY power_station.CityId ORDER BY power_station.CityId";
|
|
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql, new { UserId = User.Id })).ToList();
|
|
trans.Commit();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
trans.Rollback();
|
|
throw exception;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
return solaramount;
|
|
}
|
|
}
|
|
}
|