This commit is contained in:
dev02 2023-05-30 14:55:00 +08:00
commit 59cb26ba3d
4 changed files with 133 additions and 6 deletions

View File

@ -1,14 +1,18 @@
using FrontendWebApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Repository.BackendRepository.Interface;
using Repository.FrontendRepository.Interface;
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 +41,12 @@ namespace FrontendWebApi.ApiControllers
ApiResult<List<lightDevice>> apiResult = new ApiResult<List<lightDevice>>();
try
{
var floor_tag = await backendRepository.GetOneAsync<string>($@"
select full_name from floor where floor_guid = @floor_guid and deleted = 0
",new { floor_guid = post.floor_guid});
lightDevices = await backendRepository.GetAllAsync<lightDevice>($@"
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))
@ -79,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>()
@ -95,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)
@ -107,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>()
{
@ -124,6 +158,37 @@ namespace FrontendWebApi.ApiControllers
{ "@updated_by", myUser.userinfo_guid},
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
};
// 比較欄位
List<string> compareTargetProps = new List<string>() { "full_name", "week", "cycle", "floor_guid", "start_time", "end_time" };
List<string> compareTargetValues = new List<string>();
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<Dictionary<string, object>> ScheduleDevices = new List<Dictionary<string, object>>();
@ -138,6 +203,12 @@ namespace FrontendWebApi.ApiControllers
ScheduleDevices.Add(ScheduleDevice);
}
await backendRepository.AddMutiByCustomTable(ScheduleDevices, "schedule_device");
opeInput.parameter = JsonConvert.SerializeObject(saveSchedule);
// 若有變更才寫入 operation_log
if (saveSchedule.changeNames.Count > 0) {
await InsertOperation(opeInput);
}
}
apiResult.Code = "0000";
apiResult.Data = "成功";
@ -292,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

@ -155,5 +155,7 @@ namespace FrontendWebApi.ApiControllers
return apiResult;
}
}
}

View File

@ -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<string> devicelist { get; set; }
public List<string> changeNames { get; set; } = new List<string>();
}
public class ScheduleTable : Schedule
{
@ -44,7 +45,13 @@ namespace FrontendWebApi.Models
public List<string> Floors { get; set; }
}
public class ScheduleDevice
{
public int Id { get; set; }
public string light_schedule_guid { get; set; }
public string device_guid { get; set; }
}
}

View File

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