ProcessDataJob.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 => 'reBackThree',
  38. ];
  39. protected $jobs = [
  40. 1 => self::job_one,
  41. 2 => self::job_two,
  42. 3 => self::job_one,
  43. ];
  44. /**
  45. * Create a new job instance.
  46. * $data = [
  47. 'result' => $msg, //查询结果
  48. 'data' => $data, //用户提交的参数
  49. ];
  50. * $user 提交用户的信息
  51. * $type //1 代表产成品入库(完工操作) 2 销售出库单(包装单扫描出库)
  52. *
  53. * @return void
  54. */
  55. public function __construct($data, $user = [], $type)
  56. {
  57. $this->data = $data;
  58. $this->user = $user ?? [];
  59. $this->type = $type;
  60. }
  61. /**
  62. *
  63. * file_put_contents('charge.txt',"标记位置退出".PHP_EOL,8);
  64. * Execute the job.
  65. *
  66. * @return void
  67. */
  68. public function handle()
  69. {
  70. try {
  71. $function = $this->function[$this->type] ?? '';//调用方法 入库
  72. $function_back = $this->function_reback[$this->type] ?? '';//数据回退方法
  73. if(empty($function)) return;
  74. list($status,$msg) = $this->$function();
  75. if(! $status) $this->errorSettle($msg);
  76. } catch (\Exception $e) {
  77. $this->$function_back();
  78. $this->recordErrorTable($e->getFile() . $e->getMessage() . $e->getLine());
  79. }
  80. //输出信息
  81. $this->echoMessage(new ConsoleOutput());
  82. }
  83. protected function echoMessage(OutputInterface $output)
  84. {
  85. //输出消息
  86. $output->writeln(json_encode($this->data));
  87. }
  88. //产成品入库
  89. private function U8Rdrecord10Save(){
  90. $service = new FinishedOrderService();
  91. //标记
  92. list($status,$msg) = $service->addInJob($this->data['result'],$this->data['data'],$this->user);
  93. return [$status,$msg];
  94. }
  95. //产成品入库手机端
  96. private function U8Rdrecord10SaveMobile(){
  97. $service = new FinishedOrderService();
  98. //标记
  99. list($status,$msg) = $service->addMobileInJob($this->data['result'],$this->data['data'],$this->data['quantity_count'],$this->user);
  100. return [$status,$msg];
  101. }
  102. //产成品入库 相关数据回退
  103. private function reBackOne(){
  104. //数据回退
  105. $data = $this->data['data'];
  106. $database = $this->settleDatabase();
  107. // 连接到指定数据库连接
  108. DB::connection($database)->table('dispatch_sub')
  109. ->whereIn('id',$data['id'])
  110. ->update(['job_status' => 0]);
  111. }
  112. //产成品入库 相关数据回退
  113. private function reBackThree(){
  114. //数据回退
  115. $data = $this->data['data'];
  116. $database = $this->settleDatabase();
  117. // 连接到指定数据库连接
  118. DB::connection($database)->table('dispatch_sub')
  119. ->whereIn('id',array_column($data,'id'))
  120. ->update(['job_status' => 0]);
  121. }
  122. //销售单出库
  123. private function U8Rdrecord32Save(){
  124. $service = new FyyOrderService();
  125. list($status,$msg) = $service->addInJob($this->data['result'],$this->data['data'],$this->user);
  126. return [$status,$msg];
  127. }
  128. //销售单出库 相关数据回退
  129. private function reBackTwo(){
  130. //数据回退
  131. $data = $this->data['data'];
  132. $database = $this->settleDatabase();
  133. // 连接到指定数据库连接
  134. DB::connection($database)->table('box')
  135. ->whereIn('order_no',$data['order_no'])
  136. ->update(['state' => 0]);
  137. }
  138. private function errorSettle($msg){
  139. $redis = Redis::connection();
  140. $order_failures_key = md5(json_encode($this->data));
  141. // 从Redis中获取失败计数
  142. $failureCount = $redis->hIncrBy('order_failures', $order_failures_key, 1);
  143. //队列名
  144. $job = $this->jobs[$this->type];
  145. if ($failureCount < 1) {
  146. // 将任务重新放回队列
  147. self::dispatch($this->data,$this->user,$this->type)->onQueue($job)->delay(now()->addSeconds(2));
  148. } else {
  149. // 删除失败计数
  150. $redis->hDel('order_failures', $order_failures_key);
  151. //数据回退
  152. $function_back = $this->function_reback[$this->type] ?? '';
  153. $this->$function_back();
  154. //记录错误
  155. $this->recordErrorTable($msg);
  156. }
  157. }
  158. private function recordErrorTable($msg){
  159. $database = $this->settleDatabase();
  160. // 连接到指定数据库连接
  161. DB::connection($database)->table('error_table')->insert([
  162. 'msg' => $msg,
  163. 'data' => json_encode($this->data),
  164. 'user_id' => $this->user['id'],
  165. 'user_operation_time' => $this->user['operate_time'],
  166. 'type' => $this->type
  167. ]);
  168. }
  169. public function failed($exception)
  170. {
  171. // 记录失败错误信息到日志或其他媒介
  172. $errorMessage = $exception->getFile() . $exception->getMessage() . $exception->getLine();
  173. $this->recordErrorTable($errorMessage);
  174. }
  175. public function settleDatabase(){
  176. $zt = $this->user['zt'] ?? '';
  177. return (new FinishedOrderService())->getConnectionName($zt);
  178. }
  179. }