using Backend.Models; using Repository.BackendRepository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; namespace Backend.Services.Implement { /* * 暫時棄用 */ public class BackgroundService { private readonly IBackendRepository backendRepository; private List mode_list = new List() { "insert", "insert_list", "update", "update_list", "delete", "purge_all_insert", "purge" }; public BackgroundService(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } /// /// 加入至背景執行任務(單筆情況) /// /// /// /// /// /// /// /// public async Task AddTask(string building_ip = "", string building_guid = "", string target_table = "", string mode = "", Dictionary parameter = null, List fileInfos = null) { List temp_ips = new List(); if (!string.IsNullOrEmpty(building_ip)) { //有填寫IP,直接用 temp_ips.Add(building_ip); } else if (!string.IsNullOrEmpty(building_guid)) { //未填寫IP,抓取該棟別IP var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = building_guid }); temp_ips.Add(building.Ip_address_port); } else { //未填寫IP、棟別guid 代表全抓 var building_where = @"deleted = 0"; var buildInfos = await backendRepository.GetAllAsync("building", building_where); foreach(var build in buildInfos) { temp_ips.Add(build.Ip_address_port); } } if(temp_ips != null && temp_ips.Count > 0) { foreach(var temp_ip in temp_ips) { if (!string.IsNullOrEmpty(temp_ip) && !string.IsNullOrEmpty(target_table) && mode_list.Contains(mode.ToLower())) { Dictionary backgroundServiceTaskDic = new Dictionary() { { "@task_type", 1}, { "@target_ip", temp_ip}, { "@target_table", target_table}, { "@mode", mode.ToLower()}, { "@repeat_times", 0}, { "@is_complete", 0}, }; if (parameter != null) { var data_json = JsonSerializer.Serialize(parameter); if (!string.IsNullOrEmpty(data_json)) { backgroundServiceTaskDic.Add("@target_data", data_json); } } if (fileInfos != null && fileInfos.Count > 0) { var file_json = JsonSerializer.Serialize(fileInfos); if (!string.IsNullOrEmpty(file_json)) { backgroundServiceTaskDic.Add("@target_files", file_json); } } await backendRepository.AddOneByCustomTable(backgroundServiceTaskDic, "background_service_task"); } } } } /// /// 加入至背景執行任務(多筆情況) /// /// /// /// /// /// /// /// public async Task AddTask(string building_ip = "", string building_guid = "", string target_table = "", string mode = "", List> parameter = null, List fileInfos = null) { List temp_ips = new List(); if (!string.IsNullOrEmpty(building_ip)) { //有填寫IP,直接用 temp_ips.Add(building_ip); } else if (!string.IsNullOrEmpty(building_guid)) { //未填寫IP,抓取該棟別IP var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = building_guid }); temp_ips.Add(building.Ip_address_port); } else { //未填寫IP、棟別guid 代表全抓 var building_where = @"deleted = 0"; var buildInfos = await backendRepository.GetAllAsync("building", building_where); foreach (var build in buildInfos) { temp_ips.Add(build.Ip_address_port); } } if (temp_ips != null && temp_ips.Count > 0) { foreach (var temp_ip in temp_ips) { if (!string.IsNullOrEmpty(temp_ip) && !string.IsNullOrEmpty(target_table) && mode_list.Contains(mode.ToLower())) { Dictionary backgroundServiceTaskDic = new Dictionary() { { "@task_type", 1}, { "@target_ip", temp_ip}, { "@target_table", target_table}, { "@mode", mode.ToLower()}, { "@repeat_times", 0}, { "@is_complete", 0}, }; if (parameter != null) { var data_json = JsonSerializer.Serialize(parameter); if (!string.IsNullOrEmpty(data_json)) { backgroundServiceTaskDic.Add("@target_data", data_json); } } if (fileInfos != null && fileInfos.Count > 0) { var file_json = JsonSerializer.Serialize(fileInfos); if (!string.IsNullOrEmpty(file_json)) { backgroundServiceTaskDic.Add("@target_files", file_json); } } await backendRepository.AddOneByCustomTable(backgroundServiceTaskDic, "background_service_task"); } } } } } }