From 40396c8e8512ba94be4680538eae62dafbf355c9 Mon Sep 17 00:00:00 2001 From: b110212000 Date: Thu, 17 Jun 2021 14:31:23 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=8B=E7=B6=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PowerStationController.cs | 184 ++++++++++++++++++ SolarPower/Helper/DatabaseHelper.cs | 2 +- SolarPower/Models/PowerStation.cs | 22 +++ .../Implement/PowerStationRepository.cs | 110 +++++++++++ .../Repository/Implement/RepositoryBase.cs | 35 ++++ .../Interface/IPowerStationRepository.cs | 5 + .../Repository/Interface/IRepositoryBase.cs | 7 + .../PowerStation/PowerStationEdit.cshtml | 179 ++++++++++++++++- .../Views/PowerStation/_Operation.cshtml | 102 +++++----- SolarPower/Views/User/Index.cshtml | 4 +- 10 files changed, 602 insertions(+), 48 deletions(-) diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 540ee3f..8d7b8ae 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -216,5 +216,189 @@ 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; + } + } } 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 ac9c806..97f8c1c 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -78,4 +78,26 @@ namespace SolarPower.Models.PowerStation public string PhotovoltaicPanelSpecification { get; set; } //光電板規格 public int PhotovoltaicPanelAmount { get; set; } //光電板規格 } + + public class OperationInfo : Created + { + public int Id { get; set; } + public int PowerStationId { get; set; } + 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; } + } + 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; } + } + } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index c86f08e..30fe3e0 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Linq; +using System.Text; using System.Threading.Tasks; namespace SolarPower.Repository.Implement @@ -48,5 +49,114 @@ namespace SolarPower.Repository.Implement return result; } } + + 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; + } + } + + 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(); + } + } } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index ed47b6a..3eabe96 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 1673730..559dd5a 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -8,5 +8,10 @@ namespace SolarPower.Repository.Interface { public interface IPowerStationRepository : IRepositoryBase { + Task AddOperation(OperationInfo operation, List properties); + Task> OperationTable (int stationId); + Task OneOperationInfo (int stationId); + Task UpdateOperation(OperationInfo operation, List properties); + } } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index b83be3f..3c4f7af 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -58,5 +58,12 @@ 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 5a93d45..3397f01 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -86,11 +86,92 @@ @section Scripts{ } \ No newline at end of file diff --git a/SolarPower/Views/PowerStation/_Operation.cshtml b/SolarPower/Views/PowerStation/_Operation.cshtml index cf9f2f3..129e62e 100644 --- a/SolarPower/Views/PowerStation/_Operation.cshtml +++ b/SolarPower/Views/PowerStation/_Operation.cshtml @@ -1,64 +1,78 @@ 

運維資料

- + + 新增 +
- +
- + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
廠商類別 廠商 聯絡人 電話emailEmail 建立日期 建立人 功能
施工台達電林先生0928-123456lin@tdd.com.tw2021/06/02周杰倫 - -
清洗潔寶暴風女0928-654321storm@mavel.com2021/06/03周杰倫 - -
維運華碩雷神索爾0937-123123thor@asus.com2021/06/04周杰倫 - -
+ +
+
+ \ No newline at end of file diff --git a/SolarPower/Views/User/Index.cshtml b/SolarPower/Views/User/Index.cshtml index 2f474cc..5de7e2a 100644 --- a/SolarPower/Views/User/Index.cshtml +++ b/SolarPower/Views/User/Index.cshtml @@ -612,14 +612,14 @@ }); //#endregion - //#region 編輯系統管理員 + //#region 編輯使用者管理員 $('#user_table').on("click", "button.edit-btn", function () { $("#user-modal .modal-title").html("人員基本資料 - 編輯"); selected_id = $(this).parents('tr').attr('data-id'); - //取得單一系統管理員 + //取得單一使用者管理員 var url = "/User/GetOneUser/"; var send_data = {