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 Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Redis;
  12. use MongoDB\Driver\Exception\Exception;
  13. use Symfony\Component\Console\Output\ConsoleOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class ProcessDataJob implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $data;
  19. protected $user;
  20. protected $type;
  21. public $timeout = 200;
  22. public function __construct($data, $user, $type = 1)
  23. {
  24. $this->data = $data;
  25. $this->user = $user;
  26. $this->type = $type;
  27. }
  28. public function handle()
  29. {
  30. $service = new TPlusServerService();
  31. try {
  32. $data = $this->data;
  33. $user = $this->user;
  34. $type = $this->type;
  35. if($type == 1){
  36. //收付款单
  37. list($status, $msg) = $service->synRevenueCostFromTPlus($data, $user);
  38. }elseif($type == 2){
  39. list($status, $msg) = $service->synSalaryEmployeeFromMine($data, $user);
  40. }else{
  41. list($status, $msg) = $service->synFreightFeeFromMine($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. }