FIC_Solar/SolarPower/Controllers/StationReportController.cs

121 lines
4.5 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 StationReportController : MyBaseController<StationReportController>
{
private readonly IPowerStationRepository powerStationRepository;
private readonly IStationReportRepository stationReportRepository;
public StationReportController(IPowerStationRepository powerStationRepository, IStationReportRepository stationReportRepository) : base()
{
this.powerStationRepository = powerStationRepository;
this.stationReportRepository = stationReportRepository;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<ApiResult<Dictionary<string,List<PowerStationIdAndCity>>>> GetPowerStationNameList(string filter)
{
ApiResult<Dictionary<string, List<PowerStationIdAndCity>>> apiResult = new ApiResult<Dictionary<string, List<PowerStationIdAndCity>>>();
try
{
var powerStations = new List<PowerStationIdAndCity>();
if (IsPlatformLayer(myUser.Role.Layer))
{
powerStations = await powerStationRepository.GetPowerStationsAllWithfilter(filter);
}
else
{
powerStations = await powerStationRepository.GetPowerStationsByCompanyIdWithfilter(myUser.CompanyId,filter);
}
var siteDBNamePowerStationId = new Dictionary<string, List<PowerStationIdAndCity>>();
var powerStation_Group = powerStations.GroupBy(x => x.CityName).ToList();
foreach (var stations in powerStation_Group)
{
siteDBNamePowerStationId.Add(stations.Key, stations.ToList());
}
apiResult.Code = "0000";
apiResult.Data = siteDBNamePowerStationId;
}
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<ApiResult<List<string>>> GetTableHead(Select_table post)
{
ApiResult<List<string>> apiResult = new ApiResult<List<string>>();
List<string> inverter = new List<string>();
try
{
var powerStation = await powerStationRepository.GetOneAsync(post.PowerStation);
if(powerStation == null)
{
apiResult.Code = "0001";
apiResult.Msg = "需加入查詢電站";
return apiResult;
}
inverter = await stationReportRepository.GetInverterId(powerStation.SiteDB,post);
for (int i = 0;i<inverter.Count;i++)
{
inverter[i] = "inv_" + inverter[i].Substring(inverter[i].Length - 4, 4);
}
apiResult.Code = "0000";
apiResult.Data = inverter;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】");
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
public async Task<ApiResult<dynamic>> GetForm(Select_table post)
{
ApiResult<dynamic> apiResult = new ApiResult<dynamic>();
try
{
var a = await stationReportRepository.Gettablebody(post);
apiResult.Code = "0000";
apiResult.Data = a;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】");
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
}
}