123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Models\User;
- use App\Notifications\OrderLongTimeNoPayNotification;
- use App\Utils\RedisKeys;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Notification;
- class OrderNoPayNotificationCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'notification:order_no_pay';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '待支付订单提醒';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //
- $orders = self::order();
- self::serval($orders);
- $orderRents = self::orderRent();
- self::serval($orderRents);
- }
- protected static function serval($orders)
- {
- if (count($orders) !== 0) {
- foreach ($orders as $order) {
- $end_use_bike_time = Carbon::now()->diffInDays(Carbon::make($order->end_use_bike_time));
- $key = sprintf(RedisKeys::ORDER_NO_PAY_NO, $order->no);
- $res = app()->redis->get($key);
- if (!$res) {
- $user = User::find($order->user_id);
- if ($end_use_bike_time >= 1 && $end_use_bike_time < 3) {
- // 存两天
- app()->redis->incr($key);
- app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_THREE);
- Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
- } elseif ($end_use_bike_time >= 3 && $end_use_bike_time < 7) {
- // 存四天
- app()->redis->incr($key);
- app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_SEVEN);
- Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
- } elseif ($end_use_bike_time >= 7 && $end_use_bike_time < 30) {
- // 存23天
- app()->redis->incr($key);
- app()->redis->Expire($key, RedisKeys::ORDER_NO_PAY_NO_THIRTY);
- Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
- } elseif ($end_use_bike_time >= 30 && $end_use_bike_time < 32) {
- // 不在设置过期时间
- app()->redis->incr($key);
- Notification::send($user, new OrderLongTimeNoPayNotification($user, $order));
- } else {
- Log::info('订单id' . $order->id . '已经' . $end_use_bike_time . '天未支付');
- }
- }
- }
- }
- }
- protected static function order()
- {
- $yseterday = Carbon::now()->subDay();
- $thirtyOneAgo = Carbon::now()->subDays(31);
- $orders = Order::query()
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->where('pay_status', Order::PAY_STATUS_NO)
- ->where('end_use_bike_time', '<', $yseterday)
- ->where('end_use_bike_time', '>', $thirtyOneAgo)
- ->get();
- return $orders;
- }
- protected static function orderRent()
- {
- $yseterday = Carbon::now()->subDay();
- $thirtyOneAgo = Carbon::now()->subDays(31);
- $orders = OrderRent::query()
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->where('pay_status', Order::PAY_STATUS_NO)
- ->where('end_use_bike_time', '<', $yseterday)
- ->where('end_use_bike_time', '>', $thirtyOneAgo)
- ->get();
- return $orders;
- }
- }
|