| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\UserDict;
- use App\Models\DictInfo;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Log;
- use App\Http\Api;
- use App\Http\Api\AuthApi;
- use App\Http\Api\DictApi;
- use App\Http\Resources\UserDictResource;
- use Illuminate\Support\Str;
- class UserDictController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- //
- $result = false;
- $indexCol = [
- 'id',
- 'word',
- 'type',
- 'grammar',
- 'mean',
- 'parent',
- 'note',
- 'status',
- 'factors',
- 'confidence',
- 'dict_id',
- 'source',
- 'updated_at',
- 'creator_id'
- ];
- switch ($request->input('view')) {
- case 'all':
- # 获取studio内所有channel
- $user = AuthApi::current($request);
- if (!$user) {
- return $this->error(__('auth.failed'));
- }
- $table = UserDict::select($indexCol);
- break;
- case 'studio':
- # 获取studio内所有channel
- $user = AuthApi::current($request);
- if (!$user) {
- return $this->error(__('auth.failed'));
- }
- //判断当前用户是否有指定的studio的权限
- if ($user['user_uid'] !== \App\Http\Api\StudioApi::getIdByName($request->input('name'))) {
- return $this->error(__('auth.failed'));
- }
- $table = UserDict::select($indexCol)
- ->where('creator_id', $user["user_id"])
- ->whereIn('source', ["_USER_WBW_", "_USER_DICT_"]);
- break;
- case 'user':
- # code...
- $table = UserDict::select($indexCol)
- ->where('creator_id', $_COOKIE["user_id"])
- ->where('source', '<>', "_SYS_USER_WBW_");
- break;
- case 'word':
- $table = UserDict::select($indexCol)
- ->where('word', $request->input("word"));
- break;
- case 'community':
- $table = UserDict::select($indexCol)
- ->where('word', $request->input("word"))
- ->where('status', '>', 5)
- ->where(function ($query) {
- $query->where('source', "_USER_WBW_")
- ->orWhere('source', '_USER_DICT_');
- });
- break;
- case 'compound':
- $dict_id = DictApi::getSysDict('robot_compound');
- if ($dict_id === false) {
- $this->error('no robot_compound');
- }
- $table = UserDict::where("dict_id", $dict_id)->where("word", $request->input('word'));
- break;
- case 'dict':
- $dict_id = false;
- if ($request->has('name')) {
- $dict_id = DictApi::getSysDict($request->input('name'));
- } else if ($request->has('id')) {
- $dict_id = $request->input('id');
- }
- if ($dict_id === false) {
- $this->error('no dict', [], 404);
- }
- $table = UserDict::select($indexCol)
- ->where("dict_id", $dict_id);
- default:
- # code...
- break;
- }
- if ($request->has("search")) {
- $table->where('word', 'like', $request->input("search") . "%");
- }
- if (($request->has('word'))) {
- $table = $table->where('word', $request->input('word'));
- }
- if (($request->has('parent'))) {
- $table = $table->where('parent', $request->input('parent'));
- }
- if (($request->has('dict'))) {
- $dictId = DictInfo::where('shortname', $request->input('dict'))->value('id');
- if (Str::isUuid($dictId)) {
- $table = $table->where('dict_id', $dictId);
- }
- }
- $count = $table->count();
- $table->orderBy(
- $request->input('order', 'updated_at'),
- $request->input('dir', 'desc')
- );
- $table->skip($request->input('offset', 0))
- ->take($request->input('limit', 200));
- $result = $table->get();
- return $this->ok(["rows" => UserDictResource::collection($result), "count" => $count]);
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- //
- $user = AuthApi::current($request);
- if (!$user) {
- $this->error("not login");
- }
- $_data = json_decode($request->input("data"), true);
- switch ($request->input('view')) {
- case "dict":
- $src = "_USER_DICT_";
- break;
- case "wbw":
- $src = "_USER_WBW_";
- break;
- default:
- $this->error("not view");
- break;
- }
- #查询用户重复的数据
- $iOk = 0;
- $updateOk = 0;
- foreach ($_data as $key => $word) {
- # code...
- $table = UserDict::where('creator_id', $user["user_id"])
- ->where('word', $word["word"]);
- if (isset($word["type"])) {
- $table = $table->where('type', $word["type"]);
- }
- if (isset($word["grammar"])) {
- $table = $table->where('grammar', $word["grammar"]);
- }
- if (isset($word["parent"])) {
- $table = $table->where('parent', $word["parent"]);
- }
- if (isset($word["mean"])) {
- $table = $table->where('mean', $word["mean"]);
- }
- if (isset($word["factors"])) {
- $table = $table->where('factors', $word["factors"]);
- }
- $isDoesntExist = $table->doesntExist();
- if ($isDoesntExist) {
- #不存在插入数据
- $word["id"] = app('snowflake')->id();
- $word["source"] = $src;
- $word["create_time"] = time() * 1000;
- $word["creator_id"] = $user["user_id"];
- $id = UserDict::insert($word);
- if (isset($word["status"]) && $word["status"] > 5) {
- $updateOk = $this->update_sys_wbw($word);
- } else {
- $updateOk = true;
- }
- $this->update_redis($word);
- $iOk++;
- } else {
- //存在,修改数据
- $origin = $table->first();
- if (isset($word["note"])) {
- $origin->note = $word["note"];
- }
- if (isset($word["confidence"])) {
- $origin->confidence = $word["confidence"];
- }
- $origin->save();
- }
- }
- return $this->ok([$iOk, $updateOk]);
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- $result = UserDict::find($id);
- if ($result) {
- return $this->ok($result);
- } else {
- return $this->error("没有查询到数据");
- }
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- //
- $newData = $request->all();
- $result = UserDict::where('id', $id)
- ->update($newData);
- if ($result) {
- $updateOk = $this->update_sys_wbw($newData);
- $this->update_redis($newData);
- return $this->ok([$result, $updateOk]);
- } else {
- return $this->error("没有查询到数据");
- }
- }
- /**
- * Remove the specified resource from storage.
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy(Request $request, $id)
- {
- //
- $user = AuthApi::current($request);
- if (!$user) {
- return $this->error(__('auth.failed'), [], 403);
- }
- $user_id = $user['user_id'];
- if ($request->has("id")) {
- $arrId = json_decode($request->input("id"), true);
- $count = 0;
- $updateOk = false;
- foreach ($arrId as $key => $id) {
- # 找到对应数据
- $data = UserDict::find($id);
- //查看是否有权限删除
- if ($data->creator_id == $user_id) {
- $result = UserDict::where('id', $id)
- ->delete();
- $count += $result;
- $updateOk = $this->update_sys_wbw($data);
- $this->update_redis($data);
- }
- }
- return $this->ok([$count, $updateOk]);
- } else {
- //删除单个单词
- $userDict = UserDict::find($id);
- //判断当前用户是否有指定的studio的权限
- if ((int)$user_id !== $userDict->creator_id) {
- return $this->error(__('auth.failed'));
- }
- $delete = $userDict->delete();
- return $this->ok($delete);
- }
- }
- public function delete(Request $request)
- {
- $arrId = json_decode($request->input("id"), true);
- $count = 0;
- $updateOk = false;
- foreach ($arrId as $key => $id) {
- $data = UserDict::where('id', $id)->first();
- if ($data) {
- # 找到对应数据
- $param = [
- "id" => $id,
- 'creator_id' => $_COOKIE["user_id"]
- ];
- $del = UserDict::where($param)->delete();
- $count += $del;
- $updateOk = $this->update_sys_wbw($data);
- $this->update_redis($data);
- }
- }
- return $this->ok(['deleted' => $count]);
- }
- /*
- 更新系统wbw汇总表
- */
- private function update_sys_wbw($data)
- {
- #查询用户重复的数据
- if (!isset($data["type"])) {
- $data["type"] = null;
- }
- if (!isset($data["grammar"])) {
- $data["grammar"] = null;
- }
- if (!isset($data["parent"])) {
- $data["parent"] = null;
- }
- if (!isset($data["mean"])) {
- $data["mean"] = null;
- }
- if (!isset($data["factors"])) {
- $data["factors"] = null;
- }
- if (!isset($data["factormean"])) {
- $data["factormean"] = null;
- }
- $count = UserDict::where('word', $data["word"])
- ->where('type', $data["type"])
- ->where('grammar', $data["grammar"])
- ->where('parent', $data["parent"])
- ->where('mean', $data["mean"])
- ->where('factors', $data["factors"])
- ->where('factormean', $data["factormean"])
- ->where('source', $data["source"])
- ->count();
- if ($count === 0) {
- # 没有任何用户有这个数据
- #删除数据
- $result = UserDict::where('word', $data["word"])
- ->where('type', $data["type"])
- ->where('grammar', $data["grammar"])
- ->where('parent', $data["parent"])
- ->where('mean', $data["mean"])
- ->where('factors', $data["factors"])
- ->where('factormean', $data["factormean"])
- ->where('source', '_SYS_USER_WBW_')
- ->delete();
- return ($result);
- } else {
- #更新或新增
- #查询最早上传这个数据的用户
- $creator_id = UserDict::where('word', $data["word"])
- ->where('type', $data["type"])
- ->where('grammar', $data["grammar"])
- ->where('parent', $data["parent"])
- ->where('mean', $data["mean"])
- ->where('factors', $data["factors"])
- ->where('factormean', $data["factormean"])
- ->whereIn('source', ['_USER_WBW_', '_USER_DICT_'])
- ->orderby("created_at", 'asc')
- ->value("creator_id");
- $count = UserDict::where('word', $data["word"])
- ->where('type', $data["type"])
- ->where('grammar', $data["grammar"])
- ->where('parent', $data["parent"])
- ->where('mean', $data["mean"])
- ->where('factors', $data["factors"])
- ->where('factormean', $data["factormean"])
- ->where('source', '_SYS_USER_WBW_')
- ->count();
- if ($count === 0) {
- #社区字典没有 新增
- $result = UserDict::insert(
- [
- 'id' => app('snowflake')->id(),
- 'word' => $data["word"],
- 'type' => $data["type"],
- 'grammar' => $data["grammar"],
- 'parent' => $data["parent"],
- 'mean' => $data["mean"],
- 'factors' => $data["factors"],
- 'factormean' => $data["factormean"],
- 'language' => $data["language"],
- 'source' => "_SYS_USER_WBW_",
- 'creator_id' => $data["creator_id"],
- 'ref_counter' => 1,
- 'dict_id' => DictApi::getSysDict('community_extract'),
- "create_time" => time() * 1000
- ]
- );
- } else {
- #有,更新
- $result = UserDict::where('word', $data["word"])
- ->where('type', $data["type"])
- ->where('grammar', $data["grammar"])
- ->where('parent', $data["parent"])
- ->where('mean', $data["mean"])
- ->where('factors', $data["factors"])
- ->where('factormean', $data["factormean"])
- ->where('source', '_SYS_USER_WBW_')
- ->update(
- [
- 'creator_id' => $creator_id,
- 'ref_counter' => $count
- ]
- );
- }
- return ($result);
- }
- }
- private function update_redis($word)
- {
- #更新 redis
- $Fetch = UserDict::where(['word' => $word['word'], "source" => "_USER_WBW_"])->get();
- $redisWord = array();
- foreach ($Fetch as $one) {
- # code...
- $redisWord[] = array(
- $one["id"],
- $one["word"],
- $one["type"],
- $one["grammar"],
- $one["parent"],
- $one["mean"],
- $one["note"],
- $one["factors"],
- $one["factormean"],
- $one["status"],
- $one["confidence"],
- $one["creator_id"],
- $one["source"],
- $one["language"]
- );
- }
- $redisData = json_encode($redisWord, JSON_UNESCAPED_UNICODE);
- Redis::hSet("dict/user", $word['word'], $redisData);
- $redisData1 = Redis::hGet("dict/user", $word['word']);
- #更新redis结束
- }
- }
|