海南旅游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.

104 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. Log::info($v->timeout);
  52. if (!empty($v) && !is_null($v->timeout) && strtotime($v->timeout) < time()) {
  53. DB::beginTransaction();
  54. try {
  55. //取消订单
  56. $v->status = OrderStatus::CANCEL;
  57. $v->save();
  58. //供应商产品加库存
  59. Product::whereIn('id', explode(',', $v->product_ids))->increment('stock', $v->num);
  60. //代理商产品加库存
  61. AgentProduct::where('id', $v->agent_product_id)->increment('stock', $v->num);
  62. if (!$v->orderProductItem->isEmpty()) {
  63. $item = $v->orderProductItem->toArray();
  64. //供应商产品规格加库存
  65. foreach ($item as $v2) {
  66. if ($v2['product_spec_id']) {
  67. if (is_array($v2['product_spec_id'])) {
  68. ProductSpec::whereIn('id', $v2['product_spec_id'])->increment('stock', $v->num);
  69. } else {
  70. ProductSpec::where('id', $v2['product_spec_id'])->increment('stock', $v->num);
  71. }
  72. }
  73. }
  74. //代理商产品规格加库存
  75. foreach ($item as $v2) {
  76. if ($v2['agent_product_spec_id']) {
  77. if (is_array($v2['agent_product_spec_id'])) {
  78. AgentProductSpec::whereIn('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
  79. } else {
  80. AgentProductSpec::where('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
  81. }
  82. }
  83. }
  84. }
  85. DB::commit();
  86. } catch (\Exception $exception) {
  87. DB::rollBack();
  88. }
  89. }
  90. }
  91. }