DhammaTermController.php 7.4 KB

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