[Webapi]水電報表修改

This commit is contained in:
張家睿 2024-02-06 07:28:36 +08:00
parent 0615483bb4
commit 5ab5319116
3 changed files with 134 additions and 69 deletions

View File

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.86" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.19" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.19" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.19" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.19" />
<PackageReference Include="ncrontab" Version="3.3.1" /> <PackageReference Include="ncrontab" Version="3.3.1" />

View File

@ -19,6 +19,7 @@ using Microsoft.Extensions.Configuration;
using Serilog.Core; using Serilog.Core;
using static FrontendWebApi.ApiControllers.TenantBillController; using static FrontendWebApi.ApiControllers.TenantBillController;
using System.Reflection; using System.Reflection;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -110,7 +111,7 @@ namespace FrontendWebApi.ApiControllers
if (exception.Message.Contains($" for key 'PRIMARY'")) if (exception.Message.Contains($" for key 'PRIMARY'"))
{ {
apiResult.Code = "0001"; apiResult.Code = "0001";
apiResult.Msg = "已有相同使用者。"; apiResult.Msg = "已有相同用戶。";
} }
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
} }
@ -193,14 +194,7 @@ namespace FrontendWebApi.ApiControllers
string building_tag = tb.building_tag; string building_tag = tb.building_tag;
string ElecOrWater = tableType == "elec" ? "E4" : "W1"; string ElecOrWater = tableType == "elec" ? "E4" : "W1";
string sqlString = null; string sqlString = null;
if (building_tag == "ALL") if (building_tag == "D2")
{
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")
{ {
sqlString = sqlString =
$"SELECT bill_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name,tenant_guid " + $"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]; startMonth = tb.start_timestamp.Split("-")[0] + tb.start_timestamp.Split("-")[1];
endMonth = tb.end_timestamp.Split("-")[0] + tb.end_timestamp.Split("-")[1]; endMonth = tb.end_timestamp.Split("-")[0] + tb.end_timestamp.Split("-")[1];
} }
if (start_timestamp == "") else
{
apiResult.Code = "0001";
apiResult.Msg = "請選擇起始日期。";
return apiResult;
}
if (end_timestamp == "")
{
apiResult.Code = "0001";
apiResult.Msg = "請選擇結束日期。";
return apiResult;
}
if (startMonth == endMonth)
{ {
sqlString = 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} " + $"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}' , " + $"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= " + $"result= " +
@ -274,39 +328,44 @@ namespace FrontendWebApi.ApiControllers
$"from archive_electric_water_meter_day_{startMonth} " + $"from archive_electric_water_meter_day_{startMonth} " +
$"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + $"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " +
$"GROUP BY device_number) , " + $"GROUP BY device_number) , " +
$"bill = " + $"bill = 0 ," +
$"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " +
$"updated_at = '{updated_at}', " + $"updated_at = '{updated_at}', " +
$"tenant_guid = '{tb.tenant_guid}' " + $"tenant_guid = '{tb.tenant_guid}' " +
$"WHERE device_number = '{tb.device_number}'"; $"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); await backendRepository.ExecuteSql(sqlString);
@ -319,6 +378,11 @@ namespace FrontendWebApi.ApiControllers
{ {
apiResult.Code = "9999"; apiResult.Code = "9999";
apiResult.Msg = "修改水電參考報表失敗。"; apiResult.Msg = "修改水電參考報表失敗。";
if (exception.Message.Contains($"doesn't exist"))
{
apiResult.Code = "0001";
apiResult.Msg = "選取的區間有部分無資料。";
}
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
} }
@ -335,16 +399,15 @@ namespace FrontendWebApi.ApiControllers
{ {
string sqlString = string sqlString =
$@"SELECT $@"SELECT
a.tenant_name,a.start_timestamp,a.end_timestamp,bill_perKWH,bill_perRCV, 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 = '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 = '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 = '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 device_name_tag = 'W1' THEN bill ELSE 0 END) AS water_bill,
SUM(bill) AS total_bill SUM(bill) AS total_bill
FROM archive_electric_meter_tenant_bill a FROM archive_electric_meter_tenant_bill a
JOIN archive_electric_meter_tenant_list b on a.tenant_guid = b.tenant_guid JOIN archive_electric_meter_tenant_list b on a.tenant_guid = b.tenant_guid
GROUP BY a.tenant_name GROUP BY a.tenant_name;";
HAVING SUM(bill) != 0";
outputBill = await backendRepository.GetAllAsync<OutputBill>(sqlString); outputBill = await backendRepository.GetAllAsync<OutputBill>(sqlString);
string filePath = CreateOutputForm(outputBill); string filePath = CreateOutputForm(outputBill);

View File

@ -13,18 +13,19 @@
"JwtLifeSeconds": 3600 "JwtLifeSeconds": 3600
}, },
"FilePath": { "FilePath": {
"OutputForm": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputForm\\", // "OutputForm": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\upload\\OutputForm\\", //
"OutputFormTemplate": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\import.html", // "OutputFormTemplate": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\import.html", //
"Image": "D:\\jay.chang\\dome\\FrontendWebApi\\wwwroot\\upload\\OutputFormTemplate\\dome.png" // "Image": "C:\\jay.chang\\ibms\\FrontendWebApi\\wwwroot\\img\\dome.png" //
}, },
"DBConfig": { "DBConfig": {
"MySqlDBConfig": { "MySqlDBConfig": {
"Server": "FYlY+w0XDIz+jmF2rlZWJw==", //0.201 //"Server": "FYlY+w0XDIz+jmF2rlZWJw==", //0.201
"Port": "js2LutKe+rdjzdxMPQUrvQ==", "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.132
"Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306
//"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30
//"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp
//"Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome "Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome
"Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821 //"Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821
//"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel
//"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office
//"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //MCUT //"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //MCUT