DhammaTermController.php 6.5 KB

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