OrderSuperLossJob.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\job;
  3. use app\api\controller\OrderSuperLoss;
  4. use think\Db;
  5. use think\Log;
  6. class OrderSuperLossJob
  7. {
  8. /**
  9. * 队列消费入口(ThinkPHP queue fire)
  10. * @param $job
  11. * @param array $data
  12. */
  13. public function fire($job, $data)
  14. {
  15. try {
  16. if (empty($data['gdbh']) || empty($data['yjno'])) {
  17. Log::error('超节损队列参数缺失: ' . json_encode($data, JSON_UNESCAPED_UNICODE));
  18. $job->delete();
  19. return;
  20. }
  21. $api = new OrderSuperLoss();
  22. $result = $api->OneOrderSuperLoss($data['gdbh'], $data['yjno']);
  23. if (empty($result) || !is_array($result)) {
  24. Log::warning('超节损队列无可写入数据: ' . $data['gdbh'] . '-' . $data['yjno']);
  25. $job->delete();
  26. return;
  27. }
  28. Db::name('工单_质量考核汇总')
  29. ->where('Gy0_gdbh', $data['gdbh'])
  30. ->where('印件及工序', $data['yjno'])
  31. ->delete();
  32. $insertSql = Db::name('工单_质量考核汇总')
  33. ->fetchSql(true)
  34. ->insertAll($result);
  35. $insertRes = Db::query($insertSql);
  36. if ($insertRes === false) {
  37. throw new \RuntimeException('超节损队列写入失败');
  38. }
  39. $job->delete();
  40. } catch (\Throwable $e) {
  41. Log::error('超节损队列执行失败: ' . $e->getMessage() . '; data=' . json_encode($data, JSON_UNESCAPED_UNICODE));
  42. if ($job->attempts() >= 3) {
  43. $job->delete();
  44. } else {
  45. $job->release(10);
  46. }
  47. }
  48. }
  49. }