2023-05-11 15:46:04 +08:00
using FrontendWebApi.Models ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Logging ;
2023-06-09 15:33:33 +08:00
using MySqlX.XDevAPI ;
2023-05-12 18:42:22 +08:00
using NPOI.SS.UserModel ;
using NPOI.XSSF.UserModel ;
2023-05-11 15:46:04 +08:00
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]
2023-05-12 18:42:22 +08:00
[Route("api/ElectricList")]
public async Task < ActionResult < ApiResult < List < HydroMeterOutput > > > > ElectricList ( [ FromBody ] HydroMeterInput input )
2023-05-11 15:46:04 +08:00
{
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-12 18:42:22 +08:00
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 ) ;
}
2023-05-15 15:18:43 +08:00
else if ( input . floor_tag . Count = = 0 )
{
apiResult . Code = "0000" ;
apiResult . Data = new List < HydroMeterOutput > ( ) { } ;
return Ok ( apiResult ) ;
}
2023-05-11 15:46:04 +08:00
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-15 17:15:24 +08:00
: input . tableType = = "year" ? ( Int32 . Parse ( input . endTime ) + 1 ) . ToString ( ) + "-01-01"
2023-05-11 18:25:44 +08:00
: null ;
2023-05-11 15:46:04 +08:00
string sqlWhere = "" ;
2023-05-12 18:42:22 +08:00
string sqlGroup = "" ;
string sqlAvgRawData = "" ;
2023-05-29 16:33:47 +08:00
string dbDateName = startTime . Split ( "-" ) [ 0 ] . ToString ( ) . PadLeft ( 4 , '0' ) + startTime . Split ( "-" ) [ 1 ] . ToString ( ) . PadLeft ( 2 , '0' ) ;
2023-06-08 11:39:17 +08:00
string buildingSql = "" ;
string tag_quantity = await backendRepository . GetOneAsync < string > ( "select system_value from variable where system_type = 'obixConfig' and system_key = 'tag_quantity' and deleted = 0" ) ;
if ( tag_quantity = = "5" )
buildingSql = " and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag " ;
else
buildingSql = " and SUBSTRING_INDEX(SUBSTRING_INDEX(device_number, '_', 2), '_', -1) = @building_tag " ;
2023-05-29 16:33:47 +08:00
2023-05-12 11:06:55 +08:00
if ( input . floor_tag . Count > 0 )
2023-06-08 11:39:17 +08:00
{
if ( tag_quantity = = "5" )
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag " ;
else
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 5), '_', -1) in @floor_tag " ;
}
2023-05-12 18:42:22 +08:00
if ( input . tableType = = "year" )
{
2023-05-15 13:27:13 +08:00
sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number " ;
sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp " ;
2023-05-12 18:42:22 +08:00
}
else
2023-05-15 13:27:13 +08:00
sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp " ;
2023-05-12 18:42:22 +08:00
2023-05-29 16:33:47 +08:00
var table = input . tableType = = "year" ? "archive_electric_meter_month" : "archive_electric_meter_" + input . tableType + ( input . tableType = = "day" ? "_" + dbDateName : "" ) ;
2023-06-09 15:33:33 +08:00
var schema = await backendRepository . GetOneAsync < string > ( $"select system_value from variable where system_type = 'project_name'" ) ;
var isTable = await backendRepository . GetOneAsync < string > ( $"select table_name from information_schema.tables where table_name = '{table}' and table_schema = '{schema.Split('/')[0]}'" ) ;
if ( string . IsNullOrEmpty ( isTable ) ) //check for has table or not
{
apiResult . Code = "0000" ;
apiResult . Data = new List < HydroMeterOutput > ( ) { } ;
return Ok ( apiResult ) ;
}
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-22 18:12:40 +08:00
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" ;
2023-05-12 18:42:22 +08:00
var sql = $ @ "set @i = -1;
select fd . device_number , aemm . avg_rawdata , DATE_FORMAT ( fd . date , @dateFormat ) as timestamp
from (
select *
from (
(
SELECT DATE ( ADDDATE ( @startTime , INTERVAL @i : = @i + 1 { input . tableType } ) ) AS date
FROM { table }
HAVING @i < TIMESTAMPDIFF ( { input . tableType } , @startTime , ADDDATE ( @endTime , INTERVAL - 1 DAY ) )
) d ,
(
select device_number
from { table }
2023-06-08 11:39:17 +08:00
where start_timestamp > = @startTime and end_timestamp < @endTime and point = ' KWH ' { buildingSql }
2023-05-12 18:42:22 +08:00
{ sqlWhere }
group by device_number
) dn
)
) fd
left join (
2023-05-15 13:27:13 +08:00
select device_number , { sqlAvgRawData }
2023-05-12 18:42:22 +08:00
from { table }
2023-06-08 11:39:17 +08:00
where start_timestamp > = @startTime and end_timestamp < @endTime and point = ' KWH ' { buildingSql }
2023-05-12 18:42:22 +08:00
{ sqlWhere } { sqlGroup }
2023-05-22 18:12:40 +08:00
) aemm on aemm . start_timestamp > = { aemmStaDate } and aemm . end_timestamp < { aemmEndDate } and aemm . device_number = fd . device_number
2023-05-12 18:42:22 +08:00
order by fd . device_number , fd . date ";
2023-05-11 15:46:04 +08:00
var rawData = await backendRepository . GetAllAsync < HydroMeterRawDataOutput > ( sql ,
2023-05-12 14:55:43 +08:00
new { startTime = startTime , endtime = endTime , building_tag = input . building_tag , floor_tag = input . floor_tag , dateFormat = dateFormat } ) ;
2023-06-08 11:49:21 +08:00
List < HydroMeterOutput > list = new List < HydroMeterOutput > ( ) ;
if ( tag_quantity = = "5" )
{
list = rawData
2023-05-11 15:46:04 +08:00
. 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 ( ) ;
2023-06-08 11:49:21 +08:00
}
else
{
list = rawData
2023-06-08 11:56:50 +08:00
. GroupBy ( x = > new { building_tag = x . device_number . Split ( "_" ) [ 1 ] , floor_tag = x . device_number . Split ( "_" ) [ 4 ] , device_serial_tag = x . device_number . Split ( "_" ) [ 7 ] } )
2023-06-08 11:49:21 +08:00
. 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 ( ) ;
}
2023-05-11 15:46:04 +08:00
foreach ( var l in list )
{
l . rawData = new List < HydroMeterRawDataOutput > ( ) ;
2023-06-08 11:49:21 +08:00
if ( tag_quantity = = "5" )
{
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 )
) ;
}
else
{
l . rawData . AddRange (
2023-06-08 11:56:50 +08:00
rawData . Where ( x = > x . device_number . Split ( "_" ) [ 1 ] = = l . building_tag & & x . device_number . Split ( "_" ) [ 4 ] = = l . floor_tag & & x . device_number . Split ( "_" ) [ 7 ] = = l . device_serial_tag )
2023-06-08 11:49:21 +08:00
) ;
}
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-12 18:42:22 +08:00
l . total = l . rawData . Sum ( x = > x . avg_rawdata ) . ToString ( ) ;
2023-05-12 13:02:24 +08:00
l . price = input . price . HasValue
? ( Math . Round ( input . price . Value , 2 ) ) . ToString ( )
2023-05-12 18:42:22 +08:00
: Math . Round ( ( await backendRepository . GetOneAsync < decimal > ( "select system_value from variable where system_type = 'ElectricPrice' and deleted = 0" ) ) , 2 ) . ToString ( ) ;
l . total_price = Math . Round ( ( Decimal . Parse ( l . total ) * Decimal . Parse ( l . price ) ) , 2 ) . 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 ) ;
}
2023-05-15 12:49:15 +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 ) ;
}
2023-05-15 15:18:43 +08:00
else if ( input . floor_tag . Count = = 0 )
{
apiResult . Code = "0000" ;
apiResult . Data = new List < HydroMeterOutput > ( ) { } ;
return Ok ( apiResult ) ;
}
2023-05-11 15:46:04 +08:00
try
{
2023-05-11 18:25:44 +08:00
var startTime = input . tableType = = "day" | | input . tableType = = "week"
? input . startTime + "-01"
2023-05-15 12:49:15 +08:00
: input . tableType = = "month" | | input . tableType = = "year" ? input . startTime + "-01-01"
2023-05-12 11:06:55 +08:00
: 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-15 17:15:24 +08:00
: input . tableType = = "year" ? ( Int32 . Parse ( input . endTime ) + 1 ) . ToString ( ) + "-01-01"
2023-05-11 18:25:44 +08:00
: null ;
2023-05-29 16:33:47 +08:00
string dbDateName = startTime . Split ( "-" ) [ 0 ] . ToString ( ) . PadLeft ( 4 , '0' ) + startTime . Split ( "-" ) [ 1 ] . ToString ( ) . PadLeft ( 2 , '0' ) ;
2023-05-11 15:46:04 +08:00
string sqlWhere = "" ;
2023-05-12 18:42:22 +08:00
string sqlGroup = "" ;
string sqlAvgRawData = "" ;
2023-06-08 11:40:00 +08:00
string buildingSql = "" ;
string tag_quantity = await backendRepository . GetOneAsync < string > ( "select system_value from variable where system_type = 'obixConfig' and system_key = 'tag_quantity' and deleted = 0" ) ;
if ( tag_quantity = = "5" )
buildingSql = " and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag " ;
else
buildingSql = " and SUBSTRING_INDEX(SUBSTRING_INDEX(device_number, '_', 2), '_', -1) = @building_tag " ;
2023-05-12 11:06:55 +08:00
if ( input . floor_tag . Count > 0 )
2023-06-08 11:40:00 +08:00
{
if ( tag_quantity = = "5" )
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag " ;
else
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 5), '_', -1) in @floor_tag " ;
}
2023-05-12 18:42:22 +08:00
if ( input . tableType = = "year" )
{
2023-05-15 13:27:13 +08:00
sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number " ;
sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp " ;
2023-05-12 18:42:22 +08:00
}
else
2023-05-15 13:27:13 +08:00
sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp " ;
2023-05-12 18:42:22 +08:00
2023-05-29 16:33:47 +08:00
var table = input . tableType = = "year" ? "archive_electric_meter_month" : "archive_electric_meter_" + input . tableType + ( input . tableType = = "day" ? "_" + dbDateName : "" ) ;
2023-06-09 15:33:33 +08:00
var schema = await backendRepository . GetOneAsync < string > ( $"select system_value from variable where system_type = 'project_name'" ) ;
var isTable = await backendRepository . GetOneAsync < string > ( $"select table_name from information_schema.tables where table_name = '{table}' and table_schema = '{schema.Split('/')[0]}'" ) ;
if ( string . IsNullOrEmpty ( isTable ) ) //check for has table or not
{
apiResult . Code = "0000" ;
apiResult . Data = new List < HydroMeterOutput > ( ) { } ;
return Ok ( apiResult ) ;
}
2023-05-12 13:02:24 +08:00
var dateFormat = input . tableType = = "day" | | input . tableType = = "week" ? "%Y-%m-%d" : input . tableType = = "month" ? "%Y-%m" : input . tableType = = "year" ? "%Y" : null ;
2023-06-09 15:33:33 +08:00
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" ;
2023-05-12 18:42:22 +08:00
var sql = $ @ "set @i = -1;
select fd . device_number , aemm . avg_rawdata , DATE_FORMAT ( fd . date , @dateFormat ) as timestamp
from (
select *
from (
(
SELECT DATE ( ADDDATE ( @startTime , INTERVAL @i : = @i + 1 { input . tableType } ) ) AS date
FROM { table }
HAVING @i < TIMESTAMPDIFF ( { input . tableType } , @startTime , ADDDATE ( @endTime , INTERVAL - 1 DAY ) )
) d ,
(
select device_number
from { table }
2023-06-08 11:40:00 +08:00
where start_timestamp > = @startTime and end_timestamp < @endTime and point = ' RCV ' { buildingSql }
2023-05-12 18:42:22 +08:00
{ sqlWhere }
group by device_number
) dn
)
) fd
left join (
2023-05-15 13:27:13 +08:00
select device_number , { sqlAvgRawData }
2023-05-12 18:42:22 +08:00
from { table }
2023-06-09 15:33:33 +08:00
where start_timestamp > = { aemmStaDate } and end_timestamp < { aemmEndDate } and point = ' RCV ' { buildingSql }
2023-05-12 18:42:22 +08:00
{ sqlWhere } { sqlGroup }
) aemm on aemm . start_timestamp > = fd . date and aemm . end_timestamp < DATE_ADD ( fd . date , INTERVAL + 1 { input . tableType } ) and aemm . device_number = fd . device_number
order by fd . device_number , fd . date ";
2023-05-11 15:46:04 +08:00
var rawData = await backendRepository . GetAllAsync < HydroMeterRawDataOutput > ( sql ,
2023-05-12 15:00:57 +08:00
new { startTime = startTime , endTime = endTime , building_tag = input . building_tag , floor_tag = input . floor_tag , dateFormat = dateFormat } ) ;
2023-06-08 11:49:21 +08:00
List < HydroMeterOutput > list = new List < HydroMeterOutput > ( ) ;
if ( tag_quantity = = "5" )
{
list = rawData
2023-05-11 15:46:04 +08:00
. 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 ( ) ;
2023-06-08 11:49:21 +08:00
}
else
{
list = rawData
2023-06-08 11:56:50 +08:00
. GroupBy ( x = > new { building_tag = x . device_number . Split ( "_" ) [ 1 ] , floor_tag = x . device_number . Split ( "_" ) [ 4 ] , device_serial_tag = x . device_number . Split ( "_" ) [ 7 ] } )
2023-06-08 11:49:21 +08:00
. 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 ( ) ;
}
2023-05-11 15:46:04 +08:00
foreach ( var l in list )
{
l . rawData = new List < HydroMeterRawDataOutput > ( ) ;
2023-06-08 11:49:21 +08:00
if ( tag_quantity = = "5" )
{
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 )
) ;
}
else
{
l . rawData . AddRange (
2023-06-08 11:56:50 +08:00
rawData . Where ( x = > x . device_number . Split ( "_" ) [ 1 ] = = l . building_tag & & x . device_number . Split ( "_" ) [ 4 ] = = l . floor_tag & & x . device_number . Split ( "_" ) [ 7 ] = = l . device_serial_tag )
2023-06-08 11:49:21 +08:00
) ;
}
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-12 18:42:22 +08:00
l . total = l . rawData . Sum ( x = > x . avg_rawdata ) . 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-12 18:42:22 +08:00
l . total_price = Math . Round ( ( Decimal . Parse ( l . total ) * Decimal . Parse ( l . price ) ) , 2 ) . 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 ;
}
2023-05-12 18:42:22 +08:00
/// <summary>
/// 水電表費用
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("api/HydroMeterPrice")]
public async Task < ActionResult < ApiResult < decimal > > > Price ( [ FromBody ] HydroMeterPriceInput input )
{
ApiResult < decimal > apiResult = new ApiResult < decimal > ( jwt_str ) ;
if ( ! jwtlife )
{
apiResult . Code = "5000" ;
return BadRequest ( apiResult ) ;
}
try
{
apiResult . Code = "0000" ;
2023-05-15 15:18:43 +08:00
apiResult . Data = await backendRepository . GetOneAsync < decimal > ( $@"select system_value from variable where system_type = '{input.type}Price' and deleted = 0" ) ;
2023-05-12 18:42:22 +08:00
}
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 ;
}
[HttpPost]
[Route("api/ExportElectricList")]
2023-05-15 12:04:42 +08:00
public FileResult OpeExportExcelElec ( [ FromBody ] HydroMeterInput input )
2023-05-12 18:42:22 +08:00
{
var result = this . ElectricList ( input ) . Result . Value . Data . ToList ( ) ;
var workbook = new XSSFWorkbook ( ) ;
#region excel設定
IFont font12 = workbook . CreateFont ( ) ;
font12 . FontName = "新細明體" ;
font12 . FontHeightInPoints = 12 ;
ICellStyle style12 = workbook . CreateCellStyle ( ) ;
style12 . SetFont ( font12 ) ;
style12 . Alignment = HorizontalAlignment . Center ;
style12 . VerticalAlignment = VerticalAlignment . Center ;
IFont font12Times = workbook . CreateFont ( ) ;
font12Times . FontName = "Times New Roman" ;
font12Times . FontHeightInPoints = 12 ;
IFont font18 = workbook . CreateFont ( ) ;
font18 . FontName = "新細明體" ;
font18 . FontHeightInPoints = 18 ;
font18 . IsBold = true ;
ICellStyle styleTitle18 = workbook . CreateCellStyle ( ) ;
styleTitle18 . SetFont ( font18 ) ;
styleTitle18 . Alignment = HorizontalAlignment . Center ;
styleTitle18 . VerticalAlignment = VerticalAlignment . Center ;
ICellStyle styleLeft12 = workbook . CreateCellStyle ( ) ;
styleLeft12 . SetFont ( font12 ) ;
styleLeft12 . Alignment = HorizontalAlignment . Left ;
styleLeft12 . VerticalAlignment = VerticalAlignment . Center ;
ICellStyle styleLine12 = workbook . CreateCellStyle ( ) ;
styleLine12 . SetFont ( font12 ) ;
styleLine12 . Alignment = NPOI . SS . UserModel . HorizontalAlignment . Center ;
styleLine12 . VerticalAlignment = VerticalAlignment . Center ;
styleLine12 . BorderTop = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderBottom = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderRight = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderLeft = NPOI . SS . UserModel . BorderStyle . Thin ;
ICellStyle stylein12 = workbook . CreateCellStyle ( ) ;
stylein12 . SetFont ( font12Times ) ;
stylein12 . Alignment = NPOI . SS . UserModel . HorizontalAlignment . Left ;
stylein12 . VerticalAlignment = VerticalAlignment . Center ;
stylein12 . BorderTop = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderBottom = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderRight = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderLeft = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . WrapText = true ;
#endregion
var sheet = workbook . CreateSheet ( "電表報表" ) ;
int RowPosition = 0 ;
if ( result . Count > 0 )
{
#region set cell
IRow row = sheet . CreateRow ( RowPosition ) ;
sheet . SetColumnWidth ( 0 , 4 * 160 * 12 ) ;
sheet . SetColumnWidth ( 1 , 4 * 160 * 12 ) ;
sheet . SetColumnWidth ( 2 , 4 * 160 * 12 ) ;
int i = 0 ;
ICell cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( "棟別" ) ;
2023-05-12 18:42:22 +08:00
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "樓層" ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "設備" ) ;
cell . CellStyle = styleLine12 ;
2023-05-15 12:04:42 +08:00
foreach ( var rr in result . FirstOrDefault ( ) . rawData )
{
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( rr . timeStamp ) ;
cell . CellStyle = styleLine12 ;
}
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "小計" ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "單價" ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "金額總計" ) ;
cell . CellStyle = styleLine12 ;
#endregion
2023-05-12 18:42:22 +08:00
foreach ( var r in result )
{
2023-05-15 12:04:42 +08:00
RowPosition + = 1 ;
int k = 3 ;
row = sheet . CreateRow ( RowPosition ) ;
for ( int j = 0 ; j < = i ; j + + )
2023-05-12 18:42:22 +08:00
{
2023-05-15 12:04:42 +08:00
cell = row . CreateCell ( j ) ;
if ( j = = 0 )
{
cell . SetCellValue ( r . building_name ) ;
}
if ( j = = 1 )
{
cell . SetCellValue ( r . floor_tag ) ;
}
if ( j = = 2 )
{
cell . SetCellValue ( r . device_serial_tag ) ;
}
if ( j = = 3 )
{
foreach ( var rr in r . rawData )
{
cell . SetCellValue ( rr . avg_rawdata . ToString ( ) ) ;
j + + ;
k + + ;
cell = row . CreateCell ( j ) ;
}
}
if ( j = = k )
{
cell . SetCellValue ( r . total ) ;
}
if ( j = = k + 1 )
{
cell . SetCellValue ( r . price ) ;
}
if ( j = = k + 2 )
{
cell . SetCellValue ( r . total_price ) ;
}
cell . CellStyle = style12 ;
2023-05-12 18:42:22 +08:00
}
}
2023-05-15 12:04:42 +08:00
}
var ms = new NpoiMemoryStream
{
AllowClose = false
} ;
workbook . Write ( ms ) ;
ms . Flush ( ) ;
ms . Seek ( 0 , SeekOrigin . Begin ) ;
2023-05-15 17:10:17 +08:00
Response . Headers . Add ( "Access-Control-Expose-Headers" , "Content-Disposition" ) ;
2023-05-15 12:04:42 +08:00
return File ( ms , "application/vnd.ms-excel" , "電表報表.xlsx" ) ;
}
2023-05-15 17:10:17 +08:00
[HttpPost]
2023-06-12 16:02:18 +08:00
[Route("api/ExportElectricCompareList")]
public FileResult OpeExportCompareExcelElec ( [ FromBody ] List < HydroMeterInput > input )
2023-05-15 12:04:42 +08:00
{
2023-06-12 16:02:18 +08:00
var result = new List < HydroMeterOutput > ( ) ;
var result1 = this . ElectricList ( input [ 0 ] ) . Result . Value . Data . ToList ( ) ;
var result2 = this . ElectricList ( input [ 1 ] ) . Result . Value . Data . ToList ( ) ;
Dictionary < string , List < string > > compareDict = new Dictionary < string , List < string > > ( ) ;
2023-05-15 12:04:42 +08:00
var workbook = new XSSFWorkbook ( ) ;
#region excel設定
IFont font12 = workbook . CreateFont ( ) ;
font12 . FontName = "新細明體" ;
font12 . FontHeightInPoints = 12 ;
ICellStyle style12 = workbook . CreateCellStyle ( ) ;
style12 . SetFont ( font12 ) ;
style12 . Alignment = HorizontalAlignment . Center ;
style12 . VerticalAlignment = VerticalAlignment . Center ;
IFont font12Times = workbook . CreateFont ( ) ;
font12Times . FontName = "Times New Roman" ;
font12Times . FontHeightInPoints = 12 ;
IFont font18 = workbook . CreateFont ( ) ;
font18 . FontName = "新細明體" ;
font18 . FontHeightInPoints = 18 ;
font18 . IsBold = true ;
ICellStyle styleTitle18 = workbook . CreateCellStyle ( ) ;
styleTitle18 . SetFont ( font18 ) ;
styleTitle18 . Alignment = HorizontalAlignment . Center ;
styleTitle18 . VerticalAlignment = VerticalAlignment . Center ;
ICellStyle styleLeft12 = workbook . CreateCellStyle ( ) ;
styleLeft12 . SetFont ( font12 ) ;
styleLeft12 . Alignment = HorizontalAlignment . Left ;
styleLeft12 . VerticalAlignment = VerticalAlignment . Center ;
ICellStyle styleLine12 = workbook . CreateCellStyle ( ) ;
styleLine12 . SetFont ( font12 ) ;
styleLine12 . Alignment = NPOI . SS . UserModel . HorizontalAlignment . Center ;
styleLine12 . VerticalAlignment = VerticalAlignment . Center ;
styleLine12 . BorderTop = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderBottom = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderRight = NPOI . SS . UserModel . BorderStyle . Thin ;
styleLine12 . BorderLeft = NPOI . SS . UserModel . BorderStyle . Thin ;
ICellStyle stylein12 = workbook . CreateCellStyle ( ) ;
stylein12 . SetFont ( font12Times ) ;
stylein12 . Alignment = NPOI . SS . UserModel . HorizontalAlignment . Left ;
stylein12 . VerticalAlignment = VerticalAlignment . Center ;
stylein12 . BorderTop = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderBottom = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderRight = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . BorderLeft = NPOI . SS . UserModel . BorderStyle . Thin ;
stylein12 . WrapText = true ;
#endregion
2023-06-12 16:02:18 +08:00
var rawDataTitle = new List < string > ( ) ;
if ( input [ 0 ] . tableType = = "month" )
{
for ( var i = 1 ; i < = 12 ; i + + )
{
rawDataTitle . Add ( input [ 0 ] . startTime + "-" + i . ToString ( ) . PadLeft ( 2 , '0' ) ) ;
rawDataTitle . Add ( input [ 1 ] . startTime + "-" + i . ToString ( ) . PadLeft ( 2 , '0' ) ) ;
}
}
else if ( input [ 0 ] . tableType = = "day" )
{
var days = GetDayInMonth ( input [ 0 ] . startTime ) ;
for ( var i = 1 ; i < = days ; i + + ) {
rawDataTitle . Add ( input [ 0 ] . startTime + "-" + i . ToString ( ) . PadLeft ( 2 , '0' ) ) ;
rawDataTitle . Add ( input [ 1 ] . startTime + "-" + i . ToString ( ) . PadLeft ( 2 , '0' ) ) ;
}
}
2023-05-15 12:04:42 +08:00
var sheet = workbook . CreateSheet ( "電表報表" ) ;
int RowPosition = 0 ;
2023-06-12 16:02:18 +08:00
if ( result1 . Count > 0 | | result2 . Count > 0 )
2023-05-15 12:04:42 +08:00
{
2023-06-12 16:02:18 +08:00
result = result1 ;
foreach ( var r2 in result2 )
{
var target = result . Where ( r = > r . building_tag = = r2 . building_tag & & r . device_serial_tag = = r2 . device_serial_tag & & r . floor_tag = = r2 . floor_tag ) . FirstOrDefault ( ) ;
compareDict [ r2 . building_tag + r2 . floor_tag + r2 . device_serial_tag ] = new List < string > ( ) { r2 . total , r2 . price , r2 . total_price } ;
if ( target ! = null )
{
target . rawData = target . rawData . Concat ( r2 . rawData ) . ToList ( ) ;
}
else {
r2 . total = null ;
r2 . price = null ;
r2 . total_price = null ;
result . Add ( r2 ) ;
}
}
2023-05-15 12:04:42 +08:00
#region set cell
IRow row = sheet . CreateRow ( RowPosition ) ;
sheet . SetColumnWidth ( 0 , 4 * 160 * 12 ) ;
sheet . SetColumnWidth ( 1 , 4 * 160 * 12 ) ;
sheet . SetColumnWidth ( 2 , 4 * 160 * 12 ) ;
int i = 0 ;
ICell cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( "棟別" ) ;
2023-05-15 12:04:42 +08:00
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "樓層" ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( "設備" ) ;
cell . CellStyle = styleLine12 ;
2023-06-12 16:02:18 +08:00
foreach ( var rr in rawDataTitle )
2023-05-15 12:04:42 +08:00
{
2023-06-12 16:02:18 +08:00
sheet . SetColumnWidth ( i , 2 * 160 * 12 ) ;
2023-05-15 12:04:42 +08:00
cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( rr ) ;
2023-05-15 12:04:42 +08:00
cell . CellStyle = styleLine12 ;
}
2023-05-12 18:42:22 +08:00
cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( input [ 0 ] . startTime + "小計" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( input [ 1 ] . startTime + "小計" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
2023-05-12 18:42:22 +08:00
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( input [ 0 ] . startTime + "單價" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
2023-05-12 18:42:22 +08:00
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( input [ 1 ] . startTime + "單價" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( input [ 0 ] . startTime + "金額總計" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
cell . CellStyle = styleLine12 ;
cell = row . CreateCell ( i + + ) ;
cell . SetCellValue ( input [ 1 ] . startTime + "金額總計" ) ;
sheet . SetColumnWidth ( i - 1 , 2 * 160 * 12 ) ;
2023-05-12 18:42:22 +08:00
cell . CellStyle = styleLine12 ;
#endregion
2023-06-12 16:02:18 +08:00
2023-05-12 18:42:22 +08:00
foreach ( var r in result )
{
2023-06-12 16:02:18 +08:00
string compareDictKey = r . building_tag + r . floor_tag + r . device_serial_tag ;
2023-05-12 18:42:22 +08:00
RowPosition + = 1 ;
2023-05-15 12:04:42 +08:00
int k = 3 ;
2023-05-12 18:42:22 +08:00
row = sheet . CreateRow ( RowPosition ) ;
for ( int j = 0 ; j < = i ; j + + )
{
cell = row . CreateCell ( j ) ;
if ( j = = 0 )
{
cell . SetCellValue ( r . building_name ) ;
}
if ( j = = 1 )
{
cell . SetCellValue ( r . floor_tag ) ;
}
if ( j = = 2 )
{
cell . SetCellValue ( r . device_serial_tag ) ;
}
if ( j = = 3 )
{
2023-06-12 16:02:18 +08:00
foreach ( var rr in rawDataTitle )
2023-05-12 18:42:22 +08:00
{
2023-06-12 16:02:18 +08:00
var targetRaw = r . rawData . Where ( rd = > rd . timeStamp = = rr ) . FirstOrDefault ( ) ;
cell . SetCellValue ( targetRaw ? . avg_rawdata . ToString ( ) ? ? "" ) ;
2023-05-15 12:04:42 +08:00
j + + ;
k + + ;
cell = row . CreateCell ( j ) ;
2023-05-12 18:42:22 +08:00
}
}
2023-05-15 12:04:42 +08:00
if ( j = = k )
2023-05-12 18:42:22 +08:00
{
cell . SetCellValue ( r . total ) ;
}
2023-05-15 12:04:42 +08:00
if ( j = = k + 1 )
2023-05-12 18:42:22 +08:00
{
2023-06-12 16:02:18 +08:00
cell . SetCellValue ( compareDict . ContainsKey ( compareDictKey ) ? compareDict [ compareDictKey ] [ 0 ] : "" ) ;
2023-05-12 18:42:22 +08:00
}
2023-05-15 12:04:42 +08:00
if ( j = = k + 2 )
2023-06-12 16:02:18 +08:00
{
cell . SetCellValue ( r . price ) ;
}
if ( j = = k + 3 )
{
cell . SetCellValue ( compareDict . ContainsKey ( compareDictKey ) ? compareDict [ compareDictKey ] [ 1 ] : "" ) ;
}
if ( j = = k + 4 )
2023-05-12 18:42:22 +08:00
{
cell . SetCellValue ( r . total_price ) ;
}
2023-06-12 16:02:18 +08:00
if ( j = = k + 5 )
{
cell . SetCellValue ( compareDict . ContainsKey ( compareDictKey ) ? compareDict [ compareDictKey ] [ 2 ] : "" ) ;
}
2023-05-12 18:42:22 +08:00
cell . CellStyle = style12 ;
}
}
}
var ms = new NpoiMemoryStream
{
AllowClose = false
} ;
workbook . Write ( ms ) ;
ms . Flush ( ) ;
ms . Seek ( 0 , SeekOrigin . Begin ) ;
2023-05-15 17:10:17 +08:00
Response . Headers . Add ( "Access-Control-Expose-Headers" , "Content-Disposition" ) ;
2023-06-12 16:02:18 +08:00
return File ( ms , "application/vnd.ms-excel" , "電表報表.xlsx" ) ;
}
public static int GetDayInMonth ( string yearMonth )
{
List < string > datesList = new List < string > ( ) ;
DateTime startOfMonth = DateTime . Parse ( yearMonth + "-01" ) ;
int daysInMonth = DateTime . DaysInMonth ( startOfMonth . Year , startOfMonth . Month ) ;
2023-05-15 17:10:17 +08:00
2023-06-12 16:02:18 +08:00
return daysInMonth ;
2023-05-12 18:42:22 +08:00
}
2023-05-11 15:46:04 +08:00
}
}