2
0

ProgressChapterController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use App\Models\Tag;
  8. use App\Models\TagMap;
  9. use App\Models\PaliText;
  10. use App\Models\View;
  11. use App\Models\Like;
  12. use Illuminate\Http\Request;
  13. use App\Http\Api\StudioApi;
  14. use Illuminate\Support\Facades\Cache;
  15. class ProgressChapterController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function index(Request $request)
  23. {
  24. $minProgress = (float)$request->get('progress', 0.8);
  25. $offset = (int)$request->get('offset', 0);
  26. $limit = (int)$request->get('limit', 20);
  27. $channel_id = $request->get('channel');
  28. //
  29. $chapters = false;
  30. switch ($request->get('view')) {
  31. case 'ids':
  32. $aChannel = explode(',', $request->get('channel'));
  33. $chapters = ProgressChapter::select("channel_id")->selectRaw("uid as id")
  34. ->with(['channel' => function ($query) { //city对应上面province模型中定义的city方法名 闭包内是子查询
  35. return $query->select('*');
  36. }])
  37. ->where("book", $request->get('book'))
  38. ->where("para", $request->get('par'))
  39. ->whereIn('channel_id', $aChannel)->get();
  40. $all_count = count($chapters);
  41. break;
  42. case 'studio':
  43. #查询该studio的channel
  44. $name = $request->get('name');
  45. $studioId = StudioApi::getIdByName($request->get('name'));
  46. if ($studioId === false) {
  47. return $this->error('no user');
  48. }
  49. $table = Channel::where('owner_uid', $studioId);
  50. if ($request->get('public') === "true") {
  51. $table = $table->where('status', 30);
  52. }
  53. $channels = $table->select('uid')->get();
  54. $chapters = ProgressChapter::whereIn('progress_chapters.channel_id', $channels)
  55. ->leftJoin('pali_texts', function ($join) {
  56. $join->on('progress_chapters.book', '=', 'pali_texts.book');
  57. $join->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  58. })
  59. ->where('progress', '>', 0.85)
  60. ->orderby('progress_chapters.created_at', 'desc')
  61. ->skip($request->get("offset", 0))
  62. ->take($request->get("limit", 1000))
  63. ->get();
  64. $all_count = ProgressChapter::whereIn('progress_chapters.channel_id', $channels)
  65. ->where('progress', '>', 0.85)->count();
  66. break;
  67. case 'tag':
  68. $tm = (new TagMap)->getTable();
  69. $pc = (new ProgressChapter)->getTable();
  70. $t = (new Tag)->getTable();
  71. $query = "select t.name,count(*) from $tm tm
  72. join tags as t on tm.tag_id = t.id
  73. join progress_chapters as pc on tm.anchor_id = pc.uid
  74. where tm.table_name = 'progress_chapters' and
  75. pc.progress > ?
  76. group by t.name;";
  77. $chapters = DB::select($query, [$minProgress]);
  78. if ($chapters) {
  79. $all_count = count($chapters);
  80. } else {
  81. $all_count = 0;
  82. }
  83. break;
  84. case 'chapter-tag':
  85. if ($request->get('tags') && $request->get('tags') !== '') {
  86. $tags = explode(',', $request->get('tags'));
  87. foreach ($tags as $tag) {
  88. # code...
  89. if (!empty($tag)) {
  90. $tagNames[] = $tag;
  91. }
  92. }
  93. }
  94. $tm = (new TagMap)->getTable();
  95. $pc = (new ProgressChapter)->getTable();
  96. $tg = (new Tag)->getTable();
  97. $pt = (new PaliText)->getTable();
  98. $param[] = $minProgress;
  99. if (isset($tagNames)) {
  100. $where1 = " where co = " . count($tagNames);
  101. $a = implode(",", array_fill(0, count($tagNames), '?'));
  102. $in1 = "and t.name in ({$a})";
  103. $param = array_merge($param, $tagNames);
  104. } else {
  105. $where1 = " ";
  106. $in1 = " ";
  107. }
  108. if (Str::isUuid($channel_id)) {
  109. $channel = "and channel_id = '{$channel_id}' ";
  110. } else {
  111. $channel = "";
  112. }
  113. $query = "
  114. select tags.id,tags.name,co as count
  115. from (
  116. select tm.tag_id,count(*) as co from (
  117. select anchor_id as id from (
  118. select tm.anchor_id , count(*) as co
  119. from $tm as tm
  120. left join $tg as t on tm.tag_id = t.id
  121. left join $pc as pc on tm.anchor_id = pc.uid
  122. where tm.table_name = 'progress_chapters' and
  123. pc.progress > ?
  124. $in1
  125. $channel
  126. group by tm.anchor_id
  127. ) T
  128. $where1
  129. ) CID
  130. left join $tm as tm on tm.anchor_id = CID.id
  131. group by tm.tag_id
  132. ) tid
  133. left join $tg on $tg.id = tid.tag_id
  134. order by count desc
  135. ";
  136. if (isset($param)) {
  137. $chapters = DB::select($query, $param);
  138. } else {
  139. $chapters = DB::select($query);
  140. }
  141. $all_count = count($chapters);
  142. break;
  143. case 'lang':
  144. $chapters = ProgressChapter::select('lang')
  145. ->selectRaw('count(*) as count')
  146. ->where("progress", ">", $minProgress)
  147. ->groupBy('lang')
  148. ->get();
  149. $all_count = count($chapters);
  150. break;
  151. case 'channel-type':
  152. break;
  153. case 'channel':
  154. /**
  155. * 总共有多少channel
  156. */
  157. $chapters = ProgressChapter::select('channel_id')
  158. ->selectRaw('count(*) as count')
  159. ->with(['channel' => function ($query) {
  160. return $query->select('*');
  161. }])
  162. ->leftJoin('channels', 'progress_chapters.channel_id', '=', 'channels.uid')
  163. ->where("progress", ">", $minProgress)
  164. ->where('channels.status', '>=', 30);
  165. if (!empty($request->get('channel_type'))) {
  166. $chapters = $chapters->where('channels.type', $request->get('channel_type'));
  167. }
  168. if (!empty($request->get('lang'))) {
  169. $chapters = $chapters->where('progress_chapters.lang', $request->get('lang'));
  170. }
  171. $chapters = $chapters->groupBy('channel_id')
  172. ->orderBy('count', 'desc')
  173. ->get();
  174. foreach ($chapters as $key => $chapter) {
  175. $chapter->studio = StudioApi::getById($chapter->channel->owner_uid);
  176. }
  177. $all_count = count($chapters);
  178. break;
  179. case 'chapter_channels':
  180. /**
  181. * 某个章节 有多少channel
  182. */
  183. $chapters = ProgressChapter::select(
  184. 'book',
  185. 'para',
  186. 'progress_chapters.uid',
  187. 'progress_chapters.channel_id',
  188. 'progress',
  189. 'channels.name',
  190. 'channels.type',
  191. 'channels.owner_uid',
  192. 'progress_chapters.updated_at'
  193. )
  194. ->leftJoin('channels', 'progress_chapters.channel_id', '=', 'channels.uid')
  195. ->where("book", $request->get('book'))
  196. ->where("para", $request->get('par'))
  197. ->orderBy('progress', 'desc')
  198. ->get();
  199. foreach ($chapters as $key => $value) {
  200. # code...
  201. $chapters[$key]->views = View::where("target_id", $value->uid)->count();
  202. $likes = Like::where("target_id", $value->uid)
  203. ->groupBy("type")
  204. ->select("type")
  205. ->selectRaw("count(*)")
  206. ->get();
  207. if (isset($_COOKIE["user_uid"])) {
  208. foreach ($likes as $key1 => $like) {
  209. # 查看这些点赞里有没有我点的
  210. $myLikeId = Like::where([
  211. "target_id" => $value->uid,
  212. 'type' => $like->type,
  213. 'user_id' => $_COOKIE["user_uid"]
  214. ])->value('id');
  215. if ($myLikeId) {
  216. $likes[$key1]->selected = $myLikeId;
  217. }
  218. }
  219. }
  220. $chapters[$key]->likes = $likes;
  221. $chapters[$key]->studio = StudioApi::getById($value->owner_uid);
  222. $chapters[$key]->channel = ['uid' => $value->channel_id, 'name' => $value->name, 'type' => $value->type];
  223. $progress_key = "/chapter_dynamic/{$value->book}/{$value->para}/ch_{$value->channel_id}";
  224. $chapters[$key]->progress_line = Cache::get($progress_key);
  225. }
  226. $all_count = count($chapters);
  227. break;
  228. case 'chapter':
  229. $tm = (new TagMap)->getTable();
  230. $pc = (new ProgressChapter)->getTable();
  231. $tg = (new Tag)->getTable();
  232. $pt = (new PaliText)->getTable();
  233. //标签过滤
  234. if ($request->has('tags') && !empty($request->get('tags'))) {
  235. $tags = explode(',', $request->get('tags'));
  236. foreach ($tags as $tag) {
  237. # code...
  238. if (!empty($tag)) {
  239. $tagNames[] = $tag;
  240. }
  241. }
  242. }
  243. if (isset($tagNames)) {
  244. $where1 = " where co = " . count($tagNames);
  245. $a = implode(",", array_fill(0, count($tagNames), '?'));
  246. $in1 = "and t.name in ({$a})";
  247. $param = $tagNames;
  248. } else {
  249. $where1 = " ";
  250. $in1 = " ";
  251. }
  252. if ($request->has('studio')) {
  253. $studioId = StudioApi::getIdByName($request->get('studio'));
  254. $table = Channel::where('owner_uid', $studioId);
  255. if ($request->get('public') === "true") {
  256. $table = $table->where('status', 30);
  257. }
  258. $channels = $table->select('uid')->get();
  259. $arrChannel = [];
  260. foreach ($channels as $oneChannel) {
  261. # code...
  262. if (Str::isUuid($oneChannel->uid)) {
  263. $arrChannel[] = "'{$oneChannel->uid}'";
  264. }
  265. }
  266. $channel = "and channel_id in (" . implode(',', $arrChannel) . ") ";
  267. } else {
  268. if (Str::isUuid($channel_id)) {
  269. $channel = "and channel_id = '{$channel_id}' ";
  270. } else {
  271. $channel = "";
  272. }
  273. }
  274. //完成度过滤
  275. $param[] = $minProgress;
  276. //语言过滤
  277. if (!empty($request->get('lang'))) {
  278. $whereLang = " and pc.lang = ? ";
  279. $param[] = $request->get('lang');
  280. } else {
  281. $whereLang = " ";
  282. }
  283. //channel type过滤
  284. if ($request->has('channel_type') && !empty($request->get('channel_type'))) {
  285. $channel_type = "and ch.type = ? ";
  286. $param[] = $request->get('channel_type');
  287. } else {
  288. $channel_type = "";
  289. }
  290. $param_count = $param;
  291. $param[] = $offset;
  292. $query = "
  293. select tpc.pc_uid as uid, tpc.book ,tpc.para,tpc.channel_id,tpc.title,pt.toc,pt.path,tpc.progress,tpc.summary,tpc.created_at,tpc.updated_at
  294. from (
  295. select pcd.uid as pc_uid, ch.uid as ch_uid, book , para, channel_id,progress, title ,pcd.summary , pcd.created_at,pcd.updated_at
  296. from (
  297. select uid, book,para,lang,progress,channel_id,title,summary ,created_at ,updated_at
  298. from (
  299. select anchor_id as cid
  300. from (
  301. select tm.anchor_id , count(*) as co
  302. from $tm as tm
  303. left join $tg as t on tm.tag_id = t.id
  304. where tm.table_name = 'progress_chapters'
  305. $in1
  306. group by tm.anchor_id
  307. ) T
  308. $where1
  309. ) CID
  310. left join $pc as pc on CID.cid = pc.uid
  311. where pc.progress > ?
  312. $channel $whereLang
  313. ) pcd
  314. left join channels as ch on pcd.channel_id = ch.uid
  315. where ch.status >= 30 $channel_type
  316. order by pcd.created_at desc
  317. limit {$limit} offset ?
  318. ) tpc
  319. left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
  320. $chapters = DB::select($query, $param);
  321. foreach ($chapters as $key => $chapter) {
  322. # code...
  323. $chapter->channel = Channel::where('uid', $chapter->channel_id)->select(['name', 'owner_uid'])->first();
  324. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  325. $chapter->views = View::where("target_id", $chapter->uid)->count();
  326. $chapter->likes = Like::where(["type" => "like", "target_id" => $chapter->uid])->count();
  327. $chapter->tags = TagMap::where("anchor_id", $chapter->uid)
  328. ->leftJoin('tags', 'tag_maps.tag_id', '=', 'tags.id')
  329. ->select(['tags.id', 'tags.name', 'tags.description'])
  330. ->get();
  331. }
  332. //计算按照这个条件搜索到的总数
  333. $query = "
  334. select count(*) as count
  335. from (
  336. select *
  337. from (
  338. select anchor_id as cid
  339. from (
  340. select tm.anchor_id , count(*) as co
  341. from $tm as tm
  342. left join $tg as t on tm.tag_id = t.id
  343. where tm.table_name = 'progress_chapters'
  344. $in1
  345. group by tm.anchor_id
  346. ) T
  347. $where1
  348. ) CID
  349. left join $pc as pc on CID.cid = pc.uid
  350. where pc.progress > ?
  351. $channel $whereLang
  352. ) pcd
  353. left join channels as ch on pcd.channel_id = ch.uid
  354. where ch.status >= 30 $channel_type
  355. ";
  356. $count = DB::select($query, $param_count);
  357. $all_count = $count[0]->count;
  358. break;
  359. case 'top':
  360. break;
  361. case 'search':
  362. $key = $request->get('key');
  363. $table = ProgressChapter::where('title', 'like', "%{$key}%");
  364. //获取记录总条数
  365. $all_count = $table->count();
  366. //处理排序
  367. if ($request->has("order") && $request->has("dir")) {
  368. $table = $table->orderBy($request->get("order"), $request->get("dir"));
  369. } else {
  370. //默认排序
  371. $table = $table->orderBy('updated_at', 'desc');
  372. }
  373. //处理分页
  374. if ($request->has("limit")) {
  375. if ($request->has("offset")) {
  376. $offset = $request->get("offset");
  377. } else {
  378. $offset = 0;
  379. }
  380. $table = $table->skip($offset)->take($request->get("limit"));
  381. }
  382. //获取数据
  383. $chapters = $table->get();
  384. //TODO 移到resource
  385. foreach ($chapters as $key => $chapter) {
  386. # code...
  387. $chapter->toc = PaliText::where('book', $chapter->book)->where('paragraph', $chapter->para)->value('toc');
  388. $chapter->path = PaliText::where('book', $chapter->book)->where('paragraph', $chapter->para)->value('path');
  389. $chapter->channel = Channel::where('uid', $chapter->channel_id)->select(['name', 'owner_uid'])->first();
  390. if ($chapter->channel) {
  391. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  392. } else {
  393. $chapter->channel = [
  394. 'name' => "unknown",
  395. 'owner_uid' => "unknown",
  396. ];
  397. $chapter->studio = [
  398. 'id' => "",
  399. 'nickName' => "unknown",
  400. 'realName' => "unknown",
  401. 'avatar' => '',
  402. ];
  403. }
  404. $chapter->views = View::where("target_id", $chapter->uid)->count();
  405. $chapter->likes = Like::where(["type" => "like", "target_id" => $chapter->uid])->count();
  406. $chapter->tags = TagMap::where("anchor_id", $chapter->uid)
  407. ->leftJoin('tags', 'tag_maps.tag_id', '=', 'tags.id')
  408. ->select(['tags.id', 'tags.name', 'tags.description'])
  409. ->get();
  410. }
  411. break;
  412. case 'public':
  413. break;
  414. }
  415. if ($chapters) {
  416. return $this->ok(["rows" => $chapters, "count" => $all_count]);
  417. } else {
  418. return $this->error("no data");
  419. }
  420. }
  421. /**
  422. * Show the form for creating a new resource.
  423. *
  424. * @return \Illuminate\Http\Response
  425. */
  426. public function create()
  427. {
  428. //
  429. }
  430. /**
  431. * Store a newly created resource in storage.
  432. *
  433. * @param \Illuminate\Http\Request $request
  434. * @return \Illuminate\Http\Response
  435. */
  436. public function store(Request $request)
  437. {
  438. //
  439. }
  440. /**
  441. * Display the specified resource.
  442. *
  443. * @param \App\Models\ProgressChapter $progressChapter
  444. * @return \Illuminate\Http\Response
  445. */
  446. public function show(ProgressChapter $progressChapter)
  447. {
  448. //
  449. }
  450. /**
  451. * Show the form for editing the specified resource.
  452. *
  453. * @param \App\Models\ProgressChapter $progressChapter
  454. * @return \Illuminate\Http\Response
  455. */
  456. public function edit(ProgressChapter $progressChapter)
  457. {
  458. //
  459. }
  460. /**
  461. * Update the specified resource in storage.
  462. *
  463. * @param \Illuminate\Http\Request $request
  464. * @param \App\Models\ProgressChapter $progressChapter
  465. * @return \Illuminate\Http\Response
  466. */
  467. public function update(Request $request, ProgressChapter $progressChapter)
  468. {
  469. //
  470. }
  471. /**
  472. * Remove the specified resource from storage.
  473. *
  474. * @param \App\Models\ProgressChapter $progressChapter
  475. * @return \Illuminate\Http\Response
  476. */
  477. public function destroy(ProgressChapter $progressChapter)
  478. {
  479. //
  480. }
  481. }