From 1bfea12fcc3e3985f20c530d5bf9253b9a491f17 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Mon, 11 Mar 2024 11:23:55 +0800 Subject: [PATCH] =?UTF-8?q?[WebApi]=E4=BF=AE=E6=94=B9=E6=B0=B4=E9=9B=BB?= =?UTF-8?q?=E5=A0=B1=E8=A1=A8=E6=9C=83=E6=A0=B9=E6=93=9A=E5=AF=A6=E9=9A=9B?= =?UTF-8?q?=E8=A8=AD=E5=82=99=E7=8B=80=E6=85=8B=E9=80=B2=E8=A1=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=80=81=E6=8A=93=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/TenantBillController.cs | 56 +++++++++++++++++-- FrontendWebApi/Models/Bill.cs | 2 +- 2 files changed, 51 insertions(+), 7 deletions(-) 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