SentPrController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $output = [];
  58. $sentences = $request->get("data");
  59. foreach ($sentences as $key => $sentence) {
  60. # 先查句子信息
  61. $sentInfo = Sentence::where('book_id',$sentence['book'])
  62. ->where('paragraph',$sentence['paragraph'])
  63. ->where('word_start',$sentence['word_start'])
  64. ->where('word_end',$sentence['word_end'])
  65. ->where('channel_uid',$sentence['channel_id'])
  66. ->first();
  67. $sentPr = SentPr::where('book_id',$sentence['book'])
  68. ->where('paragraph',$sentence['paragraph'])
  69. ->where('word_start',$sentence['word_start'])
  70. ->where('word_end',$sentence['word_end'])
  71. ->where('channel_uid',$sentence['channel_id'])
  72. ->select('content','editor_uid')
  73. ->orderBy('created_at','desc')->get();
  74. if(count($sentPr)>0){
  75. if($sentInfo){
  76. $content = $sentInfo->content;
  77. }else{
  78. $content = "null";
  79. }
  80. $output[] = [
  81. 'sentence' => [
  82. 'book' => $sentence['book'],
  83. 'paragraph' => $sentence['paragraph'],
  84. 'word_start' => $sentence['word_start'],
  85. 'word_end' => $sentence['word_end'],
  86. 'channel_id' => $sentence['channel_id'],
  87. 'content' => $content,
  88. 'pr_count' => count($sentPr),
  89. ],
  90. 'pr' => $sentPr,
  91. ];
  92. }
  93. }
  94. return $this->ok(['rows'=>$output,'count'=>count($output)]);
  95. }
  96. /**
  97. * Store a newly created resource in storage.
  98. *
  99. * @param \Illuminate\Http\Request $request
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function store(Request $request)
  103. {
  104. //
  105. $user = AuthApi::current($request);
  106. if(!$user){
  107. return $this->error(__('auth.failed'),401,401);
  108. }
  109. $user_uid = $user['user_uid'];
  110. $data = $request->all();
  111. #查询是否存在
  112. #同样的内容只能提交一次
  113. $exists = SentPr::where('book_id',$data['book'])
  114. ->where('paragraph',$data['para'])
  115. ->where('word_start',$data['begin'])
  116. ->where('word_end',$data['end'])
  117. ->where('content',$data['text'])
  118. ->where('channel_uid',$data['channel'])
  119. ->exists();
  120. if($exists){
  121. return $this->error("已经存在同样的修改建议",200,200);
  122. }
  123. #不存在,新建
  124. $new = new SentPr();
  125. $new->id = app('snowflake')->id();
  126. $new->uid = Str::uuid();
  127. $new->book_id = $data['book'];
  128. $new->paragraph = $data['para'];
  129. $new->word_start = $data['begin'];
  130. $new->word_end = $data['end'];
  131. $new->channel_uid = $data['channel'];
  132. $new->editor_uid = $user_uid;
  133. $new->content = $data['text'];
  134. $new->language = Channel::where('uid',$data['channel'])->value('lang');
  135. $new->status = 1;//未处理状态
  136. $new->strlen = mb_strlen($data['text'],"UTF-8");
  137. $new->create_time = time()*1000;
  138. $new->modify_time = time()*1000;
  139. $new->save();
  140. Mq::publish('suggestion',['data'=>new SentPrResource($new),
  141. 'token'=>AuthApi::getToken($request)]);
  142. $robotMessageOk=true;
  143. $webHookMessage="";
  144. #同时返回此句子pr数量
  145. $info['book_id'] = $data['book'];
  146. $info['paragraph'] = $data['para'];
  147. $info['word_start'] = $data['begin'];
  148. $info['word_end'] = $data['end'];
  149. $info['channel_uid'] = $data['channel'];
  150. $count = SentPr::where('book_id' , $data['book'])
  151. ->where('paragraph' , $data['para'])
  152. ->where('word_start' , $data['begin'])
  153. ->where('word_end' , $data['end'])
  154. ->where('channel_uid' , $data['channel'])
  155. ->count();
  156. return $this->ok(["new"=>$info,"count"=>$count,"webhook"=>["message"=>$webHookMessage,"ok"=>$robotMessageOk]]);
  157. }
  158. /**
  159. * Display the specified resource.
  160. * @param \Illuminate\Http\Request $request
  161. * @param string $uid
  162. * @return \Illuminate\Http\Response
  163. */
  164. public function show(Request $request,string $uid)
  165. {
  166. //
  167. $pr = SentPr::where('uid',$uid)->first();
  168. if(!$pr){
  169. return $this->error('no data',404,404);
  170. }
  171. //修改notification 已读状态
  172. $user = AuthApi::current($request);
  173. if($user){
  174. Notification::where('res_id',$uid)
  175. ->where('to',$user['user_uid'])
  176. ->update(['status'=>'read']);
  177. }
  178. return $this->ok(new SentPrResource($pr));
  179. }
  180. /**
  181. * Update the specified resource in storage.
  182. *
  183. * @param \Illuminate\Http\Request $request
  184. * @param \App\Models\SentPr $sentPr
  185. * @return \Illuminate\Http\Response
  186. */
  187. public function update(Request $request, string $id)
  188. {
  189. $user = AuthApi::current($request);
  190. if(!$user){
  191. return $this->error(__('auth.failed'),401,401);
  192. }
  193. $sentPr = SentPr::find($id);
  194. if(!$sentPr){
  195. return $this->error('no res');
  196. }
  197. if($sentPr->editor_uid !== $user['user_uid']){
  198. return $this->error('not power',403,403);
  199. }
  200. $sentPr->content = $request->get('text');
  201. $sentPr->modify_time = time()*1000;
  202. $sentPr->save();
  203. return $this->ok($sentPr);
  204. }
  205. /**
  206. * Remove the specified resource from storage.
  207. *
  208. * @param string $id
  209. * @return \Illuminate\Http\Response
  210. */
  211. public function destroy(Request $request, string $id)
  212. {
  213. //
  214. $user = AuthApi::current($request);
  215. if(!$user){
  216. return $this->error(__('auth.failed'),401,401);
  217. }
  218. $old = SentPr::where('id', $id)->first();
  219. if(!$old){
  220. return $this->error('no res');
  221. }
  222. //鉴权
  223. if($old->editor_uid !== $user["user_uid"]){
  224. return $this->error(__('auth.failed'),403,403);
  225. }
  226. $result = SentPr::where('id', $id)
  227. ->where('editor_uid', $user["user_uid"])
  228. ->delete();
  229. if($result>0){
  230. #同时返回此句子pr数量
  231. $count = SentPr::where('book_id' , $old->book_id)
  232. ->where('paragraph' , $old->paragraph)
  233. ->where('word_start' , $old->word_start)
  234. ->where('word_end' , $old->word_end)
  235. ->where('channel_uid' , $old->channel_uid)
  236. ->count();
  237. return $this->ok($count);
  238. }else{
  239. return $this->error('not power',403,403);
  240. }
  241. }
  242. }