海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Jobs;
  3. use App\Common\OrderStatus;
  4. use App\Models\AgentProduct;
  5. use App\Models\AgentProductSpec;
  6. use App\Models\Order;
  7. use App\Models\Product;
  8. use App\Models\ProductSpec;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class OrderTimeout implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $orderNumber = '';
  20. protected $redis = '';
  21. /**
  22. * Create a new job instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct($orderNumber, $delay)
  27. {
  28. $this->orderNumber = $orderNumber;
  29. $this->delay = $delay;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. //需要处理的订单状态
  39. $status_arr = [OrderStatus::UNPAID, OrderStatus::PAY_EARNEST, OrderStatus::OFFLINE_UNPAID];
  40. //记录最小ID,下次查询时按ID正序查询,只处理大于该ID的订单,避免重复扫描数据库
  41. /*$min_id = Order::query()
  42. ->whereIn('status', $status_arr)
  43. ->whereNotNull('timeout')
  44. ->min('id');*/
  45. $v = Order::with('orderProductItem')
  46. // ->where('id', '>=', $min_id)
  47. ->whereIn('status', $status_arr)
  48. ->whereNotNull('timeout')
  49. ->where('order_no', $this->orderNumber)
  50. ->first();
  51. if (!empty($v) && !is_null($v->timeout) && strtotime($v->timeout) <= time()) {
  52. DB::beginTransaction();
  53. try {
  54. //取消订单
  55. $v->status = OrderStatus::CANCEL;
  56. $v->save();
  57. //供应商产品加库存
  58. Product::whereIn('id', explode(',', $v->product_ids))->increment('stock', $v->num);
  59. //代理商产品加库存
  60. AgentProduct::where('id', $v->agent_product_id)->increment('stock', $v->num);
  61. if (!$v->orderProductItem->isEmpty()) {
  62. $item = $v->orderProductItem->toArray();
  63. //供应商产品规格加库存
  64. foreach ($item as $v2) {
  65. if ($v2['product_spec_id']) {
  66. if (is_array($v2['product_spec_id'])) {
  67. ProductSpec::whereIn('id', $v2['product_spec_id'])->increment('stock', $v->num);
  68. } else {
  69. ProductSpec::where('id', $v2['product_spec_id'])->increment('stock', $v->num);
  70. }
  71. }
  72. }
  73. //代理商产品规格加库存
  74. foreach ($item as $v2) {
  75. if ($v2['agent_product_spec_id']) {
  76. if (is_array($v2['agent_product_spec_id'])) {
  77. AgentProductSpec::whereIn('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
  78. } else {
  79. AgentProductSpec::where('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
  80. }
  81. }
  82. }
  83. }
  84. DB::commit();
  85. } catch (\Exception $exception) {
  86. DB::rollBack();
  87. }
  88. }
  89. }
  90. }