UpdateWatchWorkOrdersCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Bike;
  4. use App\Models\WorkOrder;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class UpdateWatchWorkOrdersCommand extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'update:watch_worker_orders';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '更新watch工单';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. //1.查找所有大于36小时未骑行的车辆
  39. $timeOut = Carbon::now()->subHours(36);
  40. $bikes = Bike::query()
  41. ->where('is_link',Bike::LINK_ONLINE) //在线
  42. ->where('is_trouble',Bike::TROUBLE_NO) //无故障
  43. ->where('put_status',Bike::PUT_STATUS_YES) //已投放
  44. ->where('is_riding',Bike::RIDING_NO) //没人骑车
  45. ->where('last_use_bike_end_time','<',$timeOut)
  46. ->get();
  47. if(empty($bikes)) return ;
  48. // 2.插入工单
  49. foreach ($bikes as $v){
  50. $data = [
  51. 'work_no' => WorkOrder::makeWorkNo(),
  52. 'type' => WorkOrder::TYPE_WATCH,
  53. 'type_name' => WorkOrder::$typeMaps[WorkOrder::TYPE_WATCH],
  54. 'bike_no' => $v->bike_no,
  55. 'bike_id' => $v->id,
  56. 'source' => WorkOrder::SOURCE_SYSTEM,
  57. 'planned' => WorkOrder::PLANNED_STATUS_MEET,
  58. 'area_id' => $v->put_area_id,
  59. ];
  60. $verify =[
  61. 'bike_id' => $v->id,
  62. 'type' => WorkOrder::TYPE_WATCH,
  63. 'status' => WorkOrder::STATUS_NO,
  64. ];
  65. try{
  66. WorkOrder::firstOrCreate($verify,$data);
  67. }catch(\Exception $e){
  68. Log::error($e->getMessage());
  69. }
  70. }
  71. }
  72. }