From 6934f9acac248a247455f262350b2ca7f750adc1 Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Mon, 8 Aug 2022 23:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=BB=E7=AB=99=E8=B3=87=E8=A8=8A-=E9=80=86?= =?UTF-8?q?=E8=AE=8A=E5=99=A8=E7=9B=A3=E6=8E=A7:=20=E9=80=86=E8=AE=8A?= =?UTF-8?q?=E5=99=A8=E5=9C=A8=E6=96=B0=E5=A2=9E=E3=80=81=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E3=80=81=E5=88=AA=E9=99=A4=E6=99=82=EF=BC=8C=E6=9C=83=E5=8E=BB?= =?UTF-8?q?=E6=9B=B4=E6=96=B0inv=5FStatus=E8=B3=87=E6=96=99=E8=A1=A8?= =?UTF-8?q?=EF=BC=9B=E9=80=8F=E9=81=8Einv=5FStatus=E9=A1=AF=E7=A4=BA?= =?UTF-8?q?=E5=9C=A8=E9=80=86=E8=AE=8A=E5=99=A8=E7=9B=A3=E6=8E=A7=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E9=80=86=E8=AE=8A=E5=99=A8=E7=8B=80=E6=85=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PowerStationController.cs | 50 +++++- SolarPower/Models/PowerStation.cs | 14 ++ .../Implement/PowerStationRepository.cs | 156 ++++++++++++++---- .../Interface/IPowerStationRepository.cs | 10 ++ 4 files changed, 197 insertions(+), 33 deletions(-) diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 2bce9d5..6adc78a 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -2474,6 +2474,27 @@ namespace SolarPower.Controllers } await powerStationRepository.AddInverter(inverter, properties, powerStation.SiteDB); + #region 在master底下的inv_Status 新增inverter + InverterForMaster inverterForMasterInvs = new InverterForMaster() + { + DBName = powerStation.SiteDB, + InverterId = deviceController.ControllerId + Newnum, + Deleted = 0, + Enabled = post.Enabled, + Status = post.Status + }; + + List propertiesForMasterInvs = new List() + { + "DBName", + "InverterId", + "Deleted", + "Enabled", + "Status" + }; + await powerStationRepository.AddMasterInverter(inverterForMasterInvs, propertiesForMasterInvs); + #endregion + BackFillSchedule backFillSchedule = new BackFillSchedule() { UID = deviceController.ControllerId + Newnum, @@ -2537,6 +2558,24 @@ namespace SolarPower.Controllers properties.Add("WarrantyDate"); } await powerStationRepository.UpdateInverter(inverter, properties, powerStation.SiteDB); + + #region 在master底下的inv_Status更新inverter + InverterForMaster inverterForMasterInvs = new InverterForMaster() + { + DBName = powerStation.SiteDB, + Enabled = post.Enabled, + Status = post.Status + }; + + List propertiesForMasterInvs = new List() + { + "DBName", + "Enabled", + "Status" + }; + await powerStationRepository.UpdateMasterInverter(inverterForMasterInvs, propertiesForMasterInvs); + #endregion + await powerStationRepository.StationStatus(post.PowerStationId, 0); await powerStationRepository.StationStatus(post.PowerStationId, 0, powerStation.SiteDB + "."); apiResult.Code = "0000"; @@ -2659,6 +2698,15 @@ namespace SolarPower.Controllers await powerStationRepository.DeleteOneByIdWithCustomDBNameAndTable(post.SelectedId, powerStation.SiteDB, "inverter"); + var inverter = await powerStationRepository.GetOneMasterInverter(post.SelectedId, powerStation.SiteDB); + if (inverter == null ) + { + apiResult.Code = "0001"; + apiResult.Msg = "該電站沒有啟用的逆變器"; + return apiResult; + } + await powerStationRepository.DeleteOneMasterInverter(inverter.InverterId, "inv_status"); + apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } @@ -2997,7 +3045,7 @@ namespace SolarPower.Controllers apiResult.Msg = "該電站沒有啟用的逆變器"; return apiResult; } - var inverterIds = inverters.Where(x => x.Enabled == 1).Select(x => new { InverterId = x.InverterId, Type = x.Status,Name = x.InverterName}).ToList(); + var inverterIds = inverters.Where(x => x.Enabled == 1).Select(x => new { InverterId = x.InverterId, Type = x.InverterStatus, Name = x.InverterName}).ToList(); var inverterIdsname = inverterIds.Select(x => x.InverterId).ToList(); if (inverterIds.Count > 0) { diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 5e7403e..0ffedd3 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -627,7 +627,21 @@ namespace SolarPower.Models.PowerStation public string EnabledName { get; set; } public string StatusName { get; set; } public string PyrheliometerName { get; set; } + public int InverterStatus { get; set; } } + + /// + /// Master底下的逆變器資料表 + /// + public class InverterForMaster + { + public string DBName { get; set; }//資料庫名稱 + public string InverterId { get; set; }//逆變器id + public int Deleted { get; set; }//是否刪除 + public int Enabled { get; set; }//是否啟用 + public int Status { get; set; }//狀態 + } + /// /// 下拉式選單用 /// diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 4f59599..45ac7a4 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1906,6 +1906,34 @@ namespace SolarPower.Repository.Implement } } + /// + /// 在master.inv_status資料表中 新增逆變器 + /// + /// + /// + /// + public async Task AddMasterInverter(InverterForMaster inverter, List properties) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + int count; + conn.Open(); + try + { + var sql = GenerateInsertQueryWithCustomTable(properties, "inv_status"); + count = await conn.ExecuteAsync(sql, inverter); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + /// /// 取得控制器所有id /// @@ -1980,11 +2008,12 @@ namespace SolarPower.Repository.Implement try { string sql = @$"SELECT - inv.*, con.ControllerId AS ControllerName,user.Name As CreatedName,de.UID AS PyrheliometerName + inv.*, con.ControllerId AS ControllerName,user.Name As CreatedName,de.UID AS PyrheliometerName, invs.status AS InverterStatus FROM {db_name}.inverter inv LEFT JOIN {db_name}.controller con ON inv.ControllerId = con.id LEFT JOIN user ON inv.CreatedBy = user.id LEFT JOIN {db_name}.device de ON inv.Pyrheliometer = de.id + LEFT JOIN inv_status invs ON inv.InverterId = invs.InverterId WHERE inv.Deleted = 0 AND inv.ControllerId IN @Controllerid"; inverterTable = (await conn.QueryAsync(sql, new { Controllerid = controllerid }, commandTimeout: 600)).ToList(); @@ -2001,6 +2030,39 @@ namespace SolarPower.Repository.Implement } } + /// + /// 取單一逆變器 + /// + /// + /// + /// + public async Task GetOneMasterInverter(int id, string db_name) + { + + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + InverterTable inverter = new InverterTable(); + conn.Open(); + try + { + string sql = @$"SELECT id, InverterId + FROM {db_name}.inverter + WHERE Deleted = 0 AND Id = {id}"; + inverter = (await conn.QueryFirstOrDefaultAsync(sql, commandTimeout: 600)); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return inverter; + } + } + /// /// 其餘電站列表 /// @@ -2558,41 +2620,71 @@ namespace SolarPower.Repository.Implement } } - //public virtual async Task DeleteOneInverter(int id, string db_name, string table_name) - //{ - // using (IDbConnection conn = this._databaseHelper.GetConnection()) - // { - // conn.Open(); - // using (var trans = conn.BeginTransaction()) - // { - // try - // { - // var sql = $"UPDATE {db_name}.{table_name} SET Deleted = 1 WHERE Id = @Id"; + public async Task UpdateMasterInverter(InverterForMaster entity, List properties) + { + int count; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + string sql = GenerateUpdateQueryWithCustomTable(properties, "inv_status"); - // await conn.ExecuteAsync(sql, new { Id = id }, trans); + count = await conn.ExecuteAsync(sql, entity, trans); - // string updateToInv_Status = @"DROP TABLE IF EXISTS solar_master.`inv_status`; - // create table solar_master.inv_status - // select * from solar_master.v_company_inv - // where enabled = 1; - // ALTER TABLE solar_master.`inv_status` ADD INDEX inv_status_index (`inverterid`);"; + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } - // await conn.ExecuteAsync(updateToInv_Status, new { Id = id }, trans); + return count; + } + } - // trans.Commit(); - // } - // catch (Exception exception) - // { - // trans.Rollback(); - // throw exception; - // } - // finally - // { - // conn.Close(); - // } - // } - // } - //} + /// + /// 透過table_name、inverterId,軟刪除指定資料表的單一筆inverter + /// + /// + /// + /// + public async Task DeleteOneMasterInverter(string inverterId, string table_name) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = $"UPDATE {table_name} SET Deleted = 1 WHERE InverterId = {inverterId}"; + + await conn.ExecuteAsync(sql, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + + } + } + } public async Task> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name) { diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 7ee71f8..993bb33 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -399,6 +399,13 @@ namespace SolarPower.Repository.Interface /// Task AddInverter(Inverter inverter, List properties, string db_name); /// + /// 新增Master逆變器 + /// + /// + /// + /// + Task AddMasterInverter(InverterForMaster inverter, List properties); + /// /// 取得控制器所有id /// Task> GetAllDeviceControllerId(int stationId, string db_name); @@ -539,6 +546,8 @@ namespace SolarPower.Repository.Interface Task UpdatePyrheliometerHistoryMonthList(List entity); Task UpdateInverter(Inverter entity, List properties, string db_name); + Task UpdateMasterInverter(InverterForMaster entity, List properties); + Task DeleteOneMasterInverter(string inverterId, string table_name); Task Getonediv(string where, string db_name, string table_name); @@ -634,5 +643,6 @@ namespace SolarPower.Repository.Interface Task> GetAllNoStatusAsync(); Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId); Task GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name); + Task GetOneMasterInverter(int id, string db_name); } }