ProgressChapterController.php 11 KB

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