2022-11-01 19:15:02 +08:00
using FrontendWebApi.Models ;
using Microsoft.AspNetCore.Http ;
2022-11-01 10:49:22 +08:00
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 Repository.BackendRepository.Interface ;
using Repository.FrontendRepository.Interface ;
using System.IO ;
namespace FrontendWebApi.ApiControllers
{
2022-11-09 11:06:17 +08:00
//[Route("api/[controller]")]
//[ApiController]
2022-11-01 10:49:22 +08:00
public class GraphManageController : MyBaseApiController < GraphManageController >
{
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 < ApiResult < List < Variable > > > MaiSysList ( )
{
ApiResult < List < Variable > > apiResult = new ApiResult < List < Variable > > ( ) ;
List < Variable > main_system_list = new List < Variable > ( ) ;
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 < Variable > ( 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]
2022-11-09 11:06:17 +08:00
public async Task < ApiResult < List < Variable > > > SubSysList ( [ FromBody ] int main_system_tag )
2022-11-01 10:49:22 +08:00
{
ApiResult < List < Variable > > apiResult = new ApiResult < List < Variable > > ( ) ;
List < Variable > sub_system_list = new List < Variable > ( ) ;
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
2022-11-04 09:41:50 +08:00
WHERE v2 . system_type = @graph_manage_layer2 AND v2 . deleted = 0 AND v1 . system_value = @main_system_tag
2022-11-01 10:49:22 +08:00
ORDER BY v2 . system_priority , v2 . created_at desc ";
2022-11-04 09:41:50 +08:00
var param = new { @graph_manage_layer1 = graph_manage_layer1 , @graph_manage_layer2 = graph_manage_layer2 , @main_system_tag = main_system_tag } ;
2022-11-01 10:49:22 +08:00
sub_system_list = await backendRepository . GetAllAsync < Variable > ( 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 ;
}
2022-11-09 15:22:18 +08:00
2022-11-01 10:49:22 +08:00
[HttpPost]
2022-11-09 11:06:17 +08:00
public async Task < ApiResult < List < GraphList > > > GraManList ( [ FromBody ] GraphInfo gi )
2022-11-01 10:49:22 +08:00
{
ApiResult < List < GraphList > > apiResult = new ApiResult < List < GraphList > > ( ) ;
List < GraphList > graManList = new List < GraphList > ( ) ;
try
{
var sqlString = @ $ "SELECT *
FROM graph_manage gm
2022-11-04 09:41:50 +08:00
JOIN variable v1 ON v1 . system_value = @main_system_tag and v1 . system_type = @graph_manage_layer1 and v1 . deleted = 0
JOIN variable v2 ON v1 . id = v2 . system_parent_id AND v2 . system_type = @graph_manage_layer2 AND v2 . deleted = 0
WHERE v2 . system_value in @sub_system_tag AND gm . deleted = 0
2022-11-01 10:49:22 +08:00
ORDER BY gm . priority , gm . created_at desc ";
2022-11-04 09:41:50 +08:00
var param = new { @graph_manage_layer1 = graph_manage_layer1 , graph_manage_layer2 = graph_manage_layer2 , @sub_system_tag = gi . sub_system_tag , @main_system_tag = gi . main_system_tag } ;
2022-11-01 10:49:22 +08:00
graManList = await backendRepository . GetAllAsync < GraphList > ( sqlString , param ) ;
if ( gi . keyWord ! = null )
{
2022-11-04 09:41:50 +08:00
var wParam = new { @graph_manage_layer1 = graph_manage_layer1 , graph_manage_layer2 = graph_manage_layer2 , @sub_system_tag = gi . sub_system_tag , @main_system_tag = gi . main_system_tag , @keyWord = gi . keyWord } ;
2022-11-01 10:49:22 +08:00
graManList = await backendRepository . GetAllAsync < GraphList > ( @ $ "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 ) ;
}
2022-11-09 15:22:18 +08:00
return apiResult ;
}
[HttpPost]
public async Task < ApiResult < GraphList > > GraManRead ( [ FromBody ] GraphInfo gi )
{
ApiResult < GraphList > apiResult = new ApiResult < GraphList > ( ) ;
GraphList graManList = new GraphList ( ) ;
try
{
var sqlString = @ $ "SELECT *
FROM graph_manage gm
JOIN variable v1 ON v1 . system_value = @main_system_tag and v1 . system_type = @graph_manage_layer1 and v1 . deleted = 0
JOIN variable v2 ON v1 . id = v2 . system_parent_id AND v2 . system_type = @graph_manage_layer2 AND v2 . deleted = 0
WHERE v2 . system_value = @sub_system_tag AND gm . deleted = 0 AND gm . code = @code
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_tag = gi . sub_system_tag , @main_system_tag = gi . main_system_tag , @code = gi . code } ;
graManList = await backendRepository . GetOneAsync < GraphList > ( sqlString , param ) ;
if ( graManList = = null )
{
apiResult . Code = "0002" ;
apiResult . Msg = "找不到圖資" ;
return apiResult ;
}
apiResult . Code = "0000" ;
apiResult . Data = graManList ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message ) ;
}
2022-11-01 10:49:22 +08:00
return apiResult ;
}
[HttpPost]
2022-11-09 15:16:43 +08:00
public async Task < ApiResult < string > > DelOneGraMan ( [ FromBody ] GraphInfo gi )
2022-11-01 10:49:22 +08:00
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
try
{
2022-11-09 11:05:33 +08:00
var sqlString = @ $"UPDATE graph_manage SET deleted = 1, updated_at = @time, updated_by = @user WHERE code = @code AND sub_system_tag = @sub_system_tag AND main_system_tag = @main_system_tag" ;
2022-11-01 10:49:22 +08:00
2022-11-09 11:05:33 +08:00
var param = new { @code = gi . code , @sub_system_tag = gi . sub_system_tag , @main_system_tag = gi . main_system_tag , @time = DateTime . Now , @user = myUser . userinfo_guid } ;
2022-11-01 10:49:22 +08:00
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 < ApiResult < string > > EdtOneGraMan ( [ FromForm ] GraphInsInfo gii )
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
try
{
2022-11-04 09:41:50 +08:00
var sWhere = @ $"deleted = 0 AND code = @code AND sub_system_tag = @sub_system_tag AND main_system_tag = @main_system_tag" ;
var gm = await backendRepository . GetOneAsync < GraphInsInfo > ( "graph_manage" , sWhere , new { @code = gii . code , @sub_system_tag = gii . sub_system_tag , @main_system_tag = gii . main_system_tag } ) ;
2022-11-01 10:49:22 +08:00
if ( gm = = null )
{
apiResult . Code = "0001" ;
apiResult . Data = "無法找到圖資" ;
return apiResult ;
}
Dictionary < string , object > graph_manage = new Dictionary < string , object > ( ) ;
//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 ) ;
2022-11-04 09:41:50 +08:00
graph_manage . Add ( "@main_system_tag" , gii . main_system_tag ) ;
graph_manage . Add ( "@sub_system_tag" , gii . sub_system_tag ) ;
2022-11-09 11:05:33 +08:00
graph_manage . Add ( "@updated_at" , DateTime . Now ) ;
graph_manage . Add ( "@updated_by" , myUser . userinfo_guid ) ;
2022-11-01 10:49:22 +08:00
//graph_manage.Add("@priority", gii.priority);
2022-11-04 09:41:50 +08:00
await backendRepository . UpdateOneByCustomTable ( graph_manage , "graph_manage" , "code='" + gm . code + "'" + " and sub_system_tag ='" + gm . sub_system_tag + "' and main_system_tag = '" + gm . main_system_tag + "'" ) ;
2022-11-01 10:49:22 +08:00
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 < ApiResult < string > > SaveGraMan ( [ FromForm ] GraphInsInfo gii )
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
try
{
2022-11-04 09:41:50 +08:00
var sWhere = @ $"deleted = 0 AND code = @code AND sub_system_tag = @sub_system_tag AND main_system_tag = @main_system_tag" ;
var gm = await backendRepository . GetOneAsync < GraphInsInfo > ( "graph_manage" , sWhere , new { @code = gii . code , @sub_system_tag = gii . sub_system_tag , @main_system_tag = gii . main_system_tag } ) ;
2022-11-01 10:49:22 +08:00
if ( gm ! = null )
{
apiResult . Code = "0002" ;
apiResult . Data = "已有相同的編碼" ;
return apiResult ;
}
Dictionary < string , object > graph_manage = new Dictionary < string , object > ( ) ;
//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 ) ;
}
2022-11-09 15:16:43 +08:00
graph_manage . Add ( "@donOrgName" , gii . donOrgName ) ;
graph_manage . Add ( "@donSavName" , fileName ) ;
2022-11-01 10:49:22 +08:00
}
}
var newPriority = await backendRepository . GetCurrentPriority ( "graph_manage" ) ;
2022-11-09 15:16:43 +08:00
graph_manage . Add ( "@code" , gii . code ) ;
2022-11-04 09:41:50 +08:00
graph_manage . Add ( "@main_system_tag" , gii . main_system_tag ) ;
graph_manage . Add ( "@sub_system_tag" , gii . sub_system_tag ) ;
2022-11-01 10:49:22 +08:00
graph_manage . Add ( "@name" , gii . name ) ;
graph_manage . Add ( "@deleted" , 0 ) ;
graph_manage . Add ( "@priority" , newPriority + 1 ) ;
2022-11-09 11:05:33 +08:00
graph_manage . Add ( "@created_by" , myUser . userinfo_guid ) ;
graph_manage . Add ( "@created_at" , DateTime . Now ) ;
2022-11-01 10:49:22 +08:00
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 ;
}
}
2022-11-01 19:15:02 +08:00
}