2
0

ProgressChapterController.php 16 KB

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