UserDictController.php 13 KB

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