UserDictController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Http\Api;
  8. use App\Http\Api\AuthApi;
  9. use App\Http\Api\DictApi;
  10. use App\Http\Resources\UserDictResource;
  11. class UserDictController 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. //
  21. $result=false;
  22. $indexCol = ['id','word','type','grammar','mean','parent','note','factors','confidence','dict_id','updated_at','creator_id'];
  23. switch ($request->get('view')) {
  24. case 'all':
  25. # 获取studio内所有channel
  26. $user = AuthApi::current($request);
  27. if(!$user){
  28. return $this->error(__('auth.failed'));
  29. }
  30. $table = UserDict::select($indexCol);
  31. break;
  32. case 'studio':
  33. # 获取studio内所有channel
  34. $user = AuthApi::current($request);
  35. if(!$user){
  36. return $this->error(__('auth.failed'));
  37. }
  38. //判断当前用户是否有指定的studio的权限
  39. if($user['user_uid'] !== \App\Http\Api\StudioApi::getIdByName($request->get('name'))){
  40. return $this->error(__('auth.failed'));
  41. }
  42. $table = UserDict::select($indexCol)
  43. ->where('creator_id', $user["user_id"])
  44. ->whereIn('source', ["_USER_WBW_","_USER_DICT_"]);
  45. break;
  46. case 'user':
  47. # code...
  48. $table = UserDict::select($indexCol)
  49. ->where('creator_id', $_COOKIE["user_id"])
  50. ->where('source', '<>', "_SYS_USER_WBW_");
  51. break;
  52. case 'word':
  53. $table = UserDict::select($indexCol)
  54. ->where('word', $request->get("word"));
  55. break;
  56. case 'community':
  57. $table = UserDict::select($indexCol)
  58. ->where('word', $request->get("word"))
  59. ->where('source', "_USER_WBW_");;
  60. break;
  61. case 'compound':
  62. $dict_id = DictApi::getSysDict('robot_compound');
  63. if($dict_id===false){
  64. $this->error('no robot_compound');
  65. }
  66. $table = UserDict::where("dict_id",$dict_id)->where("word",$request->get('word'));
  67. break;
  68. case 'dict':
  69. $dict_id = false;
  70. if($request->has('name')){
  71. $dict_id = DictApi::getSysDict($request->get('name'));
  72. }else if($request->has('id')){
  73. $dict_id = $request->get('id');
  74. }
  75. if($dict_id===false){
  76. $this->error('no dict',[],404);
  77. }
  78. $table = UserDict::select($indexCol)
  79. ->where("dict_id",$dict_id);
  80. default:
  81. # code...
  82. break;
  83. }
  84. if($request->has("search")){
  85. $table->where('word', 'like', $request->get("search")."%");
  86. }
  87. $count = $table->count();
  88. $table->orderBy($request->get('order','updated_at'),
  89. $request->get('dir','desc'));
  90. $table->skip($request->get('offset',0))
  91. ->take($request->get('limit',200));
  92. $result = $table->get();
  93. return $this->ok(["rows"=>UserDictResource::collection($result),"count"=>$count]);
  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. //
  104. $user = AuthApi::current($request);
  105. if(!$user){
  106. $this->error("not login");
  107. }
  108. $_data = json_decode($request->get("data"),true);
  109. switch($request->get('view')){
  110. case "dict":
  111. $src = "_USER_DICT_";
  112. break;
  113. case "wbw":
  114. $src = "_USER_WBW_";
  115. break;
  116. default:
  117. $this->error("not view");
  118. break;
  119. }
  120. #查询用户重复的数据
  121. $iOk = 0;
  122. $updateOk=0;
  123. foreach ($_data as $key => $word) {
  124. # code...
  125. $table = UserDict::where('creator_id', $user["user_id"])
  126. ->where('word',$word["word"]);
  127. if(isset($word["type"])){$table = $table->where('type',$word["type"]);}
  128. if(isset($word["grammar"])){$table = $table->where('grammar',$word["grammar"]);}
  129. if(isset($word["parent"])){$table = $table->where('parent',$word["parent"]);}
  130. if(isset($word["mean"])){$table = $table->where('mean',$word["mean"]);}
  131. if(isset($word["factors"])){$table = $table->where('factors',$word["factors"]);}
  132. $isDoesntExist = $table->doesntExist();
  133. if($isDoesntExist){
  134. #不存在插入数据
  135. $word["id"]=app('snowflake')->id();
  136. $word["source"] = $src;
  137. $word["create_time"] = time()*1000;
  138. $word["creator_id"]=$user["user_id"];
  139. $id = UserDict::insert($word);
  140. $updateOk = $this->update_sys_wbw($word);
  141. $this->update_redis($word);
  142. $iOk++;
  143. }
  144. }
  145. return $this->ok([$iOk,$updateOk]);
  146. }
  147. /**
  148. * Display the specified resource.
  149. *
  150. * @param int $id
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function show($id)
  154. {
  155. //
  156. $result = UserDict::find($id);
  157. if($result){
  158. return $this->ok($result);
  159. }else{
  160. return $this->error("没有查询到数据");
  161. }
  162. }
  163. /**
  164. * Update the specified resource in storage.
  165. *
  166. * @param \Illuminate\Http\Request $request
  167. * @param int $id
  168. * @return \Illuminate\Http\Response
  169. */
  170. public function update(Request $request, $id)
  171. {
  172. //
  173. $newData = $request->all();
  174. $result = UserDict::where('id', $id)
  175. ->update($newData);
  176. if($result){
  177. $updateOk = $this->update_sys_wbw($newData);
  178. $this->update_redis($newData);
  179. return $this->ok([$result,$updateOk]);
  180. }else{
  181. return $this->error("没有查询到数据");
  182. }
  183. }
  184. /**
  185. * Remove the specified resource from storage.
  186. * @param \Illuminate\Http\Request $request
  187. * @param int $id
  188. * @return \Illuminate\Http\Response
  189. */
  190. public function destroy(Request $request,$id)
  191. {
  192. //
  193. $user = AuthApi::current($request);
  194. if(!$user){
  195. return $this->error(__('auth.failed'),[],403);
  196. }
  197. $user_id = $user['user_id'];
  198. if($request->has("id")){
  199. $arrId = json_decode($request->get("id"),true) ;
  200. $count = 0;
  201. $updateOk = false;
  202. foreach ($arrId as $key => $id) {
  203. # 找到对应数据
  204. $data = UserDict::find($id);
  205. //查看是否有权限删除
  206. if($data->creator_id == $user_id){
  207. $result = UserDict::where('id', $id)
  208. ->delete();
  209. $count += $result;
  210. $updateOk = $this->update_sys_wbw($data);
  211. $this->update_redis($data);
  212. }
  213. }
  214. return $this->ok([$count,$updateOk]);
  215. }else{
  216. //删除单个单词
  217. $userDict = UserDict::find($id);
  218. //判断当前用户是否有指定的studio的权限
  219. if((int)$user_id !== $userDict->creator_id){
  220. return $this->error(__('auth.failed'));
  221. }
  222. $delete = $userDict->delete();
  223. return $this->ok($delete);
  224. }
  225. }
  226. public function delete(Request $request){
  227. $arrId = json_decode($request->get("id"),true) ;
  228. $count = 0;
  229. $updateOk = false;
  230. foreach ($arrId as $key => $id) {
  231. $data = UserDict::where('id',$id)->first();
  232. if($data){
  233. # 找到对应数据
  234. $param = [
  235. "id"=>$id,
  236. 'creator_id'=>$_COOKIE["user_id"]
  237. ];
  238. $del = UserDict::where($param)->delete();
  239. $count += $del;
  240. $updateOk = $this->update_sys_wbw($data);
  241. $this->update_redis($data);
  242. }
  243. }
  244. return $this->ok(['deleted'=>$count]);
  245. }
  246. /*
  247. 更新系统wbw汇总表
  248. */
  249. private function update_sys_wbw($data){
  250. #查询用户重复的数据
  251. if(!isset($data["type"])){$data["type"]=null;}
  252. if(!isset($data["grammar"])){$data["grammar"]=null;}
  253. if(!isset($data["parent"])){$data["parent"]=null;}
  254. if(!isset($data["mean"])){$data["mean"]=null;}
  255. if(!isset($data["factors"])){$data["factors"]=null;}
  256. if(!isset($data["factormean"])){$data["factormean"]=null;}
  257. $count = UserDict::where('word',$data["word"])
  258. ->where('type',$data["type"])
  259. ->where('grammar',$data["grammar"])
  260. ->where('parent',$data["parent"])
  261. ->where('mean',$data["mean"])
  262. ->where('factors',$data["factors"])
  263. ->where('factormean',$data["factormean"])
  264. ->where('source',$data["source"])
  265. ->count();
  266. if($count === 0){
  267. # 没有任何用户有这个数据
  268. #删除数据
  269. $result = UserDict::where('word',$data["word"])
  270. ->where('type',$data["type"])
  271. ->where('grammar',$data["grammar"])
  272. ->where('parent',$data["parent"])
  273. ->where('mean',$data["mean"])
  274. ->where('factors',$data["factors"])
  275. ->where('factormean',$data["factormean"])
  276. ->where('source','_SYS_USER_WBW_')
  277. ->delete();
  278. return($result);
  279. }else{
  280. #更新或新增
  281. #查询最早上传这个数据的用户
  282. $creator_id = UserDict::where('word',$data["word"])
  283. ->where('type',$data["type"])
  284. ->where('grammar',$data["grammar"])
  285. ->where('parent',$data["parent"])
  286. ->where('mean',$data["mean"])
  287. ->where('factors',$data["factors"])
  288. ->where('factormean',$data["factormean"])
  289. ->whereIn('source',['_USER_WBW_','_USER_DICT_'])
  290. ->orderby("created_at",'asc')
  291. ->value("creator_id");
  292. $count = UserDict::where('word',$data["word"])
  293. ->where('type',$data["type"])
  294. ->where('grammar',$data["grammar"])
  295. ->where('parent',$data["parent"])
  296. ->where('mean',$data["mean"])
  297. ->where('factors',$data["factors"])
  298. ->where('factormean',$data["factormean"])
  299. ->where('source','_SYS_USER_WBW_')
  300. ->count();
  301. if($count === 0){
  302. #社区字典没有 新增
  303. $result = UserDict::insert(
  304. [
  305. 'id' =>app('snowflake')->id(),
  306. 'word'=>$data["word"],
  307. 'type'=>$data["type"],
  308. 'grammar'=>$data["grammar"],
  309. 'parent'=>$data["parent"],
  310. 'mean'=>$data["mean"],
  311. 'factors'=>$data["factors"],
  312. 'factormean'=>$data["factormean"],
  313. 'source'=>"_SYS_USER_WBW_",
  314. 'creator_id' => $data["creator_id"],
  315. 'ref_counter' => 1,
  316. 'dict_id' => DictApi::getSysDict('community_extract'),
  317. "create_time"=>time()*1000
  318. ]);
  319. }else{
  320. #有,更新
  321. $result = UserDict::where('word',$data["word"])
  322. ->where('type',$data["type"])
  323. ->where('grammar',$data["grammar"])
  324. ->where('parent',$data["parent"])
  325. ->where('mean',$data["mean"])
  326. ->where('factors',$data["factors"])
  327. ->where('factormean',$data["factormean"])
  328. ->where('source','_SYS_USER_WBW_')
  329. ->update(
  330. [
  331. 'creator_id'=>$creator_id,
  332. 'ref_counter'=>$count
  333. ]);
  334. }
  335. return($result);
  336. }
  337. }
  338. private function update_redis($word){
  339. #更新 redis
  340. $Fetch = UserDict::where(['word'=>$word['word'],"source"=>"_USER_WBW_"])->get();
  341. $redisWord=array();
  342. foreach ($Fetch as $one) {
  343. # code...
  344. $redisWord[] = array(
  345. $one["id"],
  346. $one["word"],
  347. $one["type"],
  348. $one["grammar"],
  349. $one["parent"],
  350. $one["mean"],
  351. $one["note"],
  352. $one["factors"],
  353. $one["factormean"],
  354. $one["status"],
  355. $one["confidence"],
  356. $one["creator_id"],
  357. $one["source"],
  358. $one["language"]
  359. );
  360. }
  361. $redisData = json_encode($redisWord,JSON_UNESCAPED_UNICODE);
  362. Redis::hSet("dict/user",$word['word'],$redisData);
  363. $redisData1 = Redis::hGet("dict/user",$word['word']);
  364. #更新redis结束
  365. }
  366. }