WbwSentenceController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\CourseApi;
  5. use App\Models\Course;
  6. use App\Models\Wbw;
  7. use App\Models\WbwBlock;
  8. use App\Services\AuthService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. class WbwSentenceController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index(Request $request)
  19. {
  20. //
  21. $channelsId = [];
  22. $result = [];
  23. $user = AuthService::current($request);
  24. $user_uid = null;
  25. if ($user) {
  26. $user_uid = $user['user_uid'];
  27. }
  28. $sentId = $request->input('book').'-'.
  29. $request->input('para').'-'.
  30. $request->input('wordStart').'-'.
  31. $request->input('wordEnd');
  32. switch ($request->input('view')) {
  33. case 'course-answer':
  34. if ($request->has('course')) {
  35. $channelsId[] = Course::where('id', $request->input('course'))
  36. ->value('channel_id');
  37. }
  38. break;
  39. case 'sent-can-read':
  40. $channels = [];
  41. if ($request->has('course')) {
  42. $channels = CourseApi::getStudentChannels($request->input('course'));
  43. $channels[] = Course::where('id', $request->input('course'))
  44. ->value('channel_id');
  45. } else {
  46. $channels = ChannelApi::getCanReadByUser($user_uid);
  47. }
  48. if ($request->has('exclude')) {
  49. // 移除无需查询的channel
  50. foreach ($channels as $key => $id) {
  51. if ($id !== $request->input('exclude')) {
  52. $channelsId[] = $id;
  53. }
  54. }
  55. } elseif ($request->has('channels')) {
  56. // 仅列出指定的channel
  57. $include = explode(',', $request->input('channels'));
  58. foreach ($channels as $key => $id) {
  59. if (in_array($id, $include)) {
  60. $channelsId[] = $id;
  61. }
  62. }
  63. } else {
  64. $channelsId = $channels;
  65. }
  66. break;
  67. }
  68. $validBlocks = WbwSentenceController::getBlocksByChannels(
  69. $channelsId,
  70. $request->input('book'),
  71. $request->input('para'),
  72. $request->input('wordStart')
  73. );
  74. foreach ($validBlocks as $key => $blockId) {
  75. $channel = WbwBlock::where('uid', $blockId)->first();
  76. $corpus = new CorpusController;
  77. $props = $corpus->getSentTpl(
  78. $sentId,
  79. [$channel->channel_uid],
  80. 'edit',
  81. true,
  82. 'react'
  83. );
  84. $result[] = $props;
  85. }
  86. return $this->ok(['rows' => $result, 'count' => count($result)]);
  87. }
  88. public static function getBlocksByChannels($channelsId, $book, $para, $wordId)
  89. {
  90. $wbwBlocksId = WbwBlock::where('book_id', $book)
  91. ->where('paragraph', $para)
  92. ->whereIn('channel_uid', $channelsId)
  93. ->select('uid')
  94. ->get();
  95. $validBlocks = Wbw::whereIn('block_uid', $wbwBlocksId)
  96. ->where('book_id', $book)
  97. ->where('paragraph', $para)
  98. ->where('wid', $wordId)
  99. ->select('block_uid')
  100. ->groupBy('block_uid')
  101. ->get();
  102. $blocksId = [];
  103. foreach ($validBlocks as $key => $block) {
  104. $blocksId[] = $block->block_uid;
  105. }
  106. return $blocksId;
  107. }
  108. public static function getWbwIdByChannels($channelsId, $book, $para, $wordId)
  109. {
  110. $validBlocks = WbwSentenceController::getBlocksByChannels($channelsId, $book, $para, $wordId);
  111. $wbwId = Wbw::whereIn('block_uid', $validBlocks)
  112. ->where('book_id', $book)
  113. ->where('paragraph', $para)
  114. ->where('wid', $wordId)
  115. ->select('uid')
  116. ->get();
  117. $id = [];
  118. foreach ($wbwId as $key => $value) {
  119. $id[] = $value->uid;
  120. }
  121. return $id;
  122. }
  123. /**
  124. * Store a newly created resource in storage.
  125. *
  126. * @return Response
  127. */
  128. public function store(Request $request)
  129. {
  130. //
  131. }
  132. /**
  133. * Display the specified resource.
  134. *
  135. * @return Response
  136. */
  137. public function show(Wbw $wbw)
  138. {
  139. //
  140. }
  141. /**
  142. * Update the specified resource in storage.
  143. *
  144. * @return Response
  145. */
  146. public function update(Request $request, Wbw $wbw)
  147. {
  148. //
  149. }
  150. /**
  151. * Remove the specified resource from storage.
  152. *
  153. * @param Wbw $wbw
  154. * @return Response
  155. */
  156. public function destroy(Request $request, string $sentId)
  157. {
  158. //
  159. // 鉴权
  160. $user = AuthService::current($request);
  161. if (! $user) {
  162. // 未登录用户
  163. return $this->error(__('auth.failed'), 401, 401);
  164. }
  165. $channelId = $request->input('channel');
  166. if (! ChannelApi::canManageByUser($channelId, $user['user_uid'])) {
  167. return $this->error(__('auth.failed'), 403, 403);
  168. }
  169. $sent = explode('-', $sentId);
  170. $wbwBlockId = WbwBlock::where('book_id', $sent[0])
  171. ->where('paragraph', $sent[1])
  172. ->where('channel_uid', $channelId)
  173. ->value('uid');
  174. $delete = Wbw::where('block_uid', $wbwBlockId)
  175. ->whereBetween('wid', [$sent[2], $sent[3]])
  176. ->delete();
  177. return $this->ok($delete);
  178. }
  179. }