TaskStatusController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Controllers;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\WatchApi;
  6. use App\Http\Resources\TaskResource;
  7. use App\Jobs\ProcessAITranslateJob;
  8. use App\Models\AiModel;
  9. use App\Models\Task;
  10. use App\Models\TaskAssignee;
  11. use App\Models\TaskRelation;
  12. use App\Services\AuthService;
  13. use App\Services\DiscussionService;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Http\Response;
  16. use Illuminate\Support\Facades\Log;
  17. class TaskStatusController extends Controller
  18. {
  19. protected $changeTasks = [];
  20. /**
  21. * Display a listing of the resource.
  22. *
  23. * @return Response
  24. */
  25. public function index()
  26. {
  27. //
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. *
  32. * @return Response
  33. */
  34. public function store(Request $request)
  35. {
  36. //
  37. }
  38. /**
  39. * Display the specified resource.
  40. *
  41. * @return Response
  42. */
  43. public function show(Task $task)
  44. {
  45. //
  46. }
  47. /**
  48. * @return void
  49. */
  50. private function pushChange(string $status, string $id)
  51. {
  52. if (! isset($this->changeTasks[$status])) {
  53. $this->changeTasks[$status] = [];
  54. }
  55. $this->changeTasks[$status][] = $id;
  56. }
  57. private function getChange(string $status)
  58. {
  59. if (! isset($this->changeTasks[$status])) {
  60. $this->changeTasks[$status] = [];
  61. }
  62. return $this->changeTasks[$status];
  63. }
  64. /**
  65. * Update the specified resource in storage.
  66. *
  67. * @param Task $task
  68. * @return Response
  69. */
  70. public function update(Request $request, string $id)
  71. {
  72. //
  73. $task = Task::findOrFail($id);
  74. $user = AuthService::current($request);
  75. if (! $user) {
  76. return $this->error(__('auth.failed'), 401, 401);
  77. }
  78. if (! TaskController::canUpdate($user['user_uid'], $task)) {
  79. return $this->error(__('auth.failed'), 403, 403);
  80. }
  81. if (! $request->has('status')) {
  82. return $this->error('no status', 400, 400);
  83. }
  84. $task->status = $request->input('status');
  85. $task->editor_id = $user['user_uid'];
  86. $task->save();
  87. if ($task->type === 'workflow') {
  88. return $this->ok(
  89. [
  90. 'rows' => TaskResource::collection(resource: [$task]),
  91. 'count' => 1,
  92. ]
  93. );
  94. }
  95. switch ($request->input('status')) {
  96. case 'published':
  97. $this->pushChange('published', $id);
  98. // 开启子任务
  99. $children = Task::where('parent_id', $id)
  100. ->where('status', 'pending')
  101. ->select('id')->get();
  102. foreach ($children as $key => $child) {
  103. $this->pushChange('published', $child->id);
  104. }
  105. break;
  106. case 'running':
  107. $task->started_at = now();
  108. $task->executor_id = $user['user_uid'];
  109. $task->save();
  110. $this->pushChange('running', $task->id);
  111. break;
  112. case 'restarted':
  113. $this->pushChange('restarted', $task->id);
  114. break;
  115. case 'quit':
  116. $this->pushChange('quit', $task->id);
  117. break;
  118. case 'done':
  119. $this->pushChange('done', $task->id);
  120. $task->finished_at = now();
  121. $preTask = [$task->id];
  122. // 开启父任务
  123. if ($task->parent_id) {
  124. $notCompleted = Task::where('parent_id', $task->parent_id)
  125. ->where('id', '!=', $task->id)
  126. ->where('status', '!=', 'done')
  127. ->count();
  128. if ($notCompleted === 0) {
  129. // 父任务已经完成
  130. $preTask[] = $task->parent_id;
  131. $this->pushChange('done', $task->parent_id);
  132. }
  133. }
  134. // 开启后置任务
  135. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  136. ->select('next_task_id')->get();
  137. foreach ($nextTasks as $key => $value) {
  138. $nextTask = Task::find($value->next_task_id);
  139. if ($nextTask->status === 'pending') {
  140. $this->pushChange('published', $value->next_task_id);
  141. }
  142. }
  143. // 开启后置任务的子任务
  144. $nextTasksChildren = Task::whereIn('parent_id', $this->getChange('published'))
  145. ->where('status', 'pending')
  146. ->select('id')->get();
  147. foreach ($nextTasksChildren as $child) {
  148. $this->pushChange('published', $child->id);
  149. }
  150. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  151. ->select('next_task_id')->get();
  152. foreach ($nextTasks as $key => $value) {
  153. $nextTask = Task::find($value->next_task_id);
  154. if ($nextTask->status === 'requested_restart') {
  155. // $runningTask[] = $value->next_task_id;
  156. $this->pushChange('running', $value->next_task_id);
  157. }
  158. }
  159. break;
  160. case 'requested_restart':
  161. $this->pushChange('requested_restart', $task->id);
  162. // 从新开启前置任务
  163. $preTasks = TaskRelation::where('next_task_id', $task->id)
  164. ->select('task_id')->get();
  165. foreach ($preTasks as $key => $value) {
  166. // $restartTask[] = $value->task_id;
  167. $this->pushChange('restarted', $value->task_id);
  168. }
  169. break;
  170. }
  171. // auto start with ai assistant
  172. $autoStart = array_merge($this->getChange('published'), $this->getChange('restarted'));
  173. foreach ($autoStart as $taskId) {
  174. $taskAssignee = TaskAssignee::where('task_id', $taskId)
  175. ->select('assignee_id')->get();
  176. $aiAssistant = AiModel::whereIn('uid', $taskAssignee)->first();
  177. if ($aiAssistant) {
  178. $aiTask = Task::find($taskId);
  179. try {
  180. $msgId = ProcessAITranslateJob::publish($taskId, $aiAssistant->uid);
  181. $aiTask->executor_id = $aiAssistant->uid;
  182. $aiTask->status = 'queue';
  183. $this->pushChange('queue', $taskId);
  184. } catch (\Exception $e) {
  185. $aiTask->status = 'pending';
  186. Log::error('ai assistant start fail', [
  187. 'task' => $taskId,
  188. 'error' => $e->getMessage(),
  189. ]);
  190. } finally {
  191. $aiTask->save();
  192. }
  193. }
  194. }
  195. $allChanged = [];
  196. $discussion = app(DiscussionService::class);
  197. foreach ($this->changeTasks as $key => $tasksId) {
  198. $allChanged = array_merge($allChanged, $tasksId);
  199. // change status in related
  200. $data = [
  201. 'status' => $key,
  202. 'editor_id' => $user['user_uid'],
  203. 'updated_at' => now(),
  204. ];
  205. if ($key === 'done') {
  206. $data['finished_at'] = now();
  207. }
  208. if ($key === 'running') {
  209. $data['started_at'] = now();
  210. }
  211. if ($key === 'restart') {
  212. $data['finished_at'] = null;
  213. }
  214. if ($key === 'quit') {
  215. $data['executor_id'] = null;
  216. }
  217. Task::whereIn('id', $tasksId)
  218. ->update($data);
  219. try {
  220. // 发送站内信
  221. $send = WatchApi::change(
  222. resId: $tasksId,
  223. from: $user['user_uid'],
  224. message: "任务状态变为 {$key}",
  225. );
  226. $editor = UserApi::getByUuid($user['user_uid']);
  227. foreach ($tasksId as $taskId) {
  228. $discussion->create([
  229. 'res_id' => $taskId,
  230. 'res_type' => 'task',
  231. 'title' => "{$editor['nickName']} 将任务状态变为 {$key}",
  232. 'content' => isset($msgId) ? "message id:{$msgId}" : null,
  233. 'editor_uid' => $user['user_uid'],
  234. ]);
  235. }
  236. } catch (\Throwable $th) {
  237. Log::error($th->getMessage());
  238. }
  239. }
  240. // changed tasks
  241. $result = Task::whereIn('id', $allChanged)
  242. ->get();
  243. return $this->ok(
  244. [
  245. 'rows' => TaskResource::collection(resource: $result),
  246. 'count' => count($result),
  247. ]
  248. );
  249. }
  250. /**
  251. * Remove the specified resource from storage.
  252. *
  253. * @return Response
  254. */
  255. public function destroy(Task $task)
  256. {
  257. //
  258. }
  259. public function canEdit(string $user_uid, Task $task)
  260. {
  261. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  262. return true;
  263. }
  264. $has = TaskAssignee::where('task_id', $task->id)
  265. ->where('assignee_id', $user_uid)->exists();
  266. if ($has) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. }