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; } + }