PozoController.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using WebApplication3.Models;
  9. using WebApplication3.Clases;
  10. namespace WebApplication3.Controllers
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class PozoController : ControllerBase
  15. {
  16. private readonly AquiferContext _context;
  17. private TsPozoController _tsPozoController;
  18. public PozoController(AquiferContext context)
  19. {
  20. _context = context;
  21. _tsPozoController = new TsPozoController(context);
  22. }
  23. // GET: api/Pozo
  24. [HttpGet]
  25. public async Task<ActionResult<IEnumerable<DatosPozoRecarga>>> GetPozos()
  26. {
  27. var pozos = await _context.Pozos.ToListAsync();
  28. List<DatosPozoRecarga> result = new();
  29. foreach(Pozo p in pozos)
  30. {
  31. DatosPozoRecarga datosPozo = new DatosPozoRecarga();
  32. datosPozo.Id = p.Id;
  33. datosPozo.Nombre = p.Nombre;
  34. datosPozo.Latitud = p.Latitud;
  35. datosPozo.Longitud = p.Longitud;
  36. datosPozo.Maximo = p.Maximo;
  37. datosPozo.Minimo = p.Minimo;
  38. datosPozo.Simulacion = p.IdSimulacion;
  39. datosPozo.Ts = _tsPozoController.GetTsPozo(p.Id);
  40. result.Add(datosPozo);
  41. }
  42. return result;
  43. }
  44. // GET: api/Pozo/5
  45. [HttpGet("{id}")]
  46. [ProducesResponseType(200)]
  47. [ProducesResponseType(404)]
  48. public async Task<ActionResult<DatosPozoRecarga>> GetPozo(uint id)
  49. {
  50. var pozo = await _context.Pozos.FindAsync(id);
  51. if (pozo == null)
  52. {
  53. return NotFound();
  54. }
  55. DatosPozoRecarga datosPozo = new DatosPozoRecarga();
  56. datosPozo.Id = pozo.Id;
  57. datosPozo.Nombre = pozo.Nombre;
  58. datosPozo.Latitud = pozo.Latitud;
  59. datosPozo.Longitud = pozo.Longitud;
  60. datosPozo.Maximo = pozo.Maximo;
  61. datosPozo.Minimo = pozo.Minimo;
  62. datosPozo.Simulacion = pozo.IdSimulacion;
  63. datosPozo.Ts = _tsPozoController.GetTsPozo(pozo.Id);
  64. return datosPozo;
  65. }
  66. [HttpGet("Simulacion/{id}")]
  67. public async Task<ActionResult<IEnumerable<DatosPozoRecarga>>> GetPozosFromSimulation(uint id)
  68. {
  69. var pozos = await _context.Pozos.Where(x => x.IdSimulacion == id).ToListAsync();
  70. List<DatosPozoRecarga> result = new();
  71. foreach (Pozo p in pozos)
  72. {
  73. DatosPozoRecarga datosPozo = new DatosPozoRecarga();
  74. datosPozo.Id = p.Id;
  75. datosPozo.Nombre = p.Nombre;
  76. datosPozo.Latitud = p.Latitud;
  77. datosPozo.Longitud = p.Longitud;
  78. datosPozo.Maximo = p.Maximo;
  79. datosPozo.Minimo = p.Minimo;
  80. datosPozo.Simulacion = p.IdSimulacion;
  81. datosPozo.Ts = _tsPozoController.GetTsPozo(p.Id);
  82. result.Add(datosPozo);
  83. }
  84. return result;
  85. }
  86. // PUT: api/Pozo/5
  87. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  88. [HttpPut("{id}")]
  89. [ProducesResponseType(201)]
  90. [ProducesResponseType(400)]
  91. [ProducesResponseType(404)]
  92. public async Task<IActionResult> PutPozo(uint id, [FromBody] DatosPozoRecarga datos)
  93. {
  94. Pozo pozo = _context.Pozos.Find(id);
  95. if (id != pozo.Id)
  96. {
  97. return BadRequest();
  98. }
  99. pozo.IdAquifero = (datos.Acuifero != null && pozo.IdAquifero != datos.Acuifero) ? datos.Acuifero : pozo.IdAquifero;
  100. pozo.IdSimulacion = (datos.Simulacion != null && pozo.IdSimulacion != datos.Simulacion) ? datos.Simulacion : pozo.IdSimulacion;
  101. pozo.Latitud = (datos.Latitud != null && pozo.Latitud != datos.Latitud) ? datos.Latitud : pozo.Latitud;
  102. pozo.Longitud = (datos.Longitud != null && pozo.Longitud != datos.Longitud) ? datos.Longitud : pozo.Longitud;
  103. pozo.Maximo = (datos.Maximo != null && pozo.Maximo != datos.Maximo) ? datos.Maximo : pozo.Maximo;
  104. pozo.Nombre = (datos.Nombre != null && pozo.Nombre != datos.Nombre) ? datos.Nombre : pozo.Nombre;
  105. _context.Entry(pozo).State = EntityState.Modified;
  106. try
  107. {
  108. await _context.SaveChangesAsync();
  109. }
  110. catch (DbUpdateConcurrencyException)
  111. {
  112. if (!PozoExists(id))
  113. {
  114. return NotFound();
  115. }
  116. else
  117. {
  118. throw;
  119. }
  120. }
  121. return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo);
  122. }
  123. // POST: api/Pozo
  124. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  125. [HttpPost]
  126. [ProducesResponseType(201)]
  127. [ProducesResponseType(409)]
  128. public async Task<ActionResult<Pozo>> PostPozo([FromBody] DatosPozoRecarga datos)
  129. {
  130. Pozo pozo = new Pozo();
  131. pozo.IdAquifero = datos.Acuifero;
  132. pozo.IdSimulacion = datos.Simulacion;
  133. pozo.Latitud = datos.Latitud;
  134. pozo.Longitud = datos.Longitud;
  135. pozo.Maximo = datos.Maximo;
  136. pozo.Minimo = datos.Minimo;
  137. pozo.Nombre = datos.Nombre;
  138. _context.Pozos.Add(pozo);
  139. try
  140. {
  141. await _context.SaveChangesAsync();
  142. foreach (TimeSerie ts in datos.Ts)
  143. {
  144. TsPozo tsPozo = new TsPozo();
  145. tsPozo.IdPozo = pozo.Id;
  146. tsPozo.MarcaTiempo = ts.Hora;
  147. tsPozo.Valor = ts.Valor;
  148. await _tsPozoController.PostTsPozo(tsPozo);
  149. }
  150. }
  151. catch (DbUpdateException)
  152. {
  153. if (PozoExists(pozo.Id))
  154. {
  155. return Conflict();
  156. }
  157. else
  158. {
  159. throw;
  160. }
  161. }
  162. return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo);
  163. }
  164. // DELETE: api/Pozo/5
  165. [HttpDelete("{id}")]
  166. [ProducesResponseType(201)]
  167. [ProducesResponseType(404)]
  168. public async Task<IActionResult> DeletePozo(uint id)
  169. {
  170. var pozo = await _context.Pozos.FindAsync(id);
  171. if (pozo == null)
  172. {
  173. return NotFound();
  174. }
  175. await _tsPozoController.DeleteTsPozoFromPozo(id);
  176. _context.Pozos.Remove(pozo);
  177. await _context.SaveChangesAsync();
  178. return NoContent();
  179. }
  180. private bool PozoExists(uint id)
  181. {
  182. return _context.Pozos.Any(e => e.Id == id);
  183. }
  184. [NonAction]
  185. public async Task<ActionResult<IEnumerable<Pozo>>> ParseoFichero(uint acuifero, string file,uint simId)
  186. {
  187. var s = acuifero;
  188. Parser wellParser = new Parser();
  189. //string file = @"C:\Users\Admin\Desktop\ASD\infoPozos.VMW";
  190. WellData[] wells = wellParser.ParseWellData(file);
  191. foreach (WellData w in wells)
  192. {
  193. Pozo pozo = w.ToPozo(acuifero, simId);
  194. _context.Pozos.Add(pozo);
  195. try
  196. {
  197. await _context.SaveChangesAsync();
  198. foreach (TimeSerieInterno ts in w.Rates)
  199. {
  200. TsPozo tsPozo = new TsPozo();
  201. tsPozo.IdPozo = pozo.Id;
  202. tsPozo.MarcaTiempo = ts.Hora;
  203. tsPozo.MarcaTiempoEnd = ts.HoraEnd;
  204. tsPozo.Valor = ts.Valor;
  205. tsPozo.InfoComplementaria = ts.Info;
  206. await _tsPozoController.PostTsPozo(tsPozo);
  207. }
  208. }
  209. catch (DbUpdateException)
  210. {
  211. if (PozoExists(pozo.Id))
  212. {
  213. return Conflict();
  214. }
  215. else
  216. {
  217. throw;
  218. }
  219. }
  220. }
  221. return await _context.Pozos.ToListAsync();
  222. }
  223. [NonAction]
  224. public async Task<ActionResult<IEnumerable<Pozo>>> PostMultipleWells(uint aquiferId,WellData[] wellDatas, uint simulationId)
  225. {
  226. foreach (WellData w in wellDatas)
  227. {
  228. Pozo pozo = w.ToPozo(aquiferId, simulationId);
  229. _context.Pozos.Add(pozo);
  230. try
  231. {
  232. await _context.SaveChangesAsync();
  233. foreach (TimeSerieInterno ts in w.Rates)
  234. {
  235. TsPozo tsPozo = new TsPozo();
  236. tsPozo.IdPozo = pozo.Id;
  237. tsPozo.MarcaTiempo = ts.Hora;
  238. tsPozo.Valor = ts.Valor;
  239. tsPozo.InfoComplementaria = ts.Info;
  240. await _tsPozoController.PostTsPozo(tsPozo);
  241. }
  242. }
  243. catch (DbUpdateException)
  244. {
  245. if (PozoExists(pozo.Id))
  246. {
  247. return Conflict();
  248. }
  249. else
  250. {
  251. throw;
  252. }
  253. }
  254. }
  255. return await _context.Pozos.ToListAsync();
  256. }
  257. [NonAction]
  258. public Pozo[] GetPozosAcuifero(uint acuiferoId)
  259. {
  260. return _context.Pozos.Where(x => x.IdAquifero == acuiferoId).ToArray<Pozo>();
  261. }
  262. [NonAction]
  263. public Pozo[] GetPozosAcuiferoSimulacionInicial(uint acuiferoId)
  264. {
  265. return _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray<Pozo>();
  266. }
  267. [NonAction]
  268. public async void SetPozosSimulacion(uint? acuiferoId, uint? simulacionId)
  269. {
  270. //Obtenemos los pozos del acuifero
  271. Pozo[] pozos = _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray<Pozo>();
  272. foreach(Pozo p in pozos)
  273. {
  274. Pozo pozo = p.Copy(simulacionId);
  275. _context.Pozos.Add(pozo);
  276. }
  277. await _context.SaveChangesAsync();
  278. }
  279. [NonAction]
  280. public List<Pozo> GetPozosSimulacion(uint? simulacionId)
  281. {
  282. List <Pozo> pozos = _context.Pozos.Where(x => x.IdSimulacion == simulacionId).ToList<Pozo>();
  283. return pozos;
  284. }
  285. [NonAction]
  286. public List<TsPozo> GetTsPozoFromPozo(uint? pozo)
  287. {
  288. List<TsPozo> tsPozos = _context.TsPozos.Where(x => x.IdPozo == pozo).ToList<TsPozo>();
  289. return tsPozos;
  290. }
  291. [NonAction]
  292. public async Task<bool> DeletePozoFromSim(uint? idSimulacion)
  293. {
  294. List<Pozo> pozos = _context.Pozos.Where(x => x.IdSimulacion == idSimulacion).ToList();
  295. bool error = false;
  296. try
  297. {
  298. foreach(Pozo p in pozos)
  299. {
  300. await DeletePozo(p.Id);
  301. }
  302. } catch(Exception ex)
  303. {
  304. error = true;
  305. Logger.WriteLog("Fail at deleting well from a simulation (simulationID: " + idSimulacion+")");
  306. Logger.WriteLog(ex.Message);
  307. Logger.WriteLog(ex.InnerException.Message);
  308. }
  309. return error;
  310. }
  311. }
  312. }