DictVocabularyController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use App\Models\DictInfo;
  5. use Illuminate\Http\Request;
  6. use App\Http\Resources\DictVocabularyResource;
  7. class DictVocabularyController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. //
  17. switch ($request->get("view")) {
  18. case 'dict_name':
  19. $id = DictInfo::where('name',$request->get("name"))->value('id');
  20. if(!$id){
  21. return $this->error('name:'.$request->get("name").' can not found.',200,200);
  22. }
  23. $table = UserDict::where('dict_id',$id)
  24. ->groupBy('word')
  25. ->selectRaw('word,count(*)');
  26. break;
  27. case 'dict_short_name':
  28. $id = DictInfo::where('shortname',$request->get("name"))->value('id');
  29. if(!$id){
  30. return $this->error('name:'.$request->get("name").' can not found.',200,200);
  31. }
  32. $table = UserDict::where('dict_id',$id)
  33. ->groupBy('word')
  34. ->selectRaw('word,count(*)');
  35. break;
  36. }
  37. if($request->get("stream") === 'true'){
  38. return response()->streamDownload(function () use ($table) {
  39. $result = $table->get();
  40. echo json_encode($result);
  41. },'dict.txt');
  42. }
  43. $count = 2;
  44. $table = $table->orderBy('word',$request->get('dir','asc'));
  45. $table = $table->skip($request->get('offset',0))
  46. ->take($request->get('limit',1000));
  47. $result = $table->get();
  48. return $this->ok([
  49. "rows"=>DictVocabularyResource::collection($result),
  50. "count"=>$count
  51. ]);
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function store(Request $request)
  60. {
  61. //
  62. }
  63. /**
  64. * Display the specified resource.
  65. *
  66. * @param \App\Models\UserDict $userDict
  67. * @return \Illuminate\Http\Response
  68. */
  69. public function show(UserDict $userDict)
  70. {
  71. //
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @param \App\Models\UserDict $userDict
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function update(Request $request, UserDict $userDict)
  81. {
  82. //
  83. }
  84. /**
  85. * Remove the specified resource from storage.
  86. *
  87. * @param \App\Models\UserDict $userDict
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function destroy(UserDict $userDict)
  91. {
  92. //
  93. }
  94. }