From 913aa9cbaf975ed186c1ced5d3fc0e249964232f Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 11 May 2023 18:25:44 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B0=B4=E9=9B=BB=E8=A1=A8=20=E5=88=A4=E6=96=B7=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HydroMeterController.cs | 30 ++++++++++++++----- FrontendWebApi/Models/HydroMeter.cs | 3 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index 213ac3e..8ff4b5e 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -32,7 +32,7 @@ namespace FrontendWebApi.ApiControllers public async Task>>> MeterList([FromBody] HydroMeterInput input) { ApiResult> apiResult = new ApiResult>(); - string tableType = "day week month"; + string tableType = "day week month year"; if (input.building_tag == null) { apiResult.Code = "9999"; @@ -48,18 +48,26 @@ namespace FrontendWebApi.ApiControllers try { + var startTime = input.tableType == "day" || input.tableType == "week" + ? input.startTime + "-01" + : input.tableType == "month" ? input.startTime + "-01-01" + : null; ; + var endTime = input.tableType == "day" || input.tableType == "week" + ? input.startTime.Split("-")[0] + (input.startTime.Split("-")[1] + 1).ToString() + "-01" + : input.tableType == "month" ? (input.startTime.Split("-")[0] + 1) + "-01-01" + : null; string sqlWhere = ""; if (input.floor_tag != null) sqlWhere = $@" and substring_index(device_number, '_', 3) == @floor_tag"; var table = "archive_electric_meter_" + input.tableType; - var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : null; + var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null; var sql = $@"select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp from @table - where start_timestamp >= @startTime and end_timestamp <= @endTime and point = 'KWH' and substring_index(device_number, '_', 1) == @building_tag + where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' and substring_index(device_number, '_', 1) == @building_tag {sqlWhere} order by created_at desc;"; var rawData = await backendRepository.GetAllAsync(sql, - new { table = table, starTime = input.startTime, endTime = input.endTime, building_tag = input.building_tag, floor_tag = input.floor_tag }); + new { table = table, starTime = startTime, endtime = endTime, building_tag = input.building_tag, floor_tag = input.floor_tag }); var list = rawData .GroupBy(x => new { building_tag = x.device_number.Split("_")[0], floor_tag = x.device_number.Split("_")[2], device_serial_tag = x.device_number.Split("_")[4] }) .Select(x => new HydroMeterOutput { building_tag = x.Key.building_tag, floor_tag = x.Key.floor_tag, device_serial_tag = x.Key.device_serial_tag }) @@ -102,7 +110,7 @@ namespace FrontendWebApi.ApiControllers public async Task>>> WaterList([FromBody] HydroMeterInput input) { ApiResult> apiResult = new ApiResult>(); - string tableType = "day week month"; + string tableType = "day week month year"; if (input.building_tag == null) { apiResult.Code = "9999"; @@ -118,6 +126,14 @@ namespace FrontendWebApi.ApiControllers try { + var startTime = input.tableType == "day" || input.tableType == "week" + ? input.startTime + "-01" + : input.tableType == "month" ? input.startTime + "-01-01" + : null; ; + var endTime = input.tableType == "day" || input.tableType == "week" + ? input.startTime.Split("-")[0] + (input.startTime.Split("-")[1] + 1).ToString() + "-01" + : input.tableType == "month" ? (input.startTime.Split("-")[0] + 1) + "-01-01" + : null; string sqlWhere = ""; if (input.floor_tag != null) sqlWhere = $@" and substring_index(device_number, '_', 3) == @floor_tag"; @@ -125,11 +141,11 @@ namespace FrontendWebApi.ApiControllers var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : null; var sql = $@"select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp from @table - where start_timestamp >= @startTime and end_timestamp <= @endTime and point = 'RCV' and substring_index(device_number, '_', 1) == @building_tag + where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'RCV' and substring_index(device_number, '_', 1) == @building_tag {sqlWhere} order by created_at desc;"; var rawData = await backendRepository.GetAllAsync(sql, - new { table = table, starTime = input.startTime, endTime = input.endTime, building_tag = input.building_tag, floor_tag = input.floor_tag, dateFormat = dateFormat }); + new { table = table, starTime = startTime, endTime = endTime, building_tag = input.building_tag, floor_tag = input.floor_tag, dateFormat = dateFormat }); var list = rawData .GroupBy(x => new { building_tag = x.device_number.Split("_")[0], floor_tag = x.device_number.Split("_")[2], device_serial_tag = x.device_number.Split("_")[4] }) .Select(x => new HydroMeterOutput { building_tag = x.Key.building_tag, floor_tag = x.Key.floor_tag, device_serial_tag = x.Key.device_serial_tag }) diff --git a/FrontendWebApi/Models/HydroMeter.cs b/FrontendWebApi/Models/HydroMeter.cs index ebafece..f130d28 100644 --- a/FrontendWebApi/Models/HydroMeter.cs +++ b/FrontendWebApi/Models/HydroMeter.cs @@ -13,8 +13,7 @@ namespace FrontendWebApi.Models public string tableType { get; set; } //day, week, month public string building_tag { get; set; } public string floor_tag { get; set; } - public DateTime startTime { get; set; } - public DateTime endTime { get; set; } + public string startTime { get; set; } } public class HydroMeterOutput