DhammaTermController.php 6.4 KB

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