ProgressChapterController.php 17 KB

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