TaskStatusController.php 8.5 KB

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