DriverOnlineLogModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Model\Model;
  5. use Hyperf\DbConnection\Db;
  6. class DriverOnlineLogModel extends Model
  7. {
  8. /**
  9. * The table associated with the model.
  10. *
  11. * @var ?string
  12. */
  13. protected ?string $table = 'driver_online_log';
  14. protected ?string $dateFormat = 'U';
  15. public bool $timestamps = false;
  16. protected int $is_status_search = 1;// 默认使用 status = 1 筛选
  17. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  18. /**
  19. * 默认查询字段
  20. *
  21. * @var array|string[]
  22. */
  23. public array $select = [
  24. '*'
  25. ];
  26. public function searchDriverIdAttribute($query, $value, array $params): mixed
  27. {
  28. if (empty($value)) {
  29. return $query;
  30. }
  31. return $query->where('driver_id', $value);
  32. }
  33. public function searchIsOverAttribute($query, $value, array $params): mixed
  34. {
  35. if (!isset($value)) {
  36. return $query;
  37. }
  38. return $query->where('is_over', $value == 1 ? 1 : 0);
  39. }
  40. /**
  41. * 校验用户资质
  42. * @param $user
  43. * @return bool
  44. */
  45. public function checkCert($user)
  46. {
  47. $car = DriverCarModel::query()->where('driver_id', $user['id'])->first();
  48. $license = DriverLicenseModel::query()->where('driver_id', $user['id'])->first();
  49. if (!isset($license['status']) || $license['status'] != 1) {
  50. return $this->error('请提交驾照信息,若已提交请耐心等待审核');
  51. }
  52. if (!isset($car['status']) || $car['status'] != 1) {
  53. return $this->error('请提交车辆信息,若已提交请耐心等待审核');
  54. }
  55. if (strtotime($car['insurance_date']) <= time()) {
  56. return $this->error('车辆保险过期了');
  57. }
  58. if (strtotime($license['end_date']) <= time()) {
  59. return $this->error('驾照过期了');
  60. }
  61. // 判断余额
  62. $wallet = DriverWalletModel::getOne($user['id'],'money');
  63. if ($wallet <= 0){
  64. return $this->error('余额不足,请充值后再行接单');
  65. }
  66. return $this->success();
  67. }
  68. public static function add(array $params)
  69. {
  70. $insert = array_merge($params, [
  71. 'status' => 1,
  72. 'create_time' => time()
  73. ]);
  74. return self::query()->insertGetId($insert);
  75. }
  76. public static function edit(int $id, array $params)
  77. {
  78. unset($params['id']);
  79. $insert = array_merge($params, [
  80. 'update_time' => time()
  81. ]);
  82. $query = self::query()->where('id', $id);
  83. return $query->update($insert);
  84. }
  85. /**
  86. * 上线
  87. * @param int $id
  88. * @return bool
  89. */
  90. public function online(int $id)
  91. {
  92. $user = (new DriverModel())->authUserInfo($id);
  93. if ($user['is_online'] == 1) {
  94. return $this->success('已经上线');
  95. }
  96. // 用户资质校验
  97. if (!$this->checkCert($user)) {
  98. return $this->error($this->getMessage());
  99. }
  100. $model = (new static());
  101. if ($model->getDetail(params: ['driver_id' => $id, 'is_over' => 0])) {
  102. return $this->success('已经上线');
  103. }
  104. Db::beginTransaction();
  105. $log = $this->add([
  106. 'driver_id' => $id,
  107. 'date' => date('Y-m-d'),
  108. 'start_at' => time()
  109. ]);
  110. $online = (new DriverModel())->edit($id, ['is_online' => 1]);
  111. if (!$log || !$online) {
  112. DB::rollBack();
  113. return $this->error('上线失败了');
  114. }
  115. DB::commit();
  116. return $this->success('上线成功');
  117. }
  118. /**
  119. * 离线
  120. * @param $id
  121. * @return bool
  122. */
  123. public function offline($id)
  124. {
  125. $user = (new DriverModel())->authUserInfo($id);
  126. if ($user['is_online'] == 0) {
  127. return $this->success('已经下线');
  128. }
  129. $model = (new static());
  130. $info = $model->getDetail(params: ['driver_id' => $id, 'is_over' => 0]);
  131. if (!$info) {
  132. return $this->success('已经下线');
  133. }
  134. DB::beginTransaction();
  135. $time = time();
  136. $log = $this->edit($info['id'], [
  137. 'end_at' => $time,
  138. 'is_over' => 1,
  139. 'duration' => intval($time - $info['start_at']),
  140. ]);
  141. $offline = (new DriverModel())->edit($id, ['is_online' => 0]);
  142. if (!$log || !$offline) {
  143. DB::rollBack();
  144. return $this->error('下线失败');
  145. }
  146. DB::commit();
  147. return $this->success('已经下线');
  148. }
  149. }