diff --git a/BackendWorkerService/BackendWorkerService.csproj b/BackendWorkerService/BackendWorkerService.csproj index 0bcb027..06a89c1 100644 --- a/BackendWorkerService/BackendWorkerService.csproj +++ b/BackendWorkerService/BackendWorkerService.csproj @@ -7,6 +7,7 @@ + diff --git a/FrontendWebApi/ApiControllers/TenantBillController.cs b/FrontendWebApi/ApiControllers/TenantBillController.cs index d2a85d8..0ced42d 100644 --- a/FrontendWebApi/ApiControllers/TenantBillController.cs +++ b/FrontendWebApi/ApiControllers/TenantBillController.cs @@ -19,6 +19,7 @@ using Microsoft.Extensions.Configuration; using Serilog.Core; using static FrontendWebApi.ApiControllers.TenantBillController; using System.Reflection; +using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -110,7 +111,7 @@ namespace FrontendWebApi.ApiControllers if (exception.Message.Contains($" for key 'PRIMARY'")) { apiResult.Code = "0001"; - apiResult.Msg = "已有相同使用者。"; + apiResult.Msg = "已有相同用戶。"; } Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } @@ -193,14 +194,7 @@ namespace FrontendWebApi.ApiControllers string building_tag = tb.building_tag; string ElecOrWater = tableType == "elec" ? "E4" : "W1"; string sqlString = null; - if (building_tag == "ALL") - { - sqlString = - $"SELECT bill_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name,tenant_guid " + - $"from {TenantBilltable} a join device b on a.device_number =b.device_number" + - $"where a.device_name_tag = '{ElecOrWater}' "; - } - else if (building_tag == "D2") + if (building_tag == "D2") { sqlString = $"SELECT bill_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name,tenant_guid " + @@ -251,22 +245,82 @@ namespace FrontendWebApi.ApiControllers startMonth = tb.start_timestamp.Split("-")[0] + tb.start_timestamp.Split("-")[1]; endMonth = tb.end_timestamp.Split("-")[0] + tb.end_timestamp.Split("-")[1]; } - if (start_timestamp == "") - { - apiResult.Code = "0001"; - apiResult.Msg = "請選擇起始日期。"; - return apiResult; - } - if (end_timestamp == "") - { - apiResult.Code = "0001"; - apiResult.Msg = "請選擇結束日期。"; - return apiResult; - } - - if (startMonth == endMonth) + else { sqlString = + $@"UPDATE {TenantBilltable} + set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), + result = 0, + bill= 0, + start_timestamp = '{start_timestamp}', + end_timestamp = '{end_timestamp}', + updated_at = '{updated_at}', + tenant_guid = '{tb.tenant_guid}' + WHERE device_number = '{tb.device_number}'"; + + await backendRepository.ExecuteSql(sqlString); + + + apiResult.Code = "0000"; + apiResult.Data = "修改成功"; + return apiResult; + } + if (tb.tenant_guid != "" && tb.tenant_guid != null) + { + if (startMonth == endMonth) + { + sqlString = + $"UPDATE {TenantBilltable} " + + $"set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , " + + $"result= " + + $"(select sum(sub_result) " + + $"from archive_electric_water_meter_day_{startMonth} " + + $"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + + $"GROUP BY device_number) , " + + $"bill = " + + $"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " + + $"updated_at = '{updated_at}', " + + $"tenant_guid = '{tb.tenant_guid}' " + + $"WHERE device_number = '{tb.device_number}'"; + } + else + { + string month = ""; + DateTime date1 = DateTime.Parse(start_timestamp); + DateTime date2 = DateTime.Parse(end_timestamp); + int monthsApart = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month; + for (var i = 1; i <= monthsApart; i++) + { + month += $@"UNION ALL + SELECT start_timestamp,device_number, sub_result + FROM archive_electric_water_meter_day_{date1.AddMonths(i).ToString("yyyyMM")} + WHERE device_number = '{tb.device_number}' "; + } + sqlString = + $@"UPDATE {TenantBilltable} + set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , + result= + (SELECT sum(sub_result) + FROM ( + SELECT start_timestamp,device_number, sub_result + FROM archive_electric_water_meter_day_{startMonth} + WHERE device_number = '{tb.device_number}' "+ + $@"{month}" + + $@" ) combined_result + WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' + GROUP BY device_number) , + bill = + ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), + updated_at = '{updated_at}', + tenant_guid = '{tb.tenant_guid}' + WHERE device_number = '{tb.device_number}'"; + } + } + else + { + if (startMonth == endMonth) + { + sqlString = $"UPDATE {TenantBilltable} " + $"set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , " + $"result= " + @@ -274,39 +328,44 @@ namespace FrontendWebApi.ApiControllers $"from archive_electric_water_meter_day_{startMonth} " + $"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + $"GROUP BY device_number) , " + - $"bill = " + - $"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " + + $"bill = 0 ," + $"updated_at = '{updated_at}', " + $"tenant_guid = '{tb.tenant_guid}' " + $"WHERE device_number = '{tb.device_number}'"; + } + else + { + string month = ""; + DateTime date1 = DateTime.Parse(start_timestamp); + DateTime date2 = DateTime.Parse(end_timestamp); + int monthsApart = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month; + for (var i = 1; i <= monthsApart; i++) + { + month += $@"UNION ALL + SELECT start_timestamp,device_number, sub_result + FROM archive_electric_water_meter_day_{date1.AddMonths(i).ToString("yyyyMM")} + WHERE device_number = '{tb.device_number}' "; + } + sqlString = + $@"UPDATE {TenantBilltable} + set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , + result= + (SELECT sum(sub_result) + FROM ( + SELECT start_timestamp,device_number, sub_result + FROM archive_electric_water_meter_day_{startMonth} + WHERE device_number = '{tb.device_number}' " + + $@"{month}" + + $@" ) combined_result + WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' + GROUP BY device_number) , + bill = 0, + updated_at = '{updated_at}', + tenant_guid = '{tb.tenant_guid}' + WHERE device_number = '{tb.device_number}'"; + } } - else - { - //sqlString = - // $"UPDATE {TenantBilltable} " + - // $"set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , " + - // $"result= " + - // $"(SELECT sum(sub_result) " + - // $"FROM ( " + - // $" SELECT start_timestamp,device_number, sub_result " + - // $" FROM archive_electric_water_meter_day_{startMonth} " + - // $" WHERE device_number = '{tb.device_number}' " + - // $" UNION ALL " + - // $" SELECT start_timestamp,device_number, sub_result " + - // $" FROM archive_electric_water_meter_day_{endMonth} " + - // $" WHERE device_number = '{tb.device_number}' " + - // $") combined_result " + - // $"WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + - // $"GROUP BY device_number) ," + - // $"bill = " + - // $"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " + - // $"updated_at = '{updated_at}', " + - // $"tenant_guid = '{tb.tenant_guid}' " + - // $"WHERE device_number = '{tb.device_number}'"; - apiResult.Code = "0001"; - apiResult.Msg = "請選擇同一個月份"; - return apiResult; - } + await backendRepository.ExecuteSql(sqlString); @@ -319,6 +378,11 @@ namespace FrontendWebApi.ApiControllers { apiResult.Code = "9999"; apiResult.Msg = "修改水電參考報表失敗。"; + if (exception.Message.Contains($"doesn't exist")) + { + apiResult.Code = "0001"; + apiResult.Msg = "選取的區間有部分無資料。"; + } Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } @@ -335,16 +399,15 @@ namespace FrontendWebApi.ApiControllers { string sqlString = $@"SELECT - a.tenant_name,a.start_timestamp,a.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(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 - HAVING SUM(bill) != 0"; + a.tenant_name,min(a.start_timestamp)as start_timestamp,max(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(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;"; outputBill = await backendRepository.GetAllAsync(sqlString); string filePath = CreateOutputForm(outputBill); diff --git a/FrontendWebApi/appsettings.Development.json b/FrontendWebApi/appsettings.Development.json index 84e653e..15b59c3 100644 --- a/FrontendWebApi/appsettings.Development.json +++ b/FrontendWebApi/appsettings.Development.json @@ -13,18 +13,19 @@ "JwtLifeSeconds": 3600 }, "FilePath": { - "OutputForm": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputForm\\", //水電報表 檔案儲存位置 - "OutputFormTemplate": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\import.html", // 水電報表範本檔位置 - "Image": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\dome.png" // 明細的圖片 + "OutputForm": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\upload\\OutputForm\\", //水電報表 檔案儲存位置 + "OutputFormTemplate": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\import.html", // 水電報表範本檔位置 + "Image": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\img\\dome.png" // 明細的圖片 }, "DBConfig": { "MySqlDBConfig": { - "Server": "FYlY+w0XDIz+jmF2rlZWJw==", //0.201 - "Port": "js2LutKe+rdjzdxMPQUrvQ==", + //"Server": "FYlY+w0XDIz+jmF2rlZWJw==", //0.201 + "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.132 + "Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp - //"Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome - "Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821 + "Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome + //"Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821 //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office //"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //MCUT