271 lines
9.6 KiB
C#
271 lines
9.6 KiB
C#
//using AspNetCore;
|
|
using ClosedXML.Excel;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
//using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.Office.Interop.Excel;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics;
|
|
|
|
using VirtualTask.Models;
|
|
using X.PagedList;
|
|
|
|
namespace VirtualTask.Controllers
|
|
{
|
|
public class RapportiniController : Controller
|
|
{
|
|
string apiUrl = string.Empty;
|
|
string urlBase = string.Empty;
|
|
string token = string.Empty;
|
|
string tenant = string.Empty;
|
|
string errMes = string.Empty;
|
|
string admin = string.Empty;
|
|
|
|
HttpClient client;
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public RapportiniController(IConfiguration configuration)
|
|
{
|
|
client = new HttpClient();
|
|
_configuration = configuration;
|
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
|
apiUrl = key;
|
|
}
|
|
|
|
#region INDEX
|
|
|
|
public IActionResult Index(string impianto, int? page = 1)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "rapportiniList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<Rapportini> modelList = new List<Rapportini>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
|
|
|
|
ViewBag.Rapportini = getImpianti();
|
|
|
|
if (!string.IsNullOrEmpty(impianto))
|
|
{
|
|
modelList = modelList.Where(s => s.codice_impianto.Contains(impianto)).ToList();
|
|
|
|
ViewData["CurrentFilter"] = impianto;
|
|
|
|
ViewBag.Impianto = impianto;
|
|
}
|
|
else
|
|
ViewData["CurrentFilter"] = null;
|
|
|
|
if (page != null && page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
|
|
var pageSize = 10;
|
|
|
|
var shortLinks = modelList
|
|
.OrderByDescending(s => s.seriale_rapportino)
|
|
.ToPagedList(page ?? 1, pageSize);
|
|
|
|
return View(shortLinks);
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
#endregion INDEX
|
|
|
|
#region metodi interni
|
|
|
|
public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin, string? impianto)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "rapportiniList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
List<Rapportini> modelList = new List<Rapportini>();
|
|
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
|
|
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
|
|
|
|
ViewData["CurrentFilter"] = impianto;
|
|
|
|
if (!impianto.Equals("System.Collections.Generic.List`1[Microsoft.AspNetCore.Mvc.Rendering.SelectListItem]")/* || impianto != null*/)
|
|
{
|
|
modelList = modelList.Where(x => x.codice_impianto.Equals(impianto)).ToList();
|
|
}
|
|
|
|
if (!dataFin.ToString().Substring(0, 10).Equals("01/01/0001"))
|
|
{
|
|
modelList = modelList.Where(x => x.data_rapportino.GetValueOrDefault().Date >= dataIni.Date && x.data_rapportino.GetValueOrDefault().Date <= dataFin.Date).ToList();
|
|
}
|
|
else
|
|
{
|
|
modelList = modelList.Where(x => x.data_rapportino.GetValueOrDefault().Date >= dataIni.Date).ToList();
|
|
}
|
|
|
|
var workbook = new XLWorkbook();
|
|
workbook.AddWorksheet("sheetName");
|
|
var ws = workbook.Worksheet("sheetName");
|
|
|
|
|
|
int col = 1;
|
|
|
|
//Scrivo intestazioni colonne
|
|
foreach (var item in modelList[0].GetType().GetProperties())
|
|
{
|
|
ws.Cell(1, col).Style = ws.Cell(1, col).Style.Font.SetBold();
|
|
ws.Cell(1, col).Value = item.Name.ToString().Replace("_", " ").ToUpper();
|
|
|
|
col++;
|
|
}
|
|
|
|
var row = 0;
|
|
|
|
foreach (var item in modelList)
|
|
{
|
|
var colonna = 1;
|
|
|
|
//righe per ogni elemento nel modelList
|
|
foreach (var prop in item.GetType().GetProperties())
|
|
{
|
|
ws.Cell(row + 2, colonna).Value = prop.GetValue(item).ToString();
|
|
|
|
colonna++;
|
|
}
|
|
|
|
row++;
|
|
}
|
|
|
|
//Salvo il file Excel
|
|
var anno = DateTime.Now.Year.ToString();
|
|
var mese = DateTime.Now.Month.ToString();
|
|
var giorno = DateTime.Now.Day.ToString();
|
|
var ore = DateTime.Now.Hour.ToString();
|
|
var minuti = DateTime.Now.Minute.ToString();
|
|
var secondi = DateTime.Now.Second.ToString();
|
|
|
|
var dateFile = anno + mese + giorno + ore + minuti + secondi;
|
|
|
|
//workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\buoni_{dateFile}.xlsx");
|
|
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
string fileName = "authors.xlsx";
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
workbook.SaveAs(stream);
|
|
var content = stream.ToArray();
|
|
return File(content, contentType, fileName);
|
|
}
|
|
|
|
//workbook.SaveAs($"..\\..\\BuoniVT\\buoni_{dateFile}.xlsx");
|
|
//workbook.Save();
|
|
|
|
|
|
// return RedirectToAction("Index");
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
private List<SelectListItem> getImpianti()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "rapportiniList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
List<Rapportini> modelList = new List<Rapportini>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
|
|
|
|
//per gestire primo elemento tendina (deve essere vuoto)
|
|
SelectListItem listItemFirt = new SelectListItem();
|
|
|
|
listItemFirt.Value = string.Empty;
|
|
listItemFirt.Text = " - Impianto";
|
|
selectItems.Add(listItemFirt);
|
|
|
|
var appoggio = string.Empty;
|
|
|
|
foreach (var role in modelList)
|
|
{
|
|
if (!appoggio.Equals(role.codice_impianto))
|
|
{
|
|
SelectListItem listItem = new SelectListItem();
|
|
string s = role.codice_impianto;
|
|
listItem.Value = role.codice_impianto;
|
|
listItem.Text = s;
|
|
selectItems.Add(listItem);
|
|
}
|
|
|
|
appoggio = role.codice_impianto;
|
|
}
|
|
}
|
|
|
|
return selectItems;
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
string e = helper.GetStringValue("errMsg");
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e });
|
|
}
|
|
|
|
#endregion metodi interni
|
|
|
|
}
|
|
}
|