2
0

ProgressChapterController.php 17 KB

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