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; - } - } + } }