using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;
namespace solarApp.Service
{
///
/// 電站原始資料 rawData
///
public class getStationSvc
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
///
/// 電站 Raw Data
///
///
///
///
///
public List get_station_raw(string reportDate, string siteDB, string siteID)
{
List ds;
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, siteid, round(KWH, 3) KWH, round(TODAYKWH, 3) TODAYKWH,
round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(SOLARHOUR, 3) SOLARHOUR , round(kwhkwp, 3) kwhkwp
from " + siteDB + ".s" + siteID + @"_station
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate";
try
{
ds = conn.Query(sql, new { reportDate = reportDate }).AsList();
conn.Close();
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
}
#region 全區使用的 view
public List create_v_station_inv(string date1, string date2)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select siteid, left(reportdate, 10) reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH,
round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count
from v_station_temp
where left(reportdate, 10) between @date1 and @date2
group by siteid, left(reportdate, 10)";
List ds = conn.Query(sql, new { date1 = date1, date2 = date2 }).AsList();
conn.Close();
return ds;
}
}
#endregion
///
/// 電站每天平均 from RawData
///
///
///
///
public List get_station_rawAvg(string date1, string date2, string siteDB, string siteID)
{
List ds;
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
//string sql = @" select siteid, left(reportdate, 10) reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH,
// round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count
// from v_station_temp
// where left(reportdate, 10) between @date1 and @date2
// group by siteid, left(reportdate, 10)";
string sql = @" select siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH,
round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count
from " + siteDB+ ".s"+ siteID + @"_station
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2
group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d')";
try
{
ds = conn.Query(sql, new { date1 = date1, date2 = date2 }).AsList();
conn.Close();
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
}
///
/// web 呈現值 station - hour
///
///
///
public List get_web_station_hour(string reportDate, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, siteid, round(KWH, 2) KWH,
round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(solarHour, 2) SOLARHOUR,
round(PR, 3) PR, round(KWHKWP, 3) KWHKWP,
round(money, 3) money, round(todaymoney, 3) todaymoney, round(totalmoney, 3) totalmoney,
round(carbon, 3) carbon, round(todayCarbon, 3) todayCarbon, round(totalCarbon, 3) totalCarbon
from power_station_history_hour
where siteID = @siteID and left(`TIMESTAMP`, 10) = '" + reportDate + "' ";
List ds = conn.Query(sql, new { siteID = siteID.Substring(0, 9) }).AsList();
conn.Close();
return ds;
}
}
///
/// web 呈現值 station - day
///
///
///
///
public List get_web_station_day(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
//string sql = @"
// select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH,
// round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money
// from power_station_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and siteid = @siteID";
string sql = @"
select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, a.siteid, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(solarHour, 2) SOLARHOUR,
round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money, count
from power_station_history_day a left join (
select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, SITEID, count(*) count
from power_station_history_hour
where siteid = @siteID and left(`TIMESTAMP`, 10) between @date1 and @date2
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), SITEID
) b on a.SITEID = b.SITEID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') = b.reportdate
where left(`TIMESTAMP`, 10) between @date1 and @date2 and a.siteid = @siteID
order by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
";
List ds = conn.Query(sql, new { date1 = date1, date2 = date2 , siteID = siteID}).AsList();
conn.Close();
return ds;
}
}
public List get_web_station_month(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TOTALKWH, 2) TOTALKWH, round(solarHour, 2) SOLARHOUR, round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money
from power_station_history_month where left(`TIMESTAMP`, 7) between @date1 and @date2 and siteid = @siteID";
List ds = conn.Query(sql, new { date1 = date1, date2 = date2, siteID = siteID }).AsList();
conn.Close();
return ds;
}
}
///
/// 取得電站資訊
///
///
public List get_station_list()
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open(); // 01 控制器編號
string sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName
from power_station
where deleted = 0 ";
List ds = conn.Query(sql).AsList();
conn.Close();
return ds;
}
}
public List get_lack_stationData(string reportDate)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open(); // a.powerstationid, p.`code` SITEID, a.`TIMESTAMP` , TODAYKWH, TOTALKWH , b.Irradiance, b.EnvTemperature, b.irrDay
string sql = @"select a.powerstationid, p.`code` SITEID, left(a.`TIMESTAMP`, 10) `TIMESTAMP`, b.TODAYKWH, b.TOTALKWH
from sensor_history_hour a
left join (
select powerstationid, left(`TIMESTAMP`, 10) `TIMESTAMP` , TODAYKWH, TOTALKWH from power_station_history_hour
where left(`TIMESTAMP`, 7) = @reportDate and hour(`TIMESTAMP`) = 12
) b on a.powerstationid = b.powerstationid and left(a.`TIMESTAMP`, 10) = b.`TIMESTAMP`
join power_station p on a.powerstationid = p.id
where left(a.`TIMESTAMP`, 7) = @reportDate and hour(a.`TIMESTAMP`) = 12 and b.TODAYKWH is null
order by 1, 2 ";
List ds = conn.Query(sql, new { reportDate = reportDate.Substring(0, 7) }, commandTimeout: 300).AsList();
conn.Close();
return ds;
}
}
}
}