2
0

ProgressChapterController.php 20 KB

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