ProcessDataJob.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\ErrorTable;
  4. use App\Model\U8Job;
  5. use App\Service\U8ServerService;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. use MongoDB\Driver\Exception\Exception;
  14. use Symfony\Component\Console\Output\ConsoleOutput;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ProcessDataJob implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $data;
  20. public $tries = 0;
  21. public $timeout = 60;
  22. //1 采购 2 销售(合同)
  23. protected $function = [
  24. 1 => 'U8PO_PomainSave',
  25. 2 => 'U8SaleOrderSave',
  26. ];
  27. public function __construct($data)
  28. {
  29. $this->data = $data;
  30. }
  31. public function handle()
  32. {
  33. try {
  34. $function = $this->function[$this->data['type']] ?? '';
  35. if(empty($function)) return;
  36. //调用同步方法
  37. $this->$function();
  38. } catch (\Throwable $e) {
  39. $this->delete();
  40. }
  41. }
  42. //采购
  43. private function U8PO_PomainSave(){
  44. $service = new U8ServerService();
  45. if(! empty($service->error)) {
  46. $service->finalSettle($this->data['id'], U8Job::one, $service->error);
  47. return;
  48. }
  49. $service->U8PO_PomainSave($this->data['id']);
  50. }
  51. //销售(合同)
  52. private function U8SaleOrderSave(){
  53. $service = new U8ServerService();
  54. if(! empty($service->error)) {
  55. $service->finalSettle($this->data['id'], U8Job::two, $service->error);
  56. return;
  57. }
  58. $service->U8SaleOrderSave($this->data['id']);
  59. }
  60. // public function failed($exception)
  61. // {
  62. // // 记录失败错误信息到日志或其他媒介
  63. // $errorMessage = $exception->getFile() . $exception->getMessage() . $exception->getLine();
  64. // $this->recordErrorTable($errorMessage);
  65. // }
  66. private function recordErrorTable($msg){
  67. $data = $this->data;
  68. ErrorTable::insert([
  69. 'msg' => $msg,
  70. 'data' => json_encode($this->data),
  71. 'user_id' => $data['user']['id'],
  72. 'user_operation_time' => $data['user']['operate_time'],
  73. 'type' => $data['type']
  74. ]);
  75. }
  76. protected function echoMessage(OutputInterface $output)
  77. {
  78. //输出消息
  79. $output->writeln(json_encode($this->data));
  80. }
  81. }