FIC_Solar/SolarPower/Controllers/AnalysisStationInfoController.cs
2021-07-27 20:11:56 +08:00

88 lines
3.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.Linq;
using System.Threading.Tasks;
namespace SolarPower.Controllers
{
public class AnalysisStationInfoController : MyBaseController<AnalysisStationInfoController>
{
private readonly IPowerStationRepository powerStationRepository;
public AnalysisStationInfoController(IPowerStationRepository powerStationRepository) : base()
{
this.powerStationRepository = powerStationRepository;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<ApiResult<Dictionary<string, Dictionary<string, List<PowerStationDevice>>>>> GetDeviceCollapse(string filter)
{
ApiResult<Dictionary<string, Dictionary<string, List<PowerStationDevice>>>> apiResult = new ApiResult<Dictionary<string, Dictionary<string, List<PowerStationDevice>>>>();
try
{
var powerStations = new List<PowerStation>();
if (IsPlatformLayer(myUser.Role.Layer))
{
powerStations = await powerStationRepository.GetAllAsync();
}
else
{
powerStations = await powerStationRepository.GetPowerStationsByCompanyId(myUser.CompanyId);
}
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 powerStationDevices = await powerStationRepository.GetPowerStationDevice(siteDBNamePowerStationId, filter);
powerStationDevices = powerStationDevices.Where(x => x.DeviceId != null).ToList();
var powerStationDevice_Group = powerStationDevices.GroupBy(x => x.CityName).ToList();
var deviceCollapse = new Dictionary<string, Dictionary<string, List<PowerStationDevice>>>();
foreach (var powerStationDevice in powerStationDevice_Group)
{
var device_Group = powerStationDevice.GroupBy(x => x.PowerStationName).ToList();
var deviceDic = new Dictionary<string, List<PowerStationDevice>>();
foreach (var inverter in device_Group)
{
deviceDic.Add(inverter.Key, inverter.ToList());
}
deviceCollapse.Add(powerStationDevice.Key, deviceDic);
}
apiResult.Code = "0000";
apiResult.Data = deviceCollapse;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】");
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
apiResult.Msg = errorCode.GetString(apiResult.Code);
return apiResult;
}
}
}