using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SolarPower.Controllers
{
    public class AnalysisInverterController : MyBaseController<AnalysisInverterController>
    {

        private readonly IPowerStationRepository powerStationRepository;

        public AnalysisInverterController(IPowerStationRepository powerStationRepository) : base()
        {
            this.powerStationRepository = powerStationRepository;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<ApiResult<Dictionary<string, Dictionary<string, List<PowerStationInverter>>>>> GetInverterCollapse(string filter)
        {
            ApiResult<Dictionary<string, Dictionary<string, List<PowerStationInverter>>>> apiResult = new ApiResult<Dictionary<string, Dictionary<string, List<PowerStationInverter>>>>();
            try
            {
                var powerStations = new List<PowerStation>();
                if (IsPlatformLayer(myUser.Role.Layer))
                {
                    powerStations = await powerStationRepository.GetAllAsync();
                }
                else
                {
                    powerStations = await powerStationRepository.GetPowerStationsByCompanyId(myUser);
                }

                var siteDBNamePowerStationId = new Dictionary<string, List<int>>();

                var powerStation_Group = powerStations.GroupBy(x => x.SiteDB).ToList();
                foreach (var stations in powerStation_Group)
                {
                    var powerStationIds = stations.Select(x => x.Id).ToList();
                    siteDBNamePowerStationId.Add(stations.Key, powerStationIds);
                }

                var powerStationInverters = await powerStationRepository.GetPowerStationInverter(siteDBNamePowerStationId, filter);

                powerStationInverters = powerStationInverters.Where(x => x.InverterId != null).ToList();

                var powerStationInverter_Group = powerStationInverters.GroupBy(x => x.CityName).ToList();

                var inverterCollapse = new Dictionary<string, Dictionary<string, List<PowerStationInverter>>>();
                foreach (var powerStationInverter in powerStationInverter_Group)
                {
                    var inverter_Group = powerStationInverter.GroupBy(x => x.PowerStationName).ToList();

                    var inverterDic = new Dictionary<string, List<PowerStationInverter>>();
                    foreach (var inverter in inverter_Group)
                    {
                        inverterDic.Add(inverter.Key, inverter.ToList());
                    }

                    inverterCollapse.Add(powerStationInverter.Key, inverterDic);
                }

                apiResult.Code = "0000";

                apiResult.Data = inverterCollapse;
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                Logger.LogError("【" + controllerName + "/" + actionName + "】");
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }

            apiResult.Msg = errorCode.GetString(apiResult.Code);
            return apiResult;
        }

        [HttpPost]
        public async Task<ApiResult<AnalysisInverter>> GetAnalysisByInverterIds(PostAnalysisInverter post)
        {
            ApiResult<AnalysisInverter> apiResult = new ApiResult<AnalysisInverter>();
            try
            {
                AnalysisInverter analysisInverter = new AnalysisInverter();
                var inverterHistories = new List<InverterHistory>();
                var stationIdWithInverterIdsList = new List<StationIdWithInverterIds>();
                List<InverterHistoryInfo> series = new List<InverterHistoryInfo>();
                var powerStation_Group = post.InverterIdInfos.GroupBy(x => x.PowerStationId).ToList();

                if (post.SearchType == 0)
                { //單日
                    var StationCodeWithInverterIdsList = new List<StationCodeWithInverterIds>();
                    foreach (var ps in powerStation_Group)
                    {
                        var powerStation = await powerStationRepository.GetOneAsync(ps.Key);
                        //找出該電站所有日照計設備(包含共享
                        var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB);


                        StationCodeWithInverterIds stationCodeWithInverterIds = new StationCodeWithInverterIds();
                        stationCodeWithInverterIds.Sensor = deviceInfos.First().ColName;
                        stationCodeWithInverterIds.SiteDB = powerStation.SiteDB;
                        stationCodeWithInverterIds.Code = powerStation.Code;
                        stationCodeWithInverterIds.InverterIds = ps.Select(x => x.InverterId).ToList();

                        StationCodeWithInverterIdsList.Add(stationCodeWithInverterIds);
                    }

                    inverterHistories = await powerStationRepository.GetInverterHistoryRowData(post.SelectedDate, StationCodeWithInverterIdsList);

                    analysisInverter.XAxis = inverterHistories.Select(x => x.TIMESTAMP).Distinct().ToList();

                    analysisInverter.MultipleYaxes = new Dictionary<string, string>(){
                            { "Irradiance", "日照度"},
                            { "DCKW", "直流功率 (KW)"},
                            { "ACKW", "輸出功率 (KW)"},
                            { "DC1V", "直流電壓1 (V)"},
                            { "DC1A", "直流電流1 (A)"},
                            { "DC1KW", "直流功率1 (KW)"},
                            { "DC2V", "直流電壓2 (V)"},
                            { "DC2A", "直流電流2 (A)"},
                            { "DC2KW", "直流功率2 (KW)"},
                            { "DC3V", "直流電壓3 (V)"},
                            { "DC3A", "直流電流3 (A)"},
                            { "DC3KW", "直流功率3 (KW)"},
                            { "DC4V", "直流電壓4 (V)"},
                            { "DC4A", "直流電流4 (A)"},
                            { "DC4KW", "直流功率4 (KW)"},
                            { "DC5V", "直流電壓5 (V)"},
                            { "DC5A", "直流電流5 (A)"},
                            { "DC5KW", "直流功率5 (KW)"},
                            { "AC1V", "交流電壓A (V)"},
                            { "AC2V", "交流電壓B (V)"},
                            { "AC3V", "交流電壓C (V)"},
                            { "AC1A", "交流電流A (A)"},
                            { "AC2A", "交流電流B (A)"},
                            { "AC3A", "交流電流C (A)"},
                            { "PR", "PR"},
                            { "RA1", "RA1 (%)"},
                            { "RA2", "RA2 (%)"},
                            { "RA3", "RA3 (%)"},
                            { "RA4", "RA4 (%)"},
                            { "RA5", "RA5 (%)"},
                        };

                    var inverterHistories_Group = inverterHistories.GroupBy(x => x.INVERTERID).ToList();

                    series = new List<InverterHistoryInfo>();



                    #region 比較欄位
                    foreach (var item in inverterHistories_Group)
                    {
                        var temp_item = item.OrderBy(x => x.TIMESTAMP).ToList();

                        InverterHistoryInfo Irradiance = new InverterHistoryInfo();
                        Irradiance.Name = string.Format("{0}:{1}", item.First().INVERTERName, "日照度");
                        Irradiance.YaxesKey = "Irradiance";
                        Irradiance.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                Irradiance.Values.Add(Math.Round(history.Irradiance, 2));
                            }
                            else
                            {
                                Irradiance.Values.Add(0);
                            }
                        }
                        series.Add(Irradiance);


                        InverterHistoryInfo DCKW = new InverterHistoryInfo();
                        DCKW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率 (KW)");
                        DCKW.YaxesKey = "DCKW";
                        DCKW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DCKW.Values.Add(Math.Round(history.DCKW, 2));
                            }
                            else
                            {
                                DCKW.Values.Add(0);
                            }
                        }
                        series.Add(DCKW);

                        InverterHistoryInfo ACKW = new InverterHistoryInfo();
                        ACKW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "輸出功率 (KW)");
                        ACKW.YaxesKey = "ACKW";
                        ACKW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                ACKW.Values.Add(Math.Round(history.ACKW, 2));
                            }
                            else
                            {
                                ACKW.Values.Add(0);
                            }
                        }
                        series.Add(ACKW);

                        InverterHistoryInfo DC1V = new InverterHistoryInfo();
                        DC1V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電壓1 (V)");
                        DC1V.YaxesKey = "DC1V";
                        DC1V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC1V.Values.Add(Math.Round(history.DC1V, 2));
                            }
                            else
                            {
                                DC1V.Values.Add(0);
                            }
                        }
                        series.Add(DC1V);

                        InverterHistoryInfo DC1A = new InverterHistoryInfo();
                        DC1A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電流1 (A)");
                        DC1A.YaxesKey = "DC1A";
                        DC1A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC1A.Values.Add(Math.Round(history.DC1A, 2));
                            }
                            else
                            {
                                DC1A.Values.Add(0);
                            }
                        }
                        series.Add(DC1A);

                        InverterHistoryInfo DC1KW = new InverterHistoryInfo();
                        DC1KW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率1 (KW)");
                        DC1KW.YaxesKey = "DC1KW";
                        DC1KW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC1KW.Values.Add(Math.Round(history.DC1W / 1000, 2));
                            }
                            else
                            {
                                DC1KW.Values.Add(0);
                            }
                        }
                        series.Add(DC1KW);

                        InverterHistoryInfo DC2V = new InverterHistoryInfo();
                        DC2V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電壓2 (V)");
                        DC2V.YaxesKey = "DC2V";
                        DC2V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC2V.Values.Add(Math.Round(history.DC2V, 2));
                            }
                            else
                            {
                                DC2V.Values.Add(0);
                            }
                        }
                        series.Add(DC2V);

                        InverterHistoryInfo DC2A = new InverterHistoryInfo();
                        DC2A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電流2 (A)");
                        DC2A.YaxesKey = "DC2A";
                        DC2A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC2A.Values.Add(Math.Round(history.DC2A, 2));
                            }
                            else
                            {
                                DC2A.Values.Add(0);
                            }
                        }
                        series.Add(DC2A);

                        InverterHistoryInfo DC2KW = new InverterHistoryInfo();
                        DC2KW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率2 (KW)");
                        DC2KW.YaxesKey = "DC2KW";
                        DC2KW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC2KW.Values.Add(Math.Round(history.DC2W / 1000, 2));
                            }
                            else
                            {
                                DC2KW.Values.Add(0);
                            }
                        }
                        series.Add(DC2KW);

                        InverterHistoryInfo DC3V = new InverterHistoryInfo();
                        DC3V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電壓3 (V)");
                        DC3V.YaxesKey = "DC3V";
                        DC3V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC3V.Values.Add(Math.Round(history.DC3V, 2));
                            }
                            else
                            {
                                DC3V.Values.Add(0);
                            }
                        }
                        series.Add(DC3V);

                        InverterHistoryInfo DC3A = new InverterHistoryInfo();
                        DC3A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電流3 (A)");
                        DC3A.YaxesKey = "DC3A";
                        DC3A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC3A.Values.Add(Math.Round(history.DC3A, 2));
                            }
                            else
                            {
                                DC3A.Values.Add(0);
                            }
                        }
                        series.Add(DC3A);

                        InverterHistoryInfo DC3KW = new InverterHistoryInfo();
                        DC3KW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率3 (KW)");
                        DC3KW.YaxesKey = "DC3KW";
                        DC3KW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC3KW.Values.Add(Math.Round(history.DC3W / 1000, 2));
                            }
                            else
                            {
                                DC3KW.Values.Add(0);
                            }
                        }
                        series.Add(DC3KW);

                        InverterHistoryInfo DC4V = new InverterHistoryInfo();
                        DC4V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電壓4 (V)");
                        DC4V.YaxesKey = "DC4V";
                        DC4V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC4V.Values.Add(Math.Round(history.DC4V, 2));
                            }
                            else
                            {
                                DC4V.Values.Add(0);
                            }
                        }
                        series.Add(DC4V);

                        InverterHistoryInfo DC4A = new InverterHistoryInfo();
                        DC4A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電流4 (A)");
                        DC4A.YaxesKey = "DC4A";
                        DC4A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC4A.Values.Add(Math.Round(history.DC4A, 2));
                            }
                            else
                            {
                                DC4A.Values.Add(0);
                            }
                        }
                        series.Add(DC4A);

                        InverterHistoryInfo DC4KW = new InverterHistoryInfo();
                        DC4KW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率4 (KW)");
                        DC4KW.YaxesKey = "DC4KW";
                        DC4KW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC4KW.Values.Add(Math.Round(history.DC4W / 1000, 2));
                            }
                            else
                            {
                                DC4KW.Values.Add(0);
                            }
                        }
                        series.Add(DC4KW);

                        InverterHistoryInfo DC5V = new InverterHistoryInfo();
                        DC5V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電壓5 (V)");
                        DC5V.YaxesKey = "DC5V";
                        DC5V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC5V.Values.Add(Math.Round(history.DC5V, 2));
                            }
                            else
                            {
                                DC5V.Values.Add(0);
                            }
                        }
                        series.Add(DC5V);

                        InverterHistoryInfo DC5A = new InverterHistoryInfo();
                        DC5A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流電流5 (A)");
                        DC5A.YaxesKey = "DC5A";
                        DC5A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC5A.Values.Add(Math.Round(history.DC5A, 2));
                            }
                            else
                            {
                                DC5A.Values.Add(0);
                            }
                        }
                        series.Add(DC5A);

                        InverterHistoryInfo DC5KW = new InverterHistoryInfo();
                        DC5KW.Name = string.Format("{0}:{1}", item.First().INVERTERName, "直流功率5 (KW)");
                        DC5KW.YaxesKey = "DC5KW";
                        DC5KW.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                DC5KW.Values.Add(Math.Round(history.DC5W / 1000, 2));
                            }
                            else
                            {
                                DC5KW.Values.Add(0);
                            }
                        }
                        series.Add(DC5KW);

                        InverterHistoryInfo AC1V = new InverterHistoryInfo();
                        AC1V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電壓A (V)");
                        AC1V.YaxesKey = "AC1V";
                        AC1V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC1V.Values.Add(Math.Round(history.AC1V, 2));
                            }
                            else
                            {
                                AC1V.Values.Add(0);
                            }
                        }
                        series.Add(AC1V);

                        InverterHistoryInfo AC2V = new InverterHistoryInfo();
                        AC2V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電壓B (V)");
                        AC2V.YaxesKey = "AC2V";
                        AC2V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC2V.Values.Add(Math.Round(history.AC2V, 2));
                            }
                            else
                            {
                                AC2V.Values.Add(0);
                            }
                        }
                        series.Add(AC2V);

                        InverterHistoryInfo AC3V = new InverterHistoryInfo();
                        AC3V.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電壓C (V)");
                        AC3V.YaxesKey = "AC3V";
                        AC3V.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC3V.Values.Add(Math.Round(history.AC3V, 2));
                            }
                            else
                            {
                                AC3V.Values.Add(0);
                            }
                        }
                        series.Add(AC3V);

                        InverterHistoryInfo AC1A = new InverterHistoryInfo();
                        AC1A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電流A (A)");
                        AC1A.YaxesKey = "AC1A";
                        AC1A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC1A.Values.Add(Math.Round(history.AC1A, 2));
                            }
                            else
                            {
                                AC1A.Values.Add(0);
                            }
                        }
                        series.Add(AC1A);

                        InverterHistoryInfo AC2A = new InverterHistoryInfo();
                        AC2A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電流B (A)");
                        AC2A.YaxesKey = "AC2A";
                        AC2A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC2A.Values.Add(Math.Round(history.AC2A, 2));
                            }
                            else
                            {
                                AC2A.Values.Add(0);
                            }
                        }
                        series.Add(AC2A);

                        InverterHistoryInfo AC3A = new InverterHistoryInfo();
                        AC3A.Name = string.Format("{0}:{1}", item.First().INVERTERName, "交流電流C (A)");
                        AC3A.YaxesKey = "AC3A";
                        AC3A.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                AC3A.Values.Add(Math.Round(history.AC3A, 2));
                            }
                            else
                            {
                                AC3A.Values.Add(0);
                            }
                        }
                        series.Add(AC3A);

                        InverterHistoryInfo PR = new InverterHistoryInfo();
                        PR.Name = string.Format("{0}:{1}", item.First().INVERTERName, "PR");
                        PR.YaxesKey = "PR";
                        PR.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                PR.Values.Add(Math.Round(history.PR, 2));
                            }
                            else
                            {
                                PR.Values.Add(0);
                            }
                        }
                        series.Add(PR);

                        InverterHistoryInfo RA1 = new InverterHistoryInfo();
                        RA1.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA1 (%)");
                        RA1.YaxesKey = "RA1";
                        RA1.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA1.Values.Add(Math.Round(history.RA1, 2));
                            }
                            else
                            {
                                RA1.Values.Add(0);
                            }
                        }
                        series.Add(RA1);

                        InverterHistoryInfo RA2 = new InverterHistoryInfo();
                        RA2.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA2 (%)");
                        RA2.YaxesKey = "RA2";
                        RA2.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA2.Values.Add(Math.Round(history.RA2, 2));
                            }
                            else
                            {
                                RA2.Values.Add(0);
                            }
                        }
                        series.Add(RA2);

                        InverterHistoryInfo RA3 = new InverterHistoryInfo();
                        RA3.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA3 (%)");
                        RA3.YaxesKey = "RA3";
                        RA3.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA3.Values.Add(Math.Round(history.RA3, 2));
                            }
                            else
                            {
                                RA3.Values.Add(0);
                            }
                        }
                        series.Add(RA3);

                        InverterHistoryInfo RA4 = new InverterHistoryInfo();
                        RA4.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA4 (%)");
                        RA4.YaxesKey = "RA4";
                        RA4.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA4.Values.Add(Math.Round(history.RA4, 2));
                            }
                            else
                            {
                                RA4.Values.Add(0);
                            }
                        }
                        series.Add(RA4);

                        InverterHistoryInfo RA5 = new InverterHistoryInfo();
                        RA5.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA5 (%)");
                        RA5.YaxesKey = "RA5";
                        RA5.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA5.Values.Add(Math.Round(history.RA5, 2));
                            }
                            else
                            {
                                RA5.Values.Add(0);
                            }
                        }
                        series.Add(RA5);

                    }
                    #endregion
                    analysisInverter.Series = series;
                }
                else
                {   //日區間、月、年
                    stationIdWithInverterIdsList = new List<StationIdWithInverterIds>();
                    foreach (var ps in powerStation_Group)
                    {
                        var powerStation = await powerStationRepository.GetOneAsync(ps.Key);

                        StationIdWithInverterIds stationIdWithInverterIds = new StationIdWithInverterIds();
                        stationIdWithInverterIds.PowerStationId = ps.Key;
                        stationIdWithInverterIds.SiteDBName = powerStation.SiteDB;
                        stationIdWithInverterIds.InverterIds = ps.Select(x => x.InverterId).ToList();

                        stationIdWithInverterIdsList.Add(stationIdWithInverterIds);
                    }

                    switch (post.SearchType)
                    {
                        case 1:

                            var date_split = post.SelectedDate.Split('-');
                            inverterHistories = await powerStationRepository.GetInverterHistoryByDate(date_split[0].Trim(), date_split[1].Trim(), stationIdWithInverterIdsList);
                            break;
                        case 2:

                            var startDate = Convert.ToDateTime(post.SelectedDate).ToString("yyyy-MM-dd");
                            var endDate = Convert.ToDateTime(post.SelectedDate).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");

                            inverterHistories = await powerStationRepository.GetInverterHistoryByDate(startDate, endDate, stationIdWithInverterIdsList);
                            break;
                        case 3:

                            var year = post.SelectedDate;

                            inverterHistories = await powerStationRepository.GetInverterHistoryByYear(year, stationIdWithInverterIdsList);

                            break;
                    }

                    analysisInverter.XAxis = inverterHistories.Select(x => x.TIMESTAMP).Distinct().ToList();
                    analysisInverter.MultipleYaxes = new Dictionary<string, string>(){
                            { "Irradiance", "日照度"},
                            { "KWH", "KWH"},
                            { "TodayKWH", "TodayKWH"},
                            { "TotalKWH", "TotalKWH"},
                            { "PR", "PR"},
                            { "RA1", "RA1 (%)"},
                            { "RA2", "RA2 (%)"},
                            { "RA3", "RA3 (%)"},
                            { "RA4", "RA4 (%)"},
                            { "RA5", "RA5 (%)"},
                        };

                    var inverterHistoriesDay_Group = inverterHistories.GroupBy(x => x.INVERTERID).ToList();

                    #region 比較欄位
                    foreach (var item in inverterHistoriesDay_Group)
                    {
                        var temp_item = item.OrderBy(x => x.TIMESTAMP).ToList();

                        InverterHistoryInfo Irradiance = new InverterHistoryInfo();
                        Irradiance.Name = string.Format("{0}:{1}", item.First().INVERTERName, "日照度");
                        Irradiance.YaxesKey = "Irradiance";
                        Irradiance.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                Irradiance.Values.Add(Math.Round(history.Irradiance, 2));
                            }
                            else
                            {
                                Irradiance.Values.Add(0);
                            }
                        }
                        series.Add(Irradiance);

                        InverterHistoryInfo KWH = new InverterHistoryInfo();
                        KWH.Name = string.Format("{0}:{1}", item.First().INVERTERName, "KWH");
                        KWH.YaxesKey = "KWH";
                        KWH.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                KWH.Values.Add(Math.Round(history.KWH, 2));
                            }
                            else
                            {
                                KWH.Values.Add(0);
                            }
                        }
                        series.Add(KWH);

                        InverterHistoryInfo TodayKWH = new InverterHistoryInfo();
                        TodayKWH.Name = string.Format("{0}:{1}", item.First().INVERTERName, "TodayKWH");
                        TodayKWH.YaxesKey = "TodayKWH";
                        TodayKWH.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                TodayKWH.Values.Add(Math.Round(history.TODAYKWH, 2));
                            }
                            else
                            {
                                TodayKWH.Values.Add(0);
                            }
                        }
                        series.Add(TodayKWH);

                        InverterHistoryInfo TotalKWH = new InverterHistoryInfo();
                        TotalKWH.Name = string.Format("{0}:{1}", item.First().INVERTERName, "TotalKWH");
                        TotalKWH.YaxesKey = "TotalKWH";
                        TotalKWH.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                TotalKWH.Values.Add(Math.Round(history.TOTALKWH, 2));
                            }
                            else
                            {
                                TotalKWH.Values.Add(0);
                            }
                        }
                        series.Add(TotalKWH);


                        InverterHistoryInfo PR = new InverterHistoryInfo();
                        PR.Name = string.Format("{0}:{1}", item.First().INVERTERName, "PR");
                        PR.YaxesKey = "PR";
                        PR.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                PR.Values.Add(Math.Round(history.PR, 2));
                            }
                            else
                            {
                                PR.Values.Add(0);
                            }
                        }
                        series.Add(PR);

                        InverterHistoryInfo RA1 = new InverterHistoryInfo();
                        RA1.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA1 (%)");
                        RA1.YaxesKey = "RA1";
                        RA1.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA1.Values.Add(Math.Round(history.RA1, 2));
                            }
                            else
                            {
                                RA1.Values.Add(0);
                            }
                        }
                        series.Add(RA1);

                        InverterHistoryInfo RA2 = new InverterHistoryInfo();
                        RA2.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA2 (%)");
                        RA2.YaxesKey = "RA2";
                        RA2.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA2.Values.Add(Math.Round(history.RA2, 2));
                            }
                            else
                            {
                                RA2.Values.Add(0);
                            }
                        }
                        series.Add(RA2);

                        InverterHistoryInfo RA3 = new InverterHistoryInfo();
                        RA3.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA3 (%)");
                        RA3.YaxesKey = "RA3";
                        RA3.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA3.Values.Add(Math.Round(history.RA3, 2));
                            }
                            else
                            {
                                RA3.Values.Add(0);
                            }
                        }
                        series.Add(RA3);

                        InverterHistoryInfo RA4 = new InverterHistoryInfo();
                        RA4.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA4 (%)");
                        RA4.YaxesKey = "RA4";
                        RA4.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA4.Values.Add(Math.Round(history.RA4, 2));
                            }
                            else
                            {
                                RA4.Values.Add(0);
                            }
                        }
                        series.Add(RA4);

                        InverterHistoryInfo RA5 = new InverterHistoryInfo();
                        RA5.Name = string.Format("{0}:{1}", item.First().INVERTERName, "RA5 (%)");
                        RA5.YaxesKey = "RA5";
                        RA5.Values = new List<double>();
                        foreach (var history in temp_item)
                        {
                            if (analysisInverter.XAxis.IndexOf(history.TIMESTAMP) > -1)
                            {
                                RA5.Values.Add(Math.Round(history.RA5, 2));
                            }
                            else
                            {
                                RA5.Values.Add(0);
                            }
                        }
                        series.Add(RA5);

                    }
                    #endregion
                    analysisInverter.Series = series;
                }

                apiResult.Code = "0000";

                apiResult.Data = analysisInverter;
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                Logger.LogError("【" + controllerName + "/" + actionName + "】");
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }

            apiResult.Msg = errorCode.GetString(apiResult.Code);
            return apiResult;
        }
    }
}
