FIC_Solar/SolarPower/Controllers/StationOverviewController.cs
2021-07-07 19:03:18 +08:00

127 lines
4.4 KiB
C#

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<StationOverviewController>
{
private readonly IOverviewRepository overviewRepository;
private string stationImageFilePath = "/upload/power_station/";
public StationOverviewController(
IOverviewRepository overviewRepository) : base()
{
this.overviewRepository = overviewRepository;
}
public IActionResult Index()
{
return View();
}
public IActionResult Info()
{
return View("~/Views/StationOverview/StationOverviewInfo.cshtml");
}
/// <summary>
/// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<PowerStation>>> GetSolarByCity(UseStatusCityGetPowerStation post)
{
ApiResult<List<PowerStation>> apiResult = new ApiResult<List<PowerStation>>();
List<PowerStation> solaramount = new List<PowerStation>();
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<ApiResult<MapOverview>> GetStationCard(StationIds post)
{
ApiResult<MapOverview> apiResult = new ApiResult<MapOverview>();
MapOverview mapOverview = new MapOverview();
try
{
List<int> powerStationIds = new List<int>() { 1, 2, 3 };
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.PowerStations = await overviewRepository.GetListPowerStationByPowerStationIds(powerStationIds);
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;
}
}
}