zz преди 4 години
родител
ревизия
510a1692e9

+ 17 - 2
app/Http/Controllers/QuickApp/Book/ChapterController.php

@@ -119,8 +119,23 @@ class ChapterController extends BaseController
             return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
         }
 
-        //已经付费 或者 书籍是否限免
-        if ($this->getOrderRecord($bid, $cid) || BookConfigService::judgeBookIsFree($bid)) {
+        // 书籍是否限免
+        $free = BookConfigService::judgeBookIsFree($bid);
+        if ($free) {
+            ReadRecordService::addReadRecord([
+                'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
+                'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
+            ]);
+            $fee  = $this->getPrice($book_info, $chapter->size);
+            $now = date('Y-m-d');
+            Redis::hincrby('qapp:book:free:virtual:'.$free->id,$now,$fee);
+            Redis::sadd('qapp:free:virtual'.$now,$free->id);
+            return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
+        }
+
+
+        //已经付费
+        if ($this->getOrderRecord($bid, $cid)) {
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
                 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence

+ 18 - 1
app/Modules/Book/Services/BookConfigService.php

@@ -530,6 +530,23 @@ class BookConfigService
         }
         return FreeBook::where('bid', $bid)
             ->whereIn('config_id', $ids)
-            ->where('is_enabled', 1)->exists();
+            ->where('is_enabled', 1)->select('id')->first();
+    }
+
+
+    public static function getByBidNoFilter($bid){
+        return   FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
+            ->where('bid',$bid)
+            ->select('free_books.id','end_time')
+            ->where('end_time','<',date('Y-m-d H:i:s'))
+            ->orderBy('free_book_config.end_time','desc')
+            ->first();
+    }
+
+    public static function chargeStats($id,$amount){
+        $now = date('Y-m-d');
+        $amount = $amount*100;
+        Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
+        Redis::sadd('qapp:wap:free:charge'.$now,$id);
     }
 }

+ 10 - 0
app/Modules/Trade/Pay/OrderPaySuccess.php

@@ -2,6 +2,7 @@
 
 namespace App\Modules\Trade\Pay;
 
+use App\Modules\Book\Services\BookConfigService;
 use App\Modules\Subscribe\Models\Order;
 use DB;
 
@@ -44,6 +45,7 @@ class OrderPaySuccess
                     'created_at' => date('Y-m-d H:i:s')
                 ]);
             }
+            self::freeBookStats($order->from_bid,$order->price);
             return $status;
         } else {
             return false;
@@ -51,6 +53,14 @@ class OrderPaySuccess
     }
 
 
+    private static function freeBookStats($bid,$price){
+        if(!$bid) return ;
+        $free_book = BookConfigService::getByBidNoFilter($bid);
+        if(!$free_book) return ;
+        if(strtotime($free_book->end_time)+14*86400 < time())   return ;
+        BookConfigService::chargeStats($free_book->id,(int)($price*100));
+    }
+
     private static function orderAcrossDay($order_info)
     {
         if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;