AreaObserver.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $merchant_id = $area->merchant_id;
  19. $location_key = Area::REDIS_AREAS_LOCATION_TAG . ":{$merchant_id}";
  20. // $area_key = Area::REDIS_AREAS_TAG . ":{$merchant_id}:";
  21. $area_key = Area::REDIS_AREAS_TAG;
  22. if ((int)$area->status === Area::STATUS_OK) {
  23. $centre = json_decode($area->area_centre, true);
  24. $redis->geoadd($location_key, $centre[0], $centre[1], $area->id);
  25. $db_area = DB::table('areas')->where('id', $area->id)->first();
  26. $redis->hset($area_key, $area->id, serialize(object_array($db_area)));
  27. } else {
  28. $redis->hdel($area_key, [$area->id]);
  29. $redis->zrem($location_key, [$area->id]);
  30. }
  31. }
  32. public function updated(Area $area)
  33. {
  34. if (!$area->isDirty('status')) return true;
  35. $redis = Redis::connection();
  36. $merchant_id = $area->merchant_id;
  37. $location_key = Area::REDIS_AREAS_LOCATION_TAG . ":{$merchant_id}";
  38. $area_key = Area::REDIS_AREAS_TAG;
  39. if ((int)$area->status === Area::STATUS_OK) {
  40. $db_area = DB::table('areas')->where('id', $area->id)->first();
  41. $centre = json_decode($db_area->area_centre, true);
  42. $redis->geoadd($location_key, $centre['longitude'], $centre['latitude'], $area->id);
  43. $redis->hset(Area::REDIS_AREAS_TAG, $area->id, serialize(object_array($db_area)));
  44. //调整停车区
  45. Artisan::call('load:stop_bike_sites');
  46. } else {
  47. $redis->hdel($area_key, [$area->id]);
  48. $redis->zrem($location_key, [$area->id]);
  49. Artisan::call('load:stop_bike_sites');
  50. }
  51. }
  52. public function deleted(Area $area)
  53. {
  54. $merchant_id = $area->merchant_id;
  55. $location_key = Area::REDIS_AREAS_LOCATION_TAG . ":{$merchant_id}";
  56. $area_key = Area::REDIS_AREAS_TAG;
  57. $redis = Redis::connection();
  58. $redis->zrem($location_key, $area->id);
  59. $redis->hdel($area_key, [$area->id]);
  60. }
  61. }