[WebAPI]更新水電報表api

This commit is contained in:
張家睿 2024-02-07 17:57:20 +08:00
parent 8efb19af68
commit 7dc344ab87

View File

@ -20,6 +20,7 @@ using Serilog.Core;
using static FrontendWebApi.ApiControllers.TenantBillController; using static FrontendWebApi.ApiControllers.TenantBillController;
using System.Reflection; using System.Reflection;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Data.SqlTypes;
@ -102,7 +103,7 @@ namespace FrontendWebApi.ApiControllers
await backendRepository.ExecuteSql(sqlString); await backendRepository.ExecuteSql(sqlString);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = "新增成功"; apiResult.Msg = "新增成功";
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -137,7 +138,7 @@ namespace FrontendWebApi.ApiControllers
await backendRepository.ExecuteSql(sqlString); await backendRepository.ExecuteSql(sqlString);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = "修改用戶成功"; apiResult.Msg = "修改用戶成功";
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -165,7 +166,7 @@ namespace FrontendWebApi.ApiControllers
await backendRepository.ExecuteSql(sqlString); await backendRepository.ExecuteSql(sqlString);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = "刪除成功"; apiResult.Msg = "刪除成功";
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -209,7 +210,7 @@ namespace FrontendWebApi.ApiControllers
$"where device_building_tag = '{building_tag}' and a.device_name_tag = '{ElecOrWater}' "; $"where device_building_tag = '{building_tag}' and a.device_name_tag = '{ElecOrWater}' ";
} }
tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString); tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString);
@ -238,151 +239,195 @@ namespace FrontendWebApi.ApiControllers
var start_timestamp = tb.start_timestamp; var start_timestamp = tb.start_timestamp;
var end_timestamp = tb.end_timestamp; var end_timestamp = tb.end_timestamp;
string sqlString = null; string sqlString = null;
string startMonth = ""; string result = "0";
string endMonth = "";
if (start_timestamp != "" && end_timestamp != "") if (tb.start_timestamp == "" || tb.end_timestamp == "" || tb.tenant_guid == "" || tb.tenant_guid == null)
{ {
startMonth = tb.start_timestamp.Split("-")[0] + tb.start_timestamp.Split("-")[1]; sqlString = $@"UPDATE {TenantBilltable}
endMonth = tb.end_timestamp.Split("-")[0] + tb.end_timestamp.Split("-")[1]; 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}' ,
else result= 0,
{ bill = 0,
sqlString = updated_at = '{updated_at}',
$@"UPDATE {TenantBilltable} tenant_guid = '{tb.tenant_guid}'
set tenant_name = (SELECT tenant_name from archive_electric_meter_tenant_list WHERE tenant_guid = '{tb.tenant_guid}'), WHERE device_number = '{tb.device_number}'";
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); await backendRepository.ExecuteSql(sqlString);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = "修改成功"; apiResult.Msg = "資料填寫不完整";
return apiResult; 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 else
{ {
if (startMonth == endMonth) List<string> howManyMonth = new List<string>();
List<string> existMonth = new List<string>();
DateTime date1 = DateTime.Parse(start_timestamp);
DateTime date2 = DateTime.Parse(end_timestamp);
int monthsApart = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month;
// 找出資料庫存在的水電月份表
string checkTabelSql = $@"SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'ibms_dome_dome' and TABLE_NAME like 'archive_electric_water_meter_day_%'";
var existTable = await backendRepository.GetAllAsync<string>(checkTabelSql);
for (var i = 0; i <= monthsApart; i++)
{ {
sqlString = howManyMonth.Add($@"archive_electric_water_meter_day_{date1.AddMonths(i).ToString("yyyyMM")}");
$"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}' , " + foreach (var item in existTable)
$"result= " + {
$"(select sum(sub_result) " + foreach (var data in howManyMonth)
$"from archive_electric_water_meter_day_{startMonth} " + {
$"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + if (item == data)
$"GROUP BY device_number) , " + {
$"bill = 0 ," + existMonth.Add(data);
$"updated_at = '{updated_at}', " + }
$"tenant_guid = '{tb.tenant_guid}' " + }
$"WHERE device_number = '{tb.device_number}'"; }
if (existMonth.Count == 0)
{
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= 0,
bill = 0,
updated_at = '{updated_at}',
tenant_guid = '{tb.tenant_guid}'
WHERE device_number = '{tb.device_number}'";
} }
else else
{ {
string month = ""; string month = $@"
DateTime date1 = DateTime.Parse(start_timestamp); SELECT start_timestamp,device_number, sub_result
DateTime date2 = DateTime.Parse(end_timestamp); FROM {existMonth[0]}
int monthsApart = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month; WHERE device_number = '{tb.device_number}'";
for (var i = 1; i <= monthsApart; i++) for (var i = 1; i < existMonth.Count; i++)
{ {
month += $@"UNION ALL month += $@"UNION ALL
SELECT start_timestamp,device_number, sub_result SELECT start_timestamp,device_number, sub_result
FROM archive_electric_water_meter_day_{date1.AddMonths(i).ToString("yyyyMM")} FROM {existMonth[i]}
WHERE device_number = '{tb.device_number}' "; WHERE device_number = '{tb.device_number}' ";
} }
string checkDataSql = $@"(SELECT sum(sub_result)
FROM ( {month}
) combined_result
WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}'
GROUP BY device_number) ";
var existData = await backendRepository.GetAllAsync<string>(checkDataSql);
if (existData.Count != 0)
{
result = $@"(SELECT sum(sub_result)
FROM ( {month}
) combined_result
WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}'
GROUP BY device_number) ";
}
sqlString = 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= {result},
(SELECT sum(sub_result) bill = ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ),
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}', 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}'";
} }
await backendRepository.ExecuteSql(sqlString);
} }
await backendRepository.ExecuteSql(sqlString);
apiResult.Code = "0000";
apiResult.Data = "修改成功";
} }
catch (Exception exception) catch (Exception exception)
{ {
apiResult.Code = "9999"; apiResult.Code = "9999";
apiResult.Msg = "修改水電參考報表失敗。"; apiResult.Msg = "修改水電參考報表失敗。";
if (exception.Message.Contains($"doesn't exist")) Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return apiResult;
}
apiResult = await CheckDay(tb);
return apiResult;
}
[HttpPost]
public async Task<ApiResult<string>> CheckDay([FromBody] TenantBill tb)
{
ApiResult<string> apiResult = new ApiResult<string>();
if (tb.start_timestamp != "" && tb.end_timestamp != "" && tb.tenant_guid != "")
{
List<TenantBill> tenantBill = new List<TenantBill>();
try
{ {
apiResult.Code = "0001"; string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}'";
apiResult.Msg = "選取的區間有部分無資料。";
tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString);
if (tenantBill.Count > 1)
{
foreach (TenantBill t in tenantBill)
{
if (t.start_timestamp == tb.start_timestamp && t.end_timestamp == tb.end_timestamp)
{
continue;
}
else
{
apiResult.Code = "0001";
apiResult.Msg = "該用戶設備的起訖日期不一致,是否將所有設備起訖日期調為一致?";
return apiResult;
}
}
apiResult.Code = "0000";
apiResult.Msg = "修改成功且該用戶的所有設備日期一致";
}
} }
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "確認日期失敗。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
}
apiResult.Code = "0000";
apiResult.Msg = "修改成功且不執行CheckDay";
return apiResult;
}
[HttpPost]
public async Task<ApiResult<string>> ChangeDay([FromBody] TenantBill tb)
{
ApiResult<string> apiResult = new ApiResult<string>();
List<TenantBill> tenantBill = new List<TenantBill>();
try
{
string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}'";
tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString);
foreach (TenantBill t in tenantBill)
{
if (t.start_timestamp == tb.start_timestamp && t.end_timestamp == tb.end_timestamp)
{
continue;
}
else
{
t.start_timestamp = tb.start_timestamp;
t.end_timestamp = tb.end_timestamp;
await UpdateTenantBill(t);
}
}
apiResult.Code = "0000";
apiResult.Msg = "修改成功";
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "更換日期失敗。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
} }
@ -399,14 +444,18 @@ namespace FrontendWebApi.ApiControllers
{ {
string sqlString = string sqlString =
$@"SELECT $@"SELECT
a.tenant_name,min(a.start_timestamp)as start_timestamp,max(a.end_timestamp) as end_timestamp,bill_perKWH,bill_perRCV, 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 = '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;";
outputBill = await backendRepository.GetAllAsync<OutputBill>(sqlString); outputBill = await backendRepository.GetAllAsync<OutputBill>(sqlString);
@ -550,12 +599,12 @@ namespace FrontendWebApi.ApiControllers
$" </div> " + $" </div> " +
$" </div> " + $" </div> " +
$" </div>"; $" </div>";
} }
htmlStr = htmlStr.Replace("{{bill}}", bill); htmlStr = htmlStr.Replace("{{bill}}", bill);
return htmlStr; return htmlStr;
} }