2
0

TestProjectCopyTask.php 3.8 KB

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