[燈控排程] 新增/編輯 紀錄 Operation_log 後端程序調整

This commit is contained in:
dev01 2023-05-30 14:36:50 +08:00
parent ed35146547
commit eb0f45ac83
2 changed files with 61 additions and 44 deletions

View File

@ -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<string> apiResult = new ApiResult<string>();
try
{
// Operation_log 輸入參數
OperationInput opeInput = new OperationInput() { operation_type = 2 };
// 取得對應樓層資料
var targetFloor = await backendRepository.GetOneAsync<Floor>($@"
select * from floor where floor_guid = @floor_guid",
new { floor_guid = saveSchedule.floor_guid});
// 取得對應燈控排程主表資料
var targetScheduleLight = await backendRepository.GetOneAsync<SaveSchedule>($@"
select * from light_schedule where light_schedule_guid = @light_schedule_guid",
new { light_schedule_guid = saveSchedule.light_schedule_guid });
// 取得對應燈控排程設備資料
var targetScheduleDevice = await backendRepository.GetAllAsync<string>($@"
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<string, object> Schedule = new Dictionary<string, object>();
var newguid = Guid.NewGuid();
Schedule = new Dictionary<string, object>()
@ -102,6 +124,7 @@ namespace FrontendWebApi.ApiControllers
{ "@end_time", saveSchedule.end_time},
{ "@created_by", myUser.userinfo_guid}
};
await backendRepository.AddOneByCustomTable(Schedule, "light_schedule");
List<Dictionary<string, object>> ScheduleDevices = new List<Dictionary<string, object>>();
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<string, object> Schedule = new Dictionary<string, object>();
Schedule = new Dictionary<string, object>()
{
@ -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<Floor>($@"
select * from floor where floor_guid = '{saveSchedule.floor_guid}'
");
// 取得對應燈控排程主表資料
var targetScheduleLight = await backendRepository.GetOneAsync<SaveSchedule>($@"
select * from light_schedule where light_schedule_guid = '{saveSchedule.light_schedule_guid}'
");
// 取得對應燈控排程設備資料
var targetScheduleDevice = await backendRepository.GetAllAsync<string>($@"
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<string> compareTargetProps = new List<string>() { "full_name", "week", "cycle", "floor_guid", "start_time", "end_time" };
List<string> compareTargetValues = new List<string>();
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<bool> InsertOperation(OperationInput input)
{
try
{
//記錄使用者操作紀錄
Dictionary<string, object> userOperatorLog = new Dictionary<string, object>()
{
{ "@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;
}
}
}
}

View File

@ -156,30 +156,6 @@ namespace FrontendWebApi.ApiControllers
return apiResult;
}
public async Task<bool> InsertOperation(OperationInput input) {
try
{
//記錄使用者操作紀錄
Dictionary<string, object> userOperatorLog = new Dictionary<string, object>()
{
{ "@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;
}
}
}
}