AutoResetUserTraffic.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Models\Config;
  5. use App\Http\Models\Order;
  6. use App\Http\Models\User;
  7. use Log;
  8. class AutoResetUserTraffic extends Command
  9. {
  10. protected $signature = 'autoResetUserTraffic';
  11. protected $description = '自动重置用户可用流量';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. $jobStartTime = microtime(true);
  19. $config = $this->systemConfig();
  20. if ($config['reset_traffic']) {
  21. $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->get();
  22. if (!$userList->isEmpty()) {
  23. foreach ($userList as $user) {
  24. if (!$user->traffic_reset_day) {
  25. continue;
  26. }
  27. // 取出用户最后购买的有效套餐 TODO:需要改掉,不应该是最后购买的套餐
  28. $order = Order::query()->with(['user', 'goods'])->whereHas('goods', function ($q) {
  29. $q->where('type', 2);
  30. })->where('user_id', $user->id)->where('is_expire', 0)->orderBy('oid', 'desc')->first();
  31. if (!$order) {
  32. continue;
  33. }
  34. // TODO:需要改掉,改成下一次重置日,每月不定时,类似搬瓦工
  35. $today = abs(date('d'));
  36. $reset_days = [$today, 29, 30, 31];
  37. if (in_array($order->user->traffic_reset_day, $reset_days)) {
  38. // 跳过本月,防止异常重置
  39. if (date('m') == date('m', strtotime($order->expire_at))) {
  40. continue;
  41. }
  42. if (date('m') == date('m', strtotime($order->created_at))) {
  43. continue;
  44. }
  45. User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
  46. }
  47. }
  48. }
  49. }
  50. $jobEndTime = microtime(true);
  51. $jobUsedTime = round(($jobEndTime - $jobStartTime) , 4);
  52. Log::info('定时任务【' . $this->description . '】耗时' . $jobUsedTime . '秒');
  53. }
  54. // 系统配置
  55. private function systemConfig()
  56. {
  57. $config = Config::query()->get();
  58. $data = [];
  59. foreach ($config as $vo) {
  60. $data[$vo->name] = $vo->value;
  61. }
  62. return $data;
  63. }
  64. }