TaskGroupController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Task;
  4. use App\Models\Project;
  5. use App\Models\TaskRelation;
  6. use App\Models\TaskAssignee;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Http\Request;
  9. use App\Http\Api\AuthApi;
  10. use Illuminate\Support\Str;
  11. class TaskGroupController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index()
  19. {
  20. //
  21. }
  22. /**
  23. * Store a newly created resource in storage.
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function store(Request $request)
  29. {
  30. //
  31. $user = AuthApi::current($request);
  32. if (!$user) {
  33. return $this->error(__('auth.failed'), 401, 401);
  34. }
  35. //获取全部的project_id
  36. $input = $request->get(key: 'data');
  37. $id = [];
  38. foreach ($input as $key => $value) {
  39. $id[$value['project_id']] = 1;
  40. }
  41. $projectsId = array_keys($id);
  42. //鉴权
  43. $projects = Project::whereIn('id', $projectsId)
  44. ->select(['id', 'owner_id'])->get();
  45. foreach ($projects as $key => $project) {
  46. $id[$project->id] = $project->owner_id;
  47. if (!TaskController::canEdit($user['user_uid'], $project->owner_id)) {
  48. Log::error(__('auth.failed'), ['user' => $user['user_uid'], 'owner' => $project->owner_id]);
  49. return $this->error(__('auth.failed'), 403, 403);
  50. }
  51. }
  52. $data = [];
  53. $trData = [];
  54. $taData = [];
  55. foreach ($input as $key => $project) {
  56. # code...
  57. $projectData = [];
  58. $tasks = [];
  59. $taskRelationData = [];
  60. $taskAssigneesData = [];
  61. foreach ($project['tasks'] as $key => $task) {
  62. $uuid = Str::uuid();
  63. $tasks[$task['id']] = $uuid;
  64. $projectData[] = [
  65. 'id' => $uuid,
  66. 'old_id' => $task['id'],
  67. 'title' => $task['title'],
  68. 'type' => 'instance',
  69. 'status' => $task['status'],
  70. 'description' => $task['description'],
  71. 'order' => $task['order'],
  72. 'parent_id' => $task['parent_id'],
  73. 'project_id' => $project['project_id'],
  74. 'owner_id' => $id[$project['project_id']],
  75. 'editor_id' => $user['user_uid'],
  76. 'created_at' => now(),
  77. 'updated_at' => now(),
  78. ];
  79. }
  80. foreach ($projectData as $key => $value) {
  81. if ($value['parent_id']) {
  82. $found = array_filter($projectData, function ($element) use ($value) {
  83. return $element['old_id'] === $value['parent_id'];
  84. });
  85. if (count($found) > 0) {
  86. $projectData[$key]['parent_id'] = $found[0]['id'];
  87. }
  88. }
  89. }
  90. foreach ($projectData as $key => $value) {
  91. unset($projectData[$key]['old_id']);
  92. }
  93. $data = [...$data, ...$projectData];
  94. //处理 task relation
  95. $tasksId = array_keys($tasks);
  96. $taskRelations = TaskRelation::whereIn('task_id', $tasksId)
  97. ->get();
  98. foreach ($taskRelations as $key => $value) {
  99. $taskRelationData[] = [
  100. 'task_id' => $tasks[$value->task_id],
  101. 'next_task_id' => $tasks[$value->next_task_id],
  102. 'editor_id' => $user['user_uid'],
  103. 'created_at' => now(),
  104. 'updated_at' => now(),
  105. ];
  106. }
  107. $trData = [...$trData, ...$taskRelationData];
  108. //处理 task assignee
  109. $ta = TaskAssignee::whereIn('task_id', $tasksId)->get();
  110. foreach ($ta as $key => $value) {
  111. $taskAssigneesData[] = [
  112. 'id' => Str::uuid(),
  113. 'task_id' => $tasks[$value->task_id],
  114. 'assignee_id' => $value->assignee_id,
  115. 'editor_id' => $user['user_uid'],
  116. 'created_at' => now(),
  117. 'updated_at' => now(),
  118. ];
  119. }
  120. $taData = [...$taData, ...$taskAssigneesData];
  121. }
  122. $taskOk = Task::insert($data);
  123. $taskRelationOk = TaskRelation::insert($trData);
  124. $taskAssigneeOk = TaskAssignee::insert($taData);
  125. if ($taskOk && $taskRelationOk && $taskAssigneeOk) {
  126. return $this->ok([
  127. 'taskCount' => count($data),
  128. 'taskRelationCount' => count($trData),
  129. 'taskAssigneeCount' => count($taData),
  130. ]);
  131. } else {
  132. return $this->error('error', 200, 200);
  133. }
  134. }
  135. /**
  136. * Display the specified resource.
  137. *
  138. * @param \App\Models\Task $task
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function show(Task $task)
  142. {
  143. //
  144. }
  145. /**
  146. * Update the specified resource in storage.
  147. *
  148. * @param \Illuminate\Http\Request $request
  149. * @param \App\Models\Task $task
  150. * @return \Illuminate\Http\Response
  151. */
  152. public function update(Request $request, Task $task)
  153. {
  154. //
  155. }
  156. /**
  157. * Remove the specified resource from storage.
  158. *
  159. * @param \App\Models\Task $task
  160. * @return \Illuminate\Http\Response
  161. */
  162. public function destroy(Task $task)
  163. {
  164. //
  165. }
  166. }