diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 78a63fb..d205b3d 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -988,7 +988,10 @@ namespace SolarPower.Controllers CreatedBy = myUser.Id, TypeName = Device.TypeName, SerialNumber = Newnum, - ControllerId = Device.ControllerId + ControllerId = Device.ControllerId, + Status = Device.Status, + Enabled = Device.Enabled, + InstallDate = Device.InstallDate }; List properties = new List() { @@ -996,17 +999,18 @@ namespace SolarPower.Controllers "ColName", "PowerStationId", "DBName", - "Id", "Name", "ProductModel", - "Remark", "TableName", "Type", "UID", "CreatedBy", "TypeName", "SerialNumber", - "ControllerId" + "ControllerId", + "Status", + "Enabled", + "InstallDate" }; await powerStationRepository.AddDevice(DeviceInfo, properties, powerStation.SiteDB); @@ -1030,7 +1034,10 @@ namespace SolarPower.Controllers Type = Device.Type, CreatedBy = myUser.Id, TypeName = Device.TypeName, - ControllerId = Device.ControllerId + ControllerId = Device.ControllerId, + Status = Device.Status, + Enabled = Device.Enabled, + InstallDate = Device.InstallDate }; List properties = new List() { @@ -1041,12 +1048,14 @@ namespace SolarPower.Controllers "Id", "Name", "ProductModel", - "Remark", "TableName", "Type", "CreatedBy", "TypeName", - "ControllerId" + "ControllerId", + "Status", + "Enabled", + "InstallDate" }; await powerStationRepository.UpdateDevice(DeviceInfo, properties, powerStation.SiteDB); apiResult.Code = "0000"; @@ -1083,6 +1092,25 @@ namespace SolarPower.Controllers a.Function = @" "; + a.StatusName = a.Status switch + { + 0 => "未啟用", + 1 => "正常", + 2 => "異常", + _ => "無資料", + }; + switch (a.Enabled) + { + case 0: + a.EnabledName = "未啟用"; + break; + case 1: + a.EnabledName = "啟用"; + break; + default: + a.EnabledName = "無資料"; + break; + } } apiResult.Data = deviceTables; } @@ -2161,14 +2189,43 @@ namespace SolarPower.Controllers if(inverter1 == null) { apiResult.Msg = "找不到資料"; + apiResult.Code = "0001"; } else { - + Inverter inverter = new Inverter() + { + Id = post.Id, + Status = post.Status, + Brand = post.Brand, + Capacity = post.Capacity, + Enabled = post.Enabled, + Model = post.Model, + InstallDate = post.InstallDate, + InverterName = post.InverterName, + Pyrheliometer = post.Pyrheliometer, + ControllerId = post.ControllerId, + UpdatedBy = myUser.Id + }; + List properties = new List() + { + "Id", + "Status", + "Brand", + "Capacity", + "Enabled", + "Model", + "InstallDate", + "InverterName", + "Pyrheliometer", + "ControllerId", + "UpdatedBy" + }; + await powerStationRepository.UpdateInverter(inverter, properties, powerStation.SiteDB); + apiResult.Code = "0000"; + apiResult.Msg = "更新成功"; } } - - } catch (Exception exception) { @@ -2226,7 +2283,7 @@ namespace SolarPower.Controllers a.EnabledName = "啟用"; break; default: - a.StatusName = "無資料"; + a.EnabledName = "無資料"; break; } if(a.PyrheliometerName == null) @@ -2397,6 +2454,33 @@ namespace SolarPower.Controllers sharedeviceTables = await powerStationRepository.shareDeviceTables(stationId, powerStation.SiteDB); foreach (DeviceTable a in sharedeviceTables) { + switch (a.Status) + { + case 0: + a.StatusName = "未啟用"; + break; + case 1: + a.StatusName = "正常"; + break; + case 2: + a.StatusName = "異常"; + break; + default: + a.StatusName = "無資料"; + break; + } + switch (a.Enabled) + { + case 0: + a.EnabledName = "未啟用"; + break; + case 1: + a.EnabledName = "啟用"; + break; + default: + a.EnabledName = "無資料"; + break; + } a.Function = @" "; } @@ -2433,6 +2517,13 @@ namespace SolarPower.Controllers powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); var ShareDevice = await powerStationRepository.GetOneWithCustomDBNameAndTableAsync(post.SelectedId, powerStation.SiteDB, "sharedevice"); + var div = await powerStationRepository.Getonediv("inv.Deleted = 0 AND con.PowerStationId = sh.PowerStationId", powerStation.SiteDB, $"sharedevice sh INNER JOIN {powerStation.SiteDB}.inverter inv ON inv.Pyrheliometer = sh.DeviceId INNER JOIN {powerStation.SiteDB}.controller con ON con.Id = inv.ControllerId"); + if (div != null) + { + apiResult.Code = "0004"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } if (ShareDevice == null) { diff --git a/SolarPower/Models/ErrorCode.cs b/SolarPower/Models/ErrorCode.cs index d7fa84f..d88f4d7 100644 --- a/SolarPower/Models/ErrorCode.cs +++ b/SolarPower/Models/ErrorCode.cs @@ -19,6 +19,7 @@ namespace SolarPower.Models { "0001", "傳入參數錯誤。" }, { "0002","請先刪除相關逆變器"}, { "0003","請先刪除相關設備"}, + { "0004","請先移除相關逆變器綁定"}, { "9986", "該帳號已被註冊,請重新輸入"}, { "9987", "查無該運維作業記錄檔案"}, { "9988", "查無該資料紀錄"}, diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index b65e300..7fec02d 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -360,9 +360,9 @@ namespace SolarPower.Models.PowerStation /// /// 設備 /// - public class DeviceInfo + public class DeviceInfo : UserInfo { - public int Id { get; set; } + private string installDate; public int PowerStationId { get; set; }//所屬電站編號 public string Name { get; set; }//名稱 public string Type { get; set; }//類型 @@ -371,15 +371,16 @@ namespace SolarPower.Models.PowerStation public string DBName { get; set; } public string TableName { get; set; } public string ColName { get; set; } - public string Remark { get; set; } public int ControllerId { get; set; } public string TypeName { get; set; }//類型名稱 + public int Enabled { get; set; }//啟用 + public int Status { get; set; }//狀態 + public string InstallDate { get { return Convert.ToDateTime(installDate).ToString("yyyy-MM-dd"); } set { installDate = value; } } //安裝日期 } public class Device : DeviceInfo { public string UID { get; set; }//設備編號 - public int CreatedBy { get; set; }//建立者 public string SerialNumber { get; set; } public string PowerStationName { get; set; } } @@ -393,6 +394,8 @@ namespace SolarPower.Models.PowerStation public string UID { get; set; }//設備編號 public string Function { get; set; }//功能 public string PowerStationName { get; set; } + public string EnabledName { get; set; }//啟用 + public string StatusName { get; set; }//狀態 } /// diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 10b1f12..cd25e12 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1691,7 +1691,9 @@ namespace SolarPower.Repository.Implement { try { - var sql = $@"SELECT Id AS Value, UID AS Text FROM {db_name}.device WHERE Deleted = 0 AND PowerStationId = {stationId} AND Type = 'PYR' "; + var sql = $@"SELECT dd.Id AS Value, dd.UID AS Text FROM {db_name}.device dd WHERE dd.Deleted = 0 AND dd.PowerStationId = {stationId} AND dd.Type = 'PYR' + UNION SELECT dd.Id AS Value, dd.UID AS Text FROM {db_name}.sharedevice sh LEFT JOIN {db_name}.device dd ON sh.DeviceId = dd.Id + WHERE dd.Deleted = 0 AND sh.PowerStationId = {stationId};"; result = (await conn.QueryAsync(sql)).ToList(); } @@ -2143,6 +2145,37 @@ namespace SolarPower.Repository.Implement } } + public async Task UpdateInverter(Inverter entity, List properties,string db_name) + { + int count; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + string sql = GenerateUpdateQueryWithCustomDBNameAndTable(properties, db_name, "inverter"); + + count = await conn.ExecuteAsync(sql, entity, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + + return count; + } + } + public async Task> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name) { List result; @@ -2430,5 +2463,24 @@ namespace SolarPower.Repository.Implement return count; } } + + public async Task Getonediv(string where, string db_name, string table_name) + { + A result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT sh.*,inv.*,con.Id,con.PowerStationId FROM {db_name}.{table_name} WHERE {where}"; + + result = await conn.QueryFirstOrDefaultAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 20e31c5..0ab0700 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -504,5 +504,9 @@ namespace SolarPower.Repository.Interface Task GetOnePyrheliometerHistoryByMonth(string month, int powerStationId); Task AddPyrheliometerHistoryMonthList(List entity, List properties); Task UpdatePyrheliometerHistoryMonthList(List entity); + + Task UpdateInverter(Inverter entity, List properties, string db_name); + + Task Getonediv(string where, string db_name, string table_name); } } diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index a607336..0351b2d 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -408,7 +408,7 @@ "info": true, "autoWidth": false, "responsive": true, - "order": [[9, "desc"]], + "order": [[13, "desc"]], "columns": [{ "data": "powerStationName" }, { @@ -416,11 +416,19 @@ }, { "data": "uid" }, { - "data": "typeName" + "data": "name" + },{ + "data": "enabledName" }, { + "data": "statusName" + },{ + "data": "typeName" + },{ "data": "brand" }, { "data": "productModel" + }, { + "data": "installDate" }, { "data": "dbName" }, { @@ -488,7 +496,7 @@ "info": true, "autoWidth": false, "responsive": true, - "order": [[9, "desc"]], + "order": [[13, "desc"]], "columns": [{ "data": "powerStationName" }, { @@ -496,11 +504,19 @@ }, { "data": "uid" }, { - "data": "typeName" + "data": "name" + },{ + "data": "enabledName" }, { + "data": "statusName" + },{ + "data": "typeName" + },{ "data": "brand" }, { "data": "productModel" + }, { + "data": "installDate" }, { "data": "dbName" }, { @@ -868,9 +884,11 @@ $("#Inverter_Pyrheliometer_modal").append($("