2023-05-11 15:46:04 +08:00
using FrontendWebApi.Models ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Logging ;
using Repository.BackendRepository.Interface ;
using Repository.FrontendRepository.Interface ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
namespace FrontendWebApi.ApiControllers
{
public class HydroMeterController : MyBaseApiController < HydroMeterController >
{
private readonly IBackendRepository backendRepository ;
private readonly IFrontendRepository frontendRepository ;
public HydroMeterController ( IBackendRepository backendRepository , IFrontendRepository frontendRepository )
{
this . backendRepository = backendRepository ;
this . frontendRepository = frontendRepository ;
}
/// <summary>
/// 電表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("api/MeterList")]
public async Task < ActionResult < ApiResult < List < HydroMeterOutput > > > > MeterList ( [ FromBody ] HydroMeterInput input )
{
2023-05-12 09:39:44 +08:00
ApiResult < List < HydroMeterOutput > > apiResult = new ApiResult < List < HydroMeterOutput > > ( jwt_str ) ;
if ( ! jwtlife )
{
apiResult . Code = "5000" ;
return BadRequest ( apiResult ) ;
}
2023-05-11 18:25:44 +08:00
string tableType = "day week month year" ;
2023-05-11 15:46:04 +08:00
if ( input . building_tag = = null )
{
apiResult . Code = "9999" ;
apiResult . Msg = "棟別沒有被選取" ;
return BadRequest ( apiResult ) ;
}
2023-05-11 18:40:23 +08:00
else if ( input . tableType = = null | | ! tableType . Contains ( input . tableType ) )
2023-05-11 15:46:04 +08:00
{
apiResult . Code = "9999" ;
apiResult . Msg = "表單類別錯誤" ;
return BadRequest ( apiResult ) ;
}
try
{
2023-05-11 18:25:44 +08:00
var startTime = input . tableType = = "day" | | input . tableType = = "week"
? input . startTime + "-01"
2023-05-12 11:06:55 +08:00
: input . tableType = = "month" | | input . tableType = = "year" ? input . startTime + "-01-01"
: null ;
2023-05-11 18:25:44 +08:00
var endTime = input . tableType = = "day" | | input . tableType = = "week"
2023-05-11 20:05:44 +08:00
? input . startTime . Split ( "-" ) [ 0 ] + "-" + ( Int32 . Parse ( input . startTime . Split ( "-" ) [ 1 ] ) + 1 ) . ToString ( ) . PadLeft ( 2 , '0' ) + "-01"
: input . tableType = = "month" ? ( Int32 . Parse ( input . startTime . Split ( "-" ) [ 0 ] ) + 1 ) + "-01-01"
2023-05-12 11:06:55 +08:00
: input . tableType = = "year" ? input . endTime + "-01-01"
2023-05-11 18:25:44 +08:00
: null ;
2023-05-11 15:46:04 +08:00
string sqlWhere = "" ;
2023-05-12 11:06:55 +08:00
if ( input . floor_tag . Count > 0 )
2023-05-12 13:02:24 +08:00
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag" ;
var table = input . tableType = = "year" ? "archive_electric_meter_day" : "archive_electric_meter_" + input . tableType ;
2023-05-11 18:25:44 +08:00
var dateFormat = input . tableType = = "day" | | input . tableType = = "week" ? "%Y-%m-%d" : input . tableType = = "month" ? "%Y-%m" : input . tableType = = "year" ? "%Y" : null ;
2023-05-11 15:46:04 +08:00
var sql = $ @ "select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp
2023-05-11 18:40:23 +08:00
from { table }
2023-05-11 20:05:44 +08:00
where start_timestamp > = @startTime and end_timestamp < @endTime and point = ' KWH ' and substring_index ( device_number , '_' , 1 ) = @building_tag
2023-05-11 15:46:04 +08:00
{ sqlWhere }
order by created_at desc ; ";
var rawData = await backendRepository . GetAllAsync < HydroMeterRawDataOutput > ( sql ,
2023-05-11 18:25:44 +08:00
new { table = table , starTime = startTime , endtime = endTime , building_tag = input . building_tag , floor_tag = input . floor_tag } ) ;
2023-05-11 15:46:04 +08:00
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 } )
. ToList ( ) ;
foreach ( var l in list )
{
l . rawData = new List < HydroMeterRawDataOutput > ( ) ;
l . rawData . AddRange (
rawData . Where ( x = > x . device_number . Split ( "_" ) [ 0 ] = = l . building_tag & & x . device_number . Split ( "_" ) [ 2 ] = = l . floor_tag & & x . device_number . Split ( "_" ) [ 4 ] = = l . device_serial_tag )
) ;
2023-05-11 17:57:45 +08:00
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 } ) ;
2023-05-11 15:46:04 +08:00
l . total = l . rawData . Count . ToString ( ) ;
2023-05-12 13:02:24 +08:00
l . price = input . price . HasValue
? ( Math . Round ( input . price . Value , 2 ) ) . ToString ( )
: Math . Round ( ( await backendRepository . GetOneAsync < decimal > ( "select system_value from variable where system_type = 'MeterPrice' and deleted = 0" ) ) , 2 ) . ToString ( ) ;
l . total_price = ( l . rawData . Count * Decimal . Parse ( l . price ) ) . ToString ( ) ;
2023-05-11 15:46:04 +08:00
}
apiResult . Code = "0000" ;
apiResult . Data = list ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
string json = System . Text . Json . JsonSerializer . Serialize ( input ) ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + json ) ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message ) ;
}
return apiResult ;
}
/// <summary>
/// 水表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("api/WaterList")]
public async Task < ActionResult < ApiResult < List < HydroMeterOutput > > > > WaterList ( [ FromBody ] HydroMeterInput input )
{
2023-05-12 09:39:44 +08:00
ApiResult < List < HydroMeterOutput > > apiResult = new ApiResult < List < HydroMeterOutput > > ( jwt_str ) ;
if ( ! jwtlife )
{
apiResult . Code = "5000" ;
return BadRequest ( apiResult ) ;
}
2023-05-11 18:25:44 +08:00
string tableType = "day week month year" ;
2023-05-11 15:46:04 +08:00
if ( input . building_tag = = null )
{
apiResult . Code = "9999" ;
apiResult . Msg = "棟別沒有被選取" ;
return BadRequest ( apiResult ) ;
}
else if ( input . tableType = = null | | tableType . Contains ( input . tableType ) )
{
apiResult . Code = "9999" ;
apiResult . Msg = "表單類別錯誤" ;
return BadRequest ( apiResult ) ;
}
try
{
2023-05-11 18:25:44 +08:00
var startTime = input . tableType = = "day" | | input . tableType = = "week"
? input . startTime + "-01"
2023-05-12 11:06:55 +08:00
: input . tableType = = "month" | | input . tableType = = "month" ? input . startTime + "-01-01"
: null ;
2023-05-11 18:25:44 +08:00
var endTime = input . tableType = = "day" | | input . tableType = = "week"
2023-05-11 20:05:44 +08:00
? input . startTime . Split ( "-" ) [ 0 ] + "-" + ( Int32 . Parse ( input . startTime . Split ( "-" ) [ 1 ] ) + 1 ) . ToString ( ) . PadLeft ( 2 , '0' ) + "-01"
: input . tableType = = "month" ? ( Int32 . Parse ( input . startTime . Split ( "-" ) [ 0 ] ) + 1 ) + "-01-01"
2023-05-12 11:06:55 +08:00
: input . tableType = = "year" ? input . endTime + "-01-01"
2023-05-11 18:25:44 +08:00
: null ;
2023-05-11 15:46:04 +08:00
string sqlWhere = "" ;
2023-05-12 11:06:55 +08:00
if ( input . floor_tag . Count > 0 )
2023-05-12 13:02:24 +08:00
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag" ;
var table = input . tableType = = "year" ? "archive_water_meter_day" : "archive_water_meter_" + input . tableType ;
var dateFormat = input . tableType = = "day" | | input . tableType = = "week" ? "%Y-%m-%d" : input . tableType = = "month" ? "%Y-%m" : input . tableType = = "year" ? "%Y" : null ;
2023-05-11 15:46:04 +08:00
var sql = $ @ "select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp
from @table
2023-05-11 20:05:44 +08:00
where start_timestamp > = @startTime and end_timestamp < @endTime and point = ' RCV ' and substring_index ( device_number , '_' , 1 ) = @building_tag
2023-05-11 15:46:04 +08:00
{ sqlWhere }
order by created_at desc ; ";
var rawData = await backendRepository . GetAllAsync < HydroMeterRawDataOutput > ( sql ,
2023-05-11 18:25:44 +08:00
new { table = table , starTime = startTime , endTime = endTime , building_tag = input . building_tag , floor_tag = input . floor_tag , dateFormat = dateFormat } ) ;
2023-05-11 15:46:04 +08:00
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 } )
. ToList ( ) ;
foreach ( var l in list )
{
l . rawData = new List < HydroMeterRawDataOutput > ( ) ;
l . rawData . AddRange (
rawData . Where ( x = > x . device_number . Split ( "_" ) [ 0 ] = = l . building_tag & & x . device_number . Split ( "_" ) [ 2 ] = = l . floor_tag & & x . device_number . Split ( "_" ) [ 4 ] = = l . device_serial_tag )
) ;
2023-05-11 17:57:45 +08:00
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 } ) ;
2023-05-11 15:46:04 +08:00
l . total = l . rawData . Count . ToString ( ) ;
2023-05-12 13:02:24 +08:00
l . price = input . price . HasValue
? ( 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 ( ) ;
2023-05-11 15:46:04 +08:00
l . total_price = ( l . rawData . Count * Int32 . Parse ( l . price ) ) . ToString ( ) ;
}
apiResult . Code = "0000" ;
apiResult . Data = list ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
string json = System . Text . Json . JsonSerializer . Serialize ( input ) ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + json ) ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message ) ;
}
return apiResult ;
}
}
}