123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Model\Model;
- use App\Utils\Common;
- use Hyperf\DbConnection\Db;
- use function Hyperf\Config\config;
- class OrderDriverModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'order_driver';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 0;// 默认使用 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 searchStatusAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('status', $value);
- }
- public function searchStartTimeGtAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('start_time','>=', $value);
- }
- /**
- * 创建顺风车行程
- * @param int $driver_id
- * @param array $params
- * @return bool
- */
- public function createTailwind(int $driver_id,array $params)
- {
- $params['driver_id'] = $driver_id;
- $params['create_time'] = time();
- if (!$this->query()->insert($params)) {
- return $this->error('创建失败');
- }
- return $this->success('创建成功');
- }
- /**
- * 接单
- * @param int $driver_id
- * @param array $order 订单 detail
- * @param int $driver_order_id
- * @return bool
- */
- public function takeOrder(int $driver_id,string $driver_name, array $order, int $driver_order_id = 0,array $params = [])
- {
- $time = time();
- // 获取代驾车辆
- $car = DriverCarModel::query()->where('driver_id',$driver_id)->where('status',1)->first();
- if (!$car){
- return $this->error('当前车辆信息错误');
- }
- $orderUp = OrderModel::query()->where('id', $order['id'])->update([
- 'driver_id' => $driver_id,
- 'driver_order_id' => $driver_order_id,
- 'driver_name' => $driver_name,
- 'car_no' => $car['car_no'],
- 'car_color' => $car['car_color'],
- 'car_brand' => $car['car_brand'],
- 'driver_lng' => $params['driver_lng'] ?? '',
- 'driver_lat' => $params['driver_lat'] ?? '',
- 'take_time' => $time,
- 'status' => 20,
- 'update_time' => $time
- ]);
- if (!$orderUp) {
- return $this->error('接单失败');
- }
- $orderPointUp = OrderPointModel::query()->where('order_id', $order['id'])->update([
- 'driver_id' => $driver_id,
- 'driver_order_id' => $driver_order_id,
- 'update_time' => $time
- ]);
- if (!$orderPointUp) {
- return $this->error('接单失败');
- }
- // 顺风车接单需要更改 顺风车路线同行信息
- if ($order['type'] == 30){
- $driverOrderUp = OrderDriverModel::query()
- ->where('id',$driver_order_id)
- ->update(['people_at_num' => Db::raw('people_at_num + '.$order['people_num'])]);
- if (!$driverOrderUp){
- return $this->error('接单失败');
- }
- }
- return $this->success('操作成功');
- }
- /**
- * 操作运送状态
- * @param int $order_id
- * @param int $driver_status
- * @return bool
- */
- public function driver_status(int $order_id, int $driver_status = 1,int $is_pay = 0)
- {
- $data = [
- 'driver_status' => $driver_status,
- 'status' => 30,
- 'update_time' => time()
- ];
- if ($driver_status == 2){
- $data['start_driver_time'] = time();
- }
- if ($driver_status == 3){
- $is_pay === 1 && $data['is_pay'] = $is_pay;
- $is_pay === 1 && $data['status'] = 40;
- $data['end_driver_time'] = time();
- }
- if (!OrderModel::query()->where('id', $order_id)->update($data)) {
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- public function driver()
- {
- return $this->hasOne(DriverModel::class, 'id', 'driver_id');
- }
- public function car()
- {
- return $this->hasOne(DriverCarModel::class, 'driver_id', 'driver_id');
- }
- public function order()
- {
- return $this->hasOne(OrderModel::class, 'driver_order_id', 'id');
- }
- public function orders()
- {
- return $this->hasMany(OrderModel::class, 'driver_order_id', 'id');
- }
- }
|