2
0

TaskStatusController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Controllers;
  4. use App\Models\Task;
  5. use App\Models\TaskRelation;
  6. use App\Models\TaskAssignee;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Http\Resources\TaskResource;
  10. use App\Http\Api\AuthApi;
  11. use App\Http\Api\WatchApi;
  12. class TaskStatusController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index()
  20. {
  21. //
  22. }
  23. /**
  24. * Store a newly created resource in storage.
  25. *
  26. * @param \Illuminate\Http\Request $request
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function store(Request $request)
  30. {
  31. //
  32. }
  33. /**
  34. * Display the specified resource.
  35. *
  36. * @param \App\Models\Task $task
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function show(Task $task)
  40. {
  41. //
  42. }
  43. /**
  44. * Update the specified resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @param \App\Models\Task $task
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function update(Request $request, string $id)
  51. {
  52. //
  53. $task = Task::findOrFail($id);
  54. $user = AuthApi::current($request);
  55. if (!$user) {
  56. return $this->error(__('auth.failed'), 401, 401);
  57. }
  58. if (!$this->canEdit($user['user_uid'], $task)) {
  59. return $this->error(__('auth.failed'), 403, 403);
  60. }
  61. if (!$request->has('status')) {
  62. return $this->error('no status', 400, 400);
  63. }
  64. $doneTask = [];
  65. $publishTask = [];
  66. switch ($request->get('status')) {
  67. case 'publish':
  68. # code...
  69. break;
  70. case 'running':
  71. $task->started_at = now();
  72. $task->executor_id = $user['user_uid'];
  73. break;
  74. case 'done':
  75. $task->finished_at = now();
  76. $preTask = [$task->id];
  77. //开启父任务
  78. if ($task->parent_id) {
  79. $notCompleted = Task::where('parent_id', $task->parent_id)
  80. ->where('id', '!=', $task->id)
  81. ->where('status', '!=', 'done')
  82. ->count();
  83. if ($notCompleted === 0) {
  84. //父任务已经完成
  85. Task::where('id', $task->parent_id)
  86. ->update([
  87. 'status' => 'done',
  88. 'editor_id' => $user['user_uid'],
  89. 'updated_at' => now(),
  90. 'finished_at' => now()
  91. ]);
  92. $doneTask[] = $task->parent_id;
  93. $preTask[] = $task->parent_id;
  94. }
  95. }
  96. //开启后置任务
  97. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  98. ->select('next_task_id')->get();
  99. foreach ($nextTasks as $key => $value) {
  100. $publishTask[] = $value->next_task_id;
  101. }
  102. //开启后置任务的子任务
  103. $nextTasksChildren = Task::whereIn('parent_id', $publishTask)
  104. ->select('id')->get();
  105. foreach ($nextTasksChildren as $child) {
  106. $publishTask[] = $child->id;
  107. }
  108. Task::whereIn('id', $publishTask)
  109. ->where('status', 'pending')
  110. ->update([
  111. 'status' => 'published',
  112. 'editor_id' => $user['user_uid'],
  113. 'updated_at' => now()
  114. ]);
  115. break;
  116. }
  117. $task->status = $request->get('status');
  118. //发送站内信
  119. $doneTask[] = $task->id;
  120. $doneSend = WatchApi::change(
  121. resId: $doneTask,
  122. from: $user['user_uid'],
  123. message: "任务状态变为 已经完成",
  124. );
  125. $pubSend = WatchApi::change(
  126. resId: $publishTask,
  127. from: $user['user_uid'],
  128. message: "任务状态变为 已经发布",
  129. );
  130. Log::debug('watch message', ['done' => $doneSend, 'published' => $pubSend]);
  131. $task->editor_id = $user['user_uid'];
  132. $task->save();
  133. $result = Task::whereIn('id', array_merge($doneTask, $publishTask))
  134. ->get();
  135. return $this->ok(
  136. [
  137. "rows" => TaskResource::collection(resource: $result),
  138. "count" => count($result),
  139. ]
  140. );
  141. }
  142. /**
  143. * Remove the specified resource from storage.
  144. *
  145. * @param \App\Models\Task $task
  146. * @return \Illuminate\Http\Response
  147. */
  148. public function destroy(Task $task)
  149. {
  150. //
  151. }
  152. public function canEdit(string $user_uid, Task $task)
  153. {
  154. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  155. return true;
  156. }
  157. $has = TaskAssignee::where('task_id', $task->id)
  158. ->where('assignee_id', $user_uid)->exists();
  159. if ($has) {
  160. return true;
  161. }
  162. return false;
  163. }
  164. }