805 lines
34 KiB
C#
805 lines
34 KiB
C#
|
using Backend.Models;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using Repository.BackendRepository.Interface;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Backend.Controllers
|
|||
|
{
|
|||
|
public class SystemCategoryController : MybaseController<SystemCategoryController>
|
|||
|
{
|
|||
|
|
|||
|
private readonly IBackendRepository backendRepository;
|
|||
|
|
|||
|
public SystemCategoryController(IBackendRepository backendRepository)
|
|||
|
{
|
|||
|
this.backendRepository = backendRepository;
|
|||
|
}
|
|||
|
|
|||
|
public IActionResult Index()
|
|||
|
{
|
|||
|
return View();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 取得系統大類清單
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<List<SystemMain>>> SystemMainList()
|
|||
|
{
|
|||
|
ApiResult<List<SystemMain>> apiResult = new ApiResult<List<SystemMain>>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var sWhere = "deleted = 0";
|
|||
|
|
|||
|
var systemMainList = await backendRepository.GetAllAsync<SystemMain>("main_system", sWhere, null, "priority ASC, created_at DESC");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = systemMainList;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 取得單一系統大類
|
|||
|
/// </summary>
|
|||
|
/// <param name="guid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<SystemMain>> GetOneSystemMain(string guid)
|
|||
|
{
|
|||
|
ApiResult<SystemMain> apiResult = new ApiResult<SystemMain>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var systemMain = await backendRepository.GetOneAsync<SystemMain>("main_system", sWhere, param);
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = systemMain;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 新增 / 修改 系統大類資料
|
|||
|
/// </summary>
|
|||
|
/// <param name="post"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> SaveSystemMain(SystemMain post)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = post.Main_system_guid };
|
|||
|
|
|||
|
var systemMain = await backendRepository.GetOneAsync<SystemMain>("main_system", sWhere, param);
|
|||
|
|
|||
|
if (systemMain == null)
|
|||
|
{
|
|||
|
//新增
|
|||
|
//產生一組GUID
|
|||
|
var guid = Guid.NewGuid(); //系統大類GUID
|
|||
|
|
|||
|
Dictionary<string, object> systemMainDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@main_system_guid", guid},
|
|||
|
{ "@full_name", post.Full_name},
|
|||
|
{ "@code", post.Code},
|
|||
|
{ "@created_by", myUserInfo.Userinfo_guid}
|
|||
|
};
|
|||
|
await backendRepository.AddOneByCustomTable(systemMainDic, "main_system");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "新增成功";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Dictionary<string, object> systemMainDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@full_name", post.Full_name},
|
|||
|
{ "@code", post.Code},
|
|||
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|||
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
|
|||
|
};
|
|||
|
|
|||
|
await backendRepository.UpdateOneByCustomTable(systemMainDic, "main_system", "main_system_guid='" + systemMain.Main_system_guid + "'");
|
|||
|
|
|||
|
var AuthCodes = await backendRepository.GetAllAsync<string>(
|
|||
|
@$"select AuthCode from auth_page ap
|
|||
|
join sub_system ss on ss.sub_system_guid = ap.ShowView
|
|||
|
join main_system ms on ms.main_system_guid = ss.main_system_guid
|
|||
|
where ms.main_system_guid = '{systemMain.Main_system_guid}'");
|
|||
|
|
|||
|
if(AuthCodes.Count > 0)
|
|||
|
{
|
|||
|
await backendRepository.ExecuteSql($@"UPDATE auth_page
|
|||
|
SET MainName = '{post.Full_name}'
|
|||
|
WHERE AuthCode IN @authCode;",new { authCode = AuthCodes });
|
|||
|
}
|
|||
|
|
|||
|
#region 新增至派送資料表
|
|||
|
var auth_Pages = await backendRepository.GetAllAsync<Auth_page>("auth_page", "");
|
|||
|
List<Dictionary<string, object>> authPagesDics = new List<Dictionary<string, object>>();
|
|||
|
foreach (var auth_page in auth_Pages)
|
|||
|
{
|
|||
|
Dictionary<string, object> authPagesDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@AuthCode", auth_page.AuthCode},
|
|||
|
{ "@AuthType", auth_page.AuthType},
|
|||
|
{ "@MainName", auth_page.MainName},
|
|||
|
{ "@SubName", auth_page.SubName},
|
|||
|
{ "@building_guid", auth_page.building_guid},
|
|||
|
{ "@ShowView", auth_page.ShowView},
|
|||
|
{ "@created_at", auth_page.created_at},
|
|||
|
};
|
|||
|
|
|||
|
authPagesDics.Add(authPagesDic);
|
|||
|
}
|
|||
|
await backendRepository.ManualInsertBackgroundServiceTask("", "", "auth_page", "purge_all_insert", authPagesDics);
|
|||
|
#endregion
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "修改成功";
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
string json = System.Text.Json.JsonSerializer.Serialize(post);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 刪除單一系統大類
|
|||
|
/// </summary>
|
|||
|
/// <param name="guid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> DeleteOneSystemMain(string guid)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var systemMain = await backendRepository.GetOneAsync<SystemMain>("main_system", sWhere, param);
|
|||
|
|
|||
|
if (systemMain == null)
|
|||
|
{
|
|||
|
apiResult.Code = "9998";
|
|||
|
apiResult.Msg = "查無該系統大類";
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
//檢查是否有未刪除的區域選單
|
|||
|
var sbuildMenu = $@"SELECT
|
|||
|
b.full_name
|
|||
|
FROM building_menu bm
|
|||
|
LEFT JOIN building b ON bm.building_guid = b.building_guid AND b.deleted = 0
|
|||
|
WHERE bm.main_system_guid = @Guid
|
|||
|
GROUP BY b.full_name";
|
|||
|
|
|||
|
var buildMenus = await backendRepository.GetAllAsync<string>(sbuildMenu, new { Guid = guid });
|
|||
|
if (buildMenus.Count > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9997";
|
|||
|
apiResult.Msg = "區域選單中尚有棟別正在使用該系統大類,故無法刪除";
|
|||
|
apiResult.Data = string.Join("<br>", buildMenus);
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
//檢查底下是否有未刪除的系統小類
|
|||
|
string sSubWhere = @$"deleted = @Deleted AND main_system_guid = @Guid";
|
|||
|
object sub_param = new { Deleted = 0, Guid = systemMain.Main_system_guid };
|
|||
|
var systemSubs = await backendRepository.GetAllAsync<SystemSub>("sub_system", sWhere, param);
|
|||
|
|
|||
|
if (systemSubs.Count > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9997";
|
|||
|
apiResult.Msg = "系統小類中尚有小類正在使用系統大類,故無法刪除";
|
|||
|
apiResult.Data = string.Join("<br>", systemSubs.Select(x => x.Full_name).ToList());
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
await backendRepository.DeleteOne(guid, "main_system", "main_system_guid");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "刪除成功";
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "main_system_guid=" + guid);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 取得系統小類清單
|
|||
|
/// </summary>
|
|||
|
/// <param name="main_system_guid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<List<SystemSub>>> SystemSubList(string main_system_guid)
|
|||
|
{
|
|||
|
ApiResult<List<SystemSub>> apiResult = new ApiResult<List<SystemSub>>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
var sWhere = @"deleted = @Deleted AND main_system_guid = @Main_system_guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Main_system_guid = main_system_guid };
|
|||
|
|
|||
|
var systemSubs = await backendRepository.GetAllAsync<SystemSub>("sub_system", sWhere, param, "priority ASC, created_at DESC");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = systemSubs;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 取得單一系統小類
|
|||
|
/// </summary>
|
|||
|
/// <param name="guid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<SystemSub>> GetOneSystemSub(string guid)
|
|||
|
{
|
|||
|
ApiResult<SystemSub> apiResult = new ApiResult<SystemSub>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var systemSub = await backendRepository.GetOneAsync<SystemSub>("sub_system", sWhere, param);
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = systemSub;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 新增 / 修改 系統小類資料
|
|||
|
/// </summary>
|
|||
|
/// <param name="post"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> SaveSystemSub(SystemSub post)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = post.Sub_system_guid };
|
|||
|
|
|||
|
var systemSub = await backendRepository.GetOneAsync<SystemSub>("sub_system", sWhere, param);
|
|||
|
|
|||
|
if (systemSub == null)
|
|||
|
{
|
|||
|
//新增
|
|||
|
//產生一組GUID
|
|||
|
var guid = Guid.NewGuid(); //GUID
|
|||
|
|
|||
|
Dictionary<string, object> systemSubDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@sub_system_guid", guid},
|
|||
|
{ "@main_system_guid", post.Main_system_guid},
|
|||
|
{ "@full_name", post.Full_name},
|
|||
|
{ "@created_by", myUserInfo.Userinfo_guid}
|
|||
|
};
|
|||
|
await backendRepository.AddOneByCustomTable(systemSubDic, "sub_system");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "新增成功";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Dictionary<string, object> systemSubDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@full_name", post.Full_name},
|
|||
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|||
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
|
|||
|
};
|
|||
|
|
|||
|
await backendRepository.UpdateOneByCustomTable(systemSubDic, "sub_system", "sub_system_guid='" + systemSub.Sub_system_guid + "'");
|
|||
|
|
|||
|
var AuthCodes = await backendRepository.GetAllAsync<string>(
|
|||
|
@$"select AuthCode from auth_page ap
|
|||
|
where ap.ShowView = '{systemSub.Sub_system_guid}'");
|
|||
|
|
|||
|
if (AuthCodes.Count > 0)
|
|||
|
{
|
|||
|
await backendRepository.ExecuteSql($@"UPDATE auth_page
|
|||
|
SET SubName = '{post.Full_name}'
|
|||
|
WHERE AuthCode IN @authCode;", new { authCode = AuthCodes });
|
|||
|
}
|
|||
|
|
|||
|
#region 新增至派送資料表
|
|||
|
var auth_Pages = await backendRepository.GetAllAsync<Auth_page>("auth_page", "");
|
|||
|
List<Dictionary<string, object>> authPagesDics = new List<Dictionary<string, object>>();
|
|||
|
foreach (var auth_page in auth_Pages)
|
|||
|
{
|
|||
|
Dictionary<string, object> authPagesDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@AuthCode", auth_page.AuthCode},
|
|||
|
{ "@AuthType", auth_page.AuthType},
|
|||
|
{ "@MainName", auth_page.MainName},
|
|||
|
{ "@SubName", auth_page.SubName},
|
|||
|
{ "@building_guid", auth_page.building_guid},
|
|||
|
{ "@ShowView", auth_page.ShowView},
|
|||
|
{ "@created_at", auth_page.created_at},
|
|||
|
};
|
|||
|
|
|||
|
authPagesDics.Add(authPagesDic);
|
|||
|
}
|
|||
|
await backendRepository.ManualInsertBackgroundServiceTask("", "", "auth_page", "purge_all_insert", authPagesDics);
|
|||
|
#endregion
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "修改成功";
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
string json = System.Text.Json.JsonSerializer.Serialize(post);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 刪除單一系統小類
|
|||
|
/// </summary>
|
|||
|
/// <param name="guid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> DeleteOneSystemSub(string guid)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var systemSub = await backendRepository.GetOneAsync<SystemSub>("sub_system", sWhere, param);
|
|||
|
|
|||
|
if (systemSub == null)
|
|||
|
{
|
|||
|
apiResult.Code = "9998";
|
|||
|
apiResult.Msg = "查無該系統小類";
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
//檢查是否有未刪除的區域選單
|
|||
|
var sbuildMenu = $@"SELECT
|
|||
|
CONCAT(b.full_name, ' - ', ms.full_name)
|
|||
|
FROM building_menu bm
|
|||
|
LEFT JOIN building b ON bm.building_guid = b.building_guid AND b.deleted = 0
|
|||
|
LEFT JOIN main_system ms ON bm.main_system_guid = ms.main_system_guid AND ms.deleted = 0
|
|||
|
WHERE bm.sub_system_guid = @Guid";
|
|||
|
|
|||
|
var buildMenus = await backendRepository.GetAllAsync<string>(sbuildMenu, new { Guid = guid });
|
|||
|
if (buildMenus.Count > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9997";
|
|||
|
apiResult.Msg = "區域選單中尚有選單正在使用該系統小類,故無法刪除";
|
|||
|
apiResult.Data = string.Join("<br>", buildMenus);
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
//檢查是否有未刪除的系統小類樓層
|
|||
|
var ssubSystemFloor = $@"SELECT
|
|||
|
CONCAT(b.full_name, ' - ', ms.full_name, ' - ', ss.full_name, ' - ', f.full_name)
|
|||
|
FROM sub_system_floor ssf
|
|||
|
LEFT JOIN building b ON ssf.building_guid = b.building_guid AND b.deleted = 0
|
|||
|
LEFT JOIN main_system ms ON ssf.main_system_guid = ms.main_system_guid AND ms.deleted = 0
|
|||
|
LEFT JOIN sub_system ss ON ssf.sub_system_guid = ss.sub_system_guid AND ss.deleted = 0
|
|||
|
LEFT JOIN floor f ON ssf.floor_guid = f.floor_guid AND f.deleted = 0
|
|||
|
WHERE ssf.sub_system_guid = @Guid
|
|||
|
AND ssf.deleted = 0";
|
|||
|
|
|||
|
var subSystemFloor = await backendRepository.GetAllAsync<string>(sbuildMenu, new { Guid = guid });
|
|||
|
if (subSystemFloor.Count > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9997";
|
|||
|
apiResult.Msg = "區域選單中尚有樓層正在使用該系統小類,故無法刪除";
|
|||
|
apiResult.Data = string.Join("<br>", subSystemFloor);
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
//檢查是否有未刪除的設備項目
|
|||
|
var sdeviceItem = $@"SELECT
|
|||
|
di.full_name
|
|||
|
FROM device_item di
|
|||
|
WHERE di.deleted = 0
|
|||
|
AND di.sub_system_guid = @Guid";
|
|||
|
|
|||
|
var deviceItems = await backendRepository.GetAllAsync<string>(sdeviceItem, new { Guid = guid });
|
|||
|
if (deviceItems.Count > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9997";
|
|||
|
apiResult.Msg = "設備項目中尚有項目正在使用該系統小類,故無法刪除";
|
|||
|
apiResult.Data = string.Join("<br>", deviceItems);
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
await backendRepository.DeleteOne(guid, "sub_system", "sub_system_guid");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "刪除成功";
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "sub_system_guid=" + guid);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> Savedevice_item(Device_item device_Item)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
//檢查是否有未刪除的區域選單
|
|||
|
if(device_Item.is_show_riserDiagram == 1)
|
|||
|
{
|
|||
|
var sql_show_riserDiagram = $@"SELECT * FROM device_item di
|
|||
|
WHERE di.sub_system_guid = @SubSystemGuid AND di.deleted = 0 AND is_show_riserDiagram = 1";
|
|||
|
|
|||
|
var is_show_riserDiagram = await backendRepository.GetAllAsync<string>(sql_show_riserDiagram, new { SubSystemGuid = device_Item.sub_system_guid });
|
|||
|
|
|||
|
if (is_show_riserDiagram.Count() > 0)
|
|||
|
{
|
|||
|
apiResult.Code = "9998";
|
|||
|
apiResult.Msg = "請先取消已選擇顯示於昇位圖點位。";
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (device_Item.device_item_guid == null)
|
|||
|
{
|
|||
|
//新增
|
|||
|
//產生一組GUID
|
|||
|
var guid = Guid.NewGuid(); //GUID
|
|||
|
|
|||
|
Dictionary<string, object> Device_itemDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@device_item_guid", guid},
|
|||
|
{ "@sub_system_guid", device_Item.sub_system_guid},
|
|||
|
{ "@full_name", device_Item.full_name},
|
|||
|
{ "@points", device_Item.points},
|
|||
|
{ "@unit", device_Item.unit},
|
|||
|
{ "@is_show", device_Item.is_show},
|
|||
|
{ "@is_show_riserDiagram", device_Item.is_show_riserDiagram},
|
|||
|
{ "@is_controll", device_Item.is_controll},
|
|||
|
{ "@is_bool", device_Item.is_bool},
|
|||
|
{ "@created_by", myUserInfo.Userinfo_guid},
|
|||
|
};
|
|||
|
await backendRepository.AddOneByCustomTable(Device_itemDic, "device_item");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "新增成功";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Dictionary<string, object> Device_itemDic = new Dictionary<string, object>()
|
|||
|
{
|
|||
|
{ "@sub_system_guid", device_Item.sub_system_guid},
|
|||
|
{ "@full_name", device_Item.full_name},
|
|||
|
{ "@points", device_Item.points},
|
|||
|
{ "@unit", device_Item.unit},
|
|||
|
{ "@is_show", device_Item.is_show},
|
|||
|
{ "@is_show_riserDiagram", device_Item.is_show_riserDiagram},
|
|||
|
{ "@is_controll", device_Item.is_controll},
|
|||
|
{ "@is_bool", device_Item.is_bool},
|
|||
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|||
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
|
|||
|
};
|
|||
|
|
|||
|
await backendRepository.UpdateOneByCustomTable(Device_itemDic, "device_item", "device_item_guid='" + device_Item.device_item_guid + "'");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "修改成功";
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
string json = System.Text.Json.JsonSerializer.Serialize(device_Item);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<List<Device_item>>> DeviceItemTable(string sub_system_guid)
|
|||
|
{
|
|||
|
ApiResult<List<Device_item>> apiResult = new ApiResult<List<Device_item>>();
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
var sWhere = @"deleted = @Deleted AND sub_system_guid = @Sub_system_guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Sub_system_guid = sub_system_guid };
|
|||
|
|
|||
|
var systemSubs = await backendRepository.GetAllAsync<Device_item>("device_item", sWhere, param, "created_at DESC");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = systemSubs;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<Device_item>> GetOneDeviceItem(string guid)
|
|||
|
{
|
|||
|
ApiResult<Device_item> apiResult = new ApiResult<Device_item>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND device_item_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var Deviceitem = await backendRepository.GetOneAsync<Device_item>("device_item", sWhere, param);
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Data = Deviceitem;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
public async Task<ApiResult<string>> DeleteOneSystemSubDeviceItem(string guid)
|
|||
|
{
|
|||
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string sWhere = @$"deleted = @Deleted AND device_item_guid = @Guid";
|
|||
|
|
|||
|
object param = new { Deleted = 0, Guid = guid };
|
|||
|
|
|||
|
var device_Item = await backendRepository.GetOneAsync<Device_item>("device_item", sWhere, param);
|
|||
|
|
|||
|
if (device_Item == null)
|
|||
|
{
|
|||
|
apiResult.Code = "9998";
|
|||
|
apiResult.Msg = "查無該設備項目";
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
await backendRepository.DeleteOne(guid, "device_item", "device_item_guid");
|
|||
|
|
|||
|
apiResult.Code = "0000";
|
|||
|
apiResult.Msg = "刪除成功";
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "device_item_guid=" + guid);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<ApiResult<bool>> HaveSamePoints(Checksame post)
|
|||
|
{
|
|||
|
ApiResult<bool> apiResult = new ApiResult<bool>();
|
|||
|
try
|
|||
|
{
|
|||
|
var point = await backendRepository.GetOneAsync<Device_item>("device_item", $" sub_system_guid = '{post.sub_system_guid}' and points = '{post.points}' and device_item_guid != '{post.device_item_guid}' and deleted = 0");
|
|||
|
if (point != null)
|
|||
|
{
|
|||
|
apiResult.Data = true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
apiResult.Data = false;
|
|||
|
}
|
|||
|
apiResult.Code = "0000";
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
string json = System.Text.Json.JsonSerializer.Serialize(post);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<ApiResult<Deletebool>> CheckCanDelete(guidandsubguid post)
|
|||
|
{
|
|||
|
ApiResult<Deletebool> apiResult = new ApiResult<Deletebool>();
|
|||
|
try
|
|||
|
{
|
|||
|
var tags = await backendRepository.GetAllAsync<Tags>(
|
|||
|
@$"select * from (select dk.device_building_tag ,dk.device_name_tag,dk.device_system_tag from device_kind dk where dk.device_normal_point_guid = '{post.guid}') dkn
|
|||
|
union(select dk.device_building_tag, dk.device_name_tag, dk.device_system_tag from device_kind dk where dk.device_close_point_guid = '{post.guid}')
|
|||
|
union(select dk.device_building_tag, dk.device_name_tag, dk.device_system_tag from device_kind dk where dk.device_error_point_guid = '{post.guid}')");
|
|||
|
|
|||
|
if (tags.Count == 0)
|
|||
|
{
|
|||
|
Deletebool deletebool = new Deletebool()
|
|||
|
{
|
|||
|
Delete = false,
|
|||
|
Reason = ""
|
|||
|
};
|
|||
|
apiResult.Data = deletebool;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Deletebool deletebool = new Deletebool()
|
|||
|
{
|
|||
|
Delete = true,
|
|||
|
Reason = ""
|
|||
|
};
|
|||
|
var unionsql = "";
|
|||
|
var last = tags.Last();
|
|||
|
foreach (var tag in tags)
|
|||
|
{
|
|||
|
unionsql += $@"select d.building_guid,d.main_system_guid,d.sub_system_guid,d.device_name_tag from device d where d.sub_system_guid = '{post.subguid}' and d.device_building_tag = '{tag.device_building_tag}' and d.device_system_tag = '{tag.device_system_tag}' and d.device_name_tag = '{tag.device_name_tag}' group by d.building_guid,d.main_system_guid,d.sub_system_guid,d.device_name_tag";
|
|||
|
if (!last.Equals(tag))
|
|||
|
{
|
|||
|
unionsql += " union ";
|
|||
|
}
|
|||
|
}
|
|||
|
var sql = @$"select ms.full_name msname,b.full_name bname,s.full_name subname,de.device_name_tag from
|
|||
|
({unionsql}) de
|
|||
|
left join main_system ms on ms.main_system_guid = de.main_system_guid
|
|||
|
left join building b on b.building_guid = de.building_guid
|
|||
|
left join sub_system s on s.sub_system_guid = de.sub_system_guid";
|
|||
|
var names = await backendRepository.GetAllAsync<GetCheckName>(sql);
|
|||
|
var count = 0;
|
|||
|
foreach (var name in names)
|
|||
|
{
|
|||
|
count++;
|
|||
|
deletebool.Reason += count.ToString() + "." + name.bname + "-" + name.msname + "-" + name.subname + "-" + name.device_name_tag + "<br>";
|
|||
|
}
|
|||
|
apiResult.Data = deletebool;
|
|||
|
}
|
|||
|
apiResult.Code = "0000";
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
string json = System.Text.Json.JsonSerializer.Serialize(post);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<ApiResult<Deletebool>> CheckCanSubDelete(string guid)
|
|||
|
{
|
|||
|
ApiResult<Deletebool> apiResult = new ApiResult<Deletebool>();
|
|||
|
try
|
|||
|
{
|
|||
|
var text = "";
|
|||
|
var item = await backendRepository.GetAllAsync<string>(@$"select device_item_guid from device_item where deleted = 0 and sub_system_guid = '{guid}'");
|
|||
|
if(item.Count > 0)
|
|||
|
{
|
|||
|
text += "項目還有尚未刪除<br>";
|
|||
|
}
|
|||
|
var menu = await backendRepository.GetAllAsync<string>($"select sub_system_guid from building_menu where sub_system_guid = '{guid}'");
|
|||
|
if (menu.Count > 0)
|
|||
|
{
|
|||
|
text += "區域選單還有尚未刪除<br>";
|
|||
|
}
|
|||
|
Deletebool deletebool = new Deletebool()
|
|||
|
{
|
|||
|
Delete = false,
|
|||
|
Reason = ""
|
|||
|
};
|
|||
|
if (text != "")
|
|||
|
{
|
|||
|
deletebool.Delete = true;
|
|||
|
deletebool.Reason = text;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
deletebool.Delete = false;
|
|||
|
}
|
|||
|
apiResult.Data = deletebool;
|
|||
|
apiResult.Code = "0000";
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
apiResult.Code = "9999";
|
|||
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + guid);
|
|||
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|||
|
}
|
|||
|
return apiResult;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|