2
0

TaskStatusController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. switch ($request->get('status')) {
  89. case 'published':
  90. $this->pushChange('published', $id);
  91. # 开启子任务
  92. $children = Task::where('parent_id', $id)
  93. ->where('status', 'pending')
  94. ->select('id')->get();
  95. foreach ($children as $key => $child) {
  96. $this->pushChange('published', $child->id);
  97. }
  98. break;
  99. case 'running':
  100. $task->started_at = now();
  101. $task->executor_id = $user['user_uid'];
  102. $this->pushChange('running', $task->id);
  103. break;
  104. case 'done':
  105. $this->pushChange('done', $task->id);
  106. $task->finished_at = now();
  107. $preTask = [$task->id];
  108. //开启父任务
  109. if ($task->parent_id) {
  110. $notCompleted = Task::where('parent_id', $task->parent_id)
  111. ->where('id', '!=', $task->id)
  112. ->where('status', '!=', 'done')
  113. ->count();
  114. if ($notCompleted === 0) {
  115. //父任务已经完成
  116. $preTask[] = $task->parent_id;
  117. $this->pushChange('done', $task->parent_id);
  118. }
  119. }
  120. //开启后置任务
  121. $nextTasks = TaskRelation::whereIn('task_id', $preTask)
  122. ->select('next_task_id')->get();
  123. foreach ($nextTasks as $key => $value) {
  124. $nextTask = Task::find($value->next_task_id);
  125. if ($nextTask->status === 'pending') {
  126. $this->pushChange('published', $value->next_task_id);
  127. }
  128. }
  129. //开启后置任务的子任务
  130. $nextTasksChildren = Task::whereIn('parent_id', $this->getChange('published'))
  131. ->where('status', 'pending')
  132. ->select('id')->get();
  133. foreach ($nextTasksChildren as $child) {
  134. $this->pushChange('published', $child->id);
  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 === 'requested_restart') {
  141. //$runningTask[] = $value->next_task_id;
  142. $this->pushChange('running', $value->next_task_id);
  143. }
  144. }
  145. break;
  146. case 'requested_restart':
  147. $this->pushChange('requested_restart', $task->id);
  148. //从新开启前置任务
  149. $preTasks = TaskRelation::where('next_task_id', $task->id)
  150. ->select('task_id')->get();
  151. foreach ($preTasks as $key => $value) {
  152. //$restartTask[] = $value->task_id;
  153. $this->pushChange('restart', $value->task_id);
  154. }
  155. break;
  156. }
  157. $task->status = $request->get('status');
  158. $task->editor_id = $user['user_uid'];
  159. $task->save();
  160. # auto start with ai assistant
  161. foreach ($this->getChange('published') as $taskId) {
  162. $taskAssignee = TaskAssignee::where('task_id', $taskId)
  163. ->select('assignee_id')->get();
  164. $aiAssistant = AiModel::whereIn('uid', $taskAssignee)->first();
  165. if ($aiAssistant) {
  166. $aiTask = Task::find($taskId);
  167. $aiTask->executor_id = $aiAssistant->uid;
  168. $aiTask->status = 'running';
  169. $aiTask->save();
  170. $this->pushChange('running', $taskId);
  171. $params = AiTaskPrepare::translate($taskId);
  172. Log::debug('ai running', ['params' => $params]);
  173. }
  174. }
  175. $allChanged = [];
  176. foreach ($this->changeTasks as $key => $tasksId) {
  177. $allChanged = array_merge($allChanged, $tasksId);
  178. #change status in related
  179. $data = [
  180. 'status' => $key,
  181. 'editor_id' => $user['user_uid'],
  182. 'updated_at' => now(),
  183. ];
  184. if ($key === 'done') {
  185. $data['finished_at'] = now();
  186. }
  187. if ($key === 'running') {
  188. $data['started_at'] = now();
  189. }
  190. if ($key === 'restart') {
  191. $data['finished_at'] = null;
  192. }
  193. Task::whereIn('id', $tasksId)
  194. ->update($data);
  195. //发送站内信
  196. $send = WatchApi::change(
  197. resId: $tasksId,
  198. from: $user['user_uid'],
  199. message: "任务状态变为 {$key}",
  200. );
  201. Log::debug('watch message', [
  202. 'send-to' => $send,
  203. ]);
  204. }
  205. //changed tasks
  206. $result = Task::whereIn('id', $allChanged)
  207. ->get();
  208. return $this->ok(
  209. [
  210. "rows" => TaskResource::collection(resource: $result),
  211. "count" => count($result),
  212. ]
  213. );
  214. }
  215. /**
  216. * Remove the specified resource from storage.
  217. *
  218. * @param \App\Models\Task $task
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function destroy(Task $task)
  222. {
  223. //
  224. }
  225. public function canEdit(string $user_uid, Task $task)
  226. {
  227. if ($user_uid === $task->owner_id || $user_uid === $task->executor_id) {
  228. return true;
  229. }
  230. $has = TaskAssignee::where('task_id', $task->id)
  231. ->where('assignee_id', $user_uid)->exists();
  232. if ($has) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. }