200 lines
8.1 KiB
C#
200 lines
8.1 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
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 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;
|
|
}
|
|
|
|
public FileResult ExportExcel(string post)
|
|
{
|
|
var postObject = JsonConvert.DeserializeObject<Excel>(post);
|
|
var workbook = new XSSFWorkbook();
|
|
#region excel設定
|
|
IFont font12 = workbook.CreateFont();
|
|
font12.FontName = "新細明體";
|
|
font12.FontHeightInPoints = 12;
|
|
ICellStyle style12 = workbook.CreateCellStyle();
|
|
style12.SetFont(font12);
|
|
style12.Alignment = HorizontalAlignment.Center;
|
|
style12.VerticalAlignment = VerticalAlignment.Center;
|
|
IFont font12Times = workbook.CreateFont();
|
|
font12Times.FontName = "Times New Roman";
|
|
font12Times.FontHeightInPoints = 12;
|
|
IFont font18 = workbook.CreateFont();
|
|
font18.FontName = "新細明體";
|
|
font18.FontHeightInPoints = 18;
|
|
font18.IsBold = true;
|
|
ICellStyle styleTitle18 = workbook.CreateCellStyle();
|
|
styleTitle18.SetFont(font18);
|
|
styleTitle18.Alignment = HorizontalAlignment.Center;
|
|
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
|
|
ICellStyle styleLeft12 = workbook.CreateCellStyle();
|
|
styleLeft12.SetFont(font12);
|
|
styleLeft12.Alignment = HorizontalAlignment.Left;
|
|
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
|
|
ICellStyle styleLine12 = workbook.CreateCellStyle();
|
|
styleLine12.SetFont(font12);
|
|
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
|
styleLine12.VerticalAlignment = VerticalAlignment.Center;
|
|
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
ICellStyle stylein12 = workbook.CreateCellStyle();
|
|
stylein12.SetFont(font12Times);
|
|
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
|
|
stylein12.VerticalAlignment = VerticalAlignment.Center;
|
|
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
stylein12.WrapText = true;
|
|
#endregion
|
|
foreach(var powerstationid in postObject.PowerStation)
|
|
{
|
|
var sheet = workbook.CreateSheet(powerstationid.Name);
|
|
Select_table select_Table = new Select_table
|
|
{
|
|
FormType = postObject.FormType,
|
|
SearchType = postObject.SearchType,
|
|
Time = postObject.Time,
|
|
PowerStation = Convert.ToInt32(powerstationid.Value)
|
|
};
|
|
var Formbody = GetForm(select_Table);
|
|
var Formhead = GetTableHead(select_Table);
|
|
int RowPosition = 0;
|
|
IRow row = sheet.CreateRow(RowPosition);
|
|
ICell cell = row.CreateCell(0);
|
|
|
|
}
|
|
|
|
var ms = new NpoiMemoryStream
|
|
{
|
|
AllowClose = false
|
|
};
|
|
workbook.Write(ms);
|
|
ms.Flush();
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|
return File(ms, "application/vnd.ms-excel", "text.xlsx");
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|