DhammaTermController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\DhammaTerm;
  4. use App\Models\Channel;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Str;
  9. use App\Http\Api\AuthApi;
  10. use App\Http\Api\StudioApi;
  11. class DhammaTermController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index(Request $request)
  19. {
  20. $result=false;
  21. $indexCol = ['id','guid','word','meaning','other_meaning','note','language','channal','created_at','updated_at'];
  22. switch ($request->get('view')) {
  23. case 'create-by-channel':
  24. # 新建术语时。根据术语所在channel 给出新建术语所需数据。如语言,备选意思等。
  25. #获取channel信息
  26. $currChannel = Channel::where('uid',$request->get('channel'))->first();
  27. if(!$currChannel){
  28. return $this->error(__('auth.failed'));
  29. }
  30. #TODO 查询studio信息
  31. #获取同studio的channel列表
  32. $studioChannels = Channel::where('owner_uid',$currChannel->owner_uid)
  33. ->select(['name','uid'])
  34. ->get();
  35. #获取全网意思列表
  36. $meanings = DhammaTerm::where('word',$request->get('word'))
  37. ->where('language',$currChannel->lang)
  38. ->select(['meaning','other_meaning'])
  39. ->get();
  40. $meaningList=[];
  41. foreach ($meanings as $key => $value) {
  42. # code...
  43. $meaning1 = [$value->meaning];
  44. if(!empty($value->other_meaning)){
  45. $meaning2 = \explode(',',$value->other_meaning);
  46. $meaning1 = array_merge($meaning1,$meaning2);
  47. }
  48. foreach ($meaning1 as $key => $value) {
  49. # code...
  50. if(isset($meaningList[$value])){
  51. $meaningList[$value]++;
  52. }else{
  53. $meaningList[$value] = 1;
  54. }
  55. }
  56. }
  57. $meaningCount = [];
  58. foreach ($meaningList as $key => $value) {
  59. # code...
  60. $meaningCount[] = ['meaning'=>$key,'count'=>$value];
  61. }
  62. return $this->ok([
  63. "word"=>$request->get('word'),
  64. "meaningCount"=>$meaningCount,
  65. "studioChannels"=>$studioChannels,
  66. "language"=>$currChannel->lang,
  67. ]);
  68. break;
  69. case 'studio':
  70. # 获取studio内所有channel
  71. $search = $request->get('search');
  72. $user = AuthApi::current($request);
  73. if($user){
  74. //判断当前用户是否有指定的studio的权限
  75. if($user['user_uid'] === StudioApi::getIdByName($request->get('name'))){
  76. $table = DhammaTerm::select($indexCol)
  77. ->where('owner', $user["user_uid"]);
  78. }else{
  79. return $this->error(__('auth.failed'));
  80. }
  81. }else{
  82. return $this->error(__('auth.failed'));
  83. }
  84. break;
  85. case 'show':
  86. return $this->ok(DhammaTerm::find($request->get('id')));
  87. break;
  88. case 'user':
  89. # code...
  90. $userUid = $_COOKIE['user_uid'];
  91. $search = $request->get('search');
  92. $table = DhammaTerm::select($indexCol)
  93. ->where('owner', $userUid);
  94. break;
  95. case 'word':
  96. $table = DhammaTerm::select($indexCol)
  97. ->where('word', $request->get("word"));
  98. break;
  99. case 'hot-meaning':
  100. $key='term/hot_meaning';
  101. $value = Cache::get($key, function()use($request) {
  102. $hotMeaning=[];
  103. $words = DhammaTerm::select('word')
  104. ->where('language',$request->get("language"))
  105. ->groupby('word')
  106. ->get();
  107. foreach ($words as $key => $word) {
  108. # code...
  109. $result = DhammaTerm::select(DB::raw('count(*) as word_count, meaning'))
  110. ->where('language',$request->get("language"))
  111. ->where('word',$word['word'])
  112. ->groupby('meaning')
  113. ->orderby('word_count','desc')
  114. ->first();
  115. if($result){
  116. $hotMeaning[]=[
  117. 'word'=>$word['word'],
  118. 'meaning'=>$result['meaning'],
  119. 'language'=>$request->get("language"),
  120. 'owner'=>'',
  121. ];
  122. }
  123. }
  124. Cache::put($key, $hotMeaning, 3600);
  125. return $hotMeaning;
  126. });
  127. return $this->ok(["rows"=>$value,"count"=>count($value)]);
  128. break;
  129. default:
  130. # code...
  131. break;
  132. }
  133. if(!empty($search)){
  134. $table->where('word', 'like', $search."%")
  135. ->orWhere('word_en', 'like', $search."%")
  136. ->orWhere('meaning', 'like', "%".$search."%");
  137. }
  138. if(!empty($request->get('order')) && !empty($request->get('dir'))){
  139. $table->orderBy($request->get('order'),$request->get('dir'));
  140. }else{
  141. $table->orderBy('updated_at','desc');
  142. }
  143. $count = $table->count();
  144. if(!empty($request->get('limit'))){
  145. $offset = 0;
  146. if(!empty($request->get("offset"))){
  147. $offset = $request->get("offset");
  148. }
  149. $table->skip($offset)->take($request->get('limit'));
  150. }
  151. $result = $table->get();
  152. if($result){
  153. return $this->ok(["rows"=>$result,"count"=>$count]);
  154. }else{
  155. return $this->error("没有查询到数据");
  156. }
  157. }
  158. /**
  159. * Store a newly created resource in storage.
  160. *
  161. * @param \Illuminate\Http\Request $request
  162. * @return \Illuminate\Http\Response
  163. */
  164. public function store(Request $request)
  165. {
  166. // validate
  167. // read more on validation at http://laravel.com/docs/validation
  168. $rules = array(
  169. 'word' => 'required',
  170. 'meaning' => 'required',
  171. 'language' => 'required'
  172. );
  173. $validator = Validator::make($request->all(), $rules);
  174. // process the login
  175. if ($validator->fails()) {
  176. return $this->error($validator);
  177. }
  178. #查询重复的
  179. /*
  180. 重复判定:
  181. 一个channel下面word+tag+language 唯一
  182. */
  183. $table = DhammaTerm::where('owner', $_COOKIE["user_uid"])
  184. ->where('word',$request->get("word"))
  185. ->where('tag',$request->get("tag"));
  186. if($request->get("channel")){
  187. $isDoesntExist = $table->where('channel',$request->get("channel"))
  188. ->doesntExist();
  189. }else{
  190. $isDoesntExist = $table->where('language',$request->get("language"))
  191. ->doesntExist();
  192. }
  193. if($isDoesntExist){
  194. #不存在插入数据
  195. $term = new DhammaTerm;
  196. $term->id=app('snowflake')->id();
  197. $term->guid=Str::uuid();
  198. $term->word=$request->get("word");
  199. $term->meaning=$request->get("meaning");
  200. $term->save();
  201. return $this->ok($data);
  202. }else{
  203. return $this->error("word existed");
  204. }
  205. // store
  206. /*
  207. $data = $request->all();
  208. $data['id'] = app('snowflake')->id();
  209. $data['guid'] = Str::uuid();
  210. DhammaTerm::create($data);
  211. */
  212. }
  213. /**
  214. * Display the specified resource.
  215. *
  216. * @param string $id
  217. * @return \Illuminate\Http\Response
  218. */
  219. public function show(Request $request,$id)
  220. {
  221. //
  222. $indexCol = ['id','guid','word','meaning','other_meaning','note','language','channal','created_at','updated_at'];
  223. $result = DhammaTerm::select($indexCol)->where('guid', $id)->first();
  224. if($result){
  225. return $this->ok($result);
  226. }else{
  227. return $this->error("没有查询到数据");
  228. }
  229. }
  230. /**
  231. * Update the specified resource in storage.
  232. *
  233. * @param \Illuminate\Http\Request $request
  234. * @param \App\Models\DhammaTerm $dhammaTerm
  235. * @return \Illuminate\Http\Response
  236. */
  237. public function update(Request $request, DhammaTerm $dhammaTerm)
  238. {
  239. //
  240. }
  241. /**
  242. * Remove the specified resource from storage.
  243. *
  244. * @param \App\Models\DhammaTerm $dhammaTerm
  245. * @return \Illuminate\Http\Response
  246. */
  247. public function destroy(DhammaTerm $dhammaTerm,Request $request)
  248. {
  249. //
  250. $arrId = json_decode($request->get("id"),true) ;
  251. $count = 0;
  252. foreach ($arrId as $key => $id) {
  253. # code...
  254. $result = DhammaTerm::where('id', $id)
  255. ->where('owner', $_COOKIE["user_uid"])
  256. ->delete();
  257. if($result){
  258. $count++;
  259. }
  260. }
  261. return $this->ok($count);
  262. }
  263. }