From f0a4d73da585a6df268bc8efdf7887ac7327117d Mon Sep 17 00:00:00 2001 From: b110212000 Date: Mon, 28 Jun 2021 15:37:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E4=BD=B5=E7=89=88=20=E9=80=86?= =?UTF-8?q?=E8=AE=8A=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PowerStationController.cs | 219 +++++++++++++++++ SolarPower/Models/ErrorCode.cs | 1 + SolarPower/Models/Operation.cs | 1 + SolarPower/Models/PowerStation.cs | 42 ++++ .../Implement/PowerStationRepository.cs | 145 ++++++++++++ .../Repository/Implement/RepositoryBase.cs | 24 ++ .../Interface/IPowerStationRepository.cs | 36 +++ .../Repository/Interface/IRepositoryBase.cs | 7 + .../PowerStation/PowerStationEdit.cshtml | 224 +++++++++++++++++- .../Views/PowerStation/_DeviceSetting.cshtml | 2 +- 10 files changed, 699 insertions(+), 2 deletions(-) diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 10e6a89..145587f 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -1936,5 +1936,224 @@ namespace SolarPower.Controllers return apiResult; } + + /// + /// 儲存控制器 + /// + /// + /// + public async Task> SaveController(DeviceControllerModal post) + { + ApiResult apiResult = new ApiResult(); + PowerStation powerStation = new PowerStation(); + powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); + + var tableName = powerStation.SiteDB + ".controller"; + try + { + for (var i = 0; i < post.Count; i++) + { + var Num = await powerStationRepository.GetCurrentSerialNumber(tableName, "PowerStationId =" + powerStation.Id); + var Newnum = GetLastSerialNumber(Num, 2); + DeviceController deviceController = new DeviceController() + { + Id = post.Id, + ControllerId = powerStation.Code + Newnum, + PowerStationId = powerStation.Id, + CreatedBy = myUser.Id, + SerialNumber = Newnum + }; + List properties = new List() + { + "Id", + "ControllerId", + "PowerStationId", + "CreatedBy", + "SerialNumber" + }; + await powerStationRepository.AddDeviceController(deviceController, properties, powerStation.SiteDB); + } + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + string json = System.Text.Json.JsonSerializer.Serialize(post); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 控制器DataTable + /// + /// + /// + public async Task DeviceControllerControllerTable (int stationId) + { + List deviceControllerTable = new List(); + ApiResult> apiResult = new ApiResult>(); + PowerStation powerStation = null; + try + { + + powerStation = await powerStationRepository.GetOneAsync(stationId); + deviceControllerTable = await powerStationRepository.DeviceControllerTable(stationId, powerStation.SiteDB); + foreach (DeviceControllerTable a in deviceControllerTable) + { + a.Function = @" + "; + + } + + apiResult.Code = "0000"; + apiResult.Data = deviceControllerTable; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId = " + stationId); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + var result = Json(new + { + data = apiResult + }); + return result; + } + /// + /// 刪除控制器 + /// + /// + /// + public async Task> DeleteOneDeviceControllerController(PostPowerStationIdAndSelectedId post) + { + ApiResult apiResult = new ApiResult(); + + PowerStation powerStation = null; + try + { + powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); + + var deviceController = await powerStationRepository.GetOneWithCustomDBNameAndTableAsync(post.SelectedId, powerStation.SiteDB, "controller"); + + if (deviceController == null) + { + apiResult.Code = "9988"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + await powerStationRepository.DeleteOneByIdWithCustomDBNameAndTable(post.SelectedId, powerStation.SiteDB, "controller"); + + apiResult.Code = "0000"; + apiResult.Msg = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + string json = System.Text.Json.JsonSerializer.Serialize(post); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 新增逆變器 + /// + /// + /// + public async Task> SaveInverter(Inverter post) + { + ApiResult apiResult = new ApiResult(); + PowerStation powerStation = null; + powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); + + var tableName = powerStation.SiteDB + ".inverter"; + try + { + var Num = await powerStationRepository.GetCurrentSerialNumber(tableName, "ControllerId =" + post.ControllerId); + var Newnum = GetLastSerialNumber(Num); + var deviceController = await powerStationRepository.GetOneWithCustomDBNameAndTableAsync(post.ControllerId, powerStation.SiteDB, "controller"); + Inverter inverter = new Inverter() + { + ControllerId = post.ControllerId, + SerialNumber = Newnum, + InverterId = powerStation.Code + "-" + deviceController.ControllerId + "-" + Newnum, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "ControllerId", + "SerialNumber", + "CreatedBy", + "InverterId" + }; + await powerStationRepository.AddInverter(inverter, properties, powerStation.SiteDB); + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + string json = System.Text.Json.JsonSerializer.Serialize(post); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 逆變器Table + /// + /// + /// + public async Task InverterTable(int stationId) + { + ApiResult> apiResult = new ApiResult>(); + + + PowerStation powerStation = null; + try + { + + powerStation = await powerStationRepository.GetOneAsync(stationId); + List deviceControllerid = await powerStationRepository.GetAllDeviceControllerId(stationId,powerStation.SiteDB); + List InverterTable = new List(); + InverterTable = await powerStationRepository.InverterTable(deviceControllerid, powerStation.SiteDB); + + + + foreach (InverterTable a in InverterTable) + { + a.Function = @" + "; + } + + apiResult.Code = "0000"; + apiResult.Data = InverterTable; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId = " + stationId); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + var result = Json(new + { + data = apiResult + }); + return result; + } } } diff --git a/SolarPower/Models/ErrorCode.cs b/SolarPower/Models/ErrorCode.cs index 26e2197..e08a777 100644 --- a/SolarPower/Models/ErrorCode.cs +++ b/SolarPower/Models/ErrorCode.cs @@ -17,6 +17,7 @@ namespace SolarPower.Models { { "0000", "OK" }, { "0001", "傳入參數錯誤。" }, + { "9988", "查無該資料紀錄"}, { "9989", "查無該運維作業記錄"}, { "9990", "查無該圖片"}, { "9991", "查無該土地房屋資訊"}, diff --git a/SolarPower/Models/Operation.cs b/SolarPower/Models/Operation.cs index 3564efc..e22c06e 100644 --- a/SolarPower/Models/Operation.cs +++ b/SolarPower/Models/Operation.cs @@ -297,4 +297,5 @@ namespace SolarPower.Models public string Notice { get; set; } public string Description { get; set; } } + } diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index b17938a..f0adb3b 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -472,5 +472,47 @@ namespace SolarPower.Models.PowerStation public string Text { get; set; } public string Value { get; set; } } + /// + /// 控制器 + /// + public class DeviceController : Created + { + public int Id { get; set; } + public int PowerStationId { get; set; } + public string SerialNumber { get; set; } + public string ControllerId { get; set; } + } + public class DeviceControllerModal : DeviceController + { + public int Count { get; set; }//數量 + } + /// + /// 控制器datatable + /// + public class DeviceControllerTable : DeviceController + { + public string CreatedName { get; set; } + public string Function { get; set; } + } + /// + /// 逆變器 + /// + public class Inverter : Created + { + public int Id { get; set; } + public int ControllerId { get; set; } + public int PowerStationId { get; set; } + public string SerialNumber { get; set; } + public string InverterId { get; set; } + } + /// + /// 控制器datatable + /// + public class InverterTable : Inverter + { + public string ControllerName { get; set; } + public string CreatedName { get; set; } + public string Function { get; set; } + } } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 2b14ddd..9a6ffae 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1384,5 +1384,150 @@ namespace SolarPower.Repository.Implement return result; } } + + public async Task AddDeviceController(DeviceController deviceController, List properties, string db_name) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + int count; + conn.Open(); + try + { + string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "controller"); + + count = await conn.ExecuteAsync(sql, deviceController); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + + public async Task> DeviceControllerTable(int stationId, string db_name) + { + + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + List deviceControllerTable = new List(); + conn.Open(); + try + { + string sql = @$"SELECT + col.ControllerId,user.Name As CreatedName ,col.CreatedAt,col.Id + FROM {db_name}.controller col + LEFT JOIN user ON col.CreatedBy = user.id + WHERE col.Deleted = 0 AND col.PowerStationId = @StationId"; + deviceControllerTable = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return deviceControllerTable; + } + } + /// + /// 新增逆變器 + /// + /// + /// + /// + /// + public async Task AddInverter(Inverter inverter, List properties, string db_name) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + int count; + conn.Open(); + try + { + string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "inverter"); + + count = await conn.ExecuteAsync(sql, inverter); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + /// + /// 取得控制器所有id + /// + /// + /// + /// + public async Task> GetAllDeviceControllerId(int stationId,string db_name) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + List count; + conn.Open(); + try + { + string sql = $"SELECT id FROM {db_name}.controller WHERE PowerStationId = {stationId}"; + count = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return count; + } + } + /// + /// 逆變器DataTable + /// + /// + /// + /// + public async Task> InverterTable(List controllerid, string db_name) + { + + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + List inverterTable = new List(); + conn.Open(); + try + { + string sql = @$"SELECT + inv.inverterId, inv.id, con.ControllerId AS ControllerName,user.Name As CreatedName ,inv.createdAt + FROM {db_name}.inverter inv + LEFT JOIN {db_name}.controller con ON inv.ControllerId = con.id + LEFT JOIN user ON inv.CreatedBy = user.id + WHERE inv.Deleted = 0 AND inv.ControllerId IN @Controllerid"; + inverterTable = (await conn.QueryAsync(sql, new { Controllerid = controllerid })).ToList(); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return inverterTable; + } + } } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index e53802f..c55929e 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -253,6 +253,30 @@ namespace SolarPower.Repository.Implement } } + /// + /// 取得單一筆資料(客製化資料庫及資料表) + /// + /// + /// + public virtual async Task GetOneWithCustomDBNameAndTableAsync(int id, string db_name, string table_name) + { + A result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT * FROM {db_name}.{table_name} WHERE id = @Id"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + /// /// 透過Id,實際刪除單一筆資料 /// diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 600739b..12815e7 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -344,5 +344,41 @@ namespace SolarPower.Repository.Interface /// /// Task> GetSolarByCity(MyUser User, List CityId); + + /// + /// 新增控制器 + /// + /// + /// + /// + /// + Task AddDeviceController(DeviceController deviceController, List properties, string db_name); + + /// + /// 控制器dataTable + /// + /// + /// + /// + Task> DeviceControllerTable(int stationId, string db_name); + /// + /// 新增逆變器 + /// + /// + /// + /// + /// + Task AddInverter(Inverter inverter, List properties, string db_name); + /// + /// 取得控制器所有id + /// + Task> GetAllDeviceControllerId(int stationId,string db_name); + /// + /// 逆變器dataTable + /// + /// + /// + /// + Task> InverterTable(List controllerid, string db_name); } } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index 5eb0719..c025807 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -96,5 +96,12 @@ namespace SolarPower.Repository.Interface /// /// Task GetCurrentSerialNumber(string Table_name, string where = ""); + + /// + /// 透過Id,取得單一資料(客製化資料庫及資料表) + /// + /// + /// + Task GetOneWithCustomDBNameAndTableAsync(int id, string db_name, string table_name); } } diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index c27e117..2acaa74 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -166,6 +166,151 @@ }, 'json'); + //#region 控制器列表 DataTable + DeviceControllerTable = $("#Controller_table").DataTable({ + "pageLength": 5, + "paging": true, + "lengthChange": false, + "searching": false, + "ordering": true, + "info": true, + "autoWidth": false, + "responsive": true, + "columns": [{ + "data": "controllerId" + }, { + "data": "createdAt" + }, { + "data": "createdName" + }, { + "data": "function" + }], + "columnDefs": [{ + 'targets': 1, + 'searchable': false, + 'orderable': false, + 'className': 'dt-body-center' + }], + "language": { + "emptyTable": "無資料...", + "processing": "處理中...", + "loadingRecords": "載入中...", + "lengthMenu": "顯示 _MENU_ 項結果", + "zeroRecords": "沒有符合的結果", + "info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項", + "infoEmpty": "顯示第 0 至 0 項結果,共 0 項", + "infoFiltered": "(從 _MAX_ 項結果中過濾)", + "infoPostFix": "", + "search": "搜尋:", + "paginate": { + "first": "第一頁", + "previous": "上一頁", + "next": "下一頁", + "last": "最後一頁" + } + }, + 'createdRow': function (row, data, dataIndex) { + $(row).attr('data-id', data.id); + }, + "ajax": { + "url": "/PowerStation/DeviceControllerControllerTable", + "type": "POST", + "data": function (d) { + d.stationId = stationId; + }, + "dataSrc": function (rel) { + if (rel.data.code == "9999") { + toast_error(rel.data.msg); + return; + } + + data = rel.data.data; + + if (data == null || data.length == 0) { + this.data = []; + } + $("#Inverter_ControllerId_modal").empty(); + + $.each(data, function (index, val) { + $("#Inverter_ControllerId_modal").append($("