DescargaController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\controllers;
  3. use app\components\data\Respuesta;
  4. use app\components\rest\JsonController;
  5. use yii\db\Query;
  6. use yii\filters\AccessControl;
  7. use yii\filters\VerbFilter;
  8. class DescargaController extends JsonController {
  9. public function behaviors() {
  10. return [
  11. 'access' => [
  12. 'class' => AccessControl::className(),
  13. 'only' => ['por-pc', 'guardar'],
  14. 'rules' => [
  15. [
  16. 'actions' => ['por-pc', 'guardar'],
  17. 'allow' => true,
  18. 'roles' => ['@'],
  19. ],
  20. ],
  21. ],
  22. ];
  23. }
  24. public function actionPorPc() {
  25. $req = \Yii::$app->getRequest();
  26. $pc = trim($req->get("pc", ""));
  27. $fi = trim($req->get("fi", ""));
  28. $ff = trim($req->get("ff", ""));
  29. $estacion = trim($req->get("estacion", ""));
  30. $query = (new Query())
  31. ->select([
  32. "{{Estacion}}.id",
  33. "{{Estacion}}.clave",
  34. "{{Estacion}}.siglas",
  35. "{{Estacion}}.frecuencia",
  36. "ciudad",
  37. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  38. "count(estacion) filter (where descargado = true) as descargados",
  39. "count(estacion) filter (where descargado = false) as pendientes"
  40. ])
  41. ->from("Descarga")
  42. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  43. ->andWhere(["pc" => $pc])
  44. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad"])
  45. ->orderBy(["fecha" => SORT_DESC]);
  46. if($estacion !== "") {
  47. $query->andWhere(["estacion" => $estacion]);
  48. }
  49. if($fi !== "") {
  50. $query->andWhere([">=", "fecha", $fi]);
  51. }
  52. if($ff !== "") {
  53. $query->andWhere(["<=", "fecha", $ff]);
  54. }
  55. return (new Respuesta($query, -1));
  56. }
  57. public function actionGuardar() {
  58. $req = \Yii::$app->getRequest();
  59. $pc = trim($req->getBodyParam("idPc", ""));
  60. $fi = trim($req->getBodyParam("fi", ""));
  61. $ff = trim($req->getBodyParam("ff", ""));
  62. $reiniciar = $req->getBodyParam("reiniciar", false);
  63. $estaciones = $req->getBodyParam("estaciones", []);
  64. $aux = [];
  65. foreach($estaciones as $e) {
  66. if(!isset($aux[$e["idCiudad"]])) {
  67. $aux[$e["idCiudad"]] = [];
  68. }
  69. $aux[$e["idCiudad"]][] = $e["clave"];
  70. }
  71. $estaciones = $aux;
  72. try {
  73. $condicion = ["AND",
  74. ["<=", "fecha", $ff],
  75. [">=", "fecha", $fi]
  76. ];
  77. $aux = ["OR"];
  78. foreach($estaciones as $idCiudad => $est) {
  79. $aux[] = [
  80. "AND",
  81. ["ciudad" => $idCiudad],
  82. ["estacion" => $est]
  83. ];
  84. }
  85. $condicion[] = $aux;
  86. $params = ["pc" => $pc];
  87. if($reiniciar) {
  88. $params["descargado"] = false;
  89. }
  90. $resultado = \Yii::$app->getDb()->createCommand()
  91. ->update("Descarga", $params, $condicion)
  92. ->execute();
  93. return (new Respuesta())
  94. ->detalle([ "actualizados" => $resultado ])
  95. ->mensaje("OK");
  96. } catch(\Exception $e) {
  97. return (new Respuesta())
  98. ->esError()
  99. ->mensaje($e->getMessage());
  100. }
  101. }
  102. }