SentPrController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\Mq;
  4. use App\Http\Resources\SentPrResource;
  5. use App\Models\Channel;
  6. use App\Models\Notification;
  7. use App\Models\Sentence;
  8. use App\Models\SentPr;
  9. use App\Services\AuthService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Response;
  12. use Illuminate\Support\Str;
  13. class SentPrController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. switch ($request->input('view')) {
  24. case 'sent-info':
  25. $table = SentPr::where('book_id', $request->input('book'))
  26. ->where('paragraph', $request->input('para'))
  27. ->where('word_start', $request->input('start'))
  28. ->where('word_end', $request->input('end'))
  29. ->where('channel_uid', $request->input('channel'));
  30. $all_count = $table->count();
  31. $result = $table->orderBy('created_at', 'desc')->get();
  32. break;
  33. }
  34. if ($result) {
  35. // 修改notification 已读状态
  36. $user = AuthService::current($request);
  37. if ($user) {
  38. $id = [];
  39. foreach ($result as $key => $row) {
  40. $id[] = $row->uid;
  41. }
  42. Notification::whereIn('res_id', $id)
  43. ->where('to', $user['user_uid'])
  44. ->update(['status' => 'read']);
  45. }
  46. return $this->ok([
  47. 'rows' => SentPrResource::collection($result),
  48. 'count' => $all_count,
  49. ]);
  50. } else {
  51. return $this->error('no data');
  52. }
  53. }
  54. public function pr_tree(Request $request)
  55. {
  56. $output = [];
  57. $sentences = $request->input('data');
  58. foreach ($sentences as $key => $sentence) {
  59. // 先查句子信息
  60. $sentInfo = Sentence::where('book_id', $sentence['book'])
  61. ->where('paragraph', $sentence['paragraph'])
  62. ->where('word_start', $sentence['word_start'])
  63. ->where('word_end', $sentence['word_end'])
  64. ->where('channel_uid', $sentence['channel_id'])
  65. ->first();
  66. $sentPr = SentPr::where('book_id', $sentence['book'])
  67. ->where('paragraph', $sentence['paragraph'])
  68. ->where('word_start', $sentence['word_start'])
  69. ->where('word_end', $sentence['word_end'])
  70. ->where('channel_uid', $sentence['channel_id'])
  71. ->select('content', 'editor_uid')
  72. ->orderBy('created_at', 'desc')->get();
  73. if (count($sentPr) > 0) {
  74. if ($sentInfo) {
  75. $content = $sentInfo->content;
  76. } else {
  77. $content = 'null';
  78. }
  79. $output[] = [
  80. 'sentence' => [
  81. 'book' => $sentence['book'],
  82. 'paragraph' => $sentence['paragraph'],
  83. 'word_start' => $sentence['word_start'],
  84. 'word_end' => $sentence['word_end'],
  85. 'channel_id' => $sentence['channel_id'],
  86. 'content' => $content,
  87. 'pr_count' => count($sentPr),
  88. ],
  89. 'pr' => $sentPr,
  90. ];
  91. }
  92. }
  93. return $this->ok(['rows' => $output, 'count' => count($output)]);
  94. }
  95. /**
  96. * Store a newly created resource in storage.
  97. *
  98. * @return Response
  99. */
  100. public function store(Request $request)
  101. {
  102. //
  103. $user = AuthService::current($request);
  104. if (! $user) {
  105. return $this->error(__('auth.failed'), 401, 401);
  106. }
  107. $user_uid = $user['user_uid'];
  108. $data = $request->all();
  109. // 查询是否存在
  110. // 同样的内容只能提交一次
  111. $exists = SentPr::where('book_id', $data['book'])
  112. ->where('paragraph', $data['para'])
  113. ->where('word_start', $data['begin'])
  114. ->where('word_end', $data['end'])
  115. ->where('content', $data['text'])
  116. ->where('channel_uid', $data['channel'])
  117. ->exists();
  118. if ($exists) {
  119. return $this->error('已经存在同样的修改建议', 200, 200);
  120. }
  121. // 不存在,新建
  122. $new = new SentPr;
  123. $new->id = app('snowflake')->id();
  124. $new->uid = Str::uuid();
  125. $new->book_id = $data['book'];
  126. $new->paragraph = $data['para'];
  127. $new->word_start = $data['begin'];
  128. $new->word_end = $data['end'];
  129. $new->channel_uid = $data['channel'];
  130. $new->editor_uid = $user_uid;
  131. $new->content = $data['text'];
  132. $new->language = Channel::where('uid', $data['channel'])->value('lang');
  133. $new->status = 1; // 未处理状态
  134. $new->strlen = mb_strlen($data['text'], 'UTF-8');
  135. $new->create_time = time() * 1000;
  136. $new->modify_time = time() * 1000;
  137. $new->save();
  138. $suggestionData = [
  139. 'data' => new SentPrResource($new),
  140. 'token' => AuthService::getToken($request),
  141. 'notification' => $request->input('notification', true),
  142. 'webhook' => $request->input('webhook', true),
  143. ];
  144. Mq::publish(
  145. 'suggestion',
  146. $suggestionData
  147. );
  148. $robotMessageOk = true;
  149. $webHookMessage = '';
  150. // 同时返回此句子pr数量
  151. $info['book_id'] = $data['book'];
  152. $info['paragraph'] = $data['para'];
  153. $info['word_start'] = $data['begin'];
  154. $info['word_end'] = $data['end'];
  155. $info['channel_uid'] = $data['channel'];
  156. $count = SentPr::where('book_id', $data['book'])
  157. ->where('paragraph', $data['para'])
  158. ->where('word_start', $data['begin'])
  159. ->where('word_end', $data['end'])
  160. ->where('channel_uid', $data['channel'])
  161. ->count();
  162. return $this->ok(['new' => $info, 'count' => $count, 'webhook' => ['message' => $webHookMessage, 'ok' => $robotMessageOk]]);
  163. }
  164. /**
  165. * Display the specified resource.
  166. *
  167. * @return Response
  168. */
  169. public function show(Request $request, string $uid)
  170. {
  171. //
  172. $pr = SentPr::where('uid', $uid)->first();
  173. if (! $pr) {
  174. return $this->error('no data', 404, 404);
  175. }
  176. // 修改notification 已读状态
  177. $user = AuthService::current($request);
  178. if ($user) {
  179. Notification::where('res_id', $uid)
  180. ->where('to', $user['user_uid'])
  181. ->update(['status' => 'read']);
  182. }
  183. return $this->ok(new SentPrResource($pr));
  184. }
  185. /**
  186. * Update the specified resource in storage.
  187. *
  188. * @param SentPr $sentPr
  189. * @return Response
  190. */
  191. public function update(Request $request, string $id)
  192. {
  193. $user = AuthService::current($request);
  194. if (! $user) {
  195. return $this->error(__('auth.failed'), 401, 401);
  196. }
  197. $sentPr = SentPr::find($id);
  198. if (! $sentPr) {
  199. return $this->error('no res');
  200. }
  201. if ($sentPr->editor_uid !== $user['user_uid']) {
  202. return $this->error('not power', 403, 403);
  203. }
  204. $sentPr->content = $request->input('text');
  205. $sentPr->modify_time = time() * 1000;
  206. $sentPr->save();
  207. return $this->ok($sentPr);
  208. }
  209. /**
  210. * Remove the specified resource from storage.
  211. *
  212. * @return Response
  213. */
  214. public function destroy(Request $request, string $id)
  215. {
  216. //
  217. $user = AuthService::current($request);
  218. if (! $user) {
  219. return $this->error(__('auth.failed'), 401, 401);
  220. }
  221. $old = SentPr::where('id', $id)->first();
  222. if (! $old) {
  223. return $this->error('no res');
  224. }
  225. // 鉴权
  226. if ($old->editor_uid !== $user['user_uid']) {
  227. return $this->error(__('auth.failed'), 403, 403);
  228. }
  229. $result = SentPr::where('id', $id)
  230. ->where('editor_uid', $user['user_uid'])
  231. ->delete();
  232. if ($result > 0) {
  233. // 同时返回此句子pr数量
  234. $count = SentPr::where('book_id', $old->book_id)
  235. ->where('paragraph', $old->paragraph)
  236. ->where('word_start', $old->word_start)
  237. ->where('word_end', $old->word_end)
  238. ->where('channel_uid', $old->channel_uid)
  239. ->count();
  240. return $this->ok($count);
  241. } else {
  242. return $this->error('not power', 403, 403);
  243. }
  244. }
  245. }