ProgressChapterController.php 17 KB

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