AreaObserver.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/19
  6. * Time: 10:09 AM
  7. */
  8. namespace App\Observers;
  9. use App\Models\Area;
  10. use Illuminate\Support\Facades\Artisan;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. class AreaObserver
  14. {
  15. public function created(Area $area)
  16. {
  17. $redis = Redis::connection();
  18. if ((int)$area->status === Area::STATUS_OK) {
  19. $centre = json_decode($area->area_centre, true);
  20. $redis->geoadd(Area::REDIS_AREAS_LOCATION_TAG, $centre[0], $centre[1], $area->id);
  21. $db_area = DB::table('areas')->where('id', $area->id)->first();
  22. $redis->hset(Area::REDIS_AREAS_TAG, $area->id, serialize(object_array($db_area)));
  23. } else {
  24. $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
  25. $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, [$area->id]);
  26. }
  27. }
  28. public function updated(Area $area)
  29. {
  30. if (!$area->isDirty('status')) return true;
  31. $redis = Redis::connection();
  32. if ((int)$area->status === Area::STATUS_OK) {
  33. $db_area = DB::table('areas')->where('id', $area->id)->first();
  34. $centre = json_decode($db_area->area_centre, true);
  35. $redis->geoadd(Area::REDIS_AREAS_LOCATION_TAG, $centre['longitude'], $centre['latitude'], $area->id);
  36. $redis->hset(Area::REDIS_AREAS_TAG, $area->id, serialize(object_array($db_area)));
  37. //调整停车区
  38. Artisan::call('load:stop_bike_sites');
  39. } else {
  40. $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
  41. $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, [$area->id]);
  42. Artisan::call('load:stop_bike_sites');
  43. }
  44. }
  45. public function deleted(Area $area)
  46. {
  47. $redis = Redis::connection();
  48. $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, $area->id);
  49. $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
  50. }
  51. }