[WebApi]修改水電報表會根據實際設備狀態進行更新、抓取

This commit is contained in:
張家睿 2024-03-11 11:23:55 +08:00
parent 53c0d252e8
commit 1bfea12fcc
2 changed files with 51 additions and 7 deletions

View File

@ -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<Device>(checkDataSql);
string checkDataSql2 = $@"select * from archive_electric_meter_tenant_bill; ";
var existData2 = await backendRepository.GetAllAsync<TenantBill>(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> tenantBill = new List<TenantBill>();
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<TenantBill>(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<TenantBill>(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

View File

@ -39,7 +39,7 @@ namespace FrontendWebApi.Models
public string building_tag { get; set; }
public List<string> building_tag_list { get; set; }
public byte Deleted { get; set; }
}
public class OutputBill