using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;

namespace solarApp.Service
{
    /// <summary>
    /// 電站原始資料 rawData
    /// </summary>
    public class getStationSvc
    {
        string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
        /// <summary>
        /// 電站 Raw Data
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="siteDB"></param>
        /// <param name="siteID"></param>
        /// <returns></returns>
        public List<raw_statino> get_station_raw(string reportDate, string siteDB, string siteID)
        {
            List<raw_statino> 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<raw_statino>(sql, new { reportDate = reportDate }).AsList<raw_statino>();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                } 
                return ds;
            }
        }
        #region 全區使用的 view
        public List<raw_station_day> 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<raw_station_day> ds = conn.Query<raw_station_day>(sql, new { date1 = date1, date2 = date2 }).AsList<raw_station_day>();
                conn.Close();
                return ds;
            }
        }

        #endregion
        /// <summary>
        /// 電站每天平均 from RawData
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public List<raw_station_day> get_station_rawAvg(string date1, string date2, string siteDB, string siteID)
        {
            List<raw_station_day> 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<raw_station_day>(sql, new { date1 = date1, date2 = date2 }).AsList<raw_station_day>();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                } 
                
                return ds;
            }
        }

        

        /// <summary>
        /// web 呈現值 station - hour
        /// </summary>
        /// <param name="reportDate"></param>
        /// <returns></returns>
        public List<web_station_hour> 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<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { siteID = siteID.Substring(0, 9) }).AsList<web_station_hour>();
                conn.Close();
                return ds;
            }
        }

        /// <summary>
        /// web 呈現值 station - day
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="invID"></param>
        /// <returns></returns>
        public List<web_station_day> 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<web_station_day> ds = conn.Query<web_station_day>(sql, new { date1 = date1, date2 = date2 , siteID = siteID}).AsList<web_station_day>();
                conn.Close();
                return ds;
            }
        }

        public List<web_station_day> 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<web_station_day> ds = conn.Query<web_station_day>(sql, new { date1 = date1, date2 = date2, siteID = siteID }).AsList<web_station_day>();
                conn.Close();
                return ds;
            }
        }

        /// <summary>
        /// 取得電站資訊
        /// </summary>
        /// <returns></returns>
        public List<station_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<station_list> ds = conn.Query<station_list>(sql).AsList<station_list>();
                conn.Close();
                return ds;
            }
        }
    }
}
