[後端] 修改水電表 判斷日期格式

This commit is contained in:
dev02 2023-05-11 18:25:44 +08:00
parent 1c88b3182e
commit 913aa9cbaf
2 changed files with 24 additions and 9 deletions

View File

@ -32,7 +32,7 @@ namespace FrontendWebApi.ApiControllers
public async Task<ActionResult<ApiResult<List<HydroMeterOutput>>>> MeterList([FromBody] HydroMeterInput input)
{
ApiResult<List<HydroMeterOutput>> apiResult = new ApiResult<List<HydroMeterOutput>>();
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<HydroMeterRawDataOutput>(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<ActionResult<ApiResult<List<HydroMeterOutput>>>> WaterList([FromBody] HydroMeterInput input)
{
ApiResult<List<HydroMeterOutput>> apiResult = new ApiResult<List<HydroMeterOutput>>();
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<HydroMeterRawDataOutput>(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 })

View File

@ -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