2
0

ProgressChapterController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. break;
  123. case 'channel-type':
  124. break;
  125. case 'channel':
  126. break;
  127. case 'chapter':
  128. $tm = (new TagMap)->getTable();
  129. $pc =(new ProgressChapter)->getTable();
  130. $tg = (new Tag)->getTable();
  131. $pt = (new PaliText)->getTable();
  132. if($request->get('tags') && $request->get('tags')!==''){
  133. $tags = explode(',',$request->get('tags'));
  134. foreach ($tags as $tag) {
  135. # code...
  136. if(!empty($tag)){
  137. $tagNames[] = $tag;
  138. }
  139. }
  140. }
  141. if(isset($tagNames)){
  142. $where1 = " where co = ".count($tagNames);
  143. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  144. $in1 = "and t.name in ({$a})";
  145. $param = $tagNames;
  146. }else{
  147. $where1 = " ";
  148. $in1 = " ";
  149. }
  150. $param[] = $minProgress;
  151. $param[] = $offset;
  152. $query = "
  153. select tpc.book ,tpc.para,tpc.channel_id,tpc.title,pt.toc,pt.path,tpc.progress,tpc.created_at,tpc.updated_at
  154. from (
  155. select * from (
  156. select anchor_id as cid from (
  157. select tm.anchor_id , count(*) as co
  158. from $tm as tm
  159. left join $tg as t on tm.tag_id = t.id
  160. where tm.table_name = 'progress_chapters'
  161. $in1
  162. group by tm.anchor_id
  163. ) T
  164. $where1
  165. ) CID
  166. left join $pc as pc on CID.cid = pc.uid
  167. where pc.progress > ?
  168. order by created_at desc
  169. limit 20 offset ?
  170. ) tpc
  171. left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
  172. $chapters = DB::select($query,$param);
  173. foreach ($chapters as $key => $value) {
  174. # code...
  175. $chapters[$key]->channel_info = Channel::where('uid',$value->channel_id)->select(['name','owner_uid'])->first();
  176. }
  177. $all_count = 10;
  178. break;
  179. }
  180. if($chapters){
  181. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  182. }else{
  183. return $this->error("no data");
  184. }
  185. }
  186. /**
  187. * Show the form for creating a new resource.
  188. *
  189. * @return \Illuminate\Http\Response
  190. */
  191. public function create()
  192. {
  193. //
  194. }
  195. /**
  196. * Store a newly created resource in storage.
  197. *
  198. * @param \Illuminate\Http\Request $request
  199. * @return \Illuminate\Http\Response
  200. */
  201. public function store(Request $request)
  202. {
  203. //
  204. }
  205. /**
  206. * Display the specified resource.
  207. *
  208. * @param \App\Models\ProgressChapter $progressChapter
  209. * @return \Illuminate\Http\Response
  210. */
  211. public function show(ProgressChapter $progressChapter)
  212. {
  213. //
  214. }
  215. /**
  216. * Show the form for editing the specified resource.
  217. *
  218. * @param \App\Models\ProgressChapter $progressChapter
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function edit(ProgressChapter $progressChapter)
  222. {
  223. //
  224. }
  225. /**
  226. * Update the specified resource in storage.
  227. *
  228. * @param \Illuminate\Http\Request $request
  229. * @param \App\Models\ProgressChapter $progressChapter
  230. * @return \Illuminate\Http\Response
  231. */
  232. public function update(Request $request, ProgressChapter $progressChapter)
  233. {
  234. //
  235. }
  236. /**
  237. * Remove the specified resource from storage.
  238. *
  239. * @param \App\Models\ProgressChapter $progressChapter
  240. * @return \Illuminate\Http\Response
  241. */
  242. public function destroy(ProgressChapter $progressChapter)
  243. {
  244. //
  245. }
  246. }