where('driver_id', $value); } public function searchIsOverAttribute($query, $value, array $params): mixed { if (!isset($value)) { return $query; } return $query->where('is_over', $value == 1 ? 1 : 0); } /** * 校验用户资质 * @param $user * @return bool */ public function checkCert($user) { $car = DriverCarModel::query()->where('driver_id', $user['id'])->first(); $license = DriverLicenseModel::query()->where('driver_id', $user['id'])->first(); if (!isset($license['status']) || $license['status'] != 1) { return $this->error('请提交驾照信息,若已提交请耐心等待审核'); } if (!isset($car['status']) || $car['status'] != 1) { return $this->error('请提交车辆信息,若已提交请耐心等待审核'); } if (strtotime($car['insurance_date']) <= time()) { return $this->error('车辆保险过期了'); } if (strtotime($license['end_date']) <= time()) { return $this->error('驾照过期了'); } // 判断余额 $wallet = DriverWalletModel::getOne($user['id'],'money'); if ($wallet <= 0){ return $this->error('余额不足,请充值后再行接单'); } return $this->success(); } public static function add(array $params) { $insert = array_merge($params, [ 'status' => 1, 'create_time' => time() ]); return self::query()->insertGetId($insert); } public static function edit(int $id, array $params) { unset($params['id']); $insert = array_merge($params, [ 'update_time' => time() ]); $query = self::query()->where('id', $id); return $query->update($insert); } /** * 上线 * @param int $id * @return bool */ public function online(int $id) { $user = (new DriverModel())->authUserInfo($id); if ($user['is_online'] == 1) { return $this->success('已经上线'); } // 用户资质校验 if (!$this->checkCert($user)) { return $this->error($this->getMessage()); } $model = (new static()); if ($model->getDetail(params: ['driver_id' => $id, 'is_over' => 0])) { return $this->success('已经上线'); } Db::beginTransaction(); $log = $this->add([ 'driver_id' => $id, 'date' => date('Y-m-d'), 'start_at' => time() ]); $online = (new DriverModel())->edit($id, ['is_online' => 1]); if (!$log || !$online) { DB::rollBack(); return $this->error('上线失败了'); } DB::commit(); return $this->success('上线成功'); } /** * 离线 * @param $id * @return bool */ public function offline($id) { $user = (new DriverModel())->authUserInfo($id); if ($user['is_online'] == 0) { return $this->success('已经下线'); } $model = (new static()); $info = $model->getDetail(params: ['driver_id' => $id, 'is_over' => 0]); if (!$info) { return $this->success('已经下线'); } DB::beginTransaction(); $time = time(); $log = $this->edit($info['id'], [ 'end_at' => $time, 'is_over' => 1, 'duration' => intval($time - $info['start_at']), ]); $offline = (new DriverModel())->edit($id, ['is_online' => 0]); if (!$log || !$offline) { DB::rollBack(); return $this->error('下线失败'); } DB::commit(); return $this->success('已经下线'); } }