123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/19
- * Time: 10:09 AM
- */
- namespace App\Observers;
- use App\Models\Area;
- use Illuminate\Support\Facades\Artisan;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- class AreaObserver
- {
- public function created(Area $area)
- {
- $redis = Redis::connection();
- if ((int)$area->status === Area::STATUS_OK) {
- $centre = json_decode($area->area_centre, true);
- $redis->geoadd(Area::REDIS_AREAS_LOCATION_TAG, $centre[0], $centre[1], $area->id);
- $db_area = DB::table('areas')->where('id', $area->id)->first();
- $redis->hset(Area::REDIS_AREAS_TAG, $area->id, serialize(object_array($db_area)));
- } else {
- $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
- $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, [$area->id]);
- }
- }
- public function updated(Area $area)
- {
- if (!$area->isDirty('status')) return true;
- $redis = Redis::connection();
- if ((int)$area->status === Area::STATUS_OK) {
- $db_area = DB::table('areas')->where('id', $area->id)->first();
- $centre = json_decode($db_area->area_centre, true);
- $redis->geoadd(Area::REDIS_AREAS_LOCATION_TAG, $centre['longitude'], $centre['latitude'], $area->id);
- $redis->hset(Area::REDIS_AREAS_TAG, $area->id, serialize(object_array($db_area)));
- //调整停车区
- Artisan::call('load:stop_bike_sites');
- } else {
- $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
- $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, [$area->id]);
- Artisan::call('load:stop_bike_sites');
- }
- }
- public function deleted(Area $area)
- {
- $redis = Redis::connection();
- $redis->zrem(Area::REDIS_AREAS_LOCATION_TAG, $area->id);
- $redis->hdel(Area::REDIS_AREAS_TAG, [$area->id]);
- }
- }
|