diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 6a75b6b..d504cb0 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -829,6 +829,18 @@ namespace SolarPower.Controllers { if (Device.Id == 0) { + PowerStation powerStation = null; + powerStation = await powerStationRepository.GetOneAsync(Device.PowerStationId); + string Number = await powerStationRepository.GetFinalSerialNumber(Device.PowerStationId, Device.Type); + var tempSerialNumber = 0; + if (!string.IsNullOrEmpty(Number)) + { + tempSerialNumber = Convert.ToInt32(Number) + 1; + } + else + { + tempSerialNumber = 1; + } Device DeviceInfo = new Device() { Brand = Device.Brand, @@ -841,9 +853,10 @@ namespace SolarPower.Controllers Remark = Device.Remark, TableName = Device.TableName, Type = Device.Type, - UID = Device.PowerStationId + "-" + Device.Type, + UID = powerStation.Code + "-" + Device.Type + "-" + tempSerialNumber.ToString().PadLeft(3,'0'), CreatedBy = myUser.Id, - TypeName = Device.TypeName + TypeName = Device.TypeName, + SerialNumber = tempSerialNumber.ToString().PadLeft(3, '0') }; List properties = new List() { @@ -859,7 +872,8 @@ namespace SolarPower.Controllers "Type", "UID", "CreatedBy", - "TypeName" + "TypeName", + "SerialNumber" }; await powerStationRepository.AddDevice(DeviceInfo,properties); @@ -878,9 +892,6 @@ namespace SolarPower.Controllers Name = Device.Name, ProductModel = Device.ProductModel, Remark = Device.Remark, - TableName = Device.TableName, - Type = Device.Type, - UID = Device.PowerStationId + "-" + Device.Type, CreatedBy = myUser.Id, TypeName = Device.TypeName }; @@ -894,9 +905,6 @@ namespace SolarPower.Controllers "Name", "ProductModel", "Remark", - "TableName", - "Type", - "UID", "CreatedBy", "TypeName" }; diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index e596ec7..8c950e2 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -819,6 +819,7 @@ INSERT INTO `variable` (`id`, `name`, `value`, `remark`) VALUES /****************************** ** 後續SQL修改從以下開始新增 ** *******************************/ +ALTER TABLE `device` ADD COLUMN `SerialNumber` VARCHAR(3) NULL DEFAULT NULL COMMENT '單一設備流水號' COLLATE 'utf8mb4_unicode_ci' AFTER `PowerStationId`;/* 設備流水號 - 2021/6/18 12:21:00 */ diff --git a/SolarPower/Helper/DatabaseHelper.cs b/SolarPower/Helper/DatabaseHelper.cs index b359893..2e57aa3 100644 --- a/SolarPower/Helper/DatabaseHelper.cs +++ b/SolarPower/Helper/DatabaseHelper.cs @@ -37,7 +37,7 @@ namespace SolarPower.Helper var passwordStr = ed.DESDecrypt(dbConfig.Password); //var connStr = $"server={serverStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;"; - var connStr = @"server=127.0.0.1;database=solar_power;user=root;password=000000;charset=utf8;"; + var connStr = @"server=127.0.0.1;port=3308;database=solar_power;user=root;password=00000000;charset=utf8;"; this._connectionString = connStr; } diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index e18f2e7..96b733a 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -330,6 +330,7 @@ namespace SolarPower.Models.PowerStation { public string UID { get; set; }//設備編號 public int CreatedBy { get; set; }//建立者 + public string SerialNumber { get; set; } } /// ///設備dataTable diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index f7ddb09..1f9c4b4 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -786,5 +786,28 @@ namespace SolarPower.Repository.Implement 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; + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 19548d4..bd3e555 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -181,5 +181,12 @@ namespace SolarPower.Repository.Interface /// /// Task UpdateException(ExceptionModal Exception, List properties); + /// + /// 取最後一個設備流水號 + /// + /// + /// + /// + Task GetFinalSerialNumber(int PowerStationId, string Type); } } diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 52d63f9..4d0c57b 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -1471,7 +1471,7 @@ toast_error(rel.msg); return; } - + document.getElementById('Device_Type_modal').disabled = true; $("#Device_Name_modal").val(rel.data.name); $("#Device_Type_modal").val(rel.data.type); $("#Device_Brand_modal").val(rel.data.brand); diff --git a/SolarPower/Views/PowerStation/_DeviceSetting.cshtml b/SolarPower/Views/PowerStation/_DeviceSetting.cshtml index 5922416..8f58788 100644 --- a/SolarPower/Views/PowerStation/_DeviceSetting.cshtml +++ b/SolarPower/Views/PowerStation/_DeviceSetting.cshtml @@ -79,7 +79,7 @@
- +