123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Model\Model;
- use Hyperf\DbConnection\Db;
- class DriverOnlineLogModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'driver_online_log';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public function searchDriverIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->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('已经下线');
- }
- }
|