upgradeUserLabels.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Http\Models\User;
  5. use App\Http\Models\UserLabel;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. class upgradeUserLabels extends Command
  9. {
  10. protected $signature = 'upgradeUserLabels';
  11. protected $description = '初始化用户默认标签';
  12. protected static $systemConfig;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. self::$systemConfig = Helpers::systemConfig();
  17. }
  18. public function handle()
  19. {
  20. if(empty(self::$systemConfig['initial_labels_for_user'])){
  21. Log::info('初始化用户默认标签失败:系统未设置默认标签');
  22. exit();
  23. }
  24. $userList = User::query()->where('status', '>=', 0)->get();
  25. foreach($userList as $user){
  26. // 跳过已经有标签的用户
  27. $count = UserLabel::query()->where('user_id', $user->id)->count();
  28. if($count){
  29. continue;
  30. }
  31. // 给用户生成默认标签
  32. $this->makeUserDefaultLabels($user->id);
  33. }
  34. }
  35. // 生成用户默认标签
  36. private function makeUserDefaultLabels($userId)
  37. {
  38. $labels = explode(',', self::$systemConfig['initial_labels_for_user']);
  39. foreach($labels as $vo){
  40. $userLabel = new UserLabel();
  41. $userLabel->user_id = $userId;
  42. $userLabel->label_id = $vo;
  43. $userLabel->save();
  44. }
  45. }
  46. }