ProgressChapterController.php 21 KB

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