OrderDriverModel.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Model\Model;
  5. use App\Utils\Common;
  6. use Hyperf\DbConnection\Db;
  7. use function Hyperf\Config\config;
  8. class OrderDriverModel extends Model
  9. {
  10. /**
  11. * The table associated with the model.
  12. *
  13. * @var ?string
  14. */
  15. protected ?string $table = 'order_driver';
  16. protected ?string $dateFormat = 'U';
  17. public bool $timestamps = false;
  18. protected int $is_status_search = 0;// 默认使用 status = 1 筛选
  19. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  20. /**
  21. * 默认查询字段
  22. *
  23. * @var array|string[]
  24. */
  25. public array $select = [
  26. '*'
  27. ];
  28. public function searchDriverIdAttribute($query, $value, array $params): mixed
  29. {
  30. if (empty($value)) {
  31. return $query;
  32. }
  33. return $query->where('driver_id', $value);
  34. }
  35. public function searchStatusAttribute($query, $value, array $params): mixed
  36. {
  37. if (empty($value)) {
  38. return $query;
  39. }
  40. return $query->where('status', $value);
  41. }
  42. public function searchStartTimeGtAttribute($query, $value, array $params): mixed
  43. {
  44. if (empty($value)) {
  45. return $query;
  46. }
  47. return $query->where('start_time','>=', $value);
  48. }
  49. /**
  50. * 创建顺风车行程
  51. * @param int $driver_id
  52. * @param array $params
  53. * @return bool
  54. */
  55. public function createTailwind(int $driver_id,array $params)
  56. {
  57. $params['driver_id'] = $driver_id;
  58. $params['create_time'] = time();
  59. if (!$this->query()->insert($params)) {
  60. return $this->error('创建失败');
  61. }
  62. return $this->success('创建成功');
  63. }
  64. /**
  65. * 接单
  66. * @param int $driver_id
  67. * @param array $order 订单 detail
  68. * @param int $driver_order_id
  69. * @return bool
  70. */
  71. public function takeOrder(int $driver_id,string $driver_name, array $order, int $driver_order_id = 0,array $params = [])
  72. {
  73. $time = time();
  74. // 获取代驾车辆
  75. $car = DriverCarModel::query()->where('driver_id',$driver_id)->where('status',1)->first();
  76. if (!$car){
  77. return $this->error('当前车辆信息错误');
  78. }
  79. $orderUp = OrderModel::query()->where('id', $order['id'])->update([
  80. 'driver_id' => $driver_id,
  81. 'driver_order_id' => $driver_order_id,
  82. 'driver_name' => $driver_name,
  83. 'car_no' => $car['car_no'],
  84. 'car_color' => $car['car_color'],
  85. 'car_brand' => $car['car_brand'],
  86. 'driver_lng' => $params['driver_lng'] ?? '',
  87. 'driver_lat' => $params['driver_lat'] ?? '',
  88. 'take_time' => $time,
  89. 'status' => 20,
  90. 'update_time' => $time
  91. ]);
  92. if (!$orderUp) {
  93. return $this->error('接单失败');
  94. }
  95. $orderPointUp = OrderPointModel::query()->where('order_id', $order['id'])->update([
  96. 'driver_id' => $driver_id,
  97. 'driver_order_id' => $driver_order_id,
  98. 'update_time' => $time
  99. ]);
  100. if (!$orderPointUp) {
  101. return $this->error('接单失败');
  102. }
  103. // 顺风车接单需要更改 顺风车路线同行信息
  104. if ($order['type'] == 30){
  105. $driverOrderUp = OrderDriverModel::query()
  106. ->where('id',$driver_order_id)
  107. ->update(['people_at_num' => Db::raw('people_at_num + '.$order['people_num'])]);
  108. if (!$driverOrderUp){
  109. return $this->error('接单失败');
  110. }
  111. }
  112. return $this->success('操作成功');
  113. }
  114. /**
  115. * 操作运送状态
  116. * @param int $order_id
  117. * @param int $driver_status
  118. * @return bool
  119. */
  120. public function driver_status(int $order_id, int $driver_status = 1,int $is_pay = 0)
  121. {
  122. $data = [
  123. 'driver_status' => $driver_status,
  124. 'status' => 30,
  125. 'update_time' => time()
  126. ];
  127. if ($driver_status == 2){
  128. $data['start_driver_time'] = time();
  129. }
  130. if ($driver_status == 3){
  131. $is_pay === 1 && $data['is_pay'] = $is_pay;
  132. $is_pay === 1 && $data['status'] = 40;
  133. $data['end_driver_time'] = time();
  134. }
  135. if (!OrderModel::query()->where('id', $order_id)->update($data)) {
  136. return $this->error('操作失败');
  137. }
  138. return $this->success('操作成功');
  139. }
  140. public function driver()
  141. {
  142. return $this->hasOne(DriverModel::class, 'id', 'driver_id');
  143. }
  144. public function car()
  145. {
  146. return $this->hasOne(DriverCarModel::class, 'driver_id', 'driver_id');
  147. }
  148. public function order()
  149. {
  150. return $this->hasOne(OrderModel::class, 'driver_order_id', 'id');
  151. }
  152. public function orders()
  153. {
  154. return $this->hasMany(OrderModel::class, 'driver_order_id', 'id');
  155. }
  156. }