From 4e5ac55c2ee0ff35d37b73bf58dfd3fc10e3459f Mon Sep 17 00:00:00 2001 From: dev02 Date: Tue, 1 Nov 2022 10:49:22 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=89=8D=E5=8F=B0]=20=E6=96=B0=E5=A2=9Ecreate?= =?UTF-8?q?,=20update,=20list,=20delete=E5=9C=96=E8=B3=87=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FrontendWebApi/ApiControllers/GraphManage.cs | 332 ++++++++++++++++++ .../ApiControllers/MyBaseApiController.cs | 2 + FrontendWebApi/Models/MyBase.cs | 1 + Repository/Models/GraphManage.cs | 41 +++ 4 files changed, 376 insertions(+) create mode 100644 FrontendWebApi/ApiControllers/GraphManage.cs create mode 100644 Repository/Models/GraphManage.cs diff --git a/FrontendWebApi/ApiControllers/GraphManage.cs b/FrontendWebApi/ApiControllers/GraphManage.cs new file mode 100644 index 0000000..c6a20c0 --- /dev/null +++ b/FrontendWebApi/ApiControllers/GraphManage.cs @@ -0,0 +1,332 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Repository.BackendRepository.Implement; +using System.Collections.Generic; +using System.Threading.Tasks; +using System; +using System.Linq; +using FrontendWebApi.Models; +using Repository.BackendRepository.Interface; +using Repository.FrontendRepository.Interface; +using static Repository.Models.GraphManage; +using NPOI.SS.UserModel; +using Microsoft.Extensions.Hosting; +using System.IO; + +namespace FrontendWebApi.ApiControllers +{ + [Route("api/[controller]")] + [ApiController] + public class GraphManageController : MyBaseApiController + { + private readonly IBackendRepository backendRepository; + private readonly IFrontendRepository frontendRepository; + private string graph_manage_layer1 = "graph_manage_layer1"; + private string graph_manage_layer2 = "graph_manage_layer2"; + + private string graphManageFileSaveAsPath = ""; + + public GraphManageController(IBackendRepository backendRepository, IFrontendRepository frontendRepository) + { + this.backendRepository = backendRepository; + this.frontendRepository = frontendRepository; + graphManageFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "graph_manage"); + } + + [HttpPost] + public async Task>> MaiSysList() + { + ApiResult> apiResult = new ApiResult>(); + List main_system_list = new List(); + + try + { + var sqlString = @$"SELECT * + FROM variable + WHERE system_type = @graph_manage_layer1 AND deleted = 0 + ORDER BY system_priority, created_at desc"; + + var param = new { @graph_manage_layer1 = graph_manage_layer1 }; + main_system_list = await backendRepository.GetAllAsync(sqlString, param); + + apiResult.Code = "0000"; + apiResult.Data = main_system_list; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task>> SubSysList(int main_system_id) + { + ApiResult> apiResult = new ApiResult>(); + List sub_system_list = new List(); + + try + { + var sqlString = @$"SELECT v2.* + FROM variable v2 + JOIN variable v1 ON v2.system_parent_id = v1.id AND v1.system_type = @graph_manage_layer1 AND v1.deleted = 0 + WHERE v2.system_type = @graph_manage_layer2 AND v2.deleted = 0 AND v1.id = @main_system_id + ORDER BY v2.system_priority, v2.created_at desc"; + + var param = new { @graph_manage_layer1 = graph_manage_layer1, @graph_manage_layer2 = graph_manage_layer2 }; + sub_system_list = await backendRepository.GetAllAsync(sqlString, param); + + apiResult.Code = "0000"; + apiResult.Data = sub_system_list; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + + } + [HttpPost] + public async Task>> GraManList(GraphInfo gi) + { + ApiResult> apiResult = new ApiResult>(); + List graManList = new List(); + + try + { + var sqlString = @$"SELECT * + FROM graph_manage gm + JOIN variable v2 ON gm.sub_system_id = v2.id AND v2.system_type = @graph_manage_layer2 AND v2.deleted = 0 + WHERE v2.id in @sub_system_id AND gm.deleted = 0 + ORDER BY gm.priority, gm.created_at desc"; + + + var param = new { @graph_manage_layer1 = graph_manage_layer1, graph_manage_layer2 = graph_manage_layer2, @sub_system_id = gi.sub_system_id }; + + graManList = await backendRepository.GetAllAsync(sqlString, param); + + if (gi.keyWord != null) + { + var wParam = new { @graph_manage_layer1 = graph_manage_layer1, graph_manage_layer2 = graph_manage_layer2, @sub_system_id = gi.sub_system_id, @keyWord = gi.keyWord }; + graManList = await backendRepository.GetAllAsync(@$"SELECT * + FROM graph_manage gm + JOIN variable v2 ON gm.sub_system_id = v2.id AND v2.system_type = @graph_manage_layer2 AND v2.deleted = 0 + WHERE v2.id in @sub_system_id AND gm.deleted = 0 + AND (code like '%@keyWord%' OR name like '%@keyWord%' OR oriOrgName like '%@keyWord%' OR donOrgName like '%@keyWord%') + ORDER BY gm.priority, gm.created_at desc", wParam); + } + + apiResult.Code = "0000"; + apiResult.Data = graManList; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> DelOneGraMan(GraphInfo gi) + { + ApiResult apiResult = new ApiResult(); + + try + { + var sqlString = @$"UPDATE graph_manage SET deleted = 0 WHERE code = @code AND sub_system_id = @sub_system_id"; + + var param = new { @code = gi.code, @sub_system_id = gi.sub_system_id }; + + await backendRepository.ExecuteSql(sqlString, param); + apiResult.Code = "0000"; + apiResult.Data = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> EdtOneGraMan([FromForm] GraphInsInfo gii) + { + ApiResult apiResult = new ApiResult(); + + try + { + var sWhere = @$"deleted = 0 AND code = @code AND sub_system_id = @sub_system_id"; + var gm = await backendRepository.GetOneAsync("graph_manage", sWhere, new { @code = gii.code, @sub_system_id = gii.sub_system_id}); + + if (gm == null) + { + apiResult.Code = "0001"; + apiResult.Data = "無法找到圖資"; + return apiResult; + } + + Dictionary graph_manage = new Dictionary(); + + //edit file + if (gii.oriOrgName != null || gii.donOrgName != null) + { + if (!System.IO.Directory.Exists(graphManageFileSaveAsPath)) + System.IO.Directory.CreateDirectory(graphManageFileSaveAsPath); + + if (gm.oriSavName != null && gii.oriOrgName != null) + { + var new_guid = Guid.NewGuid(); + + //刪除原本檔案 + FolderFunction folderFunction = new FolderFunction(); + folderFunction.DeleteFile(Path.Combine(graphManageFileSaveAsPath, gm.oriSavName)); + + var fileName = new_guid + "." + gii.oriOrgName.Split('.')[1]; + + var fullPath = Path.Combine(graphManageFileSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + gii.oriFile.CopyTo(stream); + } + + graph_manage.Add("@oriOrgName", gii.oriOrgName); + graph_manage.Add("@oriSavName", fileName); + } + + if (gm.donSavName != null && gii.donOrgName != null) + { + var new_guid = Guid.NewGuid(); + + //刪除原本檔案 + FolderFunction folderFunction = new FolderFunction(); + folderFunction.DeleteFile(Path.Combine(graphManageFileSaveAsPath, gm.donSavName)); + + var fileName = new_guid + "." + gii.donOrgName.Split('.')[1]; + + var fullPath = Path.Combine(graphManageFileSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + gii.donFile.CopyTo(stream); + } + + graph_manage.Add("@oriOrgName", gii.donOrgName); + graph_manage.Add("@oriSavName", fileName); + } + } + + graph_manage.Add("@name", gii.name); + //graph_manage.Add("@priority", gii.priority); + + await backendRepository.UpdateOneByCustomTable(graph_manage, "graph_manage", "code='" + gii.code + "'" + "sub_system_id ='" + gii.sub_system_id + "'"); + apiResult.Code = "0000"; + apiResult.Data = "修改成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + string json = System.Text.Json.JsonSerializer.Serialize(gii); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> SaveGraMan([FromForm] GraphInsInfo gii) + { + ApiResult apiResult = new ApiResult(); + + try + { + var sWhere = @$"deleted = 0 AND code = @code AND sub_system_id = @sub_system_id"; + var gm = await backendRepository.GetOneAsync("graph_manage", sWhere, new { @code = gii.code, @sub_system_id = gii.sub_system_id }); + + if (gm != null) + { + apiResult.Code = "0002"; + apiResult.Data = "已有相同的編碼"; + return apiResult; + } + + Dictionary graph_manage = new Dictionary(); + + //save file + if (gii.oriOrgName != null || gii.donOrgName != null) + { + if (!System.IO.Directory.Exists(graphManageFileSaveAsPath)) + System.IO.Directory.CreateDirectory(graphManageFileSaveAsPath); + + if (gii.oriOrgName != null) + { + var new_guid = Guid.NewGuid(); + + var fileName = new_guid + "." + gii.oriOrgName.Split('.')[1]; + + var fullPath = Path.Combine(graphManageFileSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + gii.oriFile.CopyTo(stream); + } + + graph_manage.Add("@oriOrgName", gii.oriOrgName); + graph_manage.Add("@oriSavName", fileName); + } + + if (gii.donOrgName != null) + { + var new_guid = Guid.NewGuid(); + + var fileName = new_guid + "." + gii.donOrgName.Split('.')[1]; + + var fullPath = Path.Combine(graphManageFileSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + gii.donFile.CopyTo(stream); + } + + graph_manage.Add("@oriOrgName", gii.donOrgName); + graph_manage.Add("@oriSavName", fileName); + } + } + + var newPriority = await backendRepository.GetCurrentPriority("graph_manage"); + graph_manage.Add("@name", gii.name); + graph_manage.Add("@deleted", 0); + graph_manage.Add("@priority", newPriority + 1); + + await backendRepository.AddOneByCustomTable(graph_manage, "graph_manage"); + apiResult.Code = "0000"; + apiResult.Data = "新增成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + string json = System.Text.Json.JsonSerializer.Serialize(gii); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + } +} diff --git a/FrontendWebApi/ApiControllers/MyBaseApiController.cs b/FrontendWebApi/ApiControllers/MyBaseApiController.cs index e0c665d..355e79d 100644 --- a/FrontendWebApi/ApiControllers/MyBaseApiController.cs +++ b/FrontendWebApi/ApiControllers/MyBaseApiController.cs @@ -30,6 +30,8 @@ namespace FrontendWebApi.ApiControllers protected bool jwtlife = true; public string controllerName; public string actionName; + public string main_system_type = "device_system_category_layer2"; + public string sub_system_type = "device_system_category_layer3"; public ErrorCode errorCode = new ErrorCode(); [Authorize] public override void OnActionExecuting(ActionExecutingContext filterContext) diff --git a/FrontendWebApi/Models/MyBase.cs b/FrontendWebApi/Models/MyBase.cs index 1f880d3..4a7916f 100644 --- a/FrontendWebApi/Models/MyBase.cs +++ b/FrontendWebApi/Models/MyBase.cs @@ -48,6 +48,7 @@ namespace FrontendWebApi.Models public class Variable : Actor { + public int id { get; set; } public string System_type { get; set; } public string System_key { get; set; } public string system_value { get; set; } diff --git a/Repository/Models/GraphManage.cs b/Repository/Models/GraphManage.cs new file mode 100644 index 0000000..f86b6e6 --- /dev/null +++ b/Repository/Models/GraphManage.cs @@ -0,0 +1,41 @@ +using Microsoft.AspNetCore.Http; +using System.Collections.Generic; +using System.Security; + +namespace Repository.Models +{ + public class GraphManage + { + public class GraphList + { + public string code { get; set; } + public int sub_system_id { get; set; } + public string name { get; set; } + public string oriOrgName { get; set; } + public string oriSavName { get; set; } + public string donOrgName { get; set; } + public string donSavName { get; set; } + } + + public class GraphInsInfo + { + public string code { get; set; } + public int sub_system_id { get; set; } + public string name { get; set; } + public string oriOrgName { get; set; } + public string oriSavName { get; set; } + public string donOrgName { get; set; } + public string donSavName { get; set; } + public int priority { get; set; } + public IFormFile oriFile { get; set; } + public IFormFile donFile { get; set; } + } + + public class GraphInfo + { + public string code { get; set; } + public List sub_system_id { get; set; } + public string keyWord { get; set; } + } + } +}