From ece045ab398bf1782e22f20b9a7ba86e8dd44df3 Mon Sep 17 00:00:00 2001 From: dev01 Date: Mon, 29 May 2023 18:47:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E7=87=88=E6=8E=A7=E6=8E=92=E7=A8=8B]=20op?= =?UTF-8?q?eration=5Flog=20=E7=B4=80=E9=8C=84=E7=87=88=E6=8E=A7=E6=8E=92?= =?UTF-8?q?=E7=A8=8B=E9=81=8B=E4=BD=9C=E5=8F=8A=E6=8E=A7=E5=88=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=AF=84=E5=9C=8D=E8=A8=98=E9=8C=84=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E9=A1=9E=E5=9E=8B=E7=A8=8B=E5=BA=8F=E5=BB=BA=E7=BD=AE=20|=20?= =?UTF-8?q?=E7=87=88=E6=8E=A7=E6=8E=92=E7=A8=8B=E9=83=A8=E5=88=86=E5=BE=8C?= =?UTF-8?q?=E7=AB=AF=E6=AD=A6=E6=96=B7=E6=94=B9=E5=85=AB=E6=AE=B5=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E8=AA=BF=E6=95=B4=20|=20=E9=83=A8=E5=88=86=20bug=20?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/LightScheduleController.cs | 60 ++++++++++++++++++- .../ApiControllers/UtilityController.cs | 26 ++++++++ FrontendWebApi/Models/LightSchedule.cs | 11 +++- FrontendWebApi/Models/Operation.cs | 23 ++++++- 4 files changed, 115 insertions(+), 5 deletions(-) diff --git a/FrontendWebApi/ApiControllers/LightScheduleController.cs b/FrontendWebApi/ApiControllers/LightScheduleController.cs index 846899b..c6d76d7 100644 --- a/FrontendWebApi/ApiControllers/LightScheduleController.cs +++ b/FrontendWebApi/ApiControllers/LightScheduleController.cs @@ -7,8 +7,11 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Linq.Expressions; using System.Net; using System.Net.Http; +using System.Reflection; +using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; @@ -37,8 +40,12 @@ namespace FrontendWebApi.ApiControllers ApiResult> apiResult = new ApiResult>(); try { + var floor_tag = await backendRepository.GetOneAsync($@" + select full_name from floor where floor_guid = @floor_guid and deleted = 0 + ",new { floor_guid = post.floor_guid}); + lightDevices = await backendRepository.GetAllAsync($@" - select * from device where building_guid = '{post.building_guid}' and sub_system_guid = '{post.sub_system_guid}' and floor_guid = '{post.floor_guid}' and deleted = 0 and status = 0 order by priority + select * from device where device_building_tag = '{post.building_tag}' and device_name_tag = '{post.sub_system_tag}' and device_floor_tag = '{floor_tag}' and deleted = 0 and status = 0 order by priority "); if(!String.IsNullOrEmpty(post.schedule_guid)) @@ -124,6 +131,52 @@ namespace FrontendWebApi.ApiControllers { "@updated_by", myUser.userinfo_guid}, { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} }; + + // Operation_log 輸入參數 + OperationInput opeInput = new OperationInput() { operation_type = 2, action_name = "修改" }; + // 取得對應樓層資料 + var targetFloor = await backendRepository.GetOneAsync($@" + select * from floor where floor_guid = '{saveSchedule.floor_guid}' + "); + // 取得對應燈控排程主表資料 + var targetScheduleLight = await backendRepository.GetOneAsync($@" + select * from light_schedule where light_schedule_guid = '{saveSchedule.light_schedule_guid}' + "); + // 取得對應燈控排程設備資料 + var targetScheduleDevice = await backendRepository.GetAllAsync($@" + select device_guid from schedule_device where light_schedule_guid = '{saveSchedule.light_schedule_guid}' + "); + opeInput.building_tag = targetFloor.building_tag; + opeInput.floor_tag = targetFloor.full_name; + // 比較欄位 + List compareTargetProps = new List() { "full_name", "week", "cycle", "floor_guid", "start_time", "end_time" }; + List compareTargetValues = new List(); + UtilityController utility = new UtilityController(backendRepository,frontendRepository); + Type modelType = saveSchedule.GetType(); + // 根據每個欄位比較 + foreach(var prop in compareTargetProps){ + PropertyInfo propertyInfo = modelType.GetProperty(prop); + if (propertyInfo == null) continue; + // 比較 saveSchedule 與 targetSchedule + var value = propertyInfo.GetValue(saveSchedule,null)?.ToString(); + var newValue = propertyInfo.GetValue(targetScheduleLight, null)?.ToString(); + // 只要不對就是排程變更 + if (value != newValue) { + saveSchedule.changeNames.Add("排程變更"); + break; + } + } + // 判斷是否為狀態變更 + if (targetScheduleLight.status != saveSchedule.status) { + saveSchedule.changeNames.Add("狀態變更"); + } + // 兩邊設備 guid 排序後比較 + saveSchedule.devicelist.Sort(); + targetScheduleDevice.Sort(); + if (saveSchedule.devicelist != targetScheduleDevice) { + saveSchedule.changeNames.Add("設備變更"); + } + await backendRepository.UpdateOneByCustomTable(Schedule, "light_schedule", $" light_schedule_guid = '{saveSchedule.light_schedule_guid}'"); await backendRepository.PurgeOneByGuidWithCustomDBNameAndTable("schedule_device", $" light_schedule_guid = '{saveSchedule.light_schedule_guid}'"); List> ScheduleDevices = new List>(); @@ -138,6 +191,11 @@ namespace FrontendWebApi.ApiControllers ScheduleDevices.Add(ScheduleDevice); } await backendRepository.AddMutiByCustomTable(ScheduleDevices, "schedule_device"); + + // 若有變更才寫入 operation_log + if (saveSchedule.changeNames.Count > 0) { + await utility.InsertOperation(opeInput); + } } apiResult.Code = "0000"; apiResult.Data = "成功"; diff --git a/FrontendWebApi/ApiControllers/UtilityController.cs b/FrontendWebApi/ApiControllers/UtilityController.cs index c0da082..85601c6 100644 --- a/FrontendWebApi/ApiControllers/UtilityController.cs +++ b/FrontendWebApi/ApiControllers/UtilityController.cs @@ -155,5 +155,31 @@ namespace FrontendWebApi.ApiControllers return apiResult; } + + public async Task InsertOperation(OperationInput input) { + try + { + //記錄使用者操作紀錄 + Dictionary userOperatorLog = new Dictionary() + { + { "@user_guid", myUser.userinfo_guid }, + { "@operation_type", input.operation_type }, //1:名稱修改 + { "@building_tag", input.building_tag }, + { "@main_system_tag", input.main_system_tag }, + { "@sub_system_tag", input.sub_system_tag }, + { "@floor_tag", input.floor_tag }, + { "@device_guid", input.device_guid }, + { "@action_name", input.action_name }, + { "@parameter", JsonConvert.SerializeObject(input.parameter) }, + }; + + await backendRepository.AddOneByCustomTable(userOperatorLog, "operation_log"); + + return true; + } + catch (Exception ex) { + return false; + } + } } } diff --git a/FrontendWebApi/Models/LightSchedule.cs b/FrontendWebApi/Models/LightSchedule.cs index c2f84eb..674457a 100644 --- a/FrontendWebApi/Models/LightSchedule.cs +++ b/FrontendWebApi/Models/LightSchedule.cs @@ -7,8 +7,8 @@ namespace FrontendWebApi.Models { public class GetDevicePost { - public string building_guid { get; set; } - public string sub_system_guid { get; set; } + public string building_tag { get; set; } + public string sub_system_tag { get; set; } public string floor_guid { get; set; } public string schedule_guid { get; set; } } @@ -33,6 +33,7 @@ namespace FrontendWebApi.Models public class SaveSchedule : Schedule { public List devicelist { get; set; } + public List changeNames { get; set; } = new List(); } public class ScheduleTable : Schedule { @@ -44,7 +45,13 @@ namespace FrontendWebApi.Models public List Floors { get; set; } } + public class ScheduleDevice + { + public int Id { get; set; } + public string light_schedule_guid { get; set; } + public string device_guid { get; set; } + } } diff --git a/FrontendWebApi/Models/Operation.cs b/FrontendWebApi/Models/Operation.cs index a11b0ae..887cb6c 100644 --- a/FrontendWebApi/Models/Operation.cs +++ b/FrontendWebApi/Models/Operation.cs @@ -84,7 +84,7 @@ namespace FrontendWebApi.Models public string notice { get; set; } public string description { get; set; } public string work_type_name - { + { get { Dictionary name = new Dictionary() @@ -118,7 +118,7 @@ namespace FrontendWebApi.Models } public class Operation_Record_File : Actor - { + { public int id { get; set; } public byte deleted { get; set; } public int record_id { get; set; } @@ -138,4 +138,23 @@ namespace FrontendWebApi.Models public DateTime? startdate { get; set; } public DateTime? enddate { get; set; } } + + public class OperationInput + { + public int id { get; set; } + public string user_guid { get; set;} + public string building_tag { get; set; } + public string main_system_tag { get; set;} + public string sub_system_tag { get; set;} + public string floor_tag { get; set;} + public string device_guid { get; set;} + public short operation_type { get; set;} + public string parameter { get; set;} + public string action_name { get; set;} + public string value { get; set;} + public DateTime? created_at { get; set;} + + } + + } From eb0f45ac832fee537d2aecde9afeb50ccbcd5f39 Mon Sep 17 00:00:00 2001 From: dev01 Date: Tue, 30 May 2023 14:36:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[=E7=87=88=E6=8E=A7=E6=8E=92=E7=A8=8B]=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E/=E7=B7=A8=E8=BC=AF=20=E7=B4=80=E9=8C=84=20Op?= =?UTF-8?q?eration=5Flog=20=E5=BE=8C=E7=AB=AF=E7=A8=8B=E5=BA=8F=E8=AA=BF?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/LightScheduleController.cs | 79 ++++++++++++++----- .../ApiControllers/UtilityController.cs | 26 +----- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/FrontendWebApi/ApiControllers/LightScheduleController.cs b/FrontendWebApi/ApiControllers/LightScheduleController.cs index c6d76d7..411cb19 100644 --- a/FrontendWebApi/ApiControllers/LightScheduleController.cs +++ b/FrontendWebApi/ApiControllers/LightScheduleController.cs @@ -1,6 +1,7 @@ using FrontendWebApi.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Repository.BackendRepository.Interface; using Repository.FrontendRepository.Interface; using System; @@ -86,8 +87,29 @@ namespace FrontendWebApi.ApiControllers ApiResult apiResult = new ApiResult(); try { - if(String.IsNullOrEmpty(saveSchedule.light_schedule_guid)) + // Operation_log 輸入參數 + OperationInput opeInput = new OperationInput() { operation_type = 2 }; + + // 取得對應樓層資料 + var targetFloor = await backendRepository.GetOneAsync($@" + select * from floor where floor_guid = @floor_guid", + new { floor_guid = saveSchedule.floor_guid}); + // 取得對應燈控排程主表資料 + var targetScheduleLight = await backendRepository.GetOneAsync($@" + select * from light_schedule where light_schedule_guid = @light_schedule_guid", + new { light_schedule_guid = saveSchedule.light_schedule_guid }); + // 取得對應燈控排程設備資料 + var targetScheduleDevice = await backendRepository.GetAllAsync($@" + select device_guid from schedule_device where light_schedule_guid = @light_schedule_guid", + new { light_schedule_guid = saveSchedule.light_schedule_guid }); + + + opeInput.building_tag = targetFloor.building_tag; + opeInput.floor_tag = targetFloor.full_name; + + if (String.IsNullOrEmpty(saveSchedule.light_schedule_guid)) { + opeInput.action_name = "新增"; Dictionary Schedule = new Dictionary(); var newguid = Guid.NewGuid(); Schedule = new Dictionary() @@ -102,6 +124,7 @@ namespace FrontendWebApi.ApiControllers { "@end_time", saveSchedule.end_time}, { "@created_by", myUser.userinfo_guid} }; + await backendRepository.AddOneByCustomTable(Schedule, "light_schedule"); List> ScheduleDevices = new List>(); foreach (var a in saveSchedule.devicelist) @@ -114,10 +137,14 @@ namespace FrontendWebApi.ApiControllers }; ScheduleDevices.Add(ScheduleDevice); } + opeInput.parameter = JsonConvert.SerializeObject(saveSchedule); + await backendRepository.AddMutiByCustomTable(ScheduleDevices, "schedule_device"); + await InsertOperation(opeInput); } else { + opeInput.action_name = "修改"; Dictionary Schedule = new Dictionary(); Schedule = new Dictionary() { @@ -132,26 +159,10 @@ namespace FrontendWebApi.ApiControllers { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} }; - // Operation_log 輸入參數 - OperationInput opeInput = new OperationInput() { operation_type = 2, action_name = "修改" }; - // 取得對應樓層資料 - var targetFloor = await backendRepository.GetOneAsync($@" - select * from floor where floor_guid = '{saveSchedule.floor_guid}' - "); - // 取得對應燈控排程主表資料 - var targetScheduleLight = await backendRepository.GetOneAsync($@" - select * from light_schedule where light_schedule_guid = '{saveSchedule.light_schedule_guid}' - "); - // 取得對應燈控排程設備資料 - var targetScheduleDevice = await backendRepository.GetAllAsync($@" - select device_guid from schedule_device where light_schedule_guid = '{saveSchedule.light_schedule_guid}' - "); - opeInput.building_tag = targetFloor.building_tag; - opeInput.floor_tag = targetFloor.full_name; // 比較欄位 List compareTargetProps = new List() { "full_name", "week", "cycle", "floor_guid", "start_time", "end_time" }; List compareTargetValues = new List(); - UtilityController utility = new UtilityController(backendRepository,frontendRepository); + Type modelType = saveSchedule.GetType(); // 根據每個欄位比較 foreach(var prop in compareTargetProps){ @@ -170,6 +181,7 @@ namespace FrontendWebApi.ApiControllers if (targetScheduleLight.status != saveSchedule.status) { saveSchedule.changeNames.Add("狀態變更"); } + // 兩邊設備 guid 排序後比較 saveSchedule.devicelist.Sort(); targetScheduleDevice.Sort(); @@ -192,9 +204,10 @@ namespace FrontendWebApi.ApiControllers } await backendRepository.AddMutiByCustomTable(ScheduleDevices, "schedule_device"); + opeInput.parameter = JsonConvert.SerializeObject(saveSchedule); // 若有變更才寫入 operation_log if (saveSchedule.changeNames.Count > 0) { - await utility.InsertOperation(opeInput); + await InsertOperation(opeInput); } } apiResult.Code = "0000"; @@ -350,5 +363,33 @@ namespace FrontendWebApi.ApiControllers } return Ok(apiResult); } + + public async Task InsertOperation(OperationInput input) + { + try + { + //記錄使用者操作紀錄 + Dictionary userOperatorLog = new Dictionary() + { + { "@user_guid", myUser.userinfo_guid }, + { "@operation_type", input.operation_type }, //1:名稱修改 + { "@building_tag", input.building_tag }, + { "@main_system_tag", input.main_system_tag }, + { "@sub_system_tag", input.sub_system_tag }, + { "@floor_tag", input.floor_tag }, + { "@device_guid", input.device_guid }, + { "@action_name", input.action_name }, + { "@parameter", JsonConvert.SerializeObject(input.parameter) }, + }; + + await backendRepository.AddOneByCustomTable(userOperatorLog, "operation_log"); + + return true; + } + catch (Exception ex) + { + return false; + } + } } } diff --git a/FrontendWebApi/ApiControllers/UtilityController.cs b/FrontendWebApi/ApiControllers/UtilityController.cs index 85601c6..7d2711f 100644 --- a/FrontendWebApi/ApiControllers/UtilityController.cs +++ b/FrontendWebApi/ApiControllers/UtilityController.cs @@ -156,30 +156,6 @@ namespace FrontendWebApi.ApiControllers return apiResult; } - public async Task InsertOperation(OperationInput input) { - try - { - //記錄使用者操作紀錄 - Dictionary userOperatorLog = new Dictionary() - { - { "@user_guid", myUser.userinfo_guid }, - { "@operation_type", input.operation_type }, //1:名稱修改 - { "@building_tag", input.building_tag }, - { "@main_system_tag", input.main_system_tag }, - { "@sub_system_tag", input.sub_system_tag }, - { "@floor_tag", input.floor_tag }, - { "@device_guid", input.device_guid }, - { "@action_name", input.action_name }, - { "@parameter", JsonConvert.SerializeObject(input.parameter) }, - }; - - await backendRepository.AddOneByCustomTable(userOperatorLog, "operation_log"); - - return true; - } - catch (Exception ex) { - return false; - } - } + } }