[後端] 修改Json轉換問題

This commit is contained in:
dev02 2023-07-20 10:23:32 +08:00
parent f2d6f711f0
commit d3f1edd94a
5 changed files with 23 additions and 23 deletions

View File

@ -852,15 +852,15 @@ namespace BackendWorkerService.Services.Implement
switch (name) switch (name)
{ {
case "min": case "min":
var min = Convert.ToDecimal(real["@val"].ToString()); var min = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@min_rawdata", min); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": case "max":
var max = Convert.ToDecimal(real["@val"].ToString()); var max = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@max_rawdata", max); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": case "avg":
var avg = Convert.ToDecimal(real["@val"].ToString()); var avg = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@avg_rawdata", avg); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":
@ -920,15 +920,15 @@ namespace BackendWorkerService.Services.Implement
switch (name) switch (name)
{ {
case "min": case "min":
var min = Convert.ToDecimal(real["@val"].ToString()); var min = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@min_rawdata", min); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": case "max":
var max = Convert.ToDecimal(real["@val"].ToString()); var max = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@max_rawdata", max); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": case "avg":
var avg = Convert.ToDecimal(real["@val"].ToString()); var avg = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@avg_rawdata", avg); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":

View File

@ -119,7 +119,7 @@ namespace FrontendWebApi.ApiControllers
HistoryExport hed = new HistoryExport(); HistoryExport hed = new HistoryExport();
hed.type = devicePoint.Where(x => x.device_building_tag == lhe.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault(); hed.type = devicePoint.Where(x => x.device_building_tag == lhe.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault();
hed.deviceName = device.Where(x => x.device_number == ard["@device_number"].ToString()).Select(x => x.full_name).FirstOrDefault(); hed.deviceName = device.Where(x => x.device_number == ard["@device_number"].ToString()).Select(x => x.full_name).FirstOrDefault();
hed.value = (double)((decimal)ard["@avg_rawdata"]); hed.value = ard["@avg_rawdata"].ToString() == "-1" ? "NaN" : Math.Round(Decimal.Parse(ard["@avg_rawdata"].ToString(), System.Globalization.NumberStyles.Float), 2).ToString();
hed.timestamp = Convert.ToDateTime(ard["@start_timestamp"].ToString()); hed.timestamp = Convert.ToDateTime(ard["@start_timestamp"].ToString());
hed.building_tag = lhe.building_tag; hed.building_tag = lhe.building_tag;
he.Add(hed); he.Add(hed);
@ -1322,19 +1322,19 @@ namespace FrontendWebApi.ApiControllers
switch (name) switch (name)
{ {
case "min": case "min":
var min = Convert.ToDecimal(real["@val"].ToString()); var min = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@min_rawdata", min); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": case "max":
var max = Convert.ToDecimal(real["@val"].ToString()); var max = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@max_rawdata", max); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": case "avg":
var avg = Convert.ToDecimal(real["@val"].ToString()); var avg = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@avg_rawdata", avg); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":
var sum = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); var sum = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@sum_rawdata", sum); arrangeRawData.Add("@sum_rawdata", sum);
break; break;
} }
@ -1390,19 +1390,19 @@ namespace FrontendWebApi.ApiControllers
switch (name) switch (name)
{ {
case "min": case "min":
var min = Convert.ToDecimal(real["@val"].ToString()); var min = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@min_rawdata", min); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": case "max":
var max = Convert.ToDecimal(real["@val"].ToString()); var max = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@max_rawdata", max); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": case "avg":
var avg = Convert.ToDecimal(real["@val"].ToString()); var avg = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@avg_rawdata", avg); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":
var sum = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); var sum = real["@val"].ToString() == "NaN" ? -1 : Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float);
arrangeRawData.Add("@sum_rawdata", sum); arrangeRawData.Add("@sum_rawdata", sum);
break; break;
} }

View File

@ -114,7 +114,7 @@ namespace FrontendWebApi.ApiControllers
var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})"; var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})";
var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date"; var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date";
var sql = $@"set @i = -1; var sql = $@"set @i = -1;
select fd.device_number, aemm.avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp select fd.device_number, case when aemm.avg_rawdata = -1 then 'NaN' when aemm.avg_rawdata is null then '0' else aemm.avg_rawdata end as avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp
from ( from (
select * select *
from ( from (
@ -161,7 +161,7 @@ namespace FrontendWebApi.ApiControllers
foreach (var l in list) foreach (var l in list)
{ {
l.rawData = new List<HydroMeterRawDataOutput>(); l.rawData = new List<HydroMeterRawDataOutput>();
l.device_full_name = await backendRepository.GetOneAsync<string>($"select full_name from device where device_number = '{l.device_number}' and deleted = 0"); l.device_full_name = await backendRepository.GetOneAsync<string>($"select full_name from device where device_number = '{l.device_number}'");
if (tag_quantity == "5") if (tag_quantity == "5")
{ {
l.rawData.AddRange( l.rawData.AddRange(
@ -176,7 +176,7 @@ namespace FrontendWebApi.ApiControllers
} }
l.building_name = await backendRepository.GetOneAsync<string>("select full_name from building where building_tag = @building_tag and deleted = 0", l.building_name = await backendRepository.GetOneAsync<string>("select full_name from building where building_tag = @building_tag and deleted = 0",
new { building_tag = l.building_tag }); new { building_tag = l.building_tag });
l.total = l.rawData.Sum(x => x.avg_rawdata).ToString(); l.total = l.rawData.Where(x => x.avg_rawdata != "NaN").Sum(x => Decimal.Parse(x.avg_rawdata, System.Globalization.NumberStyles.Float)).ToString();
l.price = input.price.HasValue l.price = input.price.HasValue
? (Math.Round(input.price.Value, 2)).ToString() ? (Math.Round(input.price.Value, 2)).ToString()
: Math.Round((await backendRepository.GetOneAsync<decimal>("select system_value from variable where system_type = 'ElectricPrice' and deleted = 0")), 2).ToString(); : Math.Round((await backendRepository.GetOneAsync<decimal>("select system_value from variable where system_type = 'ElectricPrice' and deleted = 0")), 2).ToString();
@ -286,7 +286,7 @@ namespace FrontendWebApi.ApiControllers
var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})"; var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})";
var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date"; var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date";
var sql = $@"set @i = -1; var sql = $@"set @i = -1;
select fd.device_number, aemm.avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp select fd.device_number, case when aemm.avg_rawdata = -1 then 'NaN' when aemm.avg_rawdata is null then '0' else aemm.avg_rawdata end as avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp
from ( from (
select * select *
from ( from (
@ -334,7 +334,7 @@ namespace FrontendWebApi.ApiControllers
foreach (var l in list) foreach (var l in list)
{ {
l.rawData = new List<HydroMeterRawDataOutput>(); l.rawData = new List<HydroMeterRawDataOutput>();
l.device_full_name = await backendRepository.GetOneAsync<string>($"select full_name from device where device_number = '{l.device_number}' and deleted = 0"); l.device_full_name = await backendRepository.GetOneAsync<string>($"select full_name from device where device_number = '{l.device_number}'");
if (tag_quantity == "5") if (tag_quantity == "5")
{ {
l.rawData.AddRange( l.rawData.AddRange(
@ -349,7 +349,7 @@ namespace FrontendWebApi.ApiControllers
} }
l.building_name = await backendRepository.GetOneAsync<string>("select full_name from building where building_tag = @building_tag and deleted = 0", l.building_name = await backendRepository.GetOneAsync<string>("select full_name from building where building_tag = @building_tag and deleted = 0",
new { building_tag = l.building_tag }); new { building_tag = l.building_tag });
l.total = l.rawData.Sum(x => x.avg_rawdata).ToString(); l.total = l.rawData.Where(x => x.avg_rawdata != "NaN").Sum(x => Decimal.Parse(x.avg_rawdata, System.Globalization.NumberStyles.Float)).ToString();
l.price = input.price.HasValue l.price = input.price.HasValue
? (Math.Round(input.price.Value, 2)).ToString() ? (Math.Round(input.price.Value, 2)).ToString()
: Math.Round((await backendRepository.GetOneAsync<decimal>("select system_value from variable where system_type = 'WaterPrice' and deleted = 0")), 2).ToString(); : Math.Round((await backendRepository.GetOneAsync<decimal>("select system_value from variable where system_type = 'WaterPrice' and deleted = 0")), 2).ToString();

View File

@ -289,7 +289,7 @@ namespace FrontendWebApi.Models
public class HistoryExport public class HistoryExport
{ {
public string deviceName { get; set; } public string deviceName { get; set; }
public double value { get; set; } public string value { get; set; }
public DateTime timestamp { get; set; } public DateTime timestamp { get; set; }
public DateTime starttime { get; set; } public DateTime starttime { get; set; }
public DateTime? endtime { get; set; } public DateTime? endtime { get; set; }

View File

@ -37,7 +37,7 @@ namespace FrontendWebApi.Models
{ {
public string timeStamp { get; set; } public string timeStamp { get; set; }
public string device_number { get; set; } public string device_number { get; set; }
public decimal avg_rawdata { get; set; } public string avg_rawdata { get; set; }
} }
public class HydroMeterPriceInput public class HydroMeterPriceInput