ProgressChapterController.php 11 KB

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