FIC_Solar/SolarPower/Repository/Implement/PowerStationRepository.cs
2021-06-17 21:41:21 +08:00

266 lines
8.7 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;
namespace SolarPower.Repository.Implement
{
public class PowerStationRepository : RepositoryBase<PowerStation>, IPowerStationRepository
{
public PowerStationRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
tableName = "power_station";
}
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 * FROM {tableName} WHERE Deleted = 0 AND Id = @Id";
result = await conn.QueryFirstOrDefaultAsync<PowerStation>(sql, new { Id = id});
if(result!= null)
{
var sql_land_building = @"SELECT * FROM land_building WHERE 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;
}
}
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;
}
}
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 = GenerateUpdateQueryOtherTable(properties, "device");
await conn.ExecuteAsync(updateQuery.ToString(), DeviceInfo, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
}