diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 96915d8..e7da930 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -1422,11 +1422,12 @@ namespace SolarPower.Controllers public async Task> SaveException(ExceptionModal exceptionModal) { ApiResult apiResult = new ApiResult(); - var url = "10.1.100.138"; try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(exceptionModal.PowerStationId); + + if (exceptionModal.Id == 0) { @@ -1446,13 +1447,42 @@ namespace SolarPower.Controllers "CreatedBy", "PowerStationId", "Id", - "LowerLimit", "Type", - "UpperLimit", + "LowerLimit", + "UpperLimit" }; + + + if(Exception.LowerLimit == -9999) + { + Exception.LowerLimit = null; + } + if (Exception.UpperLimit != -9999) + { + Exception.UpperLimit = null; + } await powerStationRepository.AddException(Exception, properties, powerStation.SiteDB); - Fetch_PostWithJSONFormat("http://" + url + ":8080/obix/config/Solar/S"+ powerStation.Code+ "01/API/PR_UpperLimit/set", exceptionModal.UpperLimit.ToString()); - Fetch_PostWithJSONFormat("http://" + url + ":8080/obix/config/Solar/S" + powerStation.Code + "01/API/PR_LowerLimit/set", exceptionModal.LowerLimit.ToString()); + + var calllist = await powerStationRepository.GetApicallList(powerStation.Id, exceptionModal.Type); + + foreach(var call in calllist ) + { + if(call.LimitValue == 0) + { + if(exceptionModal.UpperLimit != -9999) + { + Fetch_PostWithJSONFormat($"http://{call.UrlSite}/obix/config/Solar/S{powerStation.Code}01/API/{call.UrlApi}/set", exceptionModal.UpperLimit.ToString()); + } + + } + else if(call.LimitValue == 1) + { + if (exceptionModal.LowerLimit != -9999) + { + Fetch_PostWithJSONFormat($"http://{call.UrlSite}/obix/config/Solar/S{powerStation.Code}01/API/{call.UrlApi}/set", exceptionModal.LowerLimit.ToString()); + } + } + } apiResult.Code = "0000"; apiResult.Msg = "新增成功"; @@ -1475,13 +1505,42 @@ namespace SolarPower.Controllers "CreatedBy", "PowerStationId", "Id", - "LowerLimit", "Type", - "UpperLimit", + "LowerLimit", + "UpperLimit" }; + if (Exception.LowerLimit == -9999) + { + Exception.LowerLimit = null; + } + if (Exception.UpperLimit == -9999) + { + Exception.UpperLimit = null; + } + + await powerStationRepository.UpdateException(Exception, properties, powerStation.SiteDB); - Fetch_PostWithJSONFormat("http://" + url + ":8080/obix/config/Solar/S" + powerStation.Code + "01/API/PR_UpperLimit/set", exceptionModal.UpperLimit.ToString()); - Fetch_PostWithJSONFormat("http://" + url + ":8080/obix/config/Solar/S" + powerStation.Code + "01/API/PR_LowerLimit/set", exceptionModal.LowerLimit.ToString()); + + var calllist = await powerStationRepository.GetApicallList(powerStation.Id, exceptionModal.Type); + + foreach (var call in calllist) + { + if (call.LimitValue == 0) + { + if (exceptionModal.UpperLimit != -9999) + { + Fetch_PostWithJSONFormat($"http://{call.UrlSite}/obix/config/Solar/S{powerStation.Code}01/API/{call.UrlApi}/set", exceptionModal.UpperLimit.ToString()); + } + + } + else if (call.LimitValue == 1) + { + if (exceptionModal.LowerLimit != -9999) + { + Fetch_PostWithJSONFormat($"http://{call.UrlSite}/obix/config/Solar/S{powerStation.Code}01/API/{call.UrlApi}/set", exceptionModal.LowerLimit.ToString()); + } + } + } apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; @@ -1517,10 +1576,6 @@ namespace SolarPower.Controllers a.Function = @" "; - if (a.Type == 1) - { - a.TypeName = "PR值"; - } if (a.Alarm == 1) { a.AlarmName = "email通知"; @@ -2988,6 +3043,29 @@ namespace SolarPower.Controllers return apiResult; } + [HttpPost] + public async Task>> GetApicallItemList(int powerStationId) + { + ApiResult> apiResult = new ApiResult>(); + try + { + var powerStation = await powerStationRepository.GetOneAsync(powerStationId); + var ItemLists = await powerStationRepository.GetApicallItemList(powerStationId, powerStation.SiteDB); + + apiResult.Code = "0000"; + apiResult.Data = ItemLists; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + public JObject Fetch_PostWithJSONFormat(string url, string paramter) { diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 11e3389..8e975ea 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -466,9 +466,9 @@ namespace SolarPower.Models.PowerStation { public int Id { get; set; } public int PowerStationId { get; set; } - public byte Type { get; set; } - public decimal UpperLimit { get; set; } - public decimal LowerLimit { get; set; } + public string Type { get; set; } + public decimal? UpperLimit { get; set; } + public decimal? LowerLimit { get; set; } public byte Alarm { get; set; } } @@ -1034,4 +1034,12 @@ namespace SolarPower.Models.PowerStation public string UID { get; set; } } + public class ApicallList + { + public string UrlSite { get; set; } + public string UrlApi { get; set; } + public int LimitValue { get; set; } + public string Item { get; set; } + } + } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index ac3ed07..b6a6340 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -943,10 +943,18 @@ namespace SolarPower.Repository.Implement 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 + string sql = @$"SELECT pe.Type, + pe.UpperLimit, + pe.LowerLimit, + api.ItemName AS TypeName, + pe.Alarm,ps.Code AS PowerStationCode , + ps.Name AS PowerStationName, + pe.CreatedAt, + pe.Id FROM {db_name}.power_station_exception pe LEFT JOIN {db_name}.power_station ps ON pe.PowerStationId = ps.Id - WHERE pe.Deleted = 0 AND pe.PowerStationId = @StationId"; + LEFT JOIN apicall api ON api.Item = pe.Type and ps.Id = api.PowerStationId + WHERE pe.Deleted = 0 AND pe.PowerStationId = @StationId GROUP BY pe.Id"; Exception = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); } catch (Exception exception) @@ -5222,5 +5230,57 @@ namespace SolarPower.Repository.Implement return result; } } + + + public async Task> GetApicallItemList(int powerStationId,string dbname) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $@"SELECT + aa.Item AS VALUE, aa.ItemName AS `text` + FROM + ( + SELECT api.Item, api.ItemName + FROM apicall api + LEFT join + (SELECT * FROM {dbname}.power_station_exception ex WHERE ex.Deleted = 0) a + ON a.`Type` = api.Item + WHERE api.PowerStationId = {powerStationId} + AND ISNULL(a.Id) + GROUP BY api.Item + ) aa + "; + + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + public async Task> GetApicallList(int PowerStationId,string Type) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $@"SELECT * FROM apicall WHERE PowerStationId = {PowerStationId} and Item = '{Type}' order by LimitValue"; + + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index fe5aa13..757aa6c 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -593,5 +593,7 @@ namespace SolarPower.Repository.Interface Task GetInverterInfoModal(int Id, string Time, string DB, string Table); Task> GetPowerStationsAllWithfilterForGeneration(string filter); Task> GetPowerStationsByCompanyIdWithfilterForGeneration(MyUser myUser, string filter); + Task> GetApicallItemList(int powerStationId, string dbname); + Task> GetApicallList(int PowerStationId, string Type); } } diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 6b24bfd..537ec34 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -722,6 +722,7 @@ }, 'createdRow': function (row, data, dataIndex) { $(row).attr('data-id', data.id); + $(row).attr('data-typename', data.typeName); }, "ajax": { "url": "/PowerStation/ExceptionTable", @@ -936,6 +937,13 @@ }); //#endregion + + + + + + + }); //#region 代管切換 @@ -2552,6 +2560,7 @@ function AddException() { selected_id = 0; $("#Exception-modal .modal-title").html("異常設定資料 - 新增"); + urlApicallItem(); $("#Exception-form").trigger("reset"); $("#Exception-modal").modal(); } @@ -2561,13 +2570,15 @@ function SaveException() { if ($("#Exception-form").valid()) { + + var url = "/PowerStation/SaveException"; var send_data = { Id: selected_id, PowerStationId: stationId, Type: $("#Exception_Type_modal").val(), - UpperLimit: $("#Exception_UpperLimit_modal").val(), - LowerLimit: $("#Exception_LowerLimit_modal").val(), + UpperLimit: ($("#Exception_UpperLimit_modal").val() != "") ? $("#Exception_UpperLimit_modal").val() : "-9999", + LowerLimit: ($("#Exception_LowerLimit_modal").val() != "") ? $("#Exception_LowerLimit_modal").val() : "-9999", Alarm: $("#Exception_Alarm_modal").val() } $.post(url, send_data, function (rel) { @@ -2590,7 +2601,7 @@ //#region 取一筆異常設定 $('#Exception_table').on("click", "button.edit-btn", function () { $("#Exception-modal .modal-title").html("異常設定資料 - 編輯"); - + var tyname = $(this).parents('tr').attr('data-typename'); selected_id = $(this).parents('tr').attr('data-id'); //取得單一異常設定資料 @@ -2607,8 +2618,9 @@ toast_error(rel.msg); return; } - - $("#Exception_Type_modal").val(rel.data.type); + $("#Exception_Type_modal").empty(); + $("#Exception_Type_modal").append($(" -
+
- +
- +
@@ -66,7 +66,7 @@