DhammaTermController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\DhammaTerm;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Str;
  8. class DhammaTermController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. $result=false;
  18. $indexCol = ['id','guid','word','word_en','meaning','other_meaning','note','language','channal','updated_at'];
  19. switch ($request->get('view')) {
  20. case 'show':
  21. return $this->ok(DhammaTerm::find($request->get('id')));
  22. break;
  23. case 'user':
  24. # code...
  25. $userUid = $_COOKIE['user_uid'];
  26. $search = $request->get('search');
  27. $table = DhammaTerm::select($indexCol)
  28. ->where('owner', $userUid);
  29. if(!empty($search)){
  30. $table->where('word', 'like', $search."%")
  31. ->orWhere('word_en', 'like', $search."%")
  32. ->orWhere('meaning', 'like', "%".$search."%");
  33. }
  34. if(!empty($request->get('order')) && !empty($request->get('dir'))){
  35. $table->orderBy($request->get('order'),$request->get('dir'));
  36. }else{
  37. $table->orderBy('updated_at','desc');
  38. }
  39. $count = $table->count();
  40. if(!empty($request->get('limit'))){
  41. $offset = 0;
  42. if(!empty($request->get("offset"))){
  43. $offset = $request->get("offset");
  44. }
  45. $table->skip($offset)->take($request->get('limit'));
  46. }
  47. $result = $table->get();
  48. break;
  49. case 'word':
  50. $result = DhammaTerm::select($indexCol)
  51. ->where('word', $request->get("word"))
  52. ->orderBy('created_at','desc')
  53. ->get();
  54. $count = count($result);
  55. break;
  56. case 'hot-meaning':
  57. $key='term/hot_meaning';
  58. $value = Cache::get($key, function()use($request) {
  59. $hotMeaning=[];
  60. $words = DhammaTerm::select('word')
  61. ->where('language',$request->get("language"))
  62. ->groupby('word')
  63. ->get();
  64. foreach ($words as $key => $word) {
  65. # code...
  66. $result = DhammaTerm::select(DB::raw('count(*) as word_count, meaning'))
  67. ->where('language',$request->get("language"))
  68. ->where('word',$word['word'])
  69. ->groupby('meaning')
  70. ->orderby('word_count','desc')
  71. ->first();
  72. if($result){
  73. $hotMeaning[]=[
  74. 'word'=>$word['word'],
  75. 'meaning'=>$result['meaning'],
  76. 'language'=>$request->get("language"),
  77. 'owner'=>'',
  78. ];
  79. }
  80. }
  81. Cache::put($key, $hotMeaning, 3600);
  82. return $hotMeaning;
  83. });
  84. return $this->ok(["rows"=>$value,"count"=>count($value)]);
  85. break;
  86. default:
  87. # code...
  88. break;
  89. }
  90. if($result){
  91. return $this->ok(["rows"=>$result,"count"=>$count]);
  92. }else{
  93. return $this->error("没有查询到数据");
  94. }
  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. // validate
  105. // read more on validation at http://laravel.com/docs/validation
  106. $rules = array(
  107. 'word' => 'required',
  108. 'meaning' => 'required',
  109. 'language' => 'required'
  110. );
  111. $validator = Validator::make($request->all(), $rules);
  112. // process the login
  113. if ($validator->fails()) {
  114. return $this->error($validator);
  115. } else {
  116. #查询重复的
  117. /*
  118. 重复判定:
  119. 一个channel下面word+tag+language 唯一
  120. */
  121. $table = DhammaTerm::where('owner', $_COOKIE["user_uid"])
  122. ->where('word',$request->get("word"))
  123. ->where('tag',$request->get("tag"));
  124. if($request->get("channel")){
  125. $isDoesntExist = $table->where('channel',$request->get("channel"))
  126. ->doesntExist();
  127. }else{
  128. $isDoesntExist = $table->where('language',$request->get("language"))
  129. ->doesntExist();
  130. }
  131. if($isDoesntExist){
  132. #不存在插入数据
  133. $term = new DhammaTerm;
  134. $term->id=app('snowflake')->id();
  135. $term->guid=Str::uuid();
  136. $term->word=$request->get("word");
  137. $term->meaning=$request->get("meaning");
  138. $term->save();
  139. return $this->ok($data);
  140. }else{
  141. return $this->error("word existed");
  142. }
  143. // store
  144. /*
  145. $data = $request->all();
  146. $data['id'] = app('snowflake')->id();
  147. $data['guid'] = Str::uuid();
  148. DhammaTerm::create($data);
  149. */
  150. }
  151. }
  152. /**
  153. * Display the specified resource.
  154. *
  155. * @param \App\Models\DhammaTerm $dhammaTerm
  156. * @return \Illuminate\Http\Response
  157. */
  158. public function show(DhammaTerm $dhammaTerm)
  159. {
  160. //
  161. return $dhammaTerm;
  162. }
  163. /**
  164. * Update the specified resource in storage.
  165. *
  166. * @param \Illuminate\Http\Request $request
  167. * @param \App\Models\DhammaTerm $dhammaTerm
  168. * @return \Illuminate\Http\Response
  169. */
  170. public function update(Request $request, DhammaTerm $dhammaTerm)
  171. {
  172. //
  173. }
  174. /**
  175. * Remove the specified resource from storage.
  176. *
  177. * @param \App\Models\DhammaTerm $dhammaTerm
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function destroy(DhammaTerm $dhammaTerm,Request $request)
  181. {
  182. //
  183. $arrId = json_decode($request->get("id"),true) ;
  184. $count = 0;
  185. foreach ($arrId as $key => $id) {
  186. # code...
  187. $result = DhammaTerm::where('id', $id)
  188. ->where('owner', $_COOKIE["user_uid"])
  189. ->delete();
  190. if($result){
  191. $count++;
  192. }
  193. }
  194. return $this->ok($count);
  195. }
  196. }