DhammaTermController.php 6.3 KB

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