diff --git a/FrontendWebApi/ApiControllers/TenantBillController.cs b/FrontendWebApi/ApiControllers/TenantBillController.cs index c5e1593..f3954be 100644 --- a/FrontendWebApi/ApiControllers/TenantBillController.cs +++ b/FrontendWebApi/ApiControllers/TenantBillController.cs @@ -21,6 +21,7 @@ using static FrontendWebApi.ApiControllers.TenantBillController; using System.Reflection; using Microsoft.CodeAnalysis.CSharp.Syntax; using System.Data.SqlTypes; +using System.Linq; @@ -195,19 +196,62 @@ namespace FrontendWebApi.ApiControllers string building_tag = tb.building_tag; string ElecOrWater = tableType == "elec" ? "E4" : "W1"; string sqlString = null; + #region 檢查設備是否沒被刪除且正在連接 + + string checkDataSql = $@"SELECT device_number,device_name_tag,full_name + FROM device + WHERE device_name_tag = 'E4' and deleted = 0 and is_link = 1 or device_name_tag = 'W1' and deleted = 0 and is_link = 1; "; + var existData = await backendRepository.GetAllAsync(checkDataSql); + string checkDataSql2 = $@"select * from archive_electric_meter_tenant_bill; "; + var existData2 = await backendRepository.GetAllAsync(checkDataSql2); + var commonValues = existData.Select(a => a.device_number) + .Intersect(existData2.Select(b => b.device_number)) + .ToList(); + if (commonValues.Any()) + { + string updateSql = $@"UPDATE archive_electric_meter_tenant_bill + SET deleted = 0 + WHERE device_number IN ('{string.Join("', '", commonValues)}')"; + await backendRepository.ExecuteSql(updateSql); + } + var newValues = existData.Select(a => a.device_number) + .Except(existData2.Select(b => b.device_number)) + .ToList(); + if (newValues.Any()) + { + string insertSql = $@"INSERT INTO archive_electric_meter_tenant_bill (device_number, device_name_tag,full_name, created_at) + SELECT device_number, device_name_tag,full_name, NOW() + FROM device + WHERE device_number IN ('{string.Join("', '", newValues)}')"; + await backendRepository.ExecuteSql(insertSql); + } + var deletedValues = existData2.Select(a => a.device_number) + .Except(existData.Select(b => b.device_number)) + .ToList(); + if (deletedValues.Any()) + { + string updateSql = $@"UPDATE archive_electric_meter_tenant_bill + SET deleted = 1 + WHERE device_number IN ('{string.Join("', '", deletedValues)}')"; + await backendRepository.ExecuteSql(updateSql); + } + + #endregion + + 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 device_building_tag = 'D1' and a.device_name_tag = '{ElecOrWater}' || device_building_tag = 'D2' and a.device_name_tag = '{ElecOrWater}'"; + $"where device_building_tag = 'D1' and a.device_name_tag = '{ElecOrWater}' and a.deleted = 0 || device_building_tag = 'D2' and a.device_name_tag = '{ElecOrWater}' and a.deleted = 0"; } else { 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 device_building_tag = '{building_tag}' and a.device_name_tag = '{ElecOrWater}' "; + $"where device_building_tag = '{building_tag}' and a.device_name_tag = '{ElecOrWater}' and a.deleted = 0"; } @@ -357,7 +401,7 @@ namespace FrontendWebApi.ApiControllers List tenantBill = new List(); try { - string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}'"; + string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}' and a.deleted = 0"; tenantBill = await backendRepository.GetAllAsync(sqlString); if (tenantBill.Count > 1) @@ -401,7 +445,7 @@ namespace FrontendWebApi.ApiControllers try { - string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}'"; + string sqlString = $@"select * from {TenantBilltable} WHERE tenant_guid= '{tb.tenant_guid}' and a.deleted = 0"; tenantBill = await backendRepository.GetAllAsync(sqlString); foreach (TenantBill t in tenantBill) @@ -447,11 +491,11 @@ namespace FrontendWebApi.ApiControllers { if (item == buildings[0]) { - building_tag = item == "D2" ? $@"device_building_tag = 'D1' || device_building_tag = '{item}'" : $@"device_building_tag = '{item}'" ; + building_tag = item == "D2" ? $@"device_building_tag = 'D1' and a.deleted = 0 || device_building_tag = '{item}' and a.deleted = 0" : $@"device_building_tag = '{item}' and a.deleted = 0" ; } else { - building_tag += item == "D2" ? $@"|| device_building_tag = 'D1' || device_building_tag = '{item}'" : $@"|| device_building_tag = '{item}'"; + building_tag += item == "D2" ? $@"|| device_building_tag = 'D1' and a.deleted = 0 || device_building_tag = '{item}' and a.deleted = 0" : $@"|| device_building_tag = '{item}' and a.deleted = 0"; } } string checkDataSql = $@"select * from archive_electric_meter_tenant_bill a diff --git a/FrontendWebApi/Models/Bill.cs b/FrontendWebApi/Models/Bill.cs index 68a43cb..2749a44 100644 --- a/FrontendWebApi/Models/Bill.cs +++ b/FrontendWebApi/Models/Bill.cs @@ -39,7 +39,7 @@ namespace FrontendWebApi.Models public string building_tag { get; set; } public List building_tag_list { get; set; } - + public byte Deleted { get; set; } } public class OutputBill