From a6aa294e7f0cbba1a1676d613ab5131c0d4af353 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Wed, 21 Feb 2024 09:10:02 +0800 Subject: [PATCH] =?UTF-8?q?[WebAPI]=E6=96=B0=E5=A2=9E=E6=B0=B4=E9=9B=BB?= =?UTF-8?q?=E5=A0=B1=E8=A1=A8=E6=B2=92=E6=9C=89=E7=94=A8=E6=88=B6=E9=81=B8?= =?UTF-8?q?=E6=93=87=E8=A8=AD=E5=82=99=E5=B0=B1=E4=B8=8D=E8=83=BD=E5=8C=AF?= =?UTF-8?q?=E5=87=BA=E7=9A=84=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/AlarmRecordController.cs | 3 +- .../ApiControllers/TenantBillController.cs | 55 ++++++++++++++----- FrontendWebApi/Models/Bill.cs | 3 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/FrontendWebApi/ApiControllers/AlarmRecordController.cs b/FrontendWebApi/ApiControllers/AlarmRecordController.cs index d25ba3c..7411182 100644 --- a/FrontendWebApi/ApiControllers/AlarmRecordController.cs +++ b/FrontendWebApi/ApiControllers/AlarmRecordController.cs @@ -64,7 +64,8 @@ namespace FrontendWebApi.ApiControllers JOIN building b ON bm.building_tag = b.building_tag and b.deleted = 0 join variable v2 on ap.ShowView = v2.id and v2.deleted = 0 join variable v1 on v2.system_parent_id = v1.id - GROUP BY b.building_tag, b.full_name, v1.system_value, v1.system_key"; + GROUP BY b.building_tag, b.full_name, v1.system_value, v1.system_key + order by b.priority, v1.system_priority, bm.priority, v2.system_priority, v2.created_at DESC"; var alarmRecordPanelRawDatas = await frontendRepository.GetAllAsync(sqlBuildingMain, new { Account = account }); diff --git a/FrontendWebApi/ApiControllers/TenantBillController.cs b/FrontendWebApi/ApiControllers/TenantBillController.cs index ea83a74..c60292a 100644 --- a/FrontendWebApi/ApiControllers/TenantBillController.cs +++ b/FrontendWebApi/ApiControllers/TenantBillController.cs @@ -436,38 +436,65 @@ namespace FrontendWebApi.ApiControllers [HttpPost] - public async Task OutputTenantBill() + public async Task OutputTenantBill([FromBody] TenantBill tb) { List outputBill = new List(); try { - string sqlString = - $@"SELECT + List buildings = tb.building_tag_list; + string building_tag = ""; + foreach (var item in buildings) + { + if (item == buildings[0]) + { + building_tag = item == "D2" ? $@"device_building_tag = 'D1' || device_building_tag = '{item}'" : $@"device_building_tag = '{item}'" ; + } + else + { + building_tag += item == "D2" ? $@"|| device_building_tag = 'D1' || device_building_tag = '{item}'" : $@"|| device_building_tag = '{item}'"; + } + } + string checkDataSql = $@"select * from archive_electric_meter_tenant_bill a + join device c on a.device_number = c.device_number + WHERE tenant_name is not null and tenant_guid is not null and tenant_name != '' and tenant_guid !='' + AND ({building_tag})"; + var existData = await backendRepository.GetAllAsync(checkDataSql); + if (existData.Count != 0) + { + string sqlString = + $@"SELECT a.tenant_name, MIN(NULLIF(a.start_timestamp, '')) AS start_timestamp, MAX(NULLIF(a.end_timestamp, '')) AS end_timestamp, bill_perKWH, bill_perRCV, - SUM(CASE WHEN device_name_tag = 'E4' THEN result ELSE 0 END) AS elec_result, - SUM(CASE WHEN device_name_tag = 'W1' THEN result ELSE 0 END) AS water_result, - SUM(CASE WHEN device_name_tag = 'E4' THEN bill ELSE 0 END) AS elec_bill, - SUM(CASE WHEN device_name_tag = 'W1' THEN bill ELSE 0 END) AS water_bill, + SUM(CASE WHEN a.device_name_tag = 'E4' THEN result ELSE 0 END) AS elec_result, + SUM(CASE WHEN a.device_name_tag = 'W1' THEN result ELSE 0 END) AS water_result, + SUM(CASE WHEN a.device_name_tag = 'E4' THEN bill ELSE 0 END) AS elec_bill, + SUM(CASE WHEN a.device_name_tag = 'W1' THEN bill ELSE 0 END) AS water_bill, SUM(bill) AS total_bill FROM archive_electric_meter_tenant_bill a JOIN archive_electric_meter_tenant_list b ON a.tenant_guid = b.tenant_guid - GROUP BY a.tenant_name;"; + join device c on a.device_number = c.device_number + where {building_tag} "; - outputBill = await backendRepository.GetAllAsync(sqlString); - string filePath = CreateOutputForm(outputBill); + outputBill = await backendRepository.GetAllAsync(sqlString); + string filePath = CreateOutputForm(outputBill); - byte[] file = System.IO.File.ReadAllBytes(filePath); + byte[] file = System.IO.File.ReadAllBytes(filePath); - return new FileContentResult(file, "application/pdf") + return new FileContentResult(file, "application/pdf") + { + FileDownloadName = "水電報表.pdf" + }; + } + else { - FileDownloadName = "水電報表.pdf" - }; + var data = new { Code = "0001",Msg = "還沒有選擇用戶,無法匯出檔案。"}; + return StatusCode(400, data); + } } catch (Exception exception) { diff --git a/FrontendWebApi/Models/Bill.cs b/FrontendWebApi/Models/Bill.cs index 298f933..68a43cb 100644 --- a/FrontendWebApi/Models/Bill.cs +++ b/FrontendWebApi/Models/Bill.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace FrontendWebApi.Models { @@ -36,6 +37,8 @@ namespace FrontendWebApi.Models public string tenant_guid { get; set; } public string tableType { get; set; } public string building_tag { get; set; } + public List building_tag_list { get; set; } + }