TaskApi.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\Task;
  4. use App\Models\TaskRelation;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\App;
  7. use App\Tools\RedisClusters;
  8. use Illuminate\Support\Str;
  9. class TaskApi
  10. {
  11. public static function getById($id)
  12. {
  13. if (!$id) {
  14. return null;
  15. };
  16. $task = Task::where('id', $id)->first();
  17. if ($task) {
  18. return [
  19. 'id' => $id,
  20. 'title' => $task->title,
  21. 'description' => $task->description,
  22. ];
  23. } else {
  24. return null;
  25. }
  26. }
  27. public static function getListByIds($ids)
  28. {
  29. if (!$ids) {
  30. return null;
  31. };
  32. $tasks = Task::whereIn('id', $ids)->get();
  33. $output = array();
  34. foreach ($ids as $key => $id) {
  35. foreach ($tasks as $task) {
  36. if ($task->id === $id) {
  37. $output[] = [
  38. 'id' => $id,
  39. 'title' => $task->title,
  40. 'description' => $task->description,
  41. ];
  42. continue;
  43. };
  44. }
  45. }
  46. return $output;
  47. }
  48. public static function setRelationTasks($taskId, $relationTasksId, $editor_id, $relation = 'pre')
  49. {
  50. if ($relation === 'pre') {
  51. $where = 'next_task_id';
  52. $task1 = 'task_id';
  53. $task2 = 'next_task_id';
  54. } else {
  55. $where = 'task_id';
  56. $task1 = 'next_task_id';
  57. $task2 = 'task_id';
  58. }
  59. TaskApi::removeTaskRelationRedisKey($taskId, $relation);
  60. $delete = TaskRelation::where($where, $taskId)
  61. ->delete();
  62. foreach ($relationTasksId as $key => $id) {
  63. if (Str::isUuid($taskId) && Str::isUuid($id)) {
  64. $data[] = [
  65. $task1 => $id,
  66. $task2 => $taskId,
  67. 'editor_id' => $editor_id,
  68. 'created_at' => now(),
  69. 'updated_at' => now(),
  70. ];
  71. }
  72. }
  73. if (isset($data)) {
  74. TaskRelation::insert($data);
  75. }
  76. TaskApi::removeTaskRelationRedisKey($taskId, $relation);
  77. }
  78. public static function getRelationTasks($taskId, $relation = 'pre')
  79. {
  80. $key = TaskApi::taskRelationRedisKey($taskId, $relation);
  81. Log::debug('task redis key=' . $key . ' has=' . RedisClusters::has($key));
  82. $data = RedisClusters::remember($key, 3 * 24 * 3600, function () use ($taskId, $relation) {
  83. Log::debug('getRelationTasks task=' . $taskId . ' relation=' . $relation);
  84. if ($relation === 'pre') {
  85. $where = 'next_task_id';
  86. $select = 'task_id';
  87. } else {
  88. $where = 'task_id';
  89. $select = 'next_task_id';
  90. }
  91. $tasks = TaskRelation::where($where, $taskId)
  92. ->select($select)->get();
  93. $tasksId = [];
  94. foreach ($tasks as $key => $task) {
  95. $tasksId[] = $task[$select];
  96. }
  97. return TaskApi::getListByIds($tasksId);
  98. });
  99. return $data;
  100. }
  101. public static function getNextTasks($taskId)
  102. {
  103. return TaskApi::getRelationTasks($taskId, 'next');
  104. }
  105. public static function getPreTasks($taskId)
  106. {
  107. return TaskApi::getRelationTasks($taskId, 'pre');
  108. }
  109. public static function removeTaskRelationRedisKey($taskId, $relation = 'pre')
  110. {
  111. //查询相关task
  112. $relations = TaskRelation::where('task_id', $taskId)
  113. ->orWhere('next_task_id', $taskId)
  114. ->select('task_id', 'next_task_id')->get();
  115. $relationsId = [];
  116. $relationsId[$taskId] = 1;
  117. foreach ($relations as $key => $value) {
  118. $relationsId[$value->task_id] = 1;
  119. $relationsId[$value->next_task_id] = 1;
  120. }
  121. foreach ($relationsId as $taskId => $value) {
  122. $key = TaskApi::taskRelationRedisKey($taskId, 'pre');
  123. RedisClusters::forget($key);
  124. $key = TaskApi::taskRelationRedisKey($taskId, 'next');
  125. RedisClusters::forget($key);
  126. }
  127. }
  128. public static function taskRelationRedisKey($taskId, $relation = 'pre')
  129. {
  130. return "task/relation/{$relation}/{$taskId}";
  131. }
  132. }