DescargaController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. $query = (new Query())
  30. ->select([
  31. "{{Estacion}}.id",
  32. "{{Estacion}}.clave",
  33. "{{Estacion}}.siglas",
  34. "{{Estacion}}.frecuencia",
  35. "ciudad",
  36. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  37. "count(estacion) filter (where descargado = true) as descargados",
  38. "count(estacion) filter (where descargado = false) as pendientes"
  39. ])
  40. ->from("Descarga")
  41. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  42. ->andWhere(["pc" => $pc])
  43. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad"])
  44. ->orderBy(["fecha" => SORT_DESC]);
  45. if($fi !== "") {
  46. $query->andWhere([">=", "fecha", $fi]);
  47. }
  48. if($ff !== "") {
  49. $query->andWhere(["<=", "fecha", $ff]);
  50. }
  51. return (new Respuesta($query, -1));
  52. }
  53. public function actionGuardar() {
  54. $req = \Yii::$app->getRequest();
  55. $pc = trim($req->getBodyParam("idPc", ""));
  56. $fi = trim($req->getBodyParam("fi", ""));
  57. $ff = trim($req->getBodyParam("ff", ""));
  58. $reiniciar = $req->getBodyParam("reiniciar", false);
  59. $estaciones = $req->getBodyParam("estaciones", []);
  60. $aux = [];
  61. foreach($estaciones as $e) {
  62. if(!isset($aux[$e["idCiudad"]])) {
  63. $aux[$e["idCiudad"]] = [];
  64. }
  65. $aux[$e["idCiudad"]][] = $e["clave"];
  66. }
  67. $estaciones = $aux;
  68. try {
  69. $condicion = ["AND",
  70. ["<=", "fecha", $ff],
  71. [">=", "fecha", $fi]
  72. ];
  73. $aux = ["OR"];
  74. foreach($estaciones as $idCiudad => $est) {
  75. $aux[] = [
  76. "AND",
  77. ["ciudad" => $idCiudad],
  78. ["estacion" => $est]
  79. ];
  80. }
  81. $condicion[] = $aux;
  82. $params = ["pc" => $pc];
  83. if($reiniciar) {
  84. $params["descargado"] = false;
  85. }
  86. $resultado = \Yii::$app->getDb()->createCommand()
  87. ->update("Descarga", $params, $condicion)
  88. ->execute();
  89. return (new Respuesta())
  90. ->detalle([ "actualizados" => $resultado ])
  91. ->mensaje("OK");
  92. } catch(\Exception $e) {
  93. return (new Respuesta())
  94. ->esError()
  95. ->mensaje($e->getMessage());
  96. }
  97. }
  98. }