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

105 lines
2.8 KiB

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