TaskApi.php 3.9 KB

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