2022-08-19 09:51:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Remote;
|
|
|
|
|
2022-08-19 15:27:57 +00:00
|
|
|
use App\Models\WorkOrder\Reply;
|
2022-08-19 09:51:52 +00:00
|
|
|
use App\Models\WorkOrder\WorkOrder;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2022-08-19 09:51:52 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2022-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
|
|
class PushWorkOrder implements ShouldQueue
|
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
use InteractsWithQueue, Queueable, SerializesModels;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
//
|
2022-08-19 15:27:57 +00:00
|
|
|
WorkOrder::whereIn('status', ['pending', 'error'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
|
2022-08-19 09:51:52 +00:00
|
|
|
foreach ($workOrders as $workOrder) {
|
|
|
|
|
|
|
|
if ($workOrder->host->status === 'pending') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:33:26 +00:00
|
|
|
if ($workOrder->status === 'error') {
|
|
|
|
// 如果 created_at 超过 3 天 use Carbon
|
|
|
|
if (now()->diffInDays($workOrder->created_at) > 3) {
|
|
|
|
$workOrder->delete();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
$http = Http::remote($workOrder->module->api_token, $workOrder->module->url);
|
|
|
|
$workOrder->status = 'open';
|
|
|
|
|
|
|
|
$response = $http->post('work-orders', $workOrder->toArray());
|
|
|
|
|
|
|
|
if (!$response->successful()) {
|
2022-09-08 16:12:02 +00:00
|
|
|
Log::error('推送工单失败', [
|
|
|
|
'work_order_id' => $workOrder->id,
|
|
|
|
'response' => $response->body(),
|
|
|
|
]);
|
2022-08-19 09:51:52 +00:00
|
|
|
$workOrder->status = 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
$workOrder->save();
|
2022-09-08 16:12:02 +00:00
|
|
|
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|
|
|
|
});
|
2022-08-19 09:51:52 +00:00
|
|
|
|
2022-08-19 15:27:57 +00:00
|
|
|
Reply::where('is_pending', 1)->chunk(100, function ($replies) {
|
|
|
|
foreach ($replies as $reply) {
|
|
|
|
dispatch(new \App\Jobs\Remote\WorkOrder\Reply($reply));
|
2022-08-19 09:51:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|