[WebApi]修改水電報表會根據實際設備狀態進行更新、抓取
This commit is contained in:
		
							parent
							
								
									53c0d252e8
								
							
						
					
					
						commit
						1bfea12fcc
					
				@ -21,6 +21,7 @@ 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;
 | 
					using System.Data.SqlTypes;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -195,19 +196,62 @@ namespace FrontendWebApi.ApiControllers
 | 
				
			|||||||
                string building_tag = tb.building_tag;
 | 
					                string building_tag = tb.building_tag;
 | 
				
			||||||
                string ElecOrWater = tableType == "elec" ? "E4" : "W1";
 | 
					                string ElecOrWater = tableType == "elec" ? "E4" : "W1";
 | 
				
			||||||
                string sqlString = null;
 | 
					                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")
 | 
					                if (building_tag == "D2")
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    sqlString =
 | 
					                    sqlString =
 | 
				
			||||||
                   $"SELECT bill_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name,tenant_guid " +
 | 
					                   $"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 " +
 | 
					                   $"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
 | 
					                else
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    sqlString =
 | 
					                    sqlString =
 | 
				
			||||||
                   $"SELECT bill_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name,tenant_guid " +
 | 
					                   $"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 " +
 | 
					                   $"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>();
 | 
					                List<TenantBill> tenantBill = new List<TenantBill>();
 | 
				
			||||||
                try
 | 
					                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);
 | 
					                    tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString);
 | 
				
			||||||
                    if (tenantBill.Count > 1)
 | 
					                    if (tenantBill.Count > 1)
 | 
				
			||||||
@ -401,7 +445,7 @@ namespace FrontendWebApi.ApiControllers
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            try
 | 
					            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);
 | 
					                tenantBill = await backendRepository.GetAllAsync<TenantBill>(sqlString);
 | 
				
			||||||
                foreach (TenantBill t in tenantBill)
 | 
					                foreach (TenantBill t in tenantBill)
 | 
				
			||||||
@ -447,11 +491,11 @@ namespace FrontendWebApi.ApiControllers
 | 
				
			|||||||
                {
 | 
					                {
 | 
				
			||||||
                    if (item == buildings[0])
 | 
					                    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
 | 
					                    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
 | 
					                string checkDataSql = $@"select * from archive_electric_meter_tenant_bill a
 | 
				
			||||||
 | 
				
			|||||||
@ -39,7 +39,7 @@ namespace FrontendWebApi.Models
 | 
				
			|||||||
            public string building_tag { get; set; }
 | 
					            public string building_tag { get; set; }
 | 
				
			||||||
            public List<string> building_tag_list { get; set; }
 | 
					            public List<string> building_tag_list { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            public byte Deleted { get; set; }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public class OutputBill
 | 
					        public class OutputBill
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user