SentPrController.php 9.0 KB

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