ProgressChapterController.php 15 KB

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