ProcessDataJob.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\RevenueCostMain;
  4. use App\Service\TPlusServerService;
  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 MongoDB\Driver\Exception\Exception;
  11. use Symfony\Component\Console\Output\ConsoleOutput;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ProcessDataJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $data;
  17. protected $user;
  18. protected $type;
  19. public $timeout = 300;
  20. public function __construct($data, $user, $type = 1)
  21. {
  22. $this->data = $data;
  23. $this->user = $user;
  24. $this->type = $type;
  25. }
  26. public function handle()
  27. {
  28. $service = new TPlusServerService();
  29. try {
  30. $data = $this->data;
  31. $user = $this->user;
  32. $type = $this->type;
  33. if($type == 1){
  34. //收付款单
  35. list($status, $msg) = $service->synRevenueCostFromTPlus($data, $user);
  36. }elseif($type == 2){
  37. list($status, $msg) = $service->synSalaryEmployeeFromMine($data, $user);
  38. }elseif($type == 3){
  39. list($status, $msg) = $service->synFreightFeeFromMine($data, $user);
  40. }else{
  41. list($status, $msg) = $service->synItemRoadMy($data, $user);
  42. }
  43. $this->finalDo($msg, $service);
  44. } catch (\Throwable $e) {
  45. $this->finalDo("异常:" . $e->getMessage(), $service);
  46. $this->delete();
  47. }
  48. }
  49. private function finalDo($msg, $service){
  50. $type = $this->type;
  51. $service->delTableKey($type);
  52. $service->clearTmpTable($type);
  53. $user = $this->user;
  54. $data = $this->data;
  55. RevenueCostMain::insert([
  56. 'result' => $msg,
  57. 'crt_id' => $user['id'],
  58. 'crt_time' => $data['operation_time'],
  59. 'order_type' => $data['type'],
  60. ]);
  61. }
  62. protected function echoMessage(OutputInterface $output)
  63. {
  64. //输出消息
  65. $output->writeln(json_encode($this->data));
  66. }
  67. }