[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>
<ItemGroup>
<PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.86" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.19" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.19" />
<PackageReference Include="ncrontab" Version="3.3.1" />

View File

@ -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<OutputBill>(sqlString);
string filePath = CreateOutputForm(outputBill);

View File

@ -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