DictVocabularyController.php 2.6 KB

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