UserDictController.php 10 KB

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