2
0
Эх сурвалжийг харах

:sparkles: 查询 tag chapter chapter-tag

visuddhinanda@gmail.com 3 жил өмнө
parent
commit
9706856d21

+ 143 - 33
app/Http/Controllers/ProgressChapterController.php

@@ -2,8 +2,12 @@
 
 namespace App\Http\Controllers;
 
+use Illuminate\Support\Facades\DB;
 use App\Models\ProgressChapter;
 use App\Models\Channel;
+use App\Models\Tag;
+use App\Models\TagMap;
+use App\Models\PaliText;
 use Illuminate\Http\Request;
 
 class ProgressChapterController extends Controller
@@ -15,16 +19,19 @@ class ProgressChapterController extends Controller
      */
     public function index(Request $request)
     {
+        
+        if($request->get('progress')){
+            $minProgress = (float)$request->get('progress');
+        }else{
+            $minProgress = 0.8;
+        }
+        if($request->get('offset')){
+            $offset = (int)$request->get('offset');
+        }else{
+            $offset = 0;
+        }
         //
-        $selectCol = ['progress_chapters.book',
-                      'progress_chapters.para',
-                      'progress_chapters.channel_id',
-                      'progress_chapters.title',
-                      'pali_texts.toc',
-                      'progress',
-                      'progress_chapters.created_at',
-                      'progress_chapters.updated_at'
-                      ];
+
         $chapters=false;
         switch ($request->get('view')) {
 			case 'studio':
@@ -47,38 +54,141 @@ class ProgressChapterController extends Controller
                                         ->get();
                 break;
             case 'tag':
-                $aChannel = [67,68,69,70];
-                $chapters = ProgressChapter::select($selectCol)
-                                        ->whereIn('progress_chapters.book', $aChannel)
-                                        ->leftJoin('pali_texts', function($join)
-                                                {
-                                                    $join->on('progress_chapters.book', '=', 'pali_texts.book');
-                                                    $join->on('progress_chapters.para','=','pali_texts.paragraph');
-                                                })
-                                        ->orderby('progress','desc')
-                                        ->get();
+                $tm = (new TagMap)->getTable();
+                $pc =(new ProgressChapter)->getTable();
+                $t = (new Tag)->getTable();            
+                $query = "select t.name,count(*) from $tm  tm
+                            join tags as t on tm.tag_id = t.id
+                            join progress_chapters as pc on tm.anchor_id = pc.uid
+                            where tm.table_name  = 'progress_chapters' and 
+                            pc.progress > ?
+                            group by t.name;";
+                $chapters = DB::select($query, [$minProgress]);
+                if($chapters){
+                    $all_count = count($chapters);
+                }else{
+                    $all_count = 0;
+                }
                 break;
-            case 'done':
-                $chapters = ProgressChapter::select($selectCol)
-                                        ->where('progress','>',0.85)
-                                        ->leftJoin('pali_texts', function($join)
-                                            {
-                                                $join->on('progress_chapters.book', '=', 'pali_texts.book');
-                                                $join->on('progress_chapters.para','=','pali_texts.paragraph');
-                                            })
-                                        ->orderby('progress_chapters.created_at','desc')
-                                        ->skip(0)->take(20)
-                                        ->get();
+            case 'chapter-tag':
+                $tm = (new TagMap)->getTable();
+                $pc =(new ProgressChapter)->getTable();
+                $tg = (new Tag)->getTable();     
+                $pt = (new PaliText)->getTable();  
+                if($request->get('tags') && $request->get('tags')!==''){
+                    $tags = explode(',',$request->get('tags'));
+                    foreach ($tags as $tag) {
+                        # code...
+                        if(!empty($tag)){
+                            $tagNames[] = $tag;
+                        }
+                    }
+                }
+                
+                $param[] = $minProgress;
+                if(isset($tagNames)){
+                    $where1 = " where co = ".count($tagNames);
+                    $a = implode(",",array_fill(0, count($tagNames), '?')) ;
+                    $in1 = "and t.name in ({$a})";
+                    $param  = array_merge($param, $tagNames);
+                }else{
+                    $where1 = " ";
+                    $in1 = " ";
+                }
+                $query = "
+                    select tags.id,tags.name,co as count 
+                        from (
+                            select tm.tag_id,count(*) as co from (
+                                select anchor_id as id from (
+                                    select tm.anchor_id , count(*) as co
+                                        from $tm as  tm
+                                        left join $tg as t on tm.tag_id = t.id
+                                        left join $pc as pc on tm.anchor_id = pc.uid
+                                        where tm.table_name  = 'progress_chapters' and 
+                                              pc.progress  > ? 
+                                        $in1
+                                        group by tm.anchor_id
+                                ) T 
+                                    $where1
+                            ) CID 
+                            left join $tm as tm on tm.anchor_id = CID.id 
+                            group by tm.tag_id
+                        ) tid
+                        left join $tg on $tg.id = tid.tag_id
+                        order by count desc
+                    ";
+                    if(isset($param)){
+                        $chapters = DB::select($query,$param);
+                    }else{
+                        $chapters = DB::select($query);
+                    }
+                    $all_count = count($chapters);
+                break;
+            case 'lang':
+                break;
+            case 'channel-type':
+                break;
+            case 'channel':
+                break;
+            case 'chapter':
+                $tm = (new TagMap)->getTable();
+                $pc =(new ProgressChapter)->getTable();
+                $tg = (new Tag)->getTable();     
+                $pt = (new PaliText)->getTable();  
+                if($request->get('tags') && $request->get('tags')!==''){
+                    $tags = explode(',',$request->get('tags'));
+                    foreach ($tags as $tag) {
+                        # code...
+                        if(!empty($tag)){
+                            $tagNames[] = $tag;
+                        }
+                    }
+                }
+                
+                
+                if(isset($tagNames)){
+                    $where1 = " where co = ".count($tagNames);
+                    $a = implode(",",array_fill(0, count($tagNames), '?')) ;
+                    $in1 = "and t.name in ({$a})";
+                    $param = $tagNames;
+                }else{
+                    $where1 = " ";
+                    $in1 = " ";
+                }
+                $param[] = $minProgress;
+                $param[] = $offset;
+                $query = "
+                select tpc.book ,tpc.para,tpc.channel_id,tpc.title,pt.toc,pt.path,tpc.progress,tpc.created_at,tpc.updated_at 
+                    from (
+                        select * from (
+                            select anchor_id as cid from (
+                                select tm.anchor_id , count(*) as co 
+                                    from $tm as  tm
+                                    left join $tg as t on tm.tag_id = t.id
+                                    where tm.table_name  = 'progress_chapters'  
+                                    $in1
+                                    group by tm.anchor_id
+                            ) T 
+                                $where1 
+                        ) CID 
+                        left join $pc as pc on CID.cid = pc.uid 
+                        where pc.progress > ?
+                        order by created_at desc
+                        limit 20 offset ?
+                    ) tpc 
+                    left join $pt as pt on tpc.book = pt.book and tpc.para = pt.paragraph;";
+                $chapters = DB::select($query,$param);
                 foreach ($chapters as $key => $value) {
                     # code...
-                    $chapters[$key]['channel_info'] = Channel::where('uid',$value->channel_id)->select(['name','owner_uid'])->first();
+                    $chapters[$key]->channel_info = Channel::where('uid',$value->channel_id)->select(['name','owner_uid'])->first();
                 }
+                $all_count = 10;
                 break;
         }
         if($chapters){
-            return $this->ok(["rows"=>$chapters,"count"=>count($chapters)]);
+            return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
         }else{
-            return $this->error("没有查询到数据");
+            return $this->error("no data");
         }
     }