2
0

TestProjectCopyTask.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Str;
  7. class TestProjectCopyTask extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. * php artisan test:project.copy.task project-50 dd9bcba8-ad3f-4082-9b52-4f5f8acdbd5f visuddhinanda
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'test:project.copy.task {project} {task} {studio} {--token=}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '建立project 并复制task';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. $appUrl = config('app.url');
  39. $projectTitle = $this->argument('project');
  40. $taskId = $this->argument('task');
  41. $studioName = $this->argument('studio');
  42. $token = $this->option('token');
  43. // 如果 role 选项未提供(为空),提示用户输入
  44. if (empty($token)) {
  45. $token = $this->ask('Please enter the user token:');
  46. }
  47. $taskCount = $this->ask('Please enter the task count:');
  48. $url = $appUrl.'/api/v2/project-tree';
  49. $this->info('create project '.$url);
  50. $projects = [];
  51. $rootId = Str::uuid();
  52. $projects[] = [
  53. 'id' => $rootId,
  54. 'title' => $projectTitle,
  55. 'type' => 'instance',
  56. 'parent_id' => '',
  57. 'weight' => 0,
  58. 'res_id' => $rootId,
  59. ];
  60. for ($i = 0; $i < $taskCount; $i++) {
  61. $uid = Str::uuid();
  62. $projects[] = [
  63. 'id' => $uid,
  64. 'title' => "{$projectTitle}_{$i}",
  65. 'type' => 'instance',
  66. 'parent_id' => $rootId,
  67. 'weight' => 0,
  68. 'res_id' => $uid,
  69. ];
  70. }
  71. $response = Http::withToken($token)
  72. ->post($url, [
  73. 'studio_name' => $studioName,
  74. 'data' => $projects,
  75. ]);
  76. if ($response->failed()) {
  77. $this->error('project create fail'.$response->json('message'));
  78. Log::error('project create fail', ['data' => $response->body()]);
  79. return 1;
  80. }
  81. $projectsData = $response->json()['data']['rows'];
  82. $this->info('project :'.count($projectsData));
  83. // 获取task
  84. $response = Http::withToken($token)
  85. ->get($appUrl.'/api/v2/task/'.$taskId);
  86. if ($response->failed()) {
  87. $this->error('task read fail'.$response->json('message'));
  88. Log::error('task read fail', ['data' => $response->body()]);
  89. return 1;
  90. }
  91. // 建立task
  92. $task = $response->json()['data'];
  93. $taskTitle = $task['title'];
  94. $this->info('task title:'.$task['title']);
  95. $tasks = [];
  96. foreach ($projectsData as $key => $project) {
  97. if ($project['isLeaf']) {
  98. $task['title'] = "{$taskTitle}_{$key}";
  99. $tasks[] = [
  100. 'project_id' => $project['id'],
  101. 'tasks' => [$task],
  102. ];
  103. }
  104. }
  105. $response = Http::withToken($token)
  106. ->post($appUrl.'/api/v2/task-group', [
  107. 'data' => $tasks,
  108. ]);
  109. if ($response->failed()) {
  110. $this->error('task create fail'.$response->json('message'));
  111. Log::error('task create fail', ['data' => $response->body()]);
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. }