TaskApi.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. return 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. }
  90. public static function getNextTasks($taskId){
  91. return TaskApi::getRelationTasks($taskId,'next');
  92. }
  93. public static function getPreTasks($taskId){
  94. return TaskApi::getRelationTasks($taskId,'pre');
  95. }
  96. public static function removeTaskRelationRedisKey($taskId,$relation='pre'){
  97. $key = TaskApi::taskRelationRedisKey($taskId,$relation);
  98. RedisClusters::forget($key);
  99. }
  100. public static function taskRelationRedisKey($taskId,$relation='pre'){
  101. return "task/relation/{$relation}/{$taskId}";
  102. }
  103. }