2
0

TaskStatusController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. use App\Models\AiModel;
  13. use App\Services\AiTranslateService;
  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 'done':
  120. $this->pushChange('done', $task->id);
  121. $task->finished_at = now();
  122. $preTask = [$task->id];
  123. //开启父任务
  124. if ($task->parent_id) {
  125. $notCompleted = Task::where('parent_id', $task->parent_id)
  126. ->where('id', '!=', $task->id)
  127. ->where('status', '!=', 'done')
  128. ->count();
  129. if ($notCompleted === 0) {
  130. //父任务已经完成
  131. $preTask[] = $task->parent_id;
  132. $this->pushChange('done', $task->parent_id);
  133. }
  134. }
  135. //开启后置任务
  136. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  137. ->select('next_task_id')->get();
  138. foreach ($nextTasks as $key => $value) {
  139. $nextTask = Task::find($value->next_task_id);
  140. if ($nextTask->status === 'pending') {
  141. $this->pushChange('published', $value->next_task_id);
  142. }
  143. }
  144. //开启后置任务的子任务
  145. $nextTasksChildren = Task::whereIn('parent_id', $this->getChange('published'))
  146. ->where('status', 'pending')
  147. ->select('id')->get();
  148. foreach ($nextTasksChildren as $child) {
  149. $this->pushChange('published', $child->id);
  150. }
  151. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  152. ->select('next_task_id')->get();
  153. foreach ($nextTasks as $key => $value) {
  154. $nextTask = Task::find($value->next_task_id);
  155. if ($nextTask->status === 'requested_restart') {
  156. //$runningTask[] = $value->next_task_id;
  157. $this->pushChange('running', $value->next_task_id);
  158. }
  159. }
  160. break;
  161. case 'requested_restart':
  162. $this->pushChange('requested_restart', $task->id);
  163. //从新开启前置任务
  164. $preTasks = TaskRelation::where('next_task_id', $task->id)
  165. ->select('task_id')->get();
  166. foreach ($preTasks as $key => $value) {
  167. //$restartTask[] = $value->task_id;
  168. $this->pushChange('restarted', $value->task_id);
  169. }
  170. break;
  171. }
  172. # auto start with ai assistant
  173. $autoStart = array_merge($this->getChange('published'), $this->getChange('restarted'));
  174. foreach ($autoStart as $taskId) {
  175. $taskAssignee = TaskAssignee::where('task_id', $taskId)
  176. ->select('assignee_id')->get();
  177. $aiAssistant = AiModel::whereIn('uid', $taskAssignee)->first();
  178. if ($aiAssistant) {
  179. $aiTask = Task::find($taskId);
  180. try {
  181. //$ai = app(AiTranslateService::class);
  182. //$params = $ai->makeByTask($taskId, $aiAssistant->uid, true);
  183. \App\Jobs\ProcessAITranslateJob::publish($taskId, $aiAssistant->uid);
  184. $aiTask->executor_id = $aiAssistant->uid;
  185. $aiTask->status = 'queue';
  186. $this->pushChange('queue', $taskId);
  187. } catch (\Exception $e) {
  188. $aiTask->status = 'pending';
  189. Log::error('ai assistant start fail', [
  190. 'task' => $taskId,
  191. 'error' => $e->getMessage()
  192. ]);
  193. } finally {
  194. $aiTask->save();
  195. }
  196. }
  197. }
  198. $allChanged = [];
  199. foreach ($this->changeTasks as $key => $tasksId) {
  200. $allChanged = array_merge($allChanged, $tasksId);
  201. #change status in related
  202. $data = [
  203. 'status' => $key,
  204. 'editor_id' => $user['user_uid'],
  205. 'updated_at' => now(),
  206. ];
  207. if ($key === 'done') {
  208. $data['finished_at'] = now();
  209. }
  210. if ($key === 'running') {
  211. $data['started_at'] = now();
  212. }
  213. if ($key === 'restart') {
  214. $data['finished_at'] = null;
  215. }
  216. Task::whereIn('id', $tasksId)
  217. ->update($data);
  218. //发送站内信
  219. $send = WatchApi::change(
  220. resId: $tasksId,
  221. from: $user['user_uid'],
  222. message: "任务状态变为 {$key}",
  223. );
  224. Log::debug('watch message', [
  225. 'send-to' => $send,
  226. ]);
  227. }
  228. //changed tasks
  229. $result = Task::whereIn('id', $allChanged)
  230. ->get();
  231. return $this->ok(
  232. [
  233. "rows" => TaskResource::collection(resource: $result),
  234. "count" => count($result),
  235. ]
  236. );
  237. }
  238. /**
  239. * Remove the specified resource from storage.
  240. *
  241. * @param \App\Models\Task $task
  242. * @return \Illuminate\Http\Response
  243. */
  244. public function destroy(Task $task)
  245. {
  246. //
  247. }
  248. public function canEdit(string $user_uid, Task $task)
  249. {
  250. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  251. return true;
  252. }
  253. $has = TaskAssignee::where('task_id', $task->id)
  254. ->where('assignee_id', $user_uid)->exists();
  255. if ($has) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. }