UserDictController.php 16 KB

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