2
0

ProgressChapterController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. $userinfo = new \UserInfo();
  47. $studio = $userinfo->getUserByName($name);
  48. if($studio == false){
  49. return $this->error('no user');
  50. }
  51. $channels = Channel::where('owner_uid',$studio['userid'])->select('uid')->get();
  52. $aChannel = [];
  53. foreach ($channels as $channel) {
  54. # code...
  55. $aChannel[] = $channel->uid;
  56. }
  57. $chapters = ProgressChapter::whereIn('progress_chapters.channel_id', $aChannel)
  58. ->leftJoin('pali_texts', function($join)
  59. {
  60. $join->on('progress_chapters.book', '=', 'pali_texts.book');
  61. $join->on('progress_chapters.para','=','pali_texts.paragraph');
  62. })
  63. ->where('progress','>',0.85)
  64. ->orderby('progress_chapters.created_at','desc')
  65. ->get();
  66. $all_count = ProgressChapter::whereIn('progress_chapters.channel_id', $aChannel)
  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','progress_chapters.channel_id','progress','updated_at')
  186. ->with(['channel' => function($query) {
  187. return $query->select('*');
  188. }])
  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->channel->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(Str::isUuid($channel_id)){
  244. $channel = "and channel_id = '{$channel_id}' ";
  245. }else{
  246. $channel = "";
  247. }
  248. //完成度过滤
  249. $param[] = $minProgress;
  250. //语言过滤
  251. if(!empty($request->get('lang'))){
  252. $whereLang = " and pc.lang = ? ";
  253. $param[] = $request->get('lang');
  254. }else{
  255. $whereLang = " ";
  256. }
  257. //channel type过滤
  258. if($request->has('channel_type') && !empty($request->get('channel_type'))){
  259. $channel_type = "and ch.type = ? ";
  260. $param[] = $request->get('channel_type');
  261. }else{
  262. $channel_type = "";
  263. }
  264. $param_count = $param;
  265. $param[] = $offset;
  266. $query = "
  267. 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
  268. from (
  269. 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
  270. from (
  271. select uid, book,para,lang,progress,channel_id,title,summary ,created_at ,updated_at
  272. from (
  273. select anchor_id as cid
  274. from (
  275. select tm.anchor_id , count(*) as co
  276. from $tm as tm
  277. left join $tg as t on tm.tag_id = t.id
  278. where tm.table_name = 'progress_chapters'
  279. $in1
  280. group by tm.anchor_id
  281. ) T
  282. $where1
  283. ) CID
  284. left join $pc as pc on CID.cid = pc.uid
  285. where pc.progress > ?
  286. $channel $whereLang
  287. ) pcd
  288. left join channels as ch on pcd.channel_id = ch.uid
  289. where ch.status >= 30 $channel_type
  290. order by pcd.created_at desc
  291. limit {$limit} offset ?
  292. ) tpc
  293. left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
  294. $chapters = DB::select($query,$param);
  295. foreach ($chapters as $key => $chapter) {
  296. # code...
  297. $chapter->channel = Channel::where('uid',$chapter->channel_id)->select(['name','owner_uid'])->first();
  298. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  299. $chapter->views = View::where("target_id",$chapter->uid)->count();
  300. $chapter->likes = Like::where(["type"=>"like","target_id"=>$chapter->uid])->count();
  301. $chapter->tags = TagMap::where("anchor_id",$chapter->uid)
  302. ->leftJoin('tags','tag_maps.tag_id', '=', 'tags.id')
  303. ->select(['tags.id','tags.name','tags.description'])
  304. ->get();
  305. }
  306. //计算按照这个条件搜索到的总数
  307. $query = "
  308. select count(*) as count
  309. from (
  310. select *
  311. from (
  312. select anchor_id as cid
  313. from (
  314. select tm.anchor_id , count(*) as co
  315. from $tm as tm
  316. left join $tg as t on tm.tag_id = t.id
  317. where tm.table_name = 'progress_chapters'
  318. $in1
  319. group by tm.anchor_id
  320. ) T
  321. $where1
  322. ) CID
  323. left join $pc as pc on CID.cid = pc.uid
  324. where pc.progress > ?
  325. $channel $whereLang
  326. ) pcd
  327. left join channels as ch on pcd.channel_id = ch.uid
  328. where ch.status >= 30 $channel_type
  329. ";
  330. $count = DB::select($query,$param_count);
  331. $all_count = $count[0]->count;
  332. break;
  333. case 'top':
  334. break;
  335. case 'search':
  336. $key = $request->get('key');
  337. $table = ProgressChapter::where('title','like',"%{$key}%");
  338. //获取记录总条数
  339. $all_count = $table->count();
  340. //处理排序
  341. if($request->has("order") && $request->has("dir")){
  342. $table = $table->orderBy($request->get("order"),$request->get("dir"));
  343. }else{
  344. //默认排序
  345. $table = $table->orderBy('updated_at','desc');
  346. }
  347. //处理分页
  348. if($request->has("limit")){
  349. if($request->has("offset")){
  350. $offset = $request->get("offset");
  351. }else{
  352. $offset = 0;
  353. }
  354. $table = $table->skip($offset)->take($request->get("limit"));
  355. }
  356. //获取数据
  357. $chapters = $table->get();
  358. //TODO 移到resource
  359. foreach ($chapters as $key => $chapter) {
  360. # code...
  361. $chapter->toc = PaliText::where('book',$chapter->book)->where('paragraph',$chapter->para)->value('toc');
  362. $chapter->path = PaliText::where('book',$chapter->book)->where('paragraph',$chapter->para)->value('path');
  363. $chapter->channel = Channel::where('uid',$chapter->channel_id)->select(['name','owner_uid'])->first();
  364. if($chapter->channel){
  365. $chapter->studio = StudioApi::getById($chapter->channel["owner_uid"]);
  366. }else{
  367. $chapter->channel = [
  368. 'name'=>"unknown",
  369. 'owner_uid'=>"unknown",
  370. ];
  371. $chapter->studio = [
  372. 'id'=>"",
  373. 'nickName'=>"unknown",
  374. 'realName'=>"unknown",
  375. 'avatar'=>'',
  376. ];
  377. }
  378. $chapter->views = View::where("target_id",$chapter->uid)->count();
  379. $chapter->likes = Like::where(["type"=>"like","target_id"=>$chapter->uid])->count();
  380. $chapter->tags = TagMap::where("anchor_id",$chapter->uid)
  381. ->leftJoin('tags','tag_maps.tag_id', '=', 'tags.id')
  382. ->select(['tags.id','tags.name','tags.description'])
  383. ->get();
  384. }
  385. break;
  386. }
  387. if($chapters){
  388. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  389. }else{
  390. return $this->error("no data");
  391. }
  392. }
  393. /**
  394. * Show the form for creating a new resource.
  395. *
  396. * @return \Illuminate\Http\Response
  397. */
  398. public function create()
  399. {
  400. //
  401. }
  402. /**
  403. * Store a newly created resource in storage.
  404. *
  405. * @param \Illuminate\Http\Request $request
  406. * @return \Illuminate\Http\Response
  407. */
  408. public function store(Request $request)
  409. {
  410. //
  411. }
  412. /**
  413. * Display the specified resource.
  414. *
  415. * @param \App\Models\ProgressChapter $progressChapter
  416. * @return \Illuminate\Http\Response
  417. */
  418. public function show(ProgressChapter $progressChapter)
  419. {
  420. //
  421. }
  422. /**
  423. * Show the form for editing the specified resource.
  424. *
  425. * @param \App\Models\ProgressChapter $progressChapter
  426. * @return \Illuminate\Http\Response
  427. */
  428. public function edit(ProgressChapter $progressChapter)
  429. {
  430. //
  431. }
  432. /**
  433. * Update the specified resource in storage.
  434. *
  435. * @param \Illuminate\Http\Request $request
  436. * @param \App\Models\ProgressChapter $progressChapter
  437. * @return \Illuminate\Http\Response
  438. */
  439. public function update(Request $request, ProgressChapter $progressChapter)
  440. {
  441. //
  442. }
  443. /**
  444. * Remove the specified resource from storage.
  445. *
  446. * @param \App\Models\ProgressChapter $progressChapter
  447. * @return \Illuminate\Http\Response
  448. */
  449. public function destroy(ProgressChapter $progressChapter)
  450. {
  451. //
  452. }
  453. }