TaskGroupController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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('uid', $projectsId)
  44. ->select(['uid', 'owner_id'])->get();
  45. foreach ($projects as $key => $project) {
  46. $id[$project->uid] = $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. 'category' => $task['category'],
  70. 'status' => $task['status'],
  71. 'description' => $task['description'],
  72. 'order' => $task['order'],
  73. 'parent_id' => $task['parent_id'],
  74. 'project_id' => $project['project_id'],
  75. 'owner_id' => $id[$project['project_id']],
  76. 'editor_id' => $user['user_uid'],
  77. 'creator_id' => $user['user_uid'],
  78. 'created_at' => now(),
  79. 'updated_at' => now(),
  80. ];
  81. }
  82. foreach ($projectData as $key => $value) {
  83. if ($value['parent_id']) {
  84. $found = array_filter($projectData, function ($element) use ($value) {
  85. return $element['old_id'] === $value['parent_id'];
  86. });
  87. if (count($found) > 0) {
  88. $projectData[$key]['parent_id'] = $found[0]['id'];
  89. }
  90. }
  91. }
  92. foreach ($projectData as $key => $value) {
  93. unset($projectData[$key]['old_id']);
  94. }
  95. $data = [...$data, ...$projectData];
  96. //处理 task relation
  97. $tasksId = array_keys($tasks);
  98. $taskRelations = TaskRelation::whereIn('task_id', $tasksId)
  99. ->get();
  100. foreach ($taskRelations as $key => $value) {
  101. $taskRelationData[] = [
  102. 'task_id' => $tasks[$value->task_id],
  103. 'next_task_id' => $tasks[$value->next_task_id],
  104. 'editor_id' => $user['user_uid'],
  105. 'created_at' => now(),
  106. 'updated_at' => now(),
  107. ];
  108. }
  109. $trData = [...$trData, ...$taskRelationData];
  110. //处理 task assignee
  111. $ta = TaskAssignee::whereIn('task_id', $tasksId)->get();
  112. foreach ($ta as $key => $value) {
  113. $taskAssigneesData[] = [
  114. 'id' => Str::uuid(),
  115. 'task_id' => $tasks[$value->task_id],
  116. 'assignee_id' => $value->assignee_id,
  117. 'editor_id' => $user['user_uid'],
  118. 'created_at' => now(),
  119. 'updated_at' => now(),
  120. ];
  121. }
  122. $taData = [...$taData, ...$taskAssigneesData];
  123. }
  124. $taskOk = Task::insert($data);
  125. $taskRelationOk = TaskRelation::insert($trData);
  126. $taskAssigneeOk = TaskAssignee::insert($taData);
  127. if ($taskOk && $taskRelationOk && $taskAssigneeOk) {
  128. return $this->ok([
  129. 'taskCount' => count($data),
  130. 'taskRelationCount' => count($trData),
  131. 'taskAssigneeCount' => count($taData),
  132. ]);
  133. } else {
  134. return $this->error('error', 200, 200);
  135. }
  136. }
  137. /**
  138. * Display the specified resource.
  139. *
  140. * @param \App\Models\Task $task
  141. * @return \Illuminate\Http\Response
  142. */
  143. public function show(Task $task)
  144. {
  145. //
  146. }
  147. /**
  148. * Update the specified resource in storage.
  149. *
  150. * @param \Illuminate\Http\Request $request
  151. * @param \App\Models\Task $task
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function update(Request $request, Task $task)
  155. {
  156. //
  157. }
  158. /**
  159. * Remove the specified resource from storage.
  160. *
  161. * @param \App\Models\Task $task
  162. * @return \Illuminate\Http\Response
  163. */
  164. public function destroy(Task $task)
  165. {
  166. //
  167. }
  168. }