ProgressChapterController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. namespace App\Http\Controllers;
  3. require_once __DIR__.'/../../../public/app/ucenter/function.php';
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\ProgressChapter;
  7. use App\Models\Channel;
  8. use App\Models\Tag;
  9. use App\Models\TagMap;
  10. use App\Models\PaliText;
  11. use App\Models\View;
  12. use App\Models\Like;
  13. use Illuminate\Http\Request;
  14. use App\Http\Api\StudioApi;
  15. use Illuminate\Support\Facades\Cache;
  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.owner_uid','progress_chapters.updated_at')
  188. ->leftJoin('channels','progress_chapters.channel_id', '=', 'channels.uid')
  189. ->where("book",$request->get('book'))
  190. ->where("para",$request->get('par'))
  191. ->orderBy('progress','desc')
  192. ->get();
  193. foreach ($chapters as $key => $value) {
  194. # code...
  195. $chapters[$key]->views = View::where("target_id",$value->uid)->count();
  196. $likes = Like::where("target_id",$value->uid)
  197. ->groupBy("type")
  198. ->select("type")
  199. ->selectRaw("count(*)")
  200. ->get();
  201. if(isset($_COOKIE["user_uid"])){
  202. foreach ($likes as $key1 => $like) {
  203. # 查看这些点赞里有没有我点的
  204. $myLikeId =Like::where(["target_id"=>$value->uid,
  205. 'type'=>$like->type,
  206. 'user_id'=>$_COOKIE["user_uid"]])->value('id');
  207. if($myLikeId){
  208. $likes[$key1]->selected = $myLikeId;
  209. }
  210. }
  211. }
  212. $chapters[$key]->likes = $likes;
  213. $chapters[$key]->studio = StudioApi::getById($value->owner_uid);
  214. $progress_key="/chapter_dynamic/{$value->book}/{$value->para}/ch_{$value->channel_id}";
  215. $chapters[$key]->progress_line = Cache::get($progress_key);
  216. }
  217. $all_count = count($chapters);
  218. break;
  219. case 'chapter':
  220. $tm = (new TagMap)->getTable();
  221. $pc =(new ProgressChapter)->getTable();
  222. $tg = (new Tag)->getTable();
  223. $pt = (new PaliText)->getTable();
  224. //标签过滤
  225. if($request->get('tags') && $request->get('tags')!==''){
  226. $tags = explode(',',$request->get('tags'));
  227. foreach ($tags as $tag) {
  228. # code...
  229. if(!empty($tag)){
  230. $tagNames[] = $tag;
  231. }
  232. }
  233. }
  234. if(isset($tagNames)){
  235. $where1 = " where co = ".count($tagNames);
  236. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  237. $in1 = "and t.name in ({$a})";
  238. $param = $tagNames;
  239. }else{
  240. $where1 = " ";
  241. $in1 = " ";
  242. }
  243. if($request->has('studio')){
  244. $studioId = StudioApi::getIdByName($request->get('studio'));
  245. $table = Channel::where('owner_uid',$studioId);
  246. if($request->get('public')==="true"){
  247. $table = $table->where('status',30);
  248. }
  249. $channels = $table->select('uid')->get();
  250. $arrChannel = [];
  251. foreach ($channels as $oneChannel) {
  252. # code...
  253. if(Str::isUuid($oneChannel->uid)){
  254. $arrChannel[] = "'{$oneChannel->uid}'";
  255. }
  256. }
  257. $channel = "and channel_id in (".implode(',',$arrChannel).") ";
  258. }else{
  259. if(Str::isUuid($channel_id)){
  260. $channel = "and channel_id = '{$channel_id}' ";
  261. }else{
  262. $channel = "";
  263. }
  264. }
  265. //完成度过滤
  266. $param[] = $minProgress;
  267. //语言过滤
  268. if(!empty($request->get('lang'))){
  269. $whereLang = " and pc.lang = ? ";
  270. $param[] = $request->get('lang');
  271. }else{
  272. $whereLang = " ";
  273. }
  274. //channel type过滤
  275. if($request->has('channel_type') && !empty($request->get('channel_type'))){
  276. $channel_type = "and ch.type = ? ";
  277. $param[] = $request->get('channel_type');
  278. }else{
  279. $channel_type = "";
  280. }
  281. $param_count = $param;
  282. $param[] = $offset;
  283. $query = "
  284. 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
  285. from (
  286. 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
  287. from (
  288. select uid, book,para,lang,progress,channel_id,title,summary ,created_at ,updated_at
  289. from (
  290. select anchor_id as cid
  291. from (
  292. select tm.anchor_id , count(*) as co
  293. from $tm as tm
  294. left join $tg as t on tm.tag_id = t.id
  295. where tm.table_name = 'progress_chapters'
  296. $in1
  297. group by tm.anchor_id
  298. ) T
  299. $where1
  300. ) CID
  301. left join $pc as pc on CID.cid = pc.uid
  302. where pc.progress > ?
  303. $channel $whereLang
  304. ) pcd
  305. left join channels as ch on pcd.channel_id = ch.uid
  306. where ch.status >= 30 $channel_type
  307. order by pcd.created_at desc
  308. limit {$limit} offset ?
  309. ) tpc
  310. left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
  311. $chapters = DB::select($query,$param);
  312. foreach ($chapters as $key => $chapter) {
  313. # code...
  314. $chapter->channel = Channel::where('uid',$chapter->channel_id)->select(['name','owner_uid'])->first();
  315. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  316. $chapter->views = View::where("target_id",$chapter->uid)->count();
  317. $chapter->likes = Like::where(["type"=>"like","target_id"=>$chapter->uid])->count();
  318. $chapter->tags = TagMap::where("anchor_id",$chapter->uid)
  319. ->leftJoin('tags','tag_maps.tag_id', '=', 'tags.id')
  320. ->select(['tags.id','tags.name','tags.description'])
  321. ->get();
  322. }
  323. //计算按照这个条件搜索到的总数
  324. $query = "
  325. select count(*) as count
  326. from (
  327. select *
  328. from (
  329. select anchor_id as cid
  330. from (
  331. select tm.anchor_id , count(*) as co
  332. from $tm as tm
  333. left join $tg as t on tm.tag_id = t.id
  334. where tm.table_name = 'progress_chapters'
  335. $in1
  336. group by tm.anchor_id
  337. ) T
  338. $where1
  339. ) CID
  340. left join $pc as pc on CID.cid = pc.uid
  341. where pc.progress > ?
  342. $channel $whereLang
  343. ) pcd
  344. left join channels as ch on pcd.channel_id = ch.uid
  345. where ch.status >= 30 $channel_type
  346. ";
  347. $count = DB::select($query,$param_count);
  348. $all_count = $count[0]->count;
  349. break;
  350. case 'top':
  351. break;
  352. case 'search':
  353. $key = $request->get('key');
  354. $table = ProgressChapter::where('title','like',"%{$key}%");
  355. //获取记录总条数
  356. $all_count = $table->count();
  357. //处理排序
  358. if($request->has("order") && $request->has("dir")){
  359. $table = $table->orderBy($request->get("order"),$request->get("dir"));
  360. }else{
  361. //默认排序
  362. $table = $table->orderBy('updated_at','desc');
  363. }
  364. //处理分页
  365. if($request->has("limit")){
  366. if($request->has("offset")){
  367. $offset = $request->get("offset");
  368. }else{
  369. $offset = 0;
  370. }
  371. $table = $table->skip($offset)->take($request->get("limit"));
  372. }
  373. //获取数据
  374. $chapters = $table->get();
  375. //TODO 移到resource
  376. foreach ($chapters as $key => $chapter) {
  377. # code...
  378. $chapter->toc = PaliText::where('book',$chapter->book)->where('paragraph',$chapter->para)->value('toc');
  379. $chapter->path = PaliText::where('book',$chapter->book)->where('paragraph',$chapter->para)->value('path');
  380. $chapter->channel = Channel::where('uid',$chapter->channel_id)->select(['name','owner_uid'])->first();
  381. if($chapter->channel){
  382. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  383. }else{
  384. $chapter->channel = [
  385. 'name'=>"unknown",
  386. 'owner_uid'=>"unknown",
  387. ];
  388. $chapter->studio = [
  389. 'id'=>"",
  390. 'nickName'=>"unknown",
  391. 'realName'=>"unknown",
  392. 'avatar'=>'',
  393. ];
  394. }
  395. $chapter->views = View::where("target_id",$chapter->uid)->count();
  396. $chapter->likes = Like::where(["type"=>"like","target_id"=>$chapter->uid])->count();
  397. $chapter->tags = TagMap::where("anchor_id",$chapter->uid)
  398. ->leftJoin('tags','tag_maps.tag_id', '=', 'tags.id')
  399. ->select(['tags.id','tags.name','tags.description'])
  400. ->get();
  401. }
  402. break;
  403. }
  404. if($chapters){
  405. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  406. }else{
  407. return $this->error("no data");
  408. }
  409. }
  410. /**
  411. * Show the form for creating a new resource.
  412. *
  413. * @return \Illuminate\Http\Response
  414. */
  415. public function create()
  416. {
  417. //
  418. }
  419. /**
  420. * Store a newly created resource in storage.
  421. *
  422. * @param \Illuminate\Http\Request $request
  423. * @return \Illuminate\Http\Response
  424. */
  425. public function store(Request $request)
  426. {
  427. //
  428. }
  429. /**
  430. * Display the specified resource.
  431. *
  432. * @param \App\Models\ProgressChapter $progressChapter
  433. * @return \Illuminate\Http\Response
  434. */
  435. public function show(ProgressChapter $progressChapter)
  436. {
  437. //
  438. }
  439. /**
  440. * Show the form for editing the specified resource.
  441. *
  442. * @param \App\Models\ProgressChapter $progressChapter
  443. * @return \Illuminate\Http\Response
  444. */
  445. public function edit(ProgressChapter $progressChapter)
  446. {
  447. //
  448. }
  449. /**
  450. * Update the specified resource in storage.
  451. *
  452. * @param \Illuminate\Http\Request $request
  453. * @param \App\Models\ProgressChapter $progressChapter
  454. * @return \Illuminate\Http\Response
  455. */
  456. public function update(Request $request, ProgressChapter $progressChapter)
  457. {
  458. //
  459. }
  460. /**
  461. * Remove the specified resource from storage.
  462. *
  463. * @param \App\Models\ProgressChapter $progressChapter
  464. * @return \Illuminate\Http\Response
  465. */
  466. public function destroy(ProgressChapter $progressChapter)
  467. {
  468. //
  469. }
  470. }