<?php

namespace App\Modules\Book\Services;

use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\Chapter;
use Redis;
use App\Modules\Statistic\Services\WapVisitStatService;
use DB;


class BookService
{
    /**
     * 获取book中存在的分类信息id
     * @return array
     */
    public static  function getCategoryId(){
        return Book::getCategoryId();
    }

    /**
     * 修改图书描述
     * @param $bid
     * @param $intro
     * @return mixed
     *
     */
    public  static function  updateIntro($bid,$intro){
        return Book::where('id',$bid)->update(['intro'=>$intro]);
    }

    /**
     * 设置章节价格
     * @param int $channel_id
     * @param int $price
     */
    public static function setChapterPrice(int $channel_id,float $price):void{
        try{
            Redis::hset('book_chapter_price',$channel_id,$price);
        }catch (\Exception $e){

        }
    }

    /**
     * 获取渠道设置的价格
     * @param int $channel_id
     * @return int
     */
    public static function getChapterPrice(int $channel_id){
        try{
            $fee =  Redis::hget('book_chapter_price',$channel_id);
            if($fee)
                return (float)$fee;
            else
                return 0;
        }catch (\Exception $e){

        }
        return 0;
    }
    
    public static function getBookStatistics($smart_push_books){
    	// 获取书籍统计数据
    	if(!empty($smart_push_books)){
    		foreach($smart_push_books as $key=>$smart_push_book){
    			$book_statistics = WapVisitStatService::smartPushTestBookStats($smart_push_book->bid);
    			$smart_push_books[$key]->uv = $book_statistics['uv'];
    			$smart_push_books[$key]->pv = $book_statistics['pv'];
    			$smart_push_books[$key]->charge_amount = $book_statistics['charge_amount'];
    			$smart_push_books[$key]->charge_user_num = $book_statistics['charge_user_num'];
    			$smart_push_books[$key]->book_amount = $book_statistics['book_amount'];
    			$smart_push_books[$key]->book_user_num = $book_statistics['book_user_num'];
    			$smart_push_books[$key]->real_push_user_num = $book_statistics['real_push_user_num'];
    			$smart_push_books[$key]->second_chapter_uv = $book_statistics['second_chapter_uv'];
    		}
    	}
    	return $smart_push_books;
    }

    public static function getBookById($bid){
        return Book::find($bid);
    }

   
    public static function getBookStatisticsNew($smart_push_books){
        // 获取书籍统计数据
        if(!empty($smart_push_books)){
            foreach($smart_push_books as $key=>$smart_push_book){
                $book_statistics = WapVisitStatService::smartPushTestBookStatsNew($smart_push_book->bid);
                $smart_push_books[$key]->uv = $book_statistics['uv'];
                $smart_push_books[$key]->pv = $book_statistics['pv'];
                $smart_push_books[$key]->book_amount = $book_statistics['book_amount'];
                $smart_push_books[$key]->book_user_num = $book_statistics['book_user_num'];
                $smart_push_books[$key]->real_push_user_num = $book_statistics['real_push_user_num'];
                $smart_push_books[$key]->first_chapter_uv = $book_statistics['first_chapter_uv'];
                $smart_push_books[$key]->second_chapter_uv = $book_statistics['second_chapter_uv'];
                $smart_push_books[$key]->thirtieth_chapter_uv = $book_statistics['thirtieth_chapter_uv'];
                $smart_push_books[$key]->fiftieth_chapter_uv = $book_statistics['fiftieth_chapter_uv'];
                $smart_push_books[$key]->chapter_uv_110 = $book_statistics['chapter_uv_110'];
            }
        }
        return $smart_push_books;
    }
}
