12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/19
- * Time: 10:09 AM
- */
- namespace App\Observers;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Models\Bike;
- use App\Models\LocationsLog;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class BikeObserver
- {
- public function created(Bike $bike)
- {
- $bike_no = $bike->bike_no;
- $box_no = $bike->box_no;
- if ($bike_no && $box_no) {
- $redis = Redis::connection();
- $redis->hset(Bike::REDIS_BOX_NO_AND_BIKE_NO_TAG, $box_no, $bike_no);
- }
- }
- public function updated(Bike $bike)
- {
- $bike_no = $bike->bike_no;
- $box_no = $bike->box_no;
- $redis = Redis::connection();
- if ($bike->isDirty('box_no')) {
- DB::table('bikes')->where('id', $bike->id)->update([
- 'is_link' => Bike::LINK_OFFLINE
- ]);
- $old_box_no = $bike->getOriginal('box_no');
- $redis->hdel(Bike::REDIS_BOX_NO_AND_BIKE_NO_TAG, [$old_box_no]);
- }
- if ($bike_no && $box_no) {
- $redis->hset(Bike::REDIS_BOX_NO_AND_BIKE_NO_TAG, $box_no, $bike_no);
- if ($bike->isDirty('put_status')) {
- $BikeStatusInfoSyncHandler = (new BikeStatusInfoSyncHandler());
- if ((int)$bike->put_status === Bike::PUT_STATUS_NO) {
- $BikeStatusInfoSyncHandler->toBikeOffLineStatus($bike_no);
- } else {
- $location = LocationsLog::getNewestLocationByBikeNo($bike_no);
- if ($location['lat'] <= 0) {
- $location = json_decode($bike->last_location, true);
- }
- $BikeStatusInfoSyncHandler->toBikeOnLineStatus($bike_no, $location['lng'], $location['lat']);
- }
- }
- }
- }
- public function deleted(Bike $bike)
- {
- $box_no = $bike->box_no;
- $redis = Redis::connection();
- $redis->hdel(Bike::REDIS_BOX_NO_AND_BIKE_NO_TAG, [$box_no]);
- }
- }
|