TaskStatusController.php 9.8 KB

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