TaskStatusController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Http\Api\AiTaskPrepare;
  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. $this->pushChange('running', $task->id);
  114. break;
  115. case 'restarted':
  116. $this->pushChange('restarted', $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. $aiTask->executor_id = $aiAssistant->uid;
  180. $aiTask->status = 'running';
  181. $aiTask->save();
  182. $this->pushChange('running', $taskId);
  183. $params = AiTaskPrepare::translate($taskId);
  184. Log::debug('ai running', ['params' => $params]);
  185. }
  186. }
  187. $allChanged = [];
  188. foreach ($this->changeTasks as $key => $tasksId) {
  189. $allChanged = array_merge($allChanged, $tasksId);
  190. #change status in related
  191. $data = [
  192. 'status' => $key,
  193. 'editor_id' => $user['user_uid'],
  194. 'updated_at' => now(),
  195. ];
  196. if ($key === 'done') {
  197. $data['finished_at'] = now();
  198. }
  199. if ($key === 'running') {
  200. $data['started_at'] = now();
  201. }
  202. if ($key === 'restart') {
  203. $data['finished_at'] = null;
  204. }
  205. Task::whereIn('id', $tasksId)
  206. ->update($data);
  207. //发送站内信
  208. $send = WatchApi::change(
  209. resId: $tasksId,
  210. from: $user['user_uid'],
  211. message: "任务状态变为 {$key}",
  212. );
  213. Log::debug('watch message', [
  214. 'send-to' => $send,
  215. ]);
  216. }
  217. //changed tasks
  218. $result = Task::whereIn('id', $allChanged)
  219. ->get();
  220. return $this->ok(
  221. [
  222. "rows" => TaskResource::collection(resource: $result),
  223. "count" => count($result),
  224. ]
  225. );
  226. }
  227. /**
  228. * Remove the specified resource from storage.
  229. *
  230. * @param \App\Models\Task $task
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function destroy(Task $task)
  234. {
  235. //
  236. }
  237. public function canEdit(string $user_uid, Task $task)
  238. {
  239. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  240. return true;
  241. }
  242. $has = TaskAssignee::where('task_id', $task->id)
  243. ->where('assignee_id', $user_uid)->exists();
  244. if ($has) {
  245. return true;
  246. }
  247. return false;
  248. }
  249. }