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\Http\Request;
  8. use App\Services\AuthService;
  9. use Illuminate\Support\Str;
  10. class TaskGroupController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index()
  18. {
  19. //
  20. }
  21. /**
  22. * Store a newly created resource in storage.
  23. *
  24. * @param \Illuminate\Http\Request $request
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function store(Request $request)
  28. {
  29. //
  30. $user = AuthService::current($request);
  31. if (!$user) {
  32. return $this->error(__('auth.failed'), 401, 401);
  33. }
  34. //获取全部的project_id
  35. $input = $request->input(key: 'data');
  36. $id = [];
  37. foreach ($input as $key => $value) {
  38. $id[$value['project_id']] = 1;
  39. }
  40. $projectsId = array_keys($id);
  41. //鉴权
  42. $projects = Project::whereIn('uid', $projectsId)
  43. ->select(['uid', 'owner_id'])->get();
  44. foreach ($projects as $key => $project) {
  45. $id[$project->uid] = $project->owner_id;
  46. if (!TaskController::canEdit($user['user_uid'], $project->owner_id)) {
  47. return $this->error(__('auth.failed'), 403, 403);
  48. }
  49. }
  50. $data = [];
  51. $trData = [];
  52. $taData = [];
  53. foreach ($input as $key => $project) {
  54. # code...
  55. $projectData = [];
  56. $tasks = [];
  57. $taskRelationData = [];
  58. $taskAssigneesData = [];
  59. foreach ($project['tasks'] as $key => $task) {
  60. $uuid = Str::uuid();
  61. $tasks[$task['id']] = $uuid;
  62. $projectData[] = [
  63. 'id' => $uuid,
  64. 'old_id' => $task['id'],
  65. 'title' => $task['title'],
  66. 'type' => 'instance',
  67. 'category' => $task['category'],
  68. 'status' => $task['status'],
  69. 'description' => $task['description'],
  70. 'order' => $task['order'],
  71. 'parent_id' => $task['parent_id'],
  72. 'project_id' => $project['project_id'],
  73. 'owner_id' => $id[$project['project_id']],
  74. 'editor_id' => $user['user_uid'],
  75. 'creator_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. }