TaskStatusController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 (!TaskController::canUpdate($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. ->select('next_task_id')->get();
  115. foreach ($nextTasks as $key => $value) {
  116. $nextTask = Task::find($value->next_task_id);
  117. if ($nextTask->status === 'pending') {
  118. $publishTask[] = $value->next_task_id;
  119. }
  120. }
  121. //开启后置任务的子任务
  122. $nextTasksChildren = Task::whereIn('parent_id', $publishTask)
  123. ->where('status', 'pending')
  124. ->select('id')->get();
  125. foreach ($nextTasksChildren as $child) {
  126. $publishTask[] = $child->id;
  127. }
  128. Task::whereIn('id', $publishTask)
  129. ->update([
  130. 'status' => 'published',
  131. 'editor_id' => $user['user_uid'],
  132. 'updated_at' => now()
  133. ]);
  134. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  135. ->select('next_task_id')->get();
  136. foreach ($nextTasks as $key => $value) {
  137. $nextTask = Task::find($value->next_task_id);
  138. if ($nextTask->status === 'requested_restart') {
  139. $runningTask[] = $value->next_task_id;
  140. }
  141. }
  142. Task::whereIn('id', $runningTask)
  143. ->update([
  144. 'status' => 'running',
  145. 'editor_id' => $user['user_uid'],
  146. 'updated_at' => now()
  147. ]);
  148. break;
  149. case 'requested_restart':
  150. //从新开启前置任务
  151. $preTasks = TaskRelation::where('next_task_id', $task->id)
  152. ->select('task_id')->get();
  153. foreach ($preTasks as $key => $value) {
  154. $restartTask[] = $value->task_id;
  155. }
  156. Task::whereIn('id', $restartTask)
  157. ->update([
  158. 'status' => 'restarted',
  159. 'editor_id' => $user['user_uid'],
  160. 'updated_at' => now()
  161. ]);
  162. break;
  163. }
  164. $task->status = $request->get('status');
  165. //发送站内信
  166. $doneTask[] = $task->id;
  167. $doneSend = WatchApi::change(
  168. resId: $doneTask,
  169. from: $user['user_uid'],
  170. message: "任务状态变为 已经完成",
  171. );
  172. $pubSend = WatchApi::change(
  173. resId: $publishTask,
  174. from: $user['user_uid'],
  175. message: "任务状态变为 已经发布",
  176. );
  177. $restartSend = WatchApi::change(
  178. resId: $restartTask,
  179. from: $user['user_uid'],
  180. message: "任务状态变为 已经重启",
  181. );
  182. $runningSend = WatchApi::change(
  183. resId: $runningTask,
  184. from: $user['user_uid'],
  185. message: "任务状态变为 运行中",
  186. );
  187. Log::debug('watch message', [
  188. 'done' => $doneSend,
  189. 'published' => $pubSend,
  190. 'restarted' => $restartSend,
  191. 'running' => $runningSend,
  192. ]);
  193. $task->editor_id = $user['user_uid'];
  194. $task->save();
  195. $result = Task::whereIn('id', array_merge($doneTask, $publishTask, $restartTask, $runningTask))
  196. ->get();
  197. return $this->ok(
  198. [
  199. "rows" => TaskResource::collection(resource: $result),
  200. "count" => count($result),
  201. ]
  202. );
  203. }
  204. /**
  205. * Remove the specified resource from storage.
  206. *
  207. * @param \App\Models\Task $task
  208. * @return \Illuminate\Http\Response
  209. */
  210. public function destroy(Task $task)
  211. {
  212. //
  213. }
  214. public function canEdit(string $user_uid, Task $task)
  215. {
  216. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  217. return true;
  218. }
  219. $has = TaskAssignee::where('task_id', $task->id)
  220. ->where('assignee_id', $user_uid)->exists();
  221. if ($has) {
  222. return true;
  223. }
  224. return false;
  225. }
  226. }