[後端] 修改匯出程序, 更具需求更改成所有棟別資料, 修改監控系統列表排序

This commit is contained in:
dev02 2023-06-27 10:34:38 +08:00
parent dea56822c3
commit 8dbfa4938a
3 changed files with 311 additions and 280 deletions

View File

@ -176,7 +176,7 @@ namespace FrontendWebApi.ApiControllers
select distinct main_system_tag, sub_system_tag from building_menu where is_link = 1 select distinct main_system_tag, sub_system_tag from building_menu where is_link = 1
) as bm on v2.system_value = bm.sub_system_tag and v1.system_value = bm.main_system_tag ) as bm on v2.system_value = bm.sub_system_tag and v1.system_value = bm.main_system_tag
where c.account = @account where c.account = @account
order by v2.system_priority", new { @account = "webUser", @sub_system_type = sub_system_type, @main_system_type = main_system_type }); order by build.priority, v2.system_priority", new { @account = "webUser", @sub_system_type = sub_system_type, @main_system_type = main_system_type });
var building = dbsub.GroupBy(x => x.device_building_tag).ToList(); var building = dbsub.GroupBy(x => x.device_building_tag).ToList();
apiResult.Data = new List<History_Building>(); apiResult.Data = new List<History_Building>();

View File

@ -13,6 +13,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ionic.Zip; using Ionic.Zip;
using System.Collections.Immutable;
namespace FrontendWebApi.ApiControllers namespace FrontendWebApi.ApiControllers
{ {
@ -408,15 +409,17 @@ namespace FrontendWebApi.ApiControllers
[Route("api/ExportElectricList")] [Route("api/ExportElectricList")]
public FileResult OpeExportExcelElec([FromBody] HydroMeterInput input) public FileResult OpeExportExcelElec([FromBody] HydroMeterInput input)
{ {
input = new HydroMeterInput(); List<List<HydroMeterOutput>> result = new List<List<HydroMeterOutput>>();
input.building_tag = "G6"; var building = backendRepository.GetAllAsync<HydroBuildList>("select * from building where deleted = 0").Result;
input.floor_tag = new List<string>(); foreach(var b in building)
input.startTime = "2023-06"; {
input.tableType = "day"; input.building_tag = b.building_tag;
result.Add(this.ElectricList(input).Result.Value.Data.ToList());
var result = this.ElectricList(input).Result.Value.Data.ToList(); }
List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>(); List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>();
foreach (var r in result)
{
var workbook = new XSSFWorkbook(); var workbook = new XSSFWorkbook();
#region excel設定 #region excel設定
IFont font12 = workbook.CreateFont(); IFont font12 = workbook.CreateFont();
@ -459,8 +462,8 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true; stylein12.WrapText = true;
#endregion #endregion
string buildingName = r.Select(x => x.building_name).FirstOrDefault();
var sheet = workbook.CreateSheet("電表報表"); var sheet = workbook.CreateSheet($"{buildingName}電表報表");
int RowPosition = 0; int RowPosition = 0;
if (result.Count > 0) if (result.Count > 0)
{ {
@ -481,7 +484,7 @@ namespace FrontendWebApi.ApiControllers
cell.SetCellValue("設備"); cell.SetCellValue("設備");
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
foreach (var rr in result.FirstOrDefault().rawData) foreach (var rr in r.FirstOrDefault().rawData)
{ {
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue(rr.timeStamp); cell.SetCellValue(rr.timeStamp);
@ -499,7 +502,7 @@ namespace FrontendWebApi.ApiControllers
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
#endregion #endregion
foreach (var r in result) foreach (var rr in r)
{ {
RowPosition += 1; RowPosition += 1;
int k = 3; int k = 3;
@ -509,22 +512,22 @@ namespace FrontendWebApi.ApiControllers
cell = row.CreateCell(j); cell = row.CreateCell(j);
if (j == 0) if (j == 0)
{ {
cell.SetCellValue(r.building_name); cell.SetCellValue(rr.building_name);
} }
if (j == 1) if (j == 1)
{ {
cell.SetCellValue(r.floor_tag); cell.SetCellValue(rr.floor_tag);
} }
if (j == 2) if (j == 2)
{ {
cell.SetCellValue(r.device_full_name); cell.SetCellValue(rr.device_full_name);
} }
if (j == 3) if (j == 3)
{ {
foreach (var rr in r.rawData) foreach (var rrr in rr.rawData)
{ {
cell.SetCellValue(rr.avg_rawdata.ToString()); cell.SetCellValue(rrr.avg_rawdata.ToString());
j++; j++;
k++; k++;
cell = row.CreateCell(j); cell = row.CreateCell(j);
@ -533,32 +536,27 @@ namespace FrontendWebApi.ApiControllers
if (j == k) if (j == k)
{ {
cell.SetCellValue(r.total); cell.SetCellValue(rr.total);
} }
if (j == k+1) if (j == k+1)
{ {
cell.SetCellValue(r.price); cell.SetCellValue(rr.price);
} }
if (j == k+2) if (j == k+2)
{ {
cell.SetCellValue(r.total_price); cell.SetCellValue(rr.total_price);
} }
cell.CellStyle = style12; cell.CellStyle = style12;
} }
} }
} }
MemoryStream ms = new MemoryStream();
var ms = new NpoiMemoryStream
{
AllowClose = false
};
workbook.Write(ms); workbook.Write(ms);
ms.Flush(); docFile.Add(new Dictionary<string, byte[]>() { { $"{buildingName}電表報表.xlsx", ms.ToArray() } });
ms.Seek(0, SeekOrigin.Begin); }
docFile.Add(new Dictionary<string, byte[]>() { { "電表報表", ms.ToArray() } });
var memoryStream = new MemoryStream(); var zipMs = new MemoryStream();
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8)) using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{ {
foreach(var d in docFile) foreach(var d in docFile)
@ -568,21 +566,57 @@ namespace FrontendWebApi.ApiControllers
zip.AddEntry(dic.Key, dic.Value); zip.AddEntry(dic.Key, dic.Value);
} }
} }
zip.Save(memoryStream); zip.Save(zipMs);
} }
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(memoryStream, "application/octet-stream", "電表報表.zip"); return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip");
} }
[HttpPost] [HttpPost]
[Route("api/ExportElectricCompareList")] [Route("api/ExportElectricCompareList")]
public FileResult OpeExportCompareExcelElec([FromBody] List<HydroMeterInput> input) public FileResult OpeExportCompareExcelElec([FromBody] List<HydroMeterInput> input)
{ {
var result = new List<HydroMeterOutput>(); List<HydroMeterOutput> result = new List<HydroMeterOutput>();
var result1 = this.ElectricList(input[0]).Result.Value.Data.ToList(); List<List<HydroMeterOutput>> result1 = new List<List<HydroMeterOutput>>();
var result2 = this.ElectricList(input[1]).Result.Value.Data.ToList(); List<List<HydroMeterOutput>> result2 = new List<List<HydroMeterOutput>>();
List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>();
var building = backendRepository.GetAllAsync<HydroBuildList>("select * from building where deleted = 0").Result;
foreach (var b in building)
{
input[0].building_tag = b.building_tag;
input[1].building_tag = b.building_tag;
result1.Add(this.ElectricList(input[0]).Result.Value.Data.ToList());
result2.Add(this.ElectricList(input[1]).Result.Value.Data.ToList());
}
Dictionary<string, List<string>> compareDict = new Dictionary<string, List<string>>(); Dictionary<string, List<string>> compareDict = new Dictionary<string, List<string>>();
var rawDataTitle = new List<string>();
if (input[0].tableType == "month")
{
for (var i = 1; i <= 12; i++)
{
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
else if (input[0].tableType == "day")
{
var days = GetDayInMonth(input[0].startTime);
for (var i = 1 ;i <= days ; i++) {
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
foreach (var b in building)
{
var res1 = result1.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
var res2 = result2.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
if (res1.Count > 0 || res2.Count > 0)
{
var workbook = new XSSFWorkbook(); var workbook = new XSSFWorkbook();
#region excel設定 #region excel設定
IFont font12 = workbook.CreateFont(); IFont font12 = workbook.CreateFont();
@ -625,45 +659,26 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true; stylein12.WrapText = true;
#endregion #endregion
var rawDataTitle = new List<string>();
if (input[0].tableType == "month") var sheet = workbook.CreateSheet($"{b.full_name}電表報表");
{
for (var i = 1; i <= 12; i++)
{
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
else if (input[0].tableType == "day")
{
var days = GetDayInMonth(input[0].startTime);
for (var i = 1 ;i <= days ; i++) {
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
var sheet = workbook.CreateSheet("電表報表");
int RowPosition = 0; int RowPosition = 0;
if (result1.Count > 0 || result2.Count > 0) result = res1.FirstOrDefault() ?? new List<HydroMeterOutput>();
var r2 = res2.FirstOrDefault() ?? new List<HydroMeterOutput>();
foreach (var r in r2)
{ {
result = result1; var target = result.Where(x => x.building_tag == r.building_tag && x.device_serial_tag == r.device_serial_tag && x.floor_tag == r.floor_tag && x.device_number == r.device_number).FirstOrDefault();
foreach (var r2 in result2) compareDict[r.building_tag + r.floor_tag + r.device_serial_tag + r.device_number] = new List<string>() { r.total, r.price, r.total_price };
{
var target = result.Where(r => r.building_tag == r2.building_tag && r.device_serial_tag == r2.device_serial_tag && r.floor_tag == r2.floor_tag).FirstOrDefault();
compareDict[r2.building_tag + r2.floor_tag + r2.device_serial_tag] = new List<string>() { r2.total, r2.price, r2.total_price };
if (target != null) if (target != null)
{ {
target.rawData = target.rawData.Concat(r2.rawData).ToList(); target.rawData = target.rawData.Concat(r.rawData).ToList();
} }
else { else
r2.total = null; {
r2.price = null; r.total = null;
r2.total_price= null; r.price = null;
result.Add(r2); r.total_price = null;
result.Add(r);
} }
} }
#region set cell #region set cell
IRow row = sheet.CreateRow(RowPosition); IRow row = sheet.CreateRow(RowPosition);
@ -692,7 +707,7 @@ namespace FrontendWebApi.ApiControllers
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "小計"); cell.SetCellValue(input[0].startTime + "小計");
sheet.SetColumnWidth(i-1, 2 * 160 * 12); sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "小計"); cell.SetCellValue(input[1].startTime + "小計");
@ -718,7 +733,7 @@ namespace FrontendWebApi.ApiControllers
foreach (var r in result) foreach (var r in result)
{ {
string compareDictKey = r.building_tag + r.floor_tag + r.device_serial_tag; string compareDictKey = r.building_tag + r.floor_tag + r.device_serial_tag + r.device_number;
RowPosition += 1; RowPosition += 1;
int k = 3; int k = 3;
row = sheet.CreateRow(RowPosition); row = sheet.CreateRow(RowPosition);
@ -735,7 +750,7 @@ namespace FrontendWebApi.ApiControllers
} }
if (j == 2) if (j == 2)
{ {
cell.SetCellValue(r.device_serial_tag); cell.SetCellValue(r.device_full_name);
} }
if (j == 3) if (j == 3)
@ -778,17 +793,27 @@ namespace FrontendWebApi.ApiControllers
cell.CellStyle = style12; cell.CellStyle = style12;
} }
} }
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
docFile.Add(new Dictionary<string, byte[]>() { { $"{b.full_name}電表報表.xlsx", ms.ToArray() } });
}
} }
var ms = new NpoiMemoryStream var zipMs = new MemoryStream();
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{ {
AllowClose = false foreach (var d in docFile)
}; {
workbook.Write(ms); foreach (var dic in d)
ms.Flush(); {
ms.Seek(0, SeekOrigin.Begin); zip.AddEntry(dic.Key, dic.Value);
}
}
zip.Save(zipMs);
}
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(ms, "application/vnd.ms-excel", "電表報表.xlsx"); return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip");
} }
public static int GetDayInMonth(string yearMonth) public static int GetDayInMonth(string yearMonth)

View File

@ -54,4 +54,10 @@ namespace FrontendWebApi.Models
{ {
public List<HydroMeterCompareOutput> exportList { get; set; } public List<HydroMeterCompareOutput> exportList { get; set; }
} }
public class HydroBuildList
{
public string full_name { get; set; }
public string building_tag { get; set; }
}
} }