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

namespace solarApp.Service
{
    public class get_inv_svc
    {
        string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
        /// <summary>
        /// inverter 原始資料 - 5 min
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="invID"></param>
        /// <returns></returns>
        public List<raw_inv> Get_rawInv(string reportDate, string invID, string siteDB, string siteID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                //string sql = @"select ID, TIMESTAMP, SITEID, SITETYPE, CONTROLLERID, INVERTERID, SN, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, AC3V, AC3A, AC3W, AC3F, AC3WH, ACRUNTIME, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, DCRUNTIME, WH, TODAYKWH, TOTALKWH, PR, RA1, RA2, RA3, RA4, RA5 
                //               from solar_com0002.s02202000101_inv order by id desc limit 100; ";
                string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, inverterid, WH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(PR, 2) PR, RA1 
                                from " + siteDB + ".s" + siteID + @"_inv  where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 10) = '" + reportDate + "' and inverterid = '" + invID + "'";

                //List<raw_inv> ds = conn.Query<raw_inv>(sql, new { kind = kind }).ToList();
                List<raw_inv> ds =  conn.Query<raw_inv>(sql).AsList<raw_inv>();
                conn.Close();
                return ds;
            }
        }
        /// <summary>
        /// 每小時平均值 inverter 原始資料 - hour
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="invID"></param>
        /// <returns></returns>
        public List<raw_inv_hour> get_Inv_rawAvg(string reportDate, string invID, string siteDB, string siteID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                //string sql = @"select left(reportdate, 13)reportdate, inverterid, round((sum(WH)/1000), 2) KWH, round(max(TODAYKWH), 2) TODAYKWH,
                //        round(max(TOTALKWH), 2) TOTALKWH, round(max(PR), 2) PR, count(*) count
                //               from v_inv_temp where left(reportdate, 10) = '" + reportDate + "' and inverterid = '" + invID + "' " +
                //               "group by  left(reportdate, 13), inverterid";  //round(avg(PR), 2) PR,
                string sql = @"		 
    select  FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')reportdate, inverterid ,round((sum(WH)/1000), 2) KWH,
           round(max(TODAYKWH), 2) TODAYKWH, round(max(TOTALKWH), 2) TOTALKWH, round(max(PR), 2) PR, count(*) count
    from " + siteDB + ".s" + siteID + @"_inv  
    where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d')  = '" + reportDate + "' and inverterid = '" + invID + @"'
             group by  FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), inverterid
	order by 1; ";

//select a.reportdate, a.inverterid, a.KWH, b.TODAYKWH, b.TOTALKWH, b.PR, a.count from 
//	                        (
//			                        select  left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 13)reportdate, inverterid 
//					                        ,round((sum(WH)/1000), 2) KWH,   count(*) count
//			                        from " + siteDB + ".s" + siteID + @"_inv  
//			                        where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d')  = '" + reportDate + "' and inverterid = '" + invID + @"'
//                                    group by  left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 13), inverterid
//	                        )a join (
//			                        select   FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, inverterid, round(PR, 2) PR
//			                        from " + siteDB + ".s" + siteID + @"_inv    
//			                        where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') =  '" + reportDate + @"'  and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 2) = '55' and inverterid = '" + invID + @"'			
//	                        )b on a.reportdate = left(b.reportdate, 13) and a.inverterid = b.inverterid ";
                List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>();
                conn.Close();
                return ds;
            }
        }


        public List<web_inv_15min> get_web_Inv_15min(string reportDate, string invID, string siteID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                string sql = @"select `code` siteID, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H:%i') reportdate, inverterid, a.KWH, a.TODAYKWH, round(a.kwhkwp, 3) kwhkwp
                                from inverter_history_15min a join power_station b on a.powerstationID = b.id
                                where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + @"' and `code` = @siteID
                                order by cast(reportdate as datetime)"; 
                List<web_inv_15min> ds = conn.Query<web_inv_15min>(sql, new { siteID = siteID }).AsList<web_inv_15min>();
                conn.Close();
                return ds;
            }
        }
        /// <summary>
        /// web 呈現值 每小時平均值 inverter - hour
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="invID"></param>
        /// <returns></returns>
        public List<web_inv_hour> get_web_Inv_hour(string reportDate, string invID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H:%i') reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR
                               from inverter_history_hour where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + "' ";
                List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
                conn.Close();
                return ds;
            }
        }

        public List<web_inv_hour> get_web_Inv_day(string date1, string date2, string invID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                //string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(PR, 2) PR
                //               from inverter_history_day where left(`TIMESTAMP`, 10) between '" + date1 + "' and '"+ date2 +"' and inverterid = '" + invID + "' ";
                string sql = @"
select DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') reportdate, a.inverterid, round(a.KWH, 2) KWH, round(a.TODAYKWH, 2) TODAYKWH, 
			 round(a.TOTALKWH, 2) TOTALKWH, round(a.PR, 2) PR, count
from inverter_history_day a left join (
			select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, count(*) count
			from inverter_history_hour
			where left(`TIMESTAMP`, 10) between '" + date1 + "' and '" + date2 + @"' and inverterid =  '" + invID + @"'

            group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), inverterid
) b on a.inverterid = b.inverterid and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d')  = b.reportdate
where left(a.`TIMESTAMP`, 10) between '" + date1 + "' and '" + date2 + "' and a.inverterid =  '" + invID + "' order by 1";
                List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
                conn.Close();
                return ds;
            }
        }

        public List<web_inv_hour> get_web_Inv_month(string date1, string date2, string invID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR
                               from inverter_history_month where left(`TIMESTAMP`, 7) between '" + date1 + "' and '" + date2 + "' and inverterid = '" + invID + "' ";
                List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
                conn.Close();
                return ds;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="invID"></param>
        /// <returns></returns>
        public List<web_inv_list> get_Inv_list(string siteDB, string siteID)
        {
            using (MySqlConnection conn = new MySqlConnection(Connection1))
            {
                conn.Open();
                string sql = @"select dbname, inverterid from v_company_inv where dbname = @siteDB and left(inverterid, 9) = @siteID";
                List<web_inv_list> ds = conn.Query<web_inv_list>(sql, new { siteDB = siteDB, siteID = siteID }).AsList<web_inv_list>();
                conn.Close();
                return ds;
            }
        }
    }
}
