TaskStatusController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $publishTask[] = $id;
  69. # 开启子任务
  70. $children = Task::whereIn('parent_id', $id)
  71. ->where('status', 'pending')
  72. ->select('id')->get();
  73. foreach ($children as $key => $child) {
  74. $publishTask[] = $child->id;
  75. }
  76. Task::whereIn('id', $publishTask)->update([
  77. 'status' => 'published',
  78. 'editor_id' => $user['user_uid'],
  79. 'updated_at' => now()
  80. ]);
  81. break;
  82. case 'running':
  83. $task->started_at = now();
  84. $task->executor_id = $user['user_uid'];
  85. break;
  86. case 'done':
  87. $task->finished_at = now();
  88. $preTask = [$task->id];
  89. //开启父任务
  90. if ($task->parent_id) {
  91. $notCompleted = Task::where('parent_id', $task->parent_id)
  92. ->where('id', '!=', $task->id)
  93. ->where('status', '!=', 'done')
  94. ->count();
  95. if ($notCompleted === 0) {
  96. //父任务已经完成
  97. Task::where('id', $task->parent_id)
  98. ->update([
  99. 'status' => 'done',
  100. 'editor_id' => $user['user_uid'],
  101. 'updated_at' => now(),
  102. 'finished_at' => now()
  103. ]);
  104. $doneTask[] = $task->parent_id;
  105. $preTask[] = $task->parent_id;
  106. }
  107. }
  108. //开启后置任务
  109. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  110. ->select('next_task_id')->get();
  111. foreach ($nextTasks as $key => $value) {
  112. $publishTask[] = $value->next_task_id;
  113. }
  114. //开启后置任务的子任务
  115. $nextTasksChildren = Task::whereIn('parent_id', $publishTask)
  116. ->select('id')->get();
  117. foreach ($nextTasksChildren as $child) {
  118. $publishTask[] = $child->id;
  119. }
  120. Task::whereIn('id', $publishTask)
  121. ->where('status', 'pending')
  122. ->update([
  123. 'status' => 'published',
  124. 'editor_id' => $user['user_uid'],
  125. 'updated_at' => now()
  126. ]);
  127. break;
  128. }
  129. $task->status = $request->get('status');
  130. //发送站内信
  131. $doneTask[] = $task->id;
  132. $doneSend = WatchApi::change(
  133. resId: $doneTask,
  134. from: $user['user_uid'],
  135. message: "任务状态变为 已经完成",
  136. );
  137. $pubSend = WatchApi::change(
  138. resId: $publishTask,
  139. from: $user['user_uid'],
  140. message: "任务状态变为 已经发布",
  141. );
  142. Log::debug('watch message', ['done' => $doneSend, 'published' => $pubSend]);
  143. $task->editor_id = $user['user_uid'];
  144. $task->save();
  145. $result = Task::whereIn('id', array_merge($doneTask, $publishTask))
  146. ->get();
  147. return $this->ok(
  148. [
  149. "rows" => TaskResource::collection(resource: $result),
  150. "count" => count($result),
  151. ]
  152. );
  153. }
  154. /**
  155. * Remove the specified resource from storage.
  156. *
  157. * @param \App\Models\Task $task
  158. * @return \Illuminate\Http\Response
  159. */
  160. public function destroy(Task $task)
  161. {
  162. //
  163. }
  164. public function canEdit(string $user_uid, Task $task)
  165. {
  166. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  167. return true;
  168. }
  169. $has = TaskAssignee::where('task_id', $task->id)
  170. ->where('assignee_id', $user_uid)->exists();
  171. if ($has) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. }