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, IPowerStationRepository { public PowerStationRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { tableName = "power_station"; } /// /// 查詢縣市列表 /// /// public async Task> GetCitySelectOptionListAsync() { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT Id AS Value, Name AS Text FROM city"; result = (await conn.QueryAsync(sql)).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 查詢地區列表 /// /// /// public async Task> GetAreaSelectOptionListAsync(int cityId) { List 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(sql, new { CityId = cityId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過編號取得,縣市資訊 /// /// /// public async Task GetOneCityByIdAsync(int cityId) { City result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT * FROM city WHERE Id=@Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = cityId }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過編號取得,地區資訊 /// /// /// public async Task GetOneAreaByIdAsync(int areaId) { Area result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT * FROM area WHERE Id=@Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = areaId }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過地區編號,取得縣市地區代碼 /// /// /// public async Task 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(sql, new { AreaId = areaId }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過縣市地區編號,取得該縣市地區最後的流水號 /// /// /// /// public async Task 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(sql, new { CityId = cityId, AreaId = areaId }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過電站編號,取得單一電站資訊(覆寫) /// /// /// public override async Task 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(sql, new { Id = id }); if (result != null) { var db_name = result.SiteDB; var sql_operation_personnel = @$"SELECT UserId FROM {db_name}.power_station_operation_personnel op WHERE Deleted = 0 AND op.PowerStationId = @PowerStationId"; result.OperationPersonnelIds = (await conn.QueryAsync(sql_operation_personnel, new { PowerStationId = result.Id })).ToList(); var sql_land_building = @$"SELECT lb.*, u.Name AS CreatorName FROM {db_name}.land_building lb LEFT JOIN user u ON lb.CreatedBy = u.Id WHERE lb.Deleted = 0 AND PowerStationId = @PowerStationId"; result.LandBuildings = (await conn.QueryAsync(sql_land_building, new { PowerStationId = result.Id })).ToList(); } } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 新增電站資料至 主、子資料庫 /// /// /// /// /// public async Task AddOnePowerStationAsync(PowerStation entity, List properties, string db_name) { int id; int sub_count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { //新增資料 至主資料庫 string sql = GenerateInsertQuery(properties); sql += "SELECT LAST_INSERT_ID();"; id = (await conn.QueryAsync(sql, entity, trans)).Single(); //新增資料 至子資料庫 properties.Add("Id"); entity.Id = id; string sub_sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, tableName); sub_count = await conn.ExecuteAsync(sub_sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } return id; } } /// /// 修改主、子資料庫電站基本資訊 /// /// /// /// /// public async Task UpdatePowerStationInfo(UpdatePowerStationInfo entity, List properties, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { //修改主資料庫 var sql = GenerateUpdateQuery(properties); await conn.ExecuteAsync(sql, entity, trans); //修改子資料庫 var sub_sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, tableName); await conn.ExecuteAsync(sub_sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 修改主、子資料庫能源局與台電資訊 /// /// /// /// /// public async Task UpdateBoETPCInfo(UpdateBoETPCInfo entity, List properties, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { //修改主資料庫 var sql = GenerateUpdateQuery(properties); await conn.ExecuteAsync(sql, entity, trans); //修改子資料庫 var sub_sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, tableName); await conn.ExecuteAsync(sub_sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 新增 土地房屋資訊 /// /// /// /// public async Task GetOneLandBuildingInfo(int id, string db_name) { LandBuilding result; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { var sql = @$"SELECT * FROM {db_name}.land_building WHERE Deleted = 0 AND Id = @Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 新增 土地房屋資訊 /// /// /// /// /// public async Task AddOneLandBuildingInfo(LandBuilding entity, List properties, string db_name) { int id; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "land_building"); sql += "SELECT LAST_INSERT_ID();"; id = (await conn.QueryAsync(sql, entity)).Single(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return id; } } /// /// 更新 土地房屋資訊 /// /// /// /// /// public async Task UpdateLandBuildingInfo(UpdateLandBuilding entity, List properties, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, "land_building"); await conn.ExecuteAsync(sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 軟刪除土地房屋資訊 /// /// /// /// public async Task DeleteOneLandBuildingInfo(int id, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {db_name}.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(); } } } } /// /// 新增運維 /// /// /// /// /// public async Task AddOperation(OperationInfo operation, List properties, string db_name) { using (IDbConnection conn = _databaseHelper.GetConnection()) { int count; conn.Open(); try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "operation_firm"); count = await conn.ExecuteAsync(sql, operation); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } /// /// 運維DataTable /// /// /// /// public async Task> OperationTable(int stationId, string db_name) { using (IDbConnection conn = _databaseHelper.GetConnection()) { List operation = new List(); 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 {db_name}.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(sql, new { StationId = stationId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return operation; } } /// /// 選取單一運維 /// /// /// /// public async Task OneOperationInfo(int id, string db_name) { using (IDbConnection conn = _databaseHelper.GetConnection()) { OperationInfo operation; conn.Open(); try { string sql = @$"SELECT * FROM {db_name}.operation_firm WHERE Id = @Id"; operation = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return operation; } } /// /// 更新運維 /// /// /// /// /// public async Task UpdateOperation(OperationInfo operation, List properties, string db_name) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); var trans = conn.BeginTransaction(); try { var sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, "operation_firm"); await conn.ExecuteAsync(sql, operation, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } /// /// 裝置類型下拉選單 /// /// public async Task> DeviceType() { List result = new List(); 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(sql, new { name = "Type" }); Root jsonfor = JsonSerializer.Deserialize(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; } /// /// 新增裝置資料 /// /// /// /// public async Task AddDevice(Device DeviceInfo, List 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(); } } /// /// 修改裝置資料 /// /// /// /// public async Task UpdateDevice(Device DeviceInfo, List 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(); } } /// /// 裝置dataTable /// /// /// public async Task> DeviceTable(int stationId) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); List Device = new List(); try { string sql = @$"SELECT * FROM device WHERE Deleted = 0 AND PowerStationId = @StationId"; Device = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return Device; } /// /// 取單一筆DeviceInfo /// /// /// public async Task 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(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return Device; } } /// /// 新增 異常設定 /// /// /// /// public async Task AddException(ExceptionModal Exception, List 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(); } } /// /// 異常dataTable /// /// /// public async Task> ExceptionTable(int stationId) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); List Exception = new List(); 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(sql, new { StationId = stationId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return Exception; } /// /// 取一筆異常設定 /// /// /// public async Task 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(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return Exception; } } /// /// 更新異常設定 /// /// /// /// public async Task UpdateException(ExceptionModal Exception, List 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 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(sql, new { PowerStationId = PowerStationId, Type = Type }); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } return Num; } /// /// 透過電站編號,取得該電站的運維人員編號 /// /// /// /// public async Task> GetOperationPersonnelIdsByPowerStatioinId(int powerStationId, string db_name) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); try { var sql = @$"SELECT UserId FROM {db_name}.power_station_operation_personnel WHERE Deleted = 0 AND PowerStationId = @PowerStationId"; result = (await conn.QueryAsync(sql, new { PowerStationId = powerStationId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 新增電站運維人員 /// /// /// /// /// public async Task AddOperationPersonnelAsync(List entity, List properties, string db_name) { int count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "power_station_operation_personnel"); count = await conn.ExecuteAsync(sql, entity); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } /// /// 軟刪除電站運維人員 /// /// /// /// public async Task DeleteOperationPersonnel(List operationPersonnels, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {db_name}.power_station_operation_personnel SET Deleted = 1 WHERE PowerStationId = @PowerStationId AND UserId = @UserId"; await conn.ExecuteAsync(sql, operationPersonnels, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 電站管理 新增電站圖片 /// /// /// /// public async Task AddPowerStationImageAsync(List entity, List properties, string db_name) { int count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "power_station_image"); count = await conn.ExecuteAsync(sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } return count; } } /// /// 電站管理 取得所有電站圖片的資料 /// /// /// /// public async Task> GetAllPowerStationImageAsync(int powerStationId, string db_name) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {db_name}.power_station_image WHERE Deleted = 0 AND PowerStationId = @PowerStationId"; result = (await conn.QueryAsync(sql, new { PowerStationId = powerStationId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 電站管理 取得單一電站圖片的資料 /// /// /// /// public async Task GetOnePowerStationImageAsync(int id, string db_name) { PowerStationImage result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {db_name}.power_station_image WHERE Deleted = 0 AND Id = @Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } return result; } } /// /// 軟刪除 單一電站圖片 /// /// /// /// public async Task DeleteOnePowerStationImage(int id, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {db_name}.power_station_image 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(); } } } } /// /// 電站管理 取得主要卡片顯示圖 /// /// /// /// public async Task GetMainDisplayAsync(int powerStationId, string db_name) { PowerStationImage result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {db_name}.power_station_image WHERE Deleted = 0 AND IsMainDisplay = 1 AND PowerStationId = @PowerStationId"; result = await conn.QueryFirstOrDefaultAsync(sql, new { PowerStationId = powerStationId }); } catch (Exception exception) { throw exception; } return result; } } /// /// 電站管理 更新上傳圖片 /// /// /// /// /// public async Task UpdatePowerStationImage(UpdataPowerStationImage image, List properties, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, "power_station_image"); await conn.ExecuteAsync(sql, image, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 電站管理 新增單線圖 /// /// /// /// /// public async Task AddPowerStationSingleLineAsync(List entity, List properties, string db_name) { int count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "power_station_single_line_diagram"); count = await conn.ExecuteAsync(sql, entity); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } /// /// 電站管理 取得所有單線圖的資料 /// /// /// /// public async Task> GetAllPowerStationSingleLineAsync(int powerStationId, string db_name) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {db_name}.power_station_single_line_diagram WHERE Deleted = 0 AND PowerStationId = @PowerStationId"; result = (await conn.QueryAsync(sql, new { PowerStationId = powerStationId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 電站管理 取得單一單線圖的資料 /// /// /// /// public async Task GetOnePowerStationSingleLineAsync(int id, string db_name) { PowerStationSingleLine result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {db_name}.power_station_single_line_diagram WHERE Deleted = 0 AND Id = @Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } return result; } } /// /// 軟刪除 單一單線圖 /// /// /// /// public async Task DeleteOnePowerStationSingleLine(int id, string db_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {db_name}.power_station_single_line_diagram 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(); } } } } } }