ProgressChapterController.php 21 KB

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