BoxAddJob.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\ErrorTable;
  4. use App\Service\Box\BoxService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Symfony\Component\Console\Output\ConsoleOutput;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class BoxAddJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. //队列名称
  16. const job = 'hc_box';// 包装 =》 用友产成品入库
  17. protected $data;
  18. protected $user;
  19. public $tries = 0;
  20. public function __construct($data, $user = [])
  21. {
  22. $this->data = $data ?? [];
  23. $this->user = $user ?? [];
  24. }
  25. /**
  26. *
  27. * file_put_contents('charge.txt',"标记位置退出".PHP_EOL,8);
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $service = new BoxService();
  35. try {
  36. list($status,$msg) = $service->boxInSettle($this->data, $this->user);
  37. if(! $status) {
  38. $service->dellimitingSendRequestBackgNeed($service->lock_key); //删除锁
  39. $this->recordErrorTable($msg);
  40. }
  41. } catch (\Throwable $e) {
  42. $service->dellimitingSendRequestBackgNeed($service->lock_key);//删除锁
  43. $this->delete();
  44. $this->recordErrorTable($e->getFile() . $e->getMessage() . $e->getLine());
  45. }
  46. //输出信息
  47. $this->echoMessage(new ConsoleOutput());
  48. }
  49. protected function echoMessage(OutputInterface $output)
  50. {
  51. //输出消息
  52. $output->writeln(json_encode($this->data));
  53. }
  54. private function recordErrorTable($msg){
  55. // 连接到指定数据库连接
  56. ErrorTable::insert([
  57. 'msg' => $msg,
  58. 'data' => json_encode($this->data),
  59. 'user_id' => $this->user['id'] ?? 0,
  60. 'user_operation_time' => time(),
  61. 'type' => 1,
  62. 'order_no' => $data['order_no'] ?? ""
  63. ]);
  64. }
  65. }