2
0

UserDictController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\DictApi;
  4. use App\Http\Api\StudioApi;
  5. use App\Http\Resources\UserDictResource;
  6. use App\Models\DictInfo;
  7. use App\Models\UserDict;
  8. use App\Services\AuthService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Support\Facades\Redis;
  12. use Illuminate\Support\Str;
  13. class UserDictController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return 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 = AuthService::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 = AuthService::current($request);
  52. if (! $user) {
  53. return $this->error(__('auth.failed'));
  54. }
  55. // 判断当前用户是否有指定的studio的权限
  56. if ($user['user_uid'] !== 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. } elseif ($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. * @return Response
  134. */
  135. public function store(Request $request)
  136. {
  137. //
  138. $user = AuthService::current($request);
  139. if (! $user) {
  140. $this->error('not login');
  141. }
  142. $_data = json_decode($request->input('data'), true);
  143. switch ($request->input('view')) {
  144. case 'dict':
  145. $src = '_USER_DICT_';
  146. break;
  147. case 'wbw':
  148. $src = '_USER_WBW_';
  149. break;
  150. default:
  151. $this->error('not view');
  152. break;
  153. }
  154. // 查询用户重复的数据
  155. $iOk = 0;
  156. $updateOk = 0;
  157. foreach ($_data as $key => $word) {
  158. // code...
  159. $table = UserDict::where('creator_id', $user['user_id'])
  160. ->where('word', $word['word']);
  161. if (isset($word['type'])) {
  162. $table = $table->where('type', $word['type']);
  163. }
  164. if (isset($word['grammar'])) {
  165. $table = $table->where('grammar', $word['grammar']);
  166. }
  167. if (isset($word['parent'])) {
  168. $table = $table->where('parent', $word['parent']);
  169. }
  170. if (isset($word['mean'])) {
  171. $table = $table->where('mean', $word['mean']);
  172. }
  173. if (isset($word['factors'])) {
  174. $table = $table->where('factors', $word['factors']);
  175. }
  176. $isDoesntExist = $table->doesntExist();
  177. if ($isDoesntExist) {
  178. // 不存在插入数据
  179. $word['id'] = app('snowflake')->id();
  180. $word['source'] = $src;
  181. $word['create_time'] = time() * 1000;
  182. $word['creator_id'] = $user['user_id'];
  183. $id = UserDict::insert($word);
  184. if (isset($word['status']) && $word['status'] > 5) {
  185. $updateOk = $this->update_sys_wbw($word);
  186. } else {
  187. $updateOk = true;
  188. }
  189. $this->update_redis($word);
  190. $iOk++;
  191. } else {
  192. // 存在,修改数据
  193. $origin = $table->first();
  194. if (isset($word['note'])) {
  195. $origin->note = $word['note'];
  196. }
  197. if (isset($word['confidence'])) {
  198. $origin->confidence = $word['confidence'];
  199. }
  200. $origin->save();
  201. }
  202. }
  203. return $this->ok([$iOk, $updateOk]);
  204. }
  205. /**
  206. * Display the specified resource.
  207. *
  208. * @param int $id
  209. * @return Response
  210. */
  211. public function show($id)
  212. {
  213. //
  214. $result = UserDict::find($id);
  215. if ($result) {
  216. return $this->ok($result);
  217. } else {
  218. return $this->error('没有查询到数据');
  219. }
  220. }
  221. /**
  222. * Update the specified resource in storage.
  223. *
  224. * @param int $id
  225. * @return Response
  226. */
  227. public function update(Request $request, $id)
  228. {
  229. //
  230. $newData = $request->all();
  231. $result = UserDict::where('id', $id)
  232. ->update($newData);
  233. if ($result) {
  234. $updateOk = $this->update_sys_wbw($newData);
  235. $this->update_redis($newData);
  236. return $this->ok([$result, $updateOk]);
  237. } else {
  238. return $this->error('没有查询到数据');
  239. }
  240. }
  241. /**
  242. * Remove the specified resource from storage.
  243. *
  244. * @param int $id
  245. * @return Response
  246. */
  247. public function destroy(Request $request, $id)
  248. {
  249. //
  250. $user = AuthService::current($request);
  251. if (! $user) {
  252. return $this->error(__('auth.failed'), [], 403);
  253. }
  254. $user_id = $user['user_id'];
  255. if ($request->has('id')) {
  256. $arrId = json_decode($request->input('id'), true);
  257. $count = 0;
  258. $updateOk = false;
  259. foreach ($arrId as $key => $id) {
  260. // 找到对应数据
  261. $data = UserDict::find($id);
  262. // 查看是否有权限删除
  263. if ($data->creator_id == $user_id) {
  264. $result = UserDict::where('id', $id)
  265. ->delete();
  266. $count += $result;
  267. $updateOk = $this->update_sys_wbw($data);
  268. $this->update_redis($data);
  269. }
  270. }
  271. return $this->ok([$count, $updateOk]);
  272. } else {
  273. // 删除单个单词
  274. $userDict = UserDict::find($id);
  275. // 判断当前用户是否有指定的studio的权限
  276. if ((int) $user_id !== $userDict->creator_id) {
  277. return $this->error(__('auth.failed'));
  278. }
  279. $delete = $userDict->delete();
  280. return $this->ok($delete);
  281. }
  282. }
  283. public function delete(Request $request)
  284. {
  285. $arrId = json_decode($request->input('id'), true);
  286. $count = 0;
  287. $updateOk = false;
  288. foreach ($arrId as $key => $id) {
  289. $data = UserDict::where('id', $id)->first();
  290. if ($data) {
  291. // 找到对应数据
  292. $param = [
  293. 'id' => $id,
  294. 'creator_id' => $_COOKIE['user_id'],
  295. ];
  296. $del = UserDict::where($param)->delete();
  297. $count += $del;
  298. $updateOk = $this->update_sys_wbw($data);
  299. $this->update_redis($data);
  300. }
  301. }
  302. return $this->ok(['deleted' => $count]);
  303. }
  304. /*
  305. 更新系统wbw汇总表
  306. */
  307. private function update_sys_wbw($data)
  308. {
  309. // 查询用户重复的数据
  310. if (! isset($data['type'])) {
  311. $data['type'] = null;
  312. }
  313. if (! isset($data['grammar'])) {
  314. $data['grammar'] = null;
  315. }
  316. if (! isset($data['parent'])) {
  317. $data['parent'] = null;
  318. }
  319. if (! isset($data['mean'])) {
  320. $data['mean'] = null;
  321. }
  322. if (! isset($data['factors'])) {
  323. $data['factors'] = null;
  324. }
  325. if (! isset($data['factormean'])) {
  326. $data['factormean'] = null;
  327. }
  328. $count = UserDict::where('word', $data['word'])
  329. ->where('type', $data['type'])
  330. ->where('grammar', $data['grammar'])
  331. ->where('parent', $data['parent'])
  332. ->where('mean', $data['mean'])
  333. ->where('factors', $data['factors'])
  334. ->where('factormean', $data['factormean'])
  335. ->where('source', $data['source'])
  336. ->count();
  337. if ($count === 0) {
  338. // 没有任何用户有这个数据
  339. // 删除数据
  340. $result = UserDict::where('word', $data['word'])
  341. ->where('type', $data['type'])
  342. ->where('grammar', $data['grammar'])
  343. ->where('parent', $data['parent'])
  344. ->where('mean', $data['mean'])
  345. ->where('factors', $data['factors'])
  346. ->where('factormean', $data['factormean'])
  347. ->where('source', '_SYS_USER_WBW_')
  348. ->delete();
  349. return $result;
  350. } else {
  351. // 更新或新增
  352. // 查询最早上传这个数据的用户
  353. $creator_id = UserDict::where('word', $data['word'])
  354. ->where('type', $data['type'])
  355. ->where('grammar', $data['grammar'])
  356. ->where('parent', $data['parent'])
  357. ->where('mean', $data['mean'])
  358. ->where('factors', $data['factors'])
  359. ->where('factormean', $data['factormean'])
  360. ->whereIn('source', ['_USER_WBW_', '_USER_DICT_'])
  361. ->orderby('created_at', 'asc')
  362. ->value('creator_id');
  363. $count = UserDict::where('word', $data['word'])
  364. ->where('type', $data['type'])
  365. ->where('grammar', $data['grammar'])
  366. ->where('parent', $data['parent'])
  367. ->where('mean', $data['mean'])
  368. ->where('factors', $data['factors'])
  369. ->where('factormean', $data['factormean'])
  370. ->where('source', '_SYS_USER_WBW_')
  371. ->count();
  372. if ($count === 0) {
  373. // 社区字典没有 新增
  374. $result = UserDict::insert(
  375. [
  376. 'id' => app('snowflake')->id(),
  377. 'word' => $data['word'],
  378. 'type' => $data['type'],
  379. 'grammar' => $data['grammar'],
  380. 'parent' => $data['parent'],
  381. 'mean' => $data['mean'],
  382. 'factors' => $data['factors'],
  383. 'factormean' => $data['factormean'],
  384. 'language' => $data['language'],
  385. 'source' => '_SYS_USER_WBW_',
  386. 'creator_id' => $data['creator_id'],
  387. 'ref_counter' => 1,
  388. 'dict_id' => DictApi::getSysDict('community_extract'),
  389. 'create_time' => time() * 1000,
  390. ]
  391. );
  392. } else {
  393. // 有,更新
  394. $result = UserDict::where('word', $data['word'])
  395. ->where('type', $data['type'])
  396. ->where('grammar', $data['grammar'])
  397. ->where('parent', $data['parent'])
  398. ->where('mean', $data['mean'])
  399. ->where('factors', $data['factors'])
  400. ->where('factormean', $data['factormean'])
  401. ->where('source', '_SYS_USER_WBW_')
  402. ->update(
  403. [
  404. 'creator_id' => $creator_id,
  405. 'ref_counter' => $count,
  406. ]
  407. );
  408. }
  409. return $result;
  410. }
  411. }
  412. private function update_redis($word)
  413. {
  414. // 更新 redis
  415. $Fetch = UserDict::where(['word' => $word['word'], 'source' => '_USER_WBW_'])->get();
  416. $redisWord = [];
  417. foreach ($Fetch as $one) {
  418. // code...
  419. $redisWord[] = [
  420. $one['id'],
  421. $one['word'],
  422. $one['type'],
  423. $one['grammar'],
  424. $one['parent'],
  425. $one['mean'],
  426. $one['note'],
  427. $one['factors'],
  428. $one['factormean'],
  429. $one['status'],
  430. $one['confidence'],
  431. $one['creator_id'],
  432. $one['source'],
  433. $one['language'],
  434. ];
  435. }
  436. $redisData = json_encode($redisWord, JSON_UNESCAPED_UNICODE);
  437. Redis::hSet('dict/user', $word['word'], $redisData);
  438. $redisData1 = Redis::hGet('dict/user', $word['word']);
  439. // 更新redis结束
  440. }
  441. }