OrderNoPayNotificationCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Order;
  4. use App\Models\OrderRent;
  5. use App\Models\User;
  6. use App\Notifications\OrderLongTimeNoPayNotification;
  7. use App\Utils\RedisKeys;
  8. use Carbon\Carbon;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Notification;
  12. class OrderNoPayNotificationCommand extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'notification:order_no_pay';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '待支付订单提醒';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. //
  43. $orders = self::order();
  44. self::serval($orders);
  45. $orderRents = self::orderRent();
  46. self::serval($orderRents);
  47. }
  48. protected static function serval($orders)
  49. {
  50. if (count($orders) !== 0) {
  51. foreach ($orders as $order) {
  52. $end_use_bike_time = Carbon::now()->diffInDays(Carbon::make($order->end_use_bike_time));
  53. $key = sprintf(RedisKeys::ORDER_NO_PAY_NO, $order->no);
  54. $res = app()->redis->get($key);
  55. if (!$res) {
  56. $user = User::find($order->user_id);
  57. if ($end_use_bike_time >= 1 && $end_use_bike_time < 3) {
  58. // 存两天
  59. app()->redis->incr($key);
  60. app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_THREE);
  61. Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
  62. } elseif ($end_use_bike_time >= 3 && $end_use_bike_time < 7) {
  63. // 存四天
  64. app()->redis->incr($key);
  65. app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_SEVEN);
  66. Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
  67. } elseif ($end_use_bike_time >= 7 && $end_use_bike_time < 30) {
  68. // 存23天
  69. app()->redis->incr($key);
  70. app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_THIRTY);
  71. Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
  72. } elseif ($end_use_bike_time >= 30 && $end_use_bike_time < 32) {
  73. // 不在设置过期时间
  74. app()->redis->incr($key);
  75. Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
  76. } else {
  77. Log::info('订单id' . $order->id . '已经' . $end_use_bike_time . '天未支付');
  78. }
  79. }
  80. }
  81. }
  82. }
  83. protected static function order()
  84. {
  85. $yseterday = Carbon::now()->subDay();
  86. $thirtyOneAgo = Carbon::now()->subDays(31);
  87. $orders = Order::query()
  88. ->where('status', Order::STATUS_CLOSE_BIKE)
  89. ->where('pay_status', Order::PAY_STATUS_NO)
  90. ->where('end_use_bike_time', '<', $yseterday)
  91. ->where('end_use_bike_time', '>', $thirtyOneAgo)
  92. ->get();
  93. return $orders;
  94. }
  95. protected static function orderRent()
  96. {
  97. $yseterday = Carbon::now()->subDay();
  98. $thirtyOneAgo = Carbon::now()->subDays(31);
  99. $orders = OrderRent::query()
  100. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  101. ->where('pay_status', Order::PAY_STATUS_NO)
  102. ->where('end_use_bike_time', '<', $yseterday)
  103. ->where('end_use_bike_time', '>', $thirtyOneAgo)
  104. ->get();
  105. return $orders;
  106. }
  107. }