userstory_model.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. class userstory_model extends Model {
  3. public function getAllStories() {
  4. $rows = $this->query("SELECT
  5. uuid,
  6. previous_uuid,
  7. MIN(created_at) as first_event,
  8. MAX(created_at) as last_event,
  9. COUNT(*) as event_count,
  10. MAX(hotel) as hotel,
  11. MAX(order_id) as order_id,
  12. MAX(ip_address) as ip_address
  13. FROM azonics_user_events
  14. GROUP BY uuid
  15. ORDER BY last_event DESC;");
  16. // UUID change láncolatok feloldása: previous_uuid -> uuid mapping
  17. $uuidChanges = $this->query("SELECT uuid, previous_uuid FROM azonics_user_events
  18. WHERE event_type = 'uuid_change' AND previous_uuid != ''
  19. ORDER BY event_id ASC;");
  20. // Mapping: new_uuid => original_uuid (a lánc elejére mutat)
  21. $childToParent = [];
  22. foreach ($uuidChanges as $change) {
  23. $parent = $change->previous_uuid;
  24. // Ha a parent maga is egy child, kövessük a láncot
  25. if (isset($childToParent[$parent])) {
  26. $parent = $childToParent[$parent];
  27. }
  28. $childToParent[$change->uuid] = $parent;
  29. }
  30. // Sorok csoportosítása az eredeti (parent) UUID alapján
  31. $grouped = [];
  32. $order = [];
  33. foreach ($rows as $row) {
  34. $originalUUID = isset($childToParent[$row->uuid]) ? $childToParent[$row->uuid] : $row->uuid;
  35. if (!isset($grouped[$originalUUID])) {
  36. $grouped[$originalUUID] = (object)[
  37. 'uuid' => $originalUUID,
  38. 'new_uuids' => [],
  39. 'first_event' => $row->first_event,
  40. 'last_event' => $row->last_event,
  41. 'event_count' => 0,
  42. 'hotel' => '',
  43. 'order_id' => '',
  44. 'ip_address' => ''
  45. ];
  46. $order[] = $originalUUID;
  47. }
  48. $g = $grouped[$originalUUID];
  49. $g->event_count += (int)$row->event_count;
  50. if ($row->first_event < $g->first_event) $g->first_event = $row->first_event;
  51. if ($row->last_event > $g->last_event) $g->last_event = $row->last_event;
  52. if (!empty($row->hotel)) $g->hotel = $row->hotel;
  53. if (!empty($row->order_id)) $g->order_id = $row->order_id;
  54. if (!empty($row->ip_address)) $g->ip_address = $row->ip_address;
  55. if ($row->uuid !== $originalUUID) {
  56. $g->new_uuids[] = $row->uuid;
  57. }
  58. }
  59. // Rendezés last_event desc szerint
  60. usort($order, function($a, $b) use ($grouped) {
  61. return strcmp($grouped[$b]->last_event, $grouped[$a]->last_event);
  62. });
  63. $result = [];
  64. foreach ($order as $key) {
  65. $result[] = $grouped[$key];
  66. }
  67. return $result;
  68. }
  69. public function getEventsByUUID($uuid) {
  70. $uuid = $this->escapeString($uuid);
  71. return $this->query("SELECT * FROM azonics_user_events
  72. WHERE uuid = '".$uuid."' OR previous_uuid = '".$uuid."' OR uuid IN (
  73. SELECT uuid FROM azonics_user_events WHERE previous_uuid = '".$uuid."'
  74. )
  75. ORDER BY created_at ASC, event_id ASC;");
  76. }
  77. }