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.IO; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class StationOverviewController : MyBaseController { private readonly IOverviewRepository overviewRepository; private readonly IPowerStationRepository powerStationRepository; private string stationImageFilePath = "/upload/power_station/"; public StationOverviewController( IOverviewRepository overviewRepository, IPowerStationRepository powerStationRepository) : base() { this.overviewRepository = overviewRepository; this.powerStationRepository = powerStationRepository; } public IActionResult Index() { return View(); } public IActionResult Info() { return View("~/Views/StationOverview/StationOverviewInfo.cshtml"); } /// /// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量 /// /// [HttpPost] public async Task>> GetSolarByCity(UseStatusCityGetPowerStation post) { ApiResult> apiResult = new ApiResult>(); List solaramount = new List(); try { apiResult.Code = "0000"; solaramount = await overviewRepository.GetSolarByCity(myUser, post); foreach (var solar in solaramount) { solar.MainDisplay = Path.Combine(stationImageFilePath, solar.Id.ToString()) + "/" + solar.MainDisplay; } apiResult.Data = solaramount; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); } return apiResult; } [HttpPost] public async Task> GetStationCard(StationIds post) { ApiResult apiResult = new ApiResult(); MapOverview mapOverview = new MapOverview(); try { var overview = await overviewRepository.GetOverviewByPowerStationIds(post.Ids); mapOverview.Today_kwh = overview.Today_kwh; mapOverview.Total_kwh = overview.Total_kwh; mapOverview.Today_irradiance = overview.Today_irradiance; mapOverview.Avg_irradiance = overview.Avg_irradiance; mapOverview.Today_PR = overview.Today_PR; mapOverview.Avg_PR = overview.Avg_PR; mapOverview.Today_kwhkwp = overview.Today_kwhkwp; mapOverview.Avg_kwhkwp = overview.Avg_kwhkwp; mapOverview.Today_carbon = overview.Today_carbon; mapOverview.Total_carbon = overview.Total_carbon; mapOverview.CapacityDataTables = await overviewRepository.GetCapacityDataTableByPowerStationIds(post.Ids); var totalPowerStationCount = 0; var totalCapacity = 0.0; foreach (var capacity in mapOverview.CapacityDataTables) { totalPowerStationCount += capacity.SubPowerStationCount; totalCapacity += capacity.SubTotalCapacity; } mapOverview.TotalPowerStationCount = totalPowerStationCount; mapOverview.TotalCapacity = totalCapacity; mapOverview.UpdatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); apiResult.Code = "0000"; apiResult.Data = mapOverview; } 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> GetOneStationUpToDateInfo(StationIds post) { ApiResult apiResult = new ApiResult(); StationOverview stationOverview = new StationOverview(); try { var overview = await overviewRepository.GetOverviewByPowerStationIds(post.Ids); stationOverview.Today_kwh = overview.Today_kwh; stationOverview.Total_kwh = overview.Total_kwh; stationOverview.Today_irradiance = overview.Today_irradiance; stationOverview.Avg_irradiance = overview.Avg_irradiance; stationOverview.Today_PR = overview.Today_PR; stationOverview.Avg_PR = overview.Avg_PR; stationOverview.Today_kwhkwp = overview.Today_kwhkwp; stationOverview.Avg_kwhkwp = overview.Avg_kwhkwp; stationOverview.Today_monery = overview.Today_monery; stationOverview.Total_monery = overview.Total_monery; stationOverview.Today_carbon = overview.Today_carbon; stationOverview.Total_carbon = overview.Total_carbon; stationOverview.UpdatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (myUser.Role.Auths.Contains("ShowMoney")) { stationOverview.IsShowMoney = 1; } else { stationOverview.IsShowMoney = 0; stationOverview.Today_monery = 0; stationOverview.Total_monery = 0; } apiResult.Code = "0000"; apiResult.Data = stationOverview; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } public async Task> GetChartUpToDate(int id) { ApiResult apiResult = new ApiResult(); ChartUptoDate chartUptoDate = new ChartUptoDate(); try { var nowDay = DateTime.Now.ToString("yyyy-MM-dd"); //今日發電量日照度List var powerIrradianceToday = await overviewRepository.GetListPowerIrradianceTodayByPowerStationId(id, nowDay); //今日資料轉換 PowerIrradianceChart powerIrradianceTodayChart = new PowerIrradianceChart(); powerIrradianceTodayChart.Labels = powerIrradianceToday.Select(x => x.Label).ToList(); powerIrradianceTodayChart.PowerDatas = powerIrradianceToday.Select(x => x.PowerData).ToList(); powerIrradianceTodayChart.IrradianceDatas = powerIrradianceToday.Select(x => x.IrradianceData).ToList(); chartUptoDate.ChartToday = powerIrradianceTodayChart; //7日發電量日照度List var powerIrradiance7day = await overviewRepository.GetListPowerIrradiance7dayByPowerStationId(id, nowDay); //7日資料轉換 PowerIrradianceChart powerIrradiance7dayChart = new PowerIrradianceChart(); powerIrradiance7dayChart.Labels = powerIrradiance7day.Select(x => x.Label).ToList(); powerIrradiance7dayChart.PowerDatas = powerIrradiance7day.Select(x => x.PowerData).ToList(); powerIrradiance7dayChart.IrradianceDatas = powerIrradiance7day.Select(x => x.IrradianceData).ToList(); chartUptoDate.Chart7day = powerIrradiance7dayChart; //本月發電量日照度List var powerIrradianceMonth = await overviewRepository.GetListPowerIrradianceMonthByPowerStationId(id, nowDay); //今日資料轉換 PowerIrradianceChart powerIrradianceMonthChart = new PowerIrradianceChart(); powerIrradianceMonthChart.Labels = powerIrradianceMonth.Select(x => x.Label).ToList(); powerIrradianceMonthChart.PowerDatas = powerIrradianceMonth.Select(x => x.PowerData).ToList(); powerIrradianceMonthChart.IrradianceDatas = powerIrradianceMonth.Select(x => x.IrradianceData).ToList(); chartUptoDate.ChartMonth = powerIrradianceMonthChart; //本年發電量日照度List var powerIrradianceYear = await overviewRepository.GetListPowerIrradianceYearByPowerStationId(id, nowDay); //今日資料轉換 PowerIrradianceChart powerIrradianceYearChart = new PowerIrradianceChart(); powerIrradianceYearChart.Labels = powerIrradianceYear.Select(x => x.Label).ToList(); powerIrradianceYearChart.PowerDatas = powerIrradianceYear.Select(x => x.PowerData).ToList(); powerIrradianceYearChart.IrradianceDatas = powerIrradianceYear.Select(x => x.IrradianceData).ToList(); chartUptoDate.ChartYear = powerIrradianceYearChart; apiResult.Code = "0000"; apiResult.Data = chartUptoDate; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } public async Task GetExceptionTable(ExceptionSent post) { List exceptionDataTable = new List(); ApiResult> apiResult = new ApiResult>(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.Id); exceptionDataTable = await overviewRepository.GetExceptionTable(post); foreach (ExceptionDataTable a in exceptionDataTable) { if (string.IsNullOrEmpty(a.FormId)) { a.Function = @$"填寫表單"; } else { a.Function = @$"{a.FormId}"; } } apiResult.Code = "0000"; apiResult.Data = exceptionDataTable; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } } }