ProgressChapterController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Models\ProgressChapter;
  5. use App\Models\Channel;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. use App\Models\PaliText;
  9. use App\Models\View;
  10. use App\Models\Like;
  11. use Illuminate\Http\Request;
  12. class ProgressChapterController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index(Request $request)
  20. {
  21. if($request->get('progress')){
  22. $minProgress = (float)$request->get('progress');
  23. }else{
  24. $minProgress = 0.8;
  25. }
  26. if($request->get('offset')){
  27. $offset = (int)$request->get('offset');
  28. }else{
  29. $offset = 0;
  30. }
  31. //
  32. $chapters=false;
  33. switch ($request->get('view')) {
  34. case 'ids':
  35. $aChannel = explode(',',$request->get('channel'));
  36. $chapters = ProgressChapter::select("channel_id")->selectRaw("uid as id")
  37. ->with(['channel' => function($query) { //city对应上面province模型中定义的city方法名 闭包内是子查询
  38. return $query->select('*');
  39. }])
  40. ->where("book",$request->get('book'))
  41. ->where("para",$request->get('par'))
  42. ->whereIn('channel_id', $aChannel)->get();
  43. $all_count = count($chapters);
  44. break;
  45. case 'studio':
  46. #查询该studio的channel
  47. $channels = Channel::where('owner_uid',$request->get('id'))->select('uid')->get();
  48. $aChannel = [];
  49. foreach ($channels as $channel) {
  50. # code...
  51. $aChannel[] = $channel->uid;
  52. }
  53. $chapters = ProgressChapter::select($selectCol)
  54. ->whereIn('progress_chapters.channel_id', $aChannel)
  55. ->leftJoin('pali_texts', function($join)
  56. {
  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. ->get();
  63. break;
  64. case 'tag':
  65. $tm = (new TagMap)->getTable();
  66. $pc =(new ProgressChapter)->getTable();
  67. $t = (new Tag)->getTable();
  68. $query = "select t.name,count(*) from $tm tm
  69. join tags as t on tm.tag_id = t.id
  70. join progress_chapters as pc on tm.anchor_id = pc.uid
  71. where tm.table_name = 'progress_chapters' and
  72. pc.progress > ?
  73. group by t.name;";
  74. $chapters = DB::select($query, [$minProgress]);
  75. if($chapters){
  76. $all_count = count($chapters);
  77. }else{
  78. $all_count = 0;
  79. }
  80. break;
  81. case 'chapter-tag':
  82. $tm = (new TagMap)->getTable();
  83. $pc =(new ProgressChapter)->getTable();
  84. $tg = (new Tag)->getTable();
  85. $pt = (new PaliText)->getTable();
  86. if($request->get('tags') && $request->get('tags')!==''){
  87. $tags = explode(',',$request->get('tags'));
  88. foreach ($tags as $tag) {
  89. # code...
  90. if(!empty($tag)){
  91. $tagNames[] = $tag;
  92. }
  93. }
  94. }
  95. $param[] = $minProgress;
  96. if(isset($tagNames)){
  97. $where1 = " where co = ".count($tagNames);
  98. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  99. $in1 = "and t.name in ({$a})";
  100. $param = array_merge($param, $tagNames);
  101. }else{
  102. $where1 = " ";
  103. $in1 = " ";
  104. }
  105. $query = "
  106. select tags.id,tags.name,co as count
  107. from (
  108. select tm.tag_id,count(*) as co from (
  109. select anchor_id as id from (
  110. select tm.anchor_id , count(*) as co
  111. from $tm as tm
  112. left join $tg as t on tm.tag_id = t.id
  113. left join $pc as pc on tm.anchor_id = pc.uid
  114. where tm.table_name = 'progress_chapters' and
  115. pc.progress > ?
  116. $in1
  117. group by tm.anchor_id
  118. ) T
  119. $where1
  120. ) CID
  121. left join $tm as tm on tm.anchor_id = CID.id
  122. group by tm.tag_id
  123. ) tid
  124. left join $tg on $tg.id = tid.tag_id
  125. order by count desc
  126. ";
  127. if(isset($param)){
  128. $chapters = DB::select($query,$param);
  129. }else{
  130. $chapters = DB::select($query);
  131. }
  132. $all_count = count($chapters);
  133. break;
  134. case 'lang':
  135. $chapters = ProgressChapter::select('lang')
  136. ->selectRaw('count(*) as count')
  137. ->where("progress",">",$minProgress)
  138. ->groupBy('lang')
  139. ->get();
  140. $all_count = count($chapters);
  141. break;
  142. case 'channel-type':
  143. break;
  144. case 'channel':
  145. /*
  146. 总共有多少channel
  147. */
  148. $chapters = ProgressChapter::select('channel_id')
  149. ->selectRaw('count(*) as count')
  150. ->with(['channel' => function($query) { //city对应上面province模型中定义的city方法名 闭包内是子查询
  151. return $query->select('*');
  152. }])
  153. ->where("progress",">",$minProgress)
  154. ->groupBy('channel_id')
  155. ->orderBy('count','desc')
  156. ->get();
  157. $all_count = count($chapters);
  158. break;
  159. case 'chapter_channels':
  160. /*
  161. 某个章节 有多少channel
  162. */
  163. $chapters = ProgressChapter::select('book','para','progress_chapters.uid','progress_chapters.channel_id','progress','updated_at')
  164. ->with(['channel' => function($query) {
  165. return $query->select('*');
  166. }])
  167. ->where("book",$request->get('book'))
  168. ->where("para",$request->get('par'))
  169. ->orderBy('progress','desc')
  170. ->get();
  171. foreach ($chapters as $key => $value) {
  172. # code...
  173. $chapters[$key]->views = View::where("target_id",$value->uid)->count();
  174. $likes = Like::where("target_id",$value->uid)
  175. ->groupBy("type")
  176. ->select("type")
  177. ->selectRaw("count(*)")
  178. ->get();
  179. if(isset($_COOKIE["user_uid"])){
  180. foreach ($likes as $key1 => $like) {
  181. # 查看这些点赞里有没有我点的
  182. $myLikeId =Like::where(["target_id"=>$value->uid,
  183. 'type'=>$like->type,
  184. 'user_id'=>$_COOKIE["user_uid"]])->value('id');
  185. if($myLikeId){
  186. $likes[$key1]->selected = $myLikeId;
  187. }
  188. }
  189. }
  190. $chapters[$key]->likes = $likes;
  191. }
  192. $all_count = count($chapters);
  193. break;
  194. case 'chapter':
  195. $tm = (new TagMap)->getTable();
  196. $pc =(new ProgressChapter)->getTable();
  197. $tg = (new Tag)->getTable();
  198. $pt = (new PaliText)->getTable();
  199. if($request->get('tags') && $request->get('tags')!==''){
  200. $tags = explode(',',$request->get('tags'));
  201. foreach ($tags as $tag) {
  202. # code...
  203. if(!empty($tag)){
  204. $tagNames[] = $tag;
  205. }
  206. }
  207. }
  208. if(isset($tagNames)){
  209. $where1 = " where co = ".count($tagNames);
  210. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  211. $in1 = "and t.name in ({$a})";
  212. $param = $tagNames;
  213. }else{
  214. $where1 = " ";
  215. $in1 = " ";
  216. }
  217. $param[] = $minProgress;
  218. $param[] = $offset;
  219. $query = "
  220. select tpc.uid, tpc.book ,tpc.para,tpc.channel_id,tpc.title,pt.toc,pt.path,tpc.progress,tpc.summary,tpc.created_at,tpc.updated_at
  221. from (
  222. select * from (
  223. select anchor_id as cid from (
  224. select tm.anchor_id , count(*) as co
  225. from $tm as tm
  226. left join $tg as t on tm.tag_id = t.id
  227. where tm.table_name = 'progress_chapters'
  228. $in1
  229. group by tm.anchor_id
  230. ) T
  231. $where1
  232. ) CID
  233. left join $pc as pc on CID.cid = pc.uid
  234. where pc.progress > ?
  235. order by created_at desc
  236. limit 20 offset ?
  237. ) tpc
  238. left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
  239. $chapters = DB::select($query,$param);
  240. foreach ($chapters as $key => $value) {
  241. # code...
  242. $chapters[$key]->channel = Channel::where('uid',$value->channel_id)->select(['name','owner_uid'])->first();
  243. $chapters[$key]->views = View::where("target_id",$value->uid)->count();
  244. $chapters[$key]->likes = Like::where(["type"=>"like","target_id"=>$value->uid])->count();
  245. $chapters[$key]->tags = TagMap::where("anchor_id",$value->uid)
  246. ->leftJoin('tags','tag_maps.tag_id', '=', 'tags.id')
  247. ->select(['tags.id','tags.name','tags.description'])
  248. ->get();
  249. }
  250. $all_count = 10;
  251. break;
  252. }
  253. if($chapters){
  254. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  255. }else{
  256. return $this->error("no data");
  257. }
  258. }
  259. /**
  260. * Show the form for creating a new resource.
  261. *
  262. * @return \Illuminate\Http\Response
  263. */
  264. public function create()
  265. {
  266. //
  267. }
  268. /**
  269. * Store a newly created resource in storage.
  270. *
  271. * @param \Illuminate\Http\Request $request
  272. * @return \Illuminate\Http\Response
  273. */
  274. public function store(Request $request)
  275. {
  276. //
  277. }
  278. /**
  279. * Display the specified resource.
  280. *
  281. * @param \App\Models\ProgressChapter $progressChapter
  282. * @return \Illuminate\Http\Response
  283. */
  284. public function show(ProgressChapter $progressChapter)
  285. {
  286. //
  287. }
  288. /**
  289. * Show the form for editing the specified resource.
  290. *
  291. * @param \App\Models\ProgressChapter $progressChapter
  292. * @return \Illuminate\Http\Response
  293. */
  294. public function edit(ProgressChapter $progressChapter)
  295. {
  296. //
  297. }
  298. /**
  299. * Update the specified resource in storage.
  300. *
  301. * @param \Illuminate\Http\Request $request
  302. * @param \App\Models\ProgressChapter $progressChapter
  303. * @return \Illuminate\Http\Response
  304. */
  305. public function update(Request $request, ProgressChapter $progressChapter)
  306. {
  307. //
  308. }
  309. /**
  310. * Remove the specified resource from storage.
  311. *
  312. * @param \App\Models\ProgressChapter $progressChapter
  313. * @return \Illuminate\Http\Response
  314. */
  315. public function destroy(ProgressChapter $progressChapter)
  316. {
  317. //
  318. }
  319. }