修改modal, 新增即時告警匯出, 列表顯示

This commit is contained in:
dev02 2022-11-08 16:57:31 +08:00
parent 6e606c191b
commit 08953f8048
5 changed files with 2144 additions and 10 deletions

View File

@ -0,0 +1,200 @@
using FrontendWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Repository.BackendRepository.Interface;
using System.Collections.Generic;
using System;
using System.IO;
using System.Data.SqlTypes;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace FrontendWebApi.ApiControllers
{
[Route("api/[controller]")]
[ApiController]
public class AlertController : MyBaseApiController<AlertController>
{
private readonly IBackendRepository backendRepository;
public AlertController(IBackendRepository backendRepository)
{
this.backendRepository = backendRepository;
}
[HttpPost]
public FileResult OpeExportExcel(List<Alert> alerts)
{
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
ICellStyle style12 = workbook.CreateCellStyle();
style12.SetFont(font12);
style12.Alignment = HorizontalAlignment.Center;
style12.VerticalAlignment = VerticalAlignment.Center;
IFont font12Times = workbook.CreateFont();
font12Times.FontName = "Times New Roman";
font12Times.FontHeightInPoints = 12;
IFont font18 = workbook.CreateFont();
font18.FontName = "新細明體";
font18.FontHeightInPoints = 18;
font18.IsBold = true;
ICellStyle styleTitle18 = workbook.CreateCellStyle();
styleTitle18.SetFont(font18);
styleTitle18.Alignment = HorizontalAlignment.Center;
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLeft12 = workbook.CreateCellStyle();
styleLeft12.SetFont(font12);
styleLeft12.Alignment = HorizontalAlignment.Left;
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLine12 = workbook.CreateCellStyle();
styleLine12.SetFont(font12);
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleLine12.VerticalAlignment = VerticalAlignment.Center;
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
ICellStyle stylein12 = workbook.CreateCellStyle();
stylein12.SetFont(font12Times);
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stylein12.VerticalAlignment = VerticalAlignment.Center;
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var sheet = workbook.CreateSheet("即時告警");
int RowPosition = 0;
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
sheet.SetColumnWidth(3, 4 * 160 * 12);
sheet.SetColumnWidth(4, 4 * 160 * 12);
sheet.SetColumnWidth(5, 4 * 160 * 12);
sheet.SetColumnWidth(6, 4 * 160 * 12);
sheet.SetColumnWidth(7, 4 * 160 * 12);
ICell cell = row.CreateCell(0);
cell.SetCellValue("東別-樓層");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue("異常ID");
cell.CellStyle = styleLine12;
cell = row.CreateCell(2);
cell.SetCellValue("發生時間");
cell.CellStyle = styleLine12;
cell = row.CreateCell(3);
cell.SetCellValue("異常類別");
cell.CellStyle = styleLine12;
cell = row.CreateCell(4);
cell.SetCellValue("設備編號");
cell.CellStyle = styleLine12;
cell = row.CreateCell(5);
cell.SetCellValue("異常原因");
cell.CellStyle = styleLine12;
cell = row.CreateCell(6);
cell.SetCellValue("Ack 確認");
cell.CellStyle = styleLine12;
cell = row.CreateCell(7);
cell.SetCellValue("派工/維運單號");
cell.CellStyle = styleLine12;
#endregion
if (alerts.Count > 0)
{
foreach (var a in alerts)
{
var sqlString = $@"select formId from operation_record where error_code = @error_code";
var formId = backendRepository.GetOneAsync<string>(sqlString, new { @error_code = a.error_code });
RowPosition += 1;
row = sheet.CreateRow(RowPosition);
for (var i = 0; i < 8; i++)
{
cell = row.CreateCell(i);
if (i == 0)
{
cell.SetCellValue(a.building_tag + a.floor_tag);
}
if (i == 1)
{
cell.SetCellValue(a.error_code);
}
if (i == 2)
{
cell.SetCellValue(a.Created_at);
}
if (i == 3)
{
cell.SetCellValue(a.error_type);
}
if (i == 4)
{
cell.SetCellValue(a.device_number);
}
if (i == 5)
{
cell.SetCellValue(a.error_reason);
}
if (i == 6)
{
cell.SetCellValue(a.ACKconfirm);
}
if (i == 7)
{
cell.SetCellValue(formId.Result);
}
cell.CellStyle = style12;
}
}
}
var ms = new NpoiMemoryStream
{
AllowClose = false
};
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "廠商資料.xlsx");
}
[HttpPost]
public async Task<ApiResult<List<Alert>>> AlertList(List<Alert> alerts)
{
ApiResult<List<Alert>> apiResult = new ApiResult<List<Alert>>();
try
{
if(alerts.Count > 0)
{
foreach(var a in alerts)
{
var sqlString = $@"select * from operation_record where error_code = @error_code";
var formId = await backendRepository.GetOneAsync<string>(sqlString, new { @error_code = a.error_code });
a.formId = formId;
}
}
apiResult.Code = "0000";
apiResult.Data = alerts;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
string json = System.Text.Json.JsonSerializer.Serialize(alerts);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
}
}

View File

@ -224,6 +224,7 @@ namespace FrontendWebApi.ApiControllers
IRow row = sheet.CreateRow(RowPosition); IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12); sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12); sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
sheet.SetColumnWidth(3, 4 * 160 * 12); sheet.SetColumnWidth(3, 4 * 160 * 12);
sheet.SetColumnWidth(4, 4 * 160 * 12); sheet.SetColumnWidth(4, 4 * 160 * 12);
sheet.SetColumnWidth(5, 4 * 160 * 12); sheet.SetColumnWidth(5, 4 * 160 * 12);

View File

@ -0,0 +1,14 @@
namespace FrontendWebApi.Models
{
public class Alert : Actor
{
public string building_tag { get; set; }
public string floor_tag { get; set; }
public string error_code { get; set; }
public string error_type { get; set; }
public string device_number { get; set; }
public string error_reason { get; set; }
public string ACKconfirm { get; set; }
public string formId { get; set; }
}
}

View File

@ -1,9 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FrontendWebApi.Models
{
}