TaskStatusController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $restartTask = [];
  67. $runningTask = [];
  68. switch ($request->get('status')) {
  69. case 'publish':
  70. $publishTask[] = $id;
  71. # 开启子任务
  72. $children = Task::whereIn('parent_id', $id)
  73. ->where('status', 'pending')
  74. ->select('id')->get();
  75. foreach ($children as $key => $child) {
  76. $publishTask[] = $child->id;
  77. }
  78. Task::whereIn('id', $publishTask)->update([
  79. 'status' => 'published',
  80. 'editor_id' => $user['user_uid'],
  81. 'updated_at' => now()
  82. ]);
  83. break;
  84. case 'running':
  85. $task->started_at = now();
  86. $task->executor_id = $user['user_uid'];
  87. $runningTask[] = $task->id;
  88. break;
  89. case 'done':
  90. $doneTask[] = $task->id;
  91. $task->finished_at = now();
  92. $preTask = [$task->id];
  93. //开启父任务
  94. if ($task->parent_id) {
  95. $notCompleted = Task::where('parent_id', $task->parent_id)
  96. ->where('id', '!=', $task->id)
  97. ->where('status', '!=', 'done')
  98. ->count();
  99. if ($notCompleted === 0) {
  100. //父任务已经完成
  101. Task::where('id', $task->parent_id)
  102. ->update([
  103. 'status' => 'done',
  104. 'editor_id' => $user['user_uid'],
  105. 'updated_at' => now(),
  106. 'finished_at' => now()
  107. ]);
  108. $doneTask[] = $task->parent_id;
  109. $preTask[] = $task->parent_id;
  110. }
  111. }
  112. //开启后置任务
  113. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  114. ->where('status', 'pending')
  115. ->select('next_task_id')->get();
  116. foreach ($nextTasks as $key => $value) {
  117. $publishTask[] = $value->next_task_id;
  118. }
  119. //开启后置任务的子任务
  120. $nextTasksChildren = Task::whereIn('parent_id', $publishTask)
  121. ->where('status', 'pending')
  122. ->select('id')->get();
  123. foreach ($nextTasksChildren as $child) {
  124. $publishTask[] = $child->id;
  125. }
  126. Task::whereIn('id', $publishTask)
  127. ->update([
  128. 'status' => 'published',
  129. 'editor_id' => $user['user_uid'],
  130. 'updated_at' => now()
  131. ]);
  132. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  133. ->where('status', 'requested_restart')
  134. ->select('next_task_id')->get();
  135. foreach ($nextTasks as $key => $value) {
  136. $runningTask[] = $value->next_task_id;
  137. }
  138. Task::whereIn('id', $runningTask)
  139. ->update([
  140. 'status' => 'running',
  141. 'editor_id' => $user['user_uid'],
  142. 'updated_at' => now()
  143. ]);
  144. break;
  145. case 'requested_restart':
  146. //从新开启前置任务
  147. $preTasks = TaskRelation::where('next_task_id', $task->id)
  148. ->select('task_id')->get();
  149. foreach ($preTasks as $key => $value) {
  150. $restartTask[] = $value->task_id;
  151. }
  152. Task::whereIn('id', $restartTask)
  153. ->update([
  154. 'status' => 'restarted',
  155. 'editor_id' => $user['user_uid'],
  156. 'updated_at' => now()
  157. ]);
  158. break;
  159. }
  160. $task->status = $request->get('status');
  161. //发送站内信
  162. $doneTask[] = $task->id;
  163. $doneSend = WatchApi::change(
  164. resId: $doneTask,
  165. from: $user['user_uid'],
  166. message: "任务状态变为 已经完成",
  167. );
  168. $pubSend = WatchApi::change(
  169. resId: $publishTask,
  170. from: $user['user_uid'],
  171. message: "任务状态变为 已经发布",
  172. );
  173. $restartSend = WatchApi::change(
  174. resId: $restartTask,
  175. from: $user['user_uid'],
  176. message: "任务状态变为 已经重启",
  177. );
  178. $runningSend = WatchApi::change(
  179. resId: $runningTask,
  180. from: $user['user_uid'],
  181. message: "任务状态变为 运行中",
  182. );
  183. Log::debug('watch message', [
  184. 'done' => $doneSend,
  185. 'published' => $pubSend,
  186. 'restarted' => $restartSend,
  187. 'running' => $runningSend,
  188. ]);
  189. $task->editor_id = $user['user_uid'];
  190. $task->save();
  191. $result = Task::whereIn('id', array_merge($doneTask, $publishTask, $restartTask, $runningTask))
  192. ->get();
  193. return $this->ok(
  194. [
  195. "rows" => TaskResource::collection(resource: $result),
  196. "count" => count($result),
  197. ]
  198. );
  199. }
  200. /**
  201. * Remove the specified resource from storage.
  202. *
  203. * @param \App\Models\Task $task
  204. * @return \Illuminate\Http\Response
  205. */
  206. public function destroy(Task $task)
  207. {
  208. //
  209. }
  210. public function canEdit(string $user_uid, Task $task)
  211. {
  212. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  213. return true;
  214. }
  215. $has = TaskAssignee::where('task_id', $task->id)
  216. ->where('assignee_id', $user_uid)->exists();
  217. if ($has) {
  218. return true;
  219. }
  220. return false;
  221. }
  222. }