ProcessDataJob.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\Box;
  4. use App\Model\DispatchSub;
  5. use App\Model\ErrorTable;
  6. use App\Service\FinishedOrderService;
  7. use App\Service\FyyOrderService;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Redis;
  15. use MongoDB\Driver\Exception\Exception;
  16. use Symfony\Component\Console\Output\ConsoleOutput;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. class ProcessDataJob implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. //队列名称
  22. const job_one = 'finished_operation';//完工
  23. const job_two = 'box_operation';//包装
  24. protected $data;
  25. protected $user;
  26. protected $type;
  27. //1 代表产成品入库 2 销售出库单 3 代表产成品入库手机端 4 销售出库单
  28. protected $function = [
  29. 1 => 'U8Rdrecord10Save',
  30. 2 => 'U8Rdrecord32Save',
  31. 3 => 'U8Rdrecord10SaveMobile'
  32. ];
  33. //数据回退 标记了的单据数据状态改为0
  34. protected $function_reback = [
  35. 1 => 'reBackOne',
  36. 2 => 'reBackTwo',
  37. 3 => 'reBackOne',
  38. ];
  39. protected $jobs = [
  40. 1 => self::job_one,
  41. 2 => self::job_two
  42. ];
  43. /**
  44. * Create a new job instance.
  45. * $data = [
  46. 'result' => $msg, //查询结果
  47. 'data' => $data, //用户提交的参数
  48. ];
  49. * $user 提交用户的信息
  50. * $type //1 代表产成品入库(完工操作) 2 销售出库单(包装单扫描出库)
  51. *
  52. * @return void
  53. */
  54. public function __construct($data, $user = [], $type)
  55. {
  56. $this->data = $data;
  57. $this->user = $user ?? [];
  58. $this->type = $type;
  59. }
  60. /**
  61. *
  62. * file_put_contents('charge.txt',"标记位置退出".PHP_EOL,8);
  63. * Execute the job.
  64. *
  65. * @return void
  66. */
  67. public function handle()
  68. {
  69. try {
  70. $function = $this->function[$this->type] ?? '';//调用方法 入库
  71. $function_back = $this->function_reback[$this->type] ?? '';//数据回退方法
  72. if(empty($function)) return;
  73. list($status,$msg) = $this->$function();
  74. if(! $status) $this->errorSettle($msg);
  75. } catch (\Exception $e) {
  76. $this->$function_back();
  77. $this->recordErrorTable($e->getFile() . $e->getMessage() . $e->getLine());
  78. }
  79. //输出信息
  80. $this->echoMessage(new ConsoleOutput());
  81. }
  82. protected function echoMessage(OutputInterface $output)
  83. {
  84. //输出消息
  85. $output->writeln(json_encode($this->data));
  86. }
  87. //产成品入库
  88. private function U8Rdrecord10Save(){
  89. $service = new FinishedOrderService();
  90. //标记
  91. list($status,$msg) = $service->addInJob($this->data['result'],$this->data['data']);
  92. return [$status,$msg];
  93. }
  94. //产成品入库手机端
  95. private function U8Rdrecord10SaveMobile(){
  96. $service = new FinishedOrderService();
  97. //标记
  98. list($status,$msg) = $service->addMobileInJob($this->data['result'],$this->data['data'],$this->data['quantity_count']);
  99. return [$status,$msg];
  100. }
  101. //产成品入库 相关数据回退
  102. private function reBackOne(){
  103. //数据回退
  104. $data = $this->data['data'];
  105. //进入队列的数据
  106. DispatchSub::whereIn('id',$data['id'])->update([
  107. 'job_status' => 0,
  108. ]);
  109. }
  110. //销售单出库
  111. private function U8Rdrecord32Save(){
  112. $service = new FyyOrderService();
  113. list($status,$msg) = $service->addInJob($this->data['result'],$this->data['data']);
  114. return [$status,$msg];
  115. }
  116. //销售单出库 相关数据回退
  117. private function reBackTwo(){
  118. //数据回退
  119. $data = $this->data['data'];
  120. //进入队列的数据
  121. Box::where('order_no',$data['order_no'])->update([
  122. 'state' => 0,
  123. ]);
  124. }
  125. private function errorSettle($msg){
  126. $redis = Redis::connection();
  127. $order_failures_key = md5(json_encode($this->data));
  128. // 从Redis中获取失败计数
  129. $failureCount = $redis->hIncrBy('order_failures', $order_failures_key, 1);
  130. //队列名
  131. $job = $this->jobs[$this->type];
  132. if ($failureCount < 2) {
  133. // 将任务重新放回队列
  134. self::dispatch($this->data,$this->user,$this->type)->onQueue($job)->delay(now()->addSeconds(2));
  135. } else {
  136. // 删除失败计数
  137. $redis->hDel('order_failures', $order_failures_key);
  138. //数据回退
  139. $function_back = $this->function_reback[$this->type] ?? '';
  140. $this->$function_back();
  141. //记录错误
  142. $this->recordErrorTable($msg);
  143. }
  144. }
  145. private function recordErrorTable($msg){
  146. $model = new ErrorTable();
  147. $model->msg = $msg;
  148. $model->data = json_encode($this->data);
  149. $model->user_id = $this->user['id'];
  150. $model->user_operation_time = $this->user['operate_time'];
  151. $model->type = $this->type;
  152. $model->save();
  153. }
  154. public function failed($exception)
  155. {
  156. // 记录失败错误信息到日志或其他媒介
  157. $errorMessage = $exception->getFile() . $exception->getMessage() . $exception->getLine();
  158. $this->recordErrorTable($errorMessage);
  159. }
  160. }