diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 27ce2f4..7e7c324 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -72,6 +72,26 @@ namespace SolarPower.Controllers apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } + /// + /// 取裝置類型下拉選單 + /// + /// + public async Task>> GetDeviceTypeSelectOptionList() + { + ApiResult> apiResult = new ApiResult>(); + try + { + var userSelectItemLists = await powerStationRepository.DeviceType(); + apiResult.Code = "0000"; + apiResult.Data = userSelectItemLists; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + return apiResult; + } /// /// 取得縣市選單 @@ -615,6 +635,309 @@ namespace SolarPower.Controllers return apiResult; } + /// + /// 新增/修改 運維資料 + /// + /// + /// + public async Task> SaveOperation(OperationInfo post) + { + ApiResult apiResult = new ApiResult(); + try + { + if (post.Id == 0) + { + OperationInfo operation = new OperationInfo() + { + Id = post.Id, + Email = post.Email, + Name = post.Name, + Phone = post.Phone, + CreatedBy = myUser.Id, + ContactPerson = post.ContactPerson, + PowerStationId = post.PowerStationId, + Type = post.Type + }; + List properties = new List() + { + "Id", + "Email", + "Name", + "Phone", + "CreatedBy", + "ContactPerson", + "PowerStationId", + "Type" + }; + await powerStationRepository.AddOperation(operation, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + OperationInfo operation = new OperationInfo() + { + Id = post.Id, + Email = post.Email, + Name = post.Name, + Phone = post.Phone, + CreatedBy = myUser.Id, + ContactPerson = post.ContactPerson, + PowerStationId = post.PowerStationId, + Type = post.Type + }; + List properties = new List() + { + "Id", + "Email", + "Name", + "Phone", + "CreatedBy", + "ContactPerson", + "PowerStationId", + "Type" + }; + await powerStationRepository.UpdateOperation(operation, properties); + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + } + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + + return apiResult; + } + /// + /// 運維資料DataTable + /// + /// + /// + public async Task OperationTable(int stationId) + { + + List operationTable = new List(); + ApiResult> apiResult = new ApiResult>(); + try + { + apiResult.Code = "0000"; + operationTable = await powerStationRepository.OperationTable(stationId); + foreach(OperationTable a in operationTable) + { + a.Function = @" + + "; + if (a.Type == 0) + { + a.TypeName = "施工"; + } + else if(a.Type == 1) + { + a.TypeName = "清洗"; + } + else if (a.Type == 2) + { + a.TypeName = "運維"; + } + } + + apiResult.Data = operationTable; + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + var result = Json(new + { + data = apiResult + }); + + return result; + } + /// + /// 取得一筆 運維 資料 + /// + /// + /// + public async Task> GetOneOperation(int id) + { + OperationInfo operation = new OperationInfo(); + ApiResult apiResult = new ApiResult(); + try + { + apiResult.Code = "0000"; + operation = await powerStationRepository.OneOperationInfo(id); + apiResult.Data = operation; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + + return apiResult; + } + /// + /// 刪除 運維 資料 + /// + /// + /// + public async Task> DeleteOneOperation(int id) + { + ApiResult apiResult = new ApiResult(); + OperationInfo operation = new OperationInfo(); + try + { + operation = await powerStationRepository.OneOperationInfo(id); + + if (operation == null) + { + apiResult.Code = "9996"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + await powerStationRepository.DeleteOneOtherTable(operation.Id, "operation_firm"); + + apiResult.Code = "0000"; + apiResult.Msg = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 新增 / 修改 裝置資料 + /// + /// + /// + public async Task> SaveDevice(DeviceInfo Device) + { + ApiResult apiResult = new ApiResult(); + try + { + if (Device.Id == 0) + { + Device DeviceInfo = new Device() + { + Brand = Device.Brand, + ColName = Device.ColName, + PowerStationId = Device.PowerStationId, + DBName = Device.DBName, + Id = Device.Id, + Name = Device.Name, + ProductModel = Device.ProductModel, + Remark = Device.Remark, + TableName = Device.TableName, + Type = Device.Type, + UID = Device.PowerStationId + "-" + Device.Type, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "Brand", + "ColName", + "PowerStationId", + "DBName", + "Id", + "Name", + "ProductModel", + "Remark", + "TableName", + "Type", + "UID", + "CreatedBy" + }; + await powerStationRepository.AddDevice(DeviceInfo,properties); + + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + Device DeviceInfo = new Device() + { + Brand = Device.Brand, + ColName = Device.ColName, + PowerStationId = Device.PowerStationId, + DBName = Device.DBName, + Id = Device.Id, + Name = Device.Name, + ProductModel = Device.ProductModel, + Remark = Device.Remark, + TableName = Device.TableName, + Type = Device.Type, + UID = Device.PowerStationId + "-" + Device.Type, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "Brand", + "ColName", + "PowerStationId", + "DBName", + "Id", + "Name", + "ProductModel", + "Remark", + "TableName", + "Type", + "UID", + "CreatedBy" + }; + await powerStationRepository.UpdateDevice(DeviceInfo, properties); + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + } + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + return apiResult; + } + + public async Task DeviceTable(int stationId) + { + List deviceTables = new List(); + ApiResult> apiResult = new ApiResult>(); + try + { + apiResult.Code = "0000"; + deviceTables = await powerStationRepository.DeviceTable(stationId); + foreach (DeviceTable a in deviceTables) + { + a.Function = @" + + "; + } + apiResult.Data = deviceTables; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + var result = Json(new + { + data = apiResult + }); + return result; + } /// /// 軟刪除單一土地房屋資訊 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 57ede2d..67bce18 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -268,4 +268,68 @@ namespace SolarPower.Models.PowerStation public string ZipCode { get; set; } } + + public class OperationInfo : Created + { + public int Id { get; set; } + public int PowerStationId { get; set; }//電廠id + public int Type { get; set; }//廠商類別 + public string Name { get; set; }//名稱 + public string ContactPerson { get; set; }//聯絡人 + public string Phone { get; set; }//電話 + public string Email { get; set; }//Email + } + public class OperationStationId + { + public int stationId { get; set; } + } + public class OperationTable : OperationInfo + { + public string CreatedName { get; set; }//建立者名稱 + public string Function { get; set; }//功能 + public string TypeName { get; set; } + } + /// + /// 設備裝置下拉選單 + /// + public class Type + { + public string Name { get; set; } + public string EName { get; set; } + } + public class Root + { + public List Type { get; set; } + } + public class Variable + { + public string name { get; set; } + public string value { get; set; } + } + /// + /// 設備 + /// + public class DeviceInfo + { + public int Id { get; set; } + public int PowerStationId { get; set; }//所屬電站編號 + public string Name { get; set; }//名稱 + public string Type { get; set; }//類型 + public string Brand { get; set; }//廠牌 + public string ProductModel { get; set; }//型號 + public string DBName { get; set; } + public string TableName { get; set; } + public string ColName { get; set; } + public string Remark { get; set; } + } + public class Device : DeviceInfo + { + public string UID { get; set; }//設備編號 + public int CreatedBy { get; set; }//建立者 + } + public class DeviceTable : DeviceInfo + { + public string UID { get; set; }//設備編號 + public string Function { get; set; }//功能 + } } diff --git a/SolarPower/Models/User.cs b/SolarPower/Models/User.cs index 78477d7..c380ccd 100644 --- a/SolarPower/Models/User.cs +++ b/SolarPower/Models/User.cs @@ -10,7 +10,7 @@ namespace SolarPower.Models.User Suspend = 0, //停權 Normal = 1, //正常 } - + //Base Class。如由其餘需求,使用繼承 public class User : Created { diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 3e68f9b..badd5d8 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1,12 +1,15 @@ using Dapper; using SolarPower.Helper; using SolarPower.Models.PowerStation; +using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Data; using System.Linq; +using System.Text; using System.Threading.Tasks; +using System.Text.Json; namespace SolarPower.Repository.Implement { @@ -403,5 +406,241 @@ namespace SolarPower.Repository.Implement } } } + } + } + + public async Task AddOperation(OperationInfo operation, List properties) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + int count; + conn.Open(); + try + { + string sql = GenerateInsertQueryWithCustomTable(properties, "operation_firm"); + + count = await conn.ExecuteAsync(sql, operation); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return count; + } + } + /// + /// 運維DataTable + /// + /// + /// + public async Task> OperationTable (int stationId) + { + + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + List operation = new List(); + conn.Open(); + try + { + string sql = @$"SELECT operation_firm.Name, + operation_firm.PowerStationId, + operation_firm.Id,operation_firm.ContactPerson,operation_firm.Phone,operation_firm.Email,user.Name AS CreatedName,operation_firm.CreatedAt,operation_firm.Type + FROM operation_firm LEFT JOIN user ON operation_firm.CreatedBy = user.id WHERE operation_firm.Deleted = 0 AND operation_firm.PowerStationId = @StationId"; + operation = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return operation; + } + } + /// + /// 選取單一運維 + /// + /// + /// + public async Task OneOperationInfo (int id) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + OperationInfo operation; + conn.Open(); + try + { + string sql = @$"SELECT * FROM operation_firm WHERE Id = @Id"; + operation = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return operation; + } + } + + public async Task UpdateOperation(OperationInfo operation , List properties) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + var updateQuery = new StringBuilder($"UPDATE operation_firm SET "); + properties.ForEach(property => + { + if (!property.Equals("Id")) + { + updateQuery.Append($"{property}=@{property},"); + } + }); + updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma + updateQuery.Append(" WHERE id = @Id"); + await conn.ExecuteAsync(updateQuery.ToString(), operation, trans); + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + /// + /// 裝置類型下拉選單 + /// + /// + public async Task> DeviceType() + { + List result = new List(); + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + string sql = @$"SELECT * FROM variable WHERE name = @name"; + var json = await conn.QueryFirstOrDefaultAsync(sql, new { name = "Type" }); + Root jsonfor = JsonSerializer.Deserialize(json.value); + foreach(Models.PowerStation.Type a in jsonfor.Type) + { + UserSelectItemList KeyValue = new UserSelectItemList + { + Value = a.EName, + Text = a.Name + }; + result.Add(KeyValue); + } + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + /// + /// 新增裝置資料 + /// + /// + /// + /// + public async Task AddDevice(Device DeviceInfo, List properties) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + try + { + string sql = GenerateInsertQueryWithCustomTable(properties, "device"); + + await conn.ExecuteAsync(sql, DeviceInfo); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + } + /// + /// 修改裝置資料 + /// + /// + /// + /// + public async Task UpdateDevice(Device DeviceInfo, List properties) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + var updateQuery = GenerateUpdateQueryOtherTable(properties, "device"); + await conn.ExecuteAsync(updateQuery.ToString(), DeviceInfo, trans); + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + /// + /// 裝置dataTable + /// + /// + /// + public async Task> DeviceTable(int stationId) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + List Device = new List(); + try + { + string sql = @$"SELECT * FROM device WHERE Deleted = 0 AND PowerStationId = @StationId"; + Device = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return Device; + } + } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index d7f41d1..7f20ed9 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -87,6 +87,7 @@ namespace SolarPower.Repository.Implement return id; } } + /// /// 透過Id,軟刪除單一筆資料 @@ -121,6 +122,40 @@ namespace SolarPower.Repository.Implement } } } + /// + /// 透過Id,軟刪除單一筆資料(不同資料表) + /// + /// + /// + /// + public virtual async Task DeleteOneOtherTable(int id,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 id = @Id"; + + await conn.ExecuteAsync(sql, new { Id = id }, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + + } + } + } /// /// 取得所有資料 diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 1ba7ee1..d542640 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -1,4 +1,5 @@ using SolarPower.Models.PowerStation; +using SolarPower.Models.User; using System; using System.Collections.Generic; using System.Linq; @@ -97,5 +98,38 @@ namespace SolarPower.Repository.Interface /// /// Task DeleteOneLandBuildingInfo(int id); + Task AddOperation(OperationInfo operation, List properties); + /// + /// 運維dataTable + /// + /// + /// + Task> OperationTable (int stationId); + Task OneOperationInfo (int stationId); + Task UpdateOperation(OperationInfo operation, List properties); + /// + /// 裝置類型下拉式選單 + /// + /// + Task> DeviceType(); + /// + /// 新增 裝置 + /// + /// + /// + Task AddDevice(Device DeviceInfo, List properties); + /// + /// 修改 裝置 + /// + /// + /// + /// + Task UpdateDevice(Device DeviceInfo, List properties); + /// + /// 設備datatable + /// + /// + /// + Task> DeviceTable(int stationId); } } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index b83be3f..42f719d 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -58,5 +58,13 @@ namespace SolarPower.Repository.Interface /// /// Task PurgeOneAsync(int id); + /// + /// 透過Id,軟刪除單一筆資料(不同資料表) + /// + /// + /// + /// + Task DeleteOneOtherTable(int id, string tablename); + } } diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 8707093..9836ba5 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -92,11 +92,177 @@ var powerStationData; var selectedLandBuildingId; var isLandBuildingLock = false; + var selected_id = 0; $(function () { var url = new URL(location.href); stationId = url.searchParams.get('stationId'); + //#region 運維列表 DataTable + OperationTable = $("#Operation_table").DataTable({ + "paging": true, + "lengthChange": false, + "searching": false, + "ordering": true, + "info": true, + "autoWidth": false, + "responsive": true, + "order": [[7, "desc"]], + "columns": [{ + "data": "typeName" + }, { + "data": "name" + }, { + "data": "contactPerson" + }, { + "data": "phone" + }, { + "data": "email" + }, { + "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": "最後一頁" + }, + "aria": { + "sortAscending": ": 升冪排列", + "sortDescending": ": 降冪排列" + } + }, + 'createdRow': function (row, data, dataIndex) { + $(row).attr('data-id', data.id); + }, + "ajax": { + "url": "/PowerStation/OperationTable", + "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 = []; + } + + return data; + } + } + }); + //#endregion + //#region 運維列表 DataTable + DeviceTable = $("#Device_table").DataTable({ + "paging": true, + "lengthChange": false, + "searching": false, + "ordering": true, + "info": true, + "autoWidth": false, + "responsive": true, + "order": [[9, "desc"]], + "columns": [{ + "data": "uID" + }, { + "data": "name" + }, { + "data": "type" + }, { + "data": "brand" + }, { + "data": "productModel" + }, { + "data": "dBName" + }, { + "data": "tableName" + }, { + "data": "colName" + }, { + "data": "remark" + },{ + "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": "最後一頁" + }, + "aria": { + "sortAscending": ": 升冪排列", + "sortDescending": ": 降冪排列" + } + }, + 'createdRow': function (row, data, dataIndex) { + $(row).attr('data-id', data.id); + }, + "ajax": { + "url": "/PowerStation/DeviceTable", + "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 = []; + } + + return data; + } + } + }); + //#endregion //#region 電站資料 view 控制 if (stationId == 'new') { //#region 電站基本資料 @@ -201,7 +367,7 @@ $("#select_city").append($("