visuddhinanda hace 10 meses
padre
commit
ce675e9c0b
Se han modificado 1 ficheros con 129 adiciones y 145 borrados
  1. 129 145
      api-v8/app/Http/Controllers/SentenceInfoController.php

+ 129 - 145
api-v8/app/Http/Controllers/SentenceInfoController.php

@@ -7,9 +7,8 @@ use App\Models\PaliSentence;
 use App\Models\PaliText;
 
 use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Cache;
+use App\Http\Resources\SentResource;
 use App\Tools\RedisClusters;
-use Illuminate\Support\Facades\DB;
 
 class SentenceInfoController extends Controller
 {
@@ -20,20 +19,7 @@ class SentenceInfoController extends Controller
      *
      * @return \Illuminate\Http\Response
      */
-    public function index()
-    {
-        //
-    }
-
-    /**
-     * Show the form for creating a new resource.
-     *
-     * @return \Illuminate\Http\Response
-     */
-    public function create()
-    {
-        //
-    }
+    public function index(Request $request) {}
 
     /**
      * Store a newly created resource in storage.
@@ -46,12 +32,13 @@ class SentenceInfoController extends Controller
         //
     }
 
-    private function getSentProgress(Request $request,$date=''){
+    private function getSentProgress(Request $request, $date = '')
+    {
         $channel = $request->get('channel');
         $from = $request->get('from');
-        if ($request->has('to')){
+        if ($request->has('to')) {
             $to = $request->get('to');
-        }else{
+        } else {
             $to = $this->_endParagraph;
         }
 
@@ -62,82 +49,83 @@ class SentenceInfoController extends Controller
         # page
         # percent
         $view = 'strlen';
-        if($request->has('view')){
-            $view =$request->get('view');
-        }else if($request->has('type')){
-            $view =$request->get('type');
+        if ($request->has('view')) {
+            $view = $request->get('view');
+        } else if ($request->has('type')) {
+            $view = $request->get('type');
         }
 
 
         #一页书中的字符数
         $pageStrLen = 2000;
-        if($request->has('strlen')){
-            $pageStrLen =$request->get('strlen');
+        if ($request->has('strlen')) {
+            $pageStrLen = $request->get('strlen');
         }
-        if($request->has('pagelen')){
+        if ($request->has('pagelen')) {
             $pageStrLen = $request->get('pagelen');
         }
 
         # 页数
         $pageNumber = 300;
-        if($request->has('pages')){
-            $pageNumber =$request->get('pages');
+        if ($request->has('pages')) {
+            $pageNumber = $request->get('pages');
         }
 
-        $db = Sentence::where('sentences.channel_uid',$request->get('channel'))
-                    ->where('sentences.book_id','>=',$request->get('book'))
-                    ->where('sentences.paragraph','>=',$request->get('from'))
-                    ->where('sentences.paragraph','<=',$to);
-        if($view==="palistrlen"){
-            $db = $db->leftJoin('pali_texts', function($join)
-                    {
-                        $join->on('sentences.book_id', '=', 'pali_texts.book');
-                        $join->on('sentences.paragraph','=','pali_texts.paragraph');
-                    });
+        $db = Sentence::where('sentences.channel_uid', $request->get('channel'))
+            ->where('sentences.book_id', '>=', $request->get('book'))
+            ->where('sentences.paragraph', '>=', $request->get('from'))
+            ->where('sentences.paragraph', '<=', $to);
+        if ($view === "palistrlen") {
+            $db = $db->leftJoin('pali_texts', function ($join) {
+                $join->on('sentences.book_id', '=', 'pali_texts.book');
+                $join->on('sentences.paragraph', '=', 'pali_texts.paragraph');
+            });
         }
-        if(!empty($date)){
-            $db = $db->whereDate('sentences.created_at','=',$date);
+        if (!empty($date)) {
+            $db = $db->whereDate('sentences.created_at', '=', $date);
         }
-        if($view==="palistrlen"){
+        if ($view === "palistrlen") {
             return $db->sum('pali_texts.lenght');
         }
         $strlen = $db->sum('sentences.strlen');
 
-        if(is_null($strlen) || $strlen===0){
+        if (is_null($strlen) || $strlen === 0) {
             return 0;
         }
         #计算已完成百分比
         $percent = 0;
-        if(($view==='page' && !empty($request->get('pages'))) || $view==='percent' ){
+        if (($view === 'page' && !empty($request->get('pages'))) || $view === 'percent') {
             #计算完成的句子在巴利语句子表中的字符串长度百分比
-            $db = Sentence::select(['book_id','paragraph','word_start','word_end'])
-                ->where('channel_uid',$request->get('channel'))
-                ->where('book_id','>=',$request->get('book'))
-                ->where('paragraph','>=',$request->get('from'))
-                ->where('paragraph','<=',$to);
-            if(!empty($date)){
-                $db = $db->whereDate('created_at','=',$date);
+            $db = Sentence::select(['book_id', 'paragraph', 'word_start', 'word_end'])
+                ->where('channel_uid', $request->get('channel'))
+                ->where('book_id', '>=', $request->get('book'))
+                ->where('paragraph', '>=', $request->get('from'))
+                ->where('paragraph', '<=', $to);
+            if (!empty($date)) {
+                $db = $db->whereDate('created_at', '=', $date);
             }
             $sentFinished = $db->get();
             #查询这些句子的总共等效巴利语字符数
-            $allStrLen = PaliSentence::where('book',$request->get('book'))
-                            ->where('paragraph','>=',$request->get('from'))
-                            ->where('paragraph','<=',$to)
-                            ->sum('length');
+            $allStrLen = PaliSentence::where('book', $request->get('book'))
+                ->where('paragraph', '>=', $request->get('from'))
+                ->where('paragraph', '<=', $to)
+                ->sum('length');
             $para_strlen = 0;
 
             foreach ($sentFinished as $sent) {
                 # code...
-				$key_sent_id = $sent->book_id.'-'.$sent->paragraph.'-'.$sent->word_start.'-'.$sent->word_end;
-				$para_strlen += RedisClusters::remember('pali-sent/strlen/'.$key_sent_id,
-                                    config('mint.cache.expire') ,
-                                    function() use($sent) {
-                                        return PaliSentence::where('book',$sent->book_id)
-                                                ->where('paragraph',$sent->paragraph)
-                                                ->where('word_begin',$sent->word_start)
-                                                ->where('word_end',$sent->word_end)
-                                                ->value('length');
-				});
+                $key_sent_id = $sent->book_id . '-' . $sent->paragraph . '-' . $sent->word_start . '-' . $sent->word_end;
+                $para_strlen += RedisClusters::remember(
+                    'pali-sent/strlen/' . $key_sent_id,
+                    config('mint.cache.expire'),
+                    function () use ($sent) {
+                        return PaliSentence::where('book', $sent->book_id)
+                            ->where('paragraph', $sent->paragraph)
+                            ->where('word_begin', $sent->word_start)
+                            ->where('word_end', $sent->word_end)
+                            ->value('length');
+                    }
+                );
             }
 
             $percent = $para_strlen / $allStrLen;
@@ -145,16 +133,16 @@ class SentenceInfoController extends Controller
         switch ($view) {
             case 'page':
                 # 输出已经完成的页数
-                if(!empty($request->get('pages'))){
+                if (!empty($request->get('pages'))) {
                     #给了页码,用百分比计算
                     $resulte = $percent * $request->get('pages');
-                }else{
+                } else {
                     #没给页码,用每页字符数计算
                     $resulte = $strlen / $pageStrLen;
                 }
                 break;
             case 'percent': //百分比
-                $resulte = sprintf('%.2f',$percent);
+                $resulte = sprintf('%.2f', $percent);
                 break;
             case 'strlen':
             default:
@@ -163,11 +151,11 @@ class SentenceInfoController extends Controller
                 break;
         }
         #保留小数点后两位
-        $resulte = sprintf('%.2f',$resulte);
+        $resulte = sprintf('%.2f', $resulte);
         return $resulte;
     }
     /**
-    * 输出一张图片显示进度
+     * 输出一张图片显示进度
      * Display the specified resource.
      *
      * @param  \App\Models\Sentence  $sentence
@@ -182,7 +170,7 @@ class SentenceInfoController extends Controller
         switch ($request->get('view')) {
             case 'percent':
                 # code...
-                $resulte = $resulte*100;
+                $resulte = $resulte * 100;
                 $svg .= "<rect id='frontground' x='0' y='0' width='100' height='25' fill='#cccccc' ></rect>";
                 $svg .= "<text id='bg_text'  x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte%</text>";
                 $svg .= "<rect id='background' x='0' y='0' width='100' height='25' fill='#006600' clip-path='url(#textClipPath)'></rect>";
@@ -199,8 +187,8 @@ class SentenceInfoController extends Controller
         }
         $svg .= "</svg>";
 
-        return response($svg,200,[
-            'Content-Type'=>'image/svg+xml'
+        return response($svg, 200, [
+            'Content-Type' => 'image/svg+xml'
         ]);
     }
 
@@ -222,50 +210,50 @@ class SentenceInfoController extends Controller
         # page
         # percent
         $view = 'strlen';
-        if($request->has('view')){
-            $view =$request->get('view');
+        if ($request->has('view')) {
+            $view = $request->get('view');
         }
-        if($request->has('type')){
-            $view =$request->get('type');
+        if ($request->has('type')) {
+            $view = $request->get('type');
         }
 
 
 
-        $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
-        $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
+        $pagePix = ($imgHeight - $xAxisOffset) / $maxPage;
+        $dayPix = ($imgWidth - $yAxisOffset) / $maxDay;
 
         ob_clean();
         ob_start();
         $channel = $request->get('channel');
         $from = $request->get('from');
-        if ($request->has('to')){
+        if ($request->has('to')) {
             $to = $request->get('to');
-        }else{
-            $chapterLen = PaliText::where('book',$request->get('book'))->where('paragraph',$from)->value('chapter_len');
+        } else {
+            $chapterLen = PaliText::where('book', $request->get('book'))->where('paragraph', $from)->value('chapter_len');
             $to =  $from + $chapterLen - 1;
             $this->_endParagraph = $to;
         }
 
-        $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
+        $img = imagecreate($imgWidth, $imgHeight) or die('create image fail ');
 
         #颜色定义
         //background color
-        imagecolorallocate($img,255,255,255);
-        $color = imagecolorallocate($img,0,0,0);
-        $gray = imagecolorallocate($img,180,180,180);
-        $dataLineColor = imagecolorallocate($img,50,50,255);
+        imagecolorallocate($img, 255, 255, 255);
+        $color = imagecolorallocate($img, 0, 0, 0);
+        $gray = imagecolorallocate($img, 180, 180, 180);
+        $dataLineColor = imagecolorallocate($img, 50, 50, 255);
 
 
 
-        $max=0;
+        $max = 0;
         $values = [];
         #按天获取数据
-        for($i = 1; $i <= $maxDay; $i++){
+        for ($i = 1; $i <= $maxDay; $i++) {
             $day = strtotime("today -{$i} day");
-            $date = date("Y-m-d",$day);
-            $current = $this->getSentProgress($request,$date);
+            $date = date("Y-m-d", $day);
+            $current = $this->getSentProgress($request, $date);
             $values[] = $current;
-            if($max < $current){
+            if ($max < $current) {
                 $max = $current;
             }
         }
@@ -275,114 +263,110 @@ class SentenceInfoController extends Controller
         * 小于1000 满刻度是是500的整倍数
         */
 
-        if($max < $yMin){
+        if ($max < $yMin) {
             $yMax = $yMin;
-        }else{
+        } else {
             $len = strlen($max);
-            $yMax = pow(10,$len);
-            if($max < $yMax/2){
+            $yMax = pow(10, $len);
+            if ($max < $yMax / 2) {
                 $yMax = $yMax / 2;
             }
         }
         //根据满刻度像素数 计算缩放比例
-        $yPix = $imgHeight - $xAxisOffset;//y轴实际像素数
+        $yPix = $imgHeight - $xAxisOffset; //y轴实际像素数
         $rate = $yPix / $yMax;
 
         $svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\" class=\"bi bi-alarm-fill\" viewBox=\"0 0 $imgWidth $imgHeight\">";
 
         //绘制坐标轴
-        imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
-        imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
+        imageline($img, 0, $imgHeight - $xAxisOffset, $imgWidth, $imgHeight - $xAxisOffset, $color);
+        imageline($img, $yAxisOffset, $imgHeight, $yAxisOffset, 0, $color);
         // x 轴
-        $y=$imgHeight-$xAxisOffset+1;
+        $y = $imgHeight - $xAxisOffset + 1;
         $svg .= "<line x1='$yAxisOffset'  y1='$y' x2='$imgWidth'   y2='$y' style='stroke:#666666;'></line>";
         // y 轴
         $x = $yAxisOffset - 1;
-        $svg .= "<line x1='$x'  y1='0' x2='$x'   y2='".($imgHeight-$xAxisOffset)."' style='stroke:#666666;'></line>";
+        $svg .= "<line x1='$x'  y1='0' x2='$x'   y2='" . ($imgHeight - $xAxisOffset) . "' style='stroke:#666666;'></line>";
         //绘制x轴刻度线
-        for($i=0; $i<$maxDay; $i++){
-            $space= ($imgWidth-$yAxisOffset)/$maxDay;
-            $x=$imgWidth-$i*$space - $space/2;
-            $dayOffset = $maxDay-$i;
+        for ($i = 0; $i < $maxDay; $i++) {
+            $space = ($imgWidth - $yAxisOffset) / $maxDay;
+            $x = $imgWidth - $i * $space - $space / 2;
+            $dayOffset = $maxDay - $i;
             $date = strtotime("today -{$i} day");
-            $day = date("d",$date);
-            imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
-            imagestring($img,5,$x,($imgHeight-$xAxisOffset-2),$day,$color);
+            $day = date("d", $date);
+            imageline($img, $x, ($imgHeight - $xAxisOffset), $x, ($imgHeight - $xAxisOffset + 5), $gray);
+            imagestring($img, 5, $x, ($imgHeight - $xAxisOffset - 2), $day, $color);
 
-            $y = $imgHeight-$xAxisOffset+1;
+            $y = $imgHeight - $xAxisOffset + 1;
             $height = 5;
-            $svg .= "<line x1='$x'  y1='$y' x2='$x'   y2='".($y+$height)."' style='stroke:#666666;'></line>";
-            $svg .= "<text x='".($x-5)."' y='".($y+12)."' style='font-size:8px;'>$day</text>";
+            $svg .= "<line x1='$x'  y1='$y' x2='$x'   y2='" . ($y + $height) . "' style='stroke:#666666;'></line>";
+            $svg .= "<text x='" . ($x - 5) . "' y='" . ($y + 12) . "' style='font-size:8px;'>$day</text>";
         }
 
 
         //绘制y轴刻度线 将y轴五等分
         $step = $yMax / 5 * $rate;
-        for ($i=1; $i < 5; $i++) {
+        for ($i = 1; $i < 5; $i++) {
             # code...
             $yValue = $yMax / 5 * $i;
-            if($yValue>=1000000){
-                $yValue = ($yValue / 1000000 ).'m';
-            }else if($yValue>=1000){
-                $yValue = ($yValue / 1000 ).'k';
+            if ($yValue >= 1000000) {
+                $yValue = ($yValue / 1000000) . 'm';
+            } else if ($yValue >= 1000) {
+                $yValue = ($yValue / 1000) . 'k';
             }
             $x = $yAxisOffset;
-            $y = $imgHeight-$yAxisOffset-$i*$step;
-            $svg .= "<line x1='$x'  y1='$y' x2='".($x - 5)."'   y2='$y' style='stroke:#666666;'></line>";
-            $svg .= "<text x='".($x-18)."' y='".($y+4)."' style='font-size:8px;'>$yValue</text>";
+            $y = $imgHeight - $yAxisOffset - $i * $step;
+            $svg .= "<line x1='$x'  y1='$y' x2='" . ($x - 5) . "'   y2='$y' style='stroke:#666666;'></line>";
+            $svg .= "<text x='" . ($x - 18) . "' y='" . ($y + 4) . "' style='font-size:8px;'>$yValue</text>";
         }
-        for($i=1;$i < $maxPage/$yLineSpace;$i++){
-            $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
-            $y=$imgHeight-$yAxisOffset-$i*$space;
-            imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
-            imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
+        for ($i = 1; $i < $maxPage / $yLineSpace; $i++) {
+            $space = ($imgHeight - $xAxisOffset) / $maxPage * $yLineSpace;
+            $y = $imgHeight - $yAxisOffset - $i * $space;
+            imageline($img, $yAxisOffset, $y, $imgWidth, $y, $gray);
+            imagestring($img, 5, 0, $y - 5, $i * $yLineSpace, $color);
         }
-// 绘制柱状图
-        $rectWidth = $dayPix*0.9;
+        // 绘制柱状图
+        $rectWidth = $dayPix * 0.9;
         $last = 0;
         foreach ($values as $key => $value) {
             # code...
-            $value = $value*$rate;
+            $value = $value * $rate;
             $x = $imgWidth - ($dayPix * $key + $yAxisOffset);
             $y = $imgHeight - $xAxisOffset - $value;
             $svg .= "<rect x='$x' y='$y' height='{$value}' width='{$rectWidth}' style='stroke:#006600; fill: #006600'/>";
-            if($key>0){
-                imageline($img,($imgWidth - $key * $dayPix),$imgHeight - $xAxisOffset - $value,($imgWidth-($key - 1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
+            if ($key > 0) {
+                imageline($img, ($imgWidth - $key * $dayPix), $imgHeight - $xAxisOffset - $value, ($imgWidth - ($key - 1) * $dayPix), $imgHeight - $xAxisOffset - $last, $dataLineColor);
             }
             $last = $value;
         }
 
-    $svg .= "</svg>";
+        $svg .= "</svg>";
 
         imagegif($img);
         imagedestroy($img);
 
         $content = ob_get_clean();
-        return response($svg,200,[
-            'Content-Type'=>'image/svg+xml'
+        return response($svg, 200, [
+            'Content-Type' => 'image/svg+xml'
         ]);
     }
 
     /**
      * Display the specified resource.
-     *
-     * @param  \App\Models\Sentence  $sentence
-     * @return \Illuminate\Http\Response
-     */
-    public function show(Sentence $sentence)
-    {
-        //
-    }
-
-    /**
-     * Show the form for editing the specified resource.
-     *
-     * @param  \App\Models\Sentence  $sentence
+     * @param  \Illuminate\Http\Request  $request
+     * @param  string  $sentenceId
      * @return \Illuminate\Http\Response
      */
-    public function edit(Sentence $sentence)
+    public function show(Request $request, string $sentenceId)
     {
         //
+        $sentence = Sentence::where('book_id', $request->get('book'))
+            ->where('paragraph', $request->get('par'))
+            ->where('word_start', $request->get('start'))
+            ->where('word_end', $request->get('end'))
+            ->where('channel_uid', $request->get('channel'))
+            ->firstOrFail();
+        return $this->ok(new SentResource($sentence));
     }
 
     /**