SoftwayWeb/Controllers/DestinazioniController.cs

393 lines
14 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using X.PagedList;
namespace SoftwayWeb.Controllers
{
public class DestinazioniController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
SessionHelper helper;
public DestinazioniController(IConfiguration configuration)
{
_configuration = configuration;
client = new HttpClient();
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo, int? page = 1)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
string autista = string.Empty;
string dataGi = string.Empty;
string mezzo = string.Empty;
string dataGiro2 = string.Empty;
ViewBag.CodAutista = codAutista;
ViewBag.Autista = nomeAutista;
ViewBag.CodMezzo = codMezzo;
ViewBag.Automezzo = desMezzo;
ViewBag.dataGiro = dataGiro.ToString("dd/MM/yyyy");
if (!string.IsNullOrEmpty(codAutista))
{
helper.SetStringValue("codAutista", codAutista.TrimEnd());
helper.SetStringValue("nomeAutista", nomeAutista.TrimEnd());
}
else
{
string codAut = helper.GetStringValue("codAutista");
codAutista = codAut.TrimEnd();
ViewBag.CodAutista = codAutista;
string nomeAut = helper.GetStringValue("nomeAutista");
ViewBag.Autista = nomeAut;
}
if (!string.IsNullOrEmpty(codMezzo))
{
helper.SetStringValue("codMezzo", codMezzo.TrimEnd());
helper.SetStringValue("desMezzo", desMezzo.TrimEnd());
}
else
{
string mez = helper.GetStringValue("codMezzo").TrimEnd();
codMezzo = mez;
ViewBag.CodMezzo = codMezzo;
string nomeMezzo = helper.GetStringValue("desMezzo");
ViewBag.Automezzo = nomeMezzo;
}
if (dataGiro.Date != DateTime.MinValue)
{
helper.SetStringValue("dataGiro", dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"));
dataGi = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
helper.SetStringValue("dataGiro2", dataGiro.ToString("dd/MM/yyyy"));
}
else
{
dataGi = helper.GetStringValue("dataGiro");
string dataG = helper.GetStringValue("dataGiro2");
ViewBag.dataGiro = dataG;
}
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo";
urlBase = urlBase + "?autista=" + codAutista + "&dataGiro=" + dataGi + "&mezzo=" + codMezzo;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Destinazioni_Out>>(dato);
modelList = modelList.Where(x => x.serialeGiro == id).ToList();
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortList = modelList.ToPagedList(page ?? 1, pageSize);
return View(shortList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
public IActionResult EliminaDestinazione(string serial)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaDestinazioneBySerial";
urlBase = urlBase + "?seriale=" + serial;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Destinazioni_Out model = new Destinazioni_Out();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<Destinazioni_Out>(dato);
//qui metodo post per scrivere su pimodgir
Modgir mg=new Modgir();
mg.Piserial = model.Brserial;
mg.Pidata = model.DataCarico;
mg.Picommit = model.CodCommittente;
mg.Pidesdiv = model.CodSede;
mg.Pitarga = model.CodAutomezzo;
mg.Piautist = model.CodAutista;
mg.Pitiprec = "D";
urlBase = apiUrl + "ModificaGiro/addModgir";
baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(mg);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result;
if (response2.IsSuccessStatusCode)
{
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return RedirectToAction("Index", "Destinazioni", new
{
//string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo
id = model.serialeGiro,
codAutista = model.CodAutista,
nomeAutista=model.Autista,
dataGiro = model.DataCarico,
codMezzo=model.CodAutomezzo,
desMezzo = model.DescAutomezzo
});
}
public IActionResult ModificaDestinazione(string serial)
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaDestinazioneBySerial";
urlBase = urlBase + "?seriale=" + serial;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Destinazioni_Out model = new Destinazioni_Out();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<Destinazioni_Out>(dato);
List < SelectListItem > autisti = new List < SelectListItem>();
autisti = getAutisti();
autisti.Find(c => c.Value.Trim().Equals(model.CodAutista)).Selected = true;
ViewBag.Autisti = autisti;
List<SelectListItem> mezzi = new List<SelectListItem>();
mezzi = getMezzi();
mezzi.Find(c => c.Value.Equals(model.CodAutomezzo)).Selected = true;
ViewBag.CodMezzo = mezzi;
model.CodAutistaOld = model.CodAutista;
model.CodAutomezzoOld = model.CodAutomezzo;
model.AutistaOld = model.Autista;
model.DescAutomezzoOld = model.DescAutomezzo;
return View(model);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
[HttpPost]
public IActionResult ModificaDestinazionePost(Destinazioni_Out model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
//qui metodo post per scrivere su pimodgir
Modgir mg = new Modgir();
mg.Piserial = model.Brserial;
mg.Pidata = model.DataCarico;
mg.Picommit = model.CodCommittente;
mg.Pidesdiv = model.CodSede;
mg.Pitarga = model.CodAutomezzo;
mg.Piautist = model.CodAutista;
mg.Pitiprec = "M";
mg.Pisergir = model.serialeGiro;
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "ModificaGiro/addModgir";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(mg);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result;
if (response2.IsSuccessStatusCode)
{
return RedirectToAction("Index", "Destinazioni", new
{
id = model.serialeGiro,
codAutista = model.CodAutistaOld,
nomeAutista = model.AutistaOld,
dataGiro = model.DataCarico,
codMezzo = model.CodAutomezzoOld,
desMezzo = model.DescAutomezzoOld
});
}
else
{
errMes = response2.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
private List<SelectListItem> getAutisti()
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaAutisti";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Personale> modelList = new List<Personale>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Personale>>(data);
//per gestire primo elemento tendina (deve essere vuoto)
SelectListItem listItemFirt = new SelectListItem();
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
string s = role.Catcodice + " - " + role.Catnome;
listItem.Value = role.Catcodice;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> getMezzi()
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaAutomezzi";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Automezzi> listMezzi = new List<Automezzi>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
listMezzi = JsonConvert.DeserializeObject<List<Automezzi>>(data);
listMezzi = listMezzi.OrderBy(x => x.CauTarga).ToList();
//per gestire primo elemento tendina (deve essere vuoto)
SelectListItem listItemFirt = new SelectListItem();
listItemFirt.Value = string.Empty;
listItemFirt.Text = " - Automezzo";
selectItems.Add(listItemFirt);
foreach (var mezzo in listMezzi)
{
SelectListItem listItem = new SelectListItem();
List<SelectListItem> listItemOrderby = new List<SelectListItem>();
string s = mezzo.CauTarga + " - " + mezzo.CauDesc;
listItem.Value = mezzo.CauTarga;
listItem.Text = s;
selectItems.Add(listItem);
}
}
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 });
}
}
}