<?php

namespace App\Modules\Book\Services;

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

class BookService
{
    public static function decodeBidStatic(string $bid)
    {
        $decodeBid = Hashids::decode($bid);
        $decodeBid = is_array($decodeBid) ? array_shift($decodeBid) : $decodeBid;
        if(empty($decodeBid)){
            myLog("QappBookBidDecodeError")->info("bid : {$bid}, decodeBid : ".var_export($decodeBid,true));
        }
        return intval($decodeBid);
    }

    /**
     * 获取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 getBookById($bid)
    {
        return Book::find($bid);
    }


    public static function getVipSequence($bid,$distribution_channel_id,$send_order_id){
        if(!$send_order_id) return 0;
        $account_send_order = QappSendOrder::getSendOrderById($send_order_id);
        $key = 'channel:chapterfee:setting:' . $distribution_channel_id;
        if($account_send_order){
            $account = isset($account_send_order['account'])?$account_send_order['account']:'';
            if($account){
                $key = 'channel:chapterfee:setting:' . $distribution_channel_id.':'.$account;
            }
        }
        return Redis::hget($key, $bid);
    }


    public static function getPrice($book_info,$distribution_channel_id,$size,$account=''){
        // 书默认--- 收费类型
        $calculate_price_type = $book_info->calculate_price_type;
        
        // 渠道bid--- 收费类型
        $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id,  $book_info->bid);
        if($channel_calculate_price_type){
            $calculate_price_type = $channel_calculate_price_type;
        }
        
        // 账户bid优先--- 收费类型
        $channel_account_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id.':'.$account,  $book_info->bid);
        if($channel_account_calculate_price_type){
             $calculate_price_type = $channel_account_calculate_price_type;
        }
        
        // 渠道bid价格
	    $distribution_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id,$book_info->bid);
	    
	    // 账户bid默认价格
	    $distribution_account_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id.':'.$account, $book_info->bid);
	    
        //固定价格
        if(strtolower($calculate_price_type) == 'const'){
            $price = (int)$book_info->unit_price;
	        if($distribution_channel_id_price){
                $price = (int)$distribution_channel_id_price;
            }
            if($distribution_account_channel_id_price){
                $price = (int)$distribution_account_channel_id_price;
            }
            
            $price = $price < 30 ? 30 : $price;
            
            //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$price);

            return $price;
        }
        
        
        //千字价格
        $channel_fee = self::getChapterPrice($distribution_channel_id);
        if ($channel_fee) {
            $price_rate = $channel_fee / 100;
        } else {
            $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
        }

        if($book_info->unit_price){
            $price_rate = $book_info->unit_price;
        }
	    if($distribution_channel_id_price){
            $price_rate = $distribution_channel_id_price;
        }
        if($distribution_account_channel_id_price){
            $price_rate = $distribution_account_channel_id_price;
        }

        $fee = ceil($size * $price_rate);
        if($fee >189) $fee = 189;
        if($fee <37) $fee = 37;
        
        //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$fee.' size:'.$size.' $price_rate:'.$price_rate);
        
        return $fee;
    }

    /**
     * 切章后，[是否切章,是否改名]
     * @param $bid
     * @return array
     */
    public static function splitContent($bid){
        $config = DB::table('book_split_operate_record')->where('bid',$bid)->select('chapter_name_type','is_process')->orderBy('id','desc')->first();
        if(!$config) return [0,0];
        if($config->is_process == 2){
            return [1,$config->chapter_name_type];
        }
        return [0,0];
    }


}
