ProcessDataJob.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  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. protected $user;
  21. protected $type;
  22. public $timeout = 300;
  23. public function __construct($data, $user, $type = 1)
  24. {
  25. $this->data = $data;
  26. $this->user = $user;
  27. $this->type = $type;
  28. }
  29. public function handle()
  30. {
  31. $service = new TPlusServerService();
  32. try {
  33. $data = $this->data;
  34. $user = $this->user;
  35. $type = $this->type;
  36. if($type == 1){
  37. //收付款单
  38. list($status, $msg) = $service->synRevenueCostFromTPlus($data, $user);
  39. }elseif($type == 2){
  40. list($status, $msg) = $service->synSalaryEmployeeFromMine($data, $user);
  41. }else{
  42. list($status, $msg) = $service->synFreightFeeFromMine($data, $user);
  43. }
  44. $this->finalDo($msg, $service);
  45. } catch (\Throwable $e) {
  46. $this->finalDo("异常:" . $e->getMessage(), $service);
  47. $this->delete();
  48. }
  49. }
  50. private function finalDo($msg, $service){
  51. $type = $this->type;
  52. $service->delTableKey($type);
  53. $service->clearTmpTable($type);
  54. $user = $this->user;
  55. $data = $this->data;
  56. RevenueCostMain::insert([
  57. 'result' => $msg,
  58. 'crt_id' => $user['id'],
  59. 'crt_time' => $data['operation_time'],
  60. 'order_type' => $data['type'],
  61. ]);
  62. }
  63. public function failed(\Throwable $exception)
  64. {
  65. Log::error('ProcessDataJob 失败', [
  66. 'error' => $exception->getMessage(),
  67. 'data' => $this->data,
  68. ]);
  69. // 保底清理
  70. try {
  71. $service = new \App\Service\TPlusServerService();
  72. $service->delTableKey($this->type);
  73. } catch (\Throwable $e) {
  74. Log::warning('清理 Redis Key 失败:' . $e->getMessage());
  75. }
  76. }
  77. protected function echoMessage(OutputInterface $output)
  78. {
  79. //输出消息
  80. $output->writeln(json_encode($this->data));
  81. }
  82. }