ProgressChapterController.php 19 KB

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