<?php

namespace App\Http\Controllers\Wap\Book;

use App\Modules\Book\Services\BookService;
use Illuminate\Http\Request;
use App\Http\Controllers\Wap\BaseController;
use Cookie;
use Redis;
use App\Modules\Book\Services\ChapterService;
use App\Modules\User\Services\ReadRecordService;
use App\Http\Controllers\Wap\Book\Transformers\ChapterTransformer;
use App\Modules\Book\Services\BookConfigService;
use App\Http\Controllers\Wap\Book\Transformers\ChapterListTransformer;
use App\Modules\Subscribe\Services\BookOrderService;
use App\Modules\Subscribe\Services\ChapterOrderService;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\Subscribe\Services\ChapterReminderService;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use Hashids;
use GuzzleHttp\Client;
use Log;
use DB;
use App\Modules\User\Services\UserSignService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\User\Services\UserDeepReadTagService;
use App\Modules\Book\Services\BookSubscribleChapterService;
use App\Modules\Channel\Services\ChannelSubscribeSettingService;

class ChapterController extends BaseController
{

    /**
     * @apiDefine Chapter 章节
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 章节列表不分页
     * @api {get} books/{bid}/allcatalog 章节列表不分页
     * @apiGroup Chapter
     * @apiName getCatalog
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {Array}       data.list 分页结果集
     * @apiSuccess {Int}         data.list.bid   bid
     * @apiSuccess {Int}         data.list.chapter_id   章节id
     * @apiSuccess {String}      data.list.chapter_name   章节名称
     * @apiSuccess {Int}         data.list.chapter_sequence   序号
     * @apiSuccess {Int}         data.list.chapter_is_vip   是否vip
     * @apiSuccess {Int}         data.list.chapter_size   章节大小
     * @apiSuccess {Int}         data.list.prev_cid   上一章节id
     * @apiSuccess {Int}         data.list.next_cid   下一章节
     * @apiSuccess {String}      data.list.recent_update_at   更新时间
     * @apiSuccess {String}      data.list.is_need_subscirbe   是否强制关注
     * @apiSuccess {String}      data.list.is_show_price   是否显示价格
     * @apiSuccess {String}      data.list.price   价格
     * @apiSuccess {object}      data.meta   分页信息
     * @apiSuccess {Int}         data.meta.total  总条数
     * @apiSuccess {Int}         data.meta.per_page  每页条数
     * @apiSuccess {Int}         data.meta.current_page 当前页
     * @apiSuccess {Int}         data.meta.last_page  最后页
     * @apiSuccess {String}      data.meta.next_page_url  下一页
     * @apiSuccess {String}      data.meta.prev_page_url  上一页
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:
     *            [
     *             {
     *               bid: 5,
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               is_need_subscirbe: 1,
     *            },
     *             {
     *               bid: 5,
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               is_need_subscirbe: 1,
     *            },
     *          ]
     *       }
     */
    public function getCatalog(Request $request, $t, $domain, $bid)
    {
        $bid = Hashids::decode($bid)[0];
        $lists = ChapterService::getChapterLists($bid);
        $book_info = BookConfigService::getBookById($bid);
        if(!$book_info){
            return response()->error('PARAM_ERROR');
        }
        $is_show_price = $this->showChapterPrice($bid,$book_info->charge_type);
        foreach ($lists as $v) {
            $v->is_show_price = $is_show_price;
            $v->price = '';
            if($is_show_price){
                $v->price = $this->getPrice($book_info,$v);
            }
        }
        return response()->collection(new ChapterListTransformer, $lists);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 章节列表分页
     * @api {get} books/{bid}/catalog 章节列表分页
     * @apiGroup Chapter
     * @apiName getCatalogPerPage
     * @apiParam   {Int}         page_size  分页大小(默认15)
     * @apiParam   {Int}         page  页码(默认1)
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {Array}       data.list 分页结果集
     * @apiSuccess {Int}         data.list.bid   bid
     * @apiSuccess {Int}         data.list.chapter_id   章节id
     * @apiSuccess {String}      data.list.chapter_name   章节名称
     * @apiSuccess {Int}         data.list.chapter_sequence   序号
     * @apiSuccess {Int}         data.list.chapter_is_vip   是否vip
     * @apiSuccess {Int}         data.list.chapter_size   章节大小
     * @apiSuccess {Int}         data.list.prev_cid   上一章节id
     * @apiSuccess {Int}         data.list.next_cid   下一章节
     * @apiSuccess {String}      data.list.recent_update_at   更新时间
     * @apiSuccess {String}      data.list.is_need_subscirbe   是否强制关注
     * @apiSuccess {String}      data.list.is_show_price   是否显示价格
     * @apiSuccess {String}      data.list.price   价格
     * @apiSuccess {object}      data.meta   分页信息
     * @apiSuccess {Int}         data.meta.total  总条数
     * @apiSuccess {Int}         data.meta.per_page  每页条数
     * @apiSuccess {Int}         data.meta.current_page 当前页
     * @apiSuccess {Int}         data.meta.last_page  最后页
     * @apiSuccess {String}      data.meta.next_page_url  下一页
     * @apiSuccess {String}      data.meta.prev_page_url  上一页
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:
     *            list:[
     *             {
     *               bid: 5,
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               is_need_subscirbe: 1,
     *            },
     *             {
     *               bid: 5,
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               is_need_subscirbe: 1,
     *            },
     *          ]
     *          meta:{
     *              total: 1253,
     *              per_page: 15,
     *              current_page: 1,
     *              last_page: 84,
     *              next_page_url: "http://myapi.cn/api/books/1/chapter?page=2",
     *              prev_page_url: ""
     *         }
     *       }
     */
    public function getCatalogPerPage(Request $request, $t, $domain, $bid)
    {
        $bid_array = Hashids::decode($bid);

        if (isset($bid_array[0])) {
            $bid = $bid_array[0];
        } else {
            return response()->error('PARAM_ERROR');
        }
        $book_info = BookConfigService::getBookById($bid);
        if (!$book_info) {
            return response()->error('PARAM_ERROR');
        }
        $page_size = $request->input('page_size', 15);

        $res = ChapterService::getChapterListsPage($bid, $page_size);
        $is_show_price = $this->showChapterPrice($bid,$book_info->charge_type);
        foreach ($res as $v) {
            $v->is_show_price = $is_show_price;
            $v->price = '';
            if($is_show_price){
                $v->price = $this->getPrice($book_info,$v);
            }
        }
        return response()->pagination(new ChapterListTransformer, $res);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 章节内容
     * @api {get} books/{bid}/chapters/{chapter_id} 章节内容
     * @apiGroup Chapter
     * @apiName index
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {Int}         data.chapter_id   章节id
     * @apiSuccess {String}      data.chapter_name   章节名称
     * @apiSuccess {Int}         data.chapter_sequence   序号
     * @apiSuccess {Int}         data.chapter_is_vip   是否vip
     * @apiSuccess {Int}         data.chapter_size   章节大小
     * @apiSuccess {Int}         data.prev_cid   上一章节id
     * @apiSuccess {Int}         data.next_cid   下一章节
     * @apiSuccess {String}      data.recent_update_at   更新时间
     * @apiSuccess {String}      data.chapter_content  章节内容
     * @apiSuccess {Int}         data.is_need_subscirbe  是否强制关注(删除)
     * @apiSuccess {Int}         data.is_show_subscribe_link  是否显示底部强关链接
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               chapter_content: "叶妩被司行霈的阴阳怪气一吓，思路偏得太远了。 她张口结舌，忘记了自己要说什么。",
     *            }
     *       }
     */
    public function index(Request $request, $t, $domain, $bid, $cid)
    {
        if (!$this->checkUid()) {
            return response()->error('WAP_NOT_LOGIN');
        }
        $oldbid = $bid;
        $bid = Hashids::decode($bid)[0];
        //获取图书信息
        $book_info = BookConfigService::getBookById($bid);


        if (empty($book_info)) return response()->error('WAP_SYS_ERROR');

        //图书域名,book_configs表的promotion_domain 要跟当前的主机名匹配
        if ($this->isVisitDomainEqualBookDomain($book_info->promotion_domain)) {
            $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
            //promotion_domain可以是多个，以逗号隔开
            $domain_head = explode('.', _domain());
            $book_domain = explode(',', $book_info->promotion_domain);
            $from_type = Cookie::get('from') ? Cookie::get('from') : 'main';
            $url = 'https://' . $domain_head[0] . '.' . $book_domain[0] . '/reader?bid=' . $oldbid . '&cid=' . $cid . '&send_order_id=' . $send_order_id . '&fromtype=' . $from_type;
            return response()->error('WAP_DOMAIN_NOT_MATCH', [
                'url' => $url
            ]);
        }

        //下架图书不能看
        if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
            return response()->error('WAP_OFF_SHELF');
        }
        //屏蔽一本书
        if ($bid == 1042 && $this->distribution_channel_id == 123) {
            return response()->error('WAP_OFF_SHELF');
        }

        if ($book_info->is_on_shelf == 4) {
            //版权到期,有阅读记录还可以继续阅读，订购
            if (!ReadRecordService::getRecordByUidBid($this->uid, $bid)) {
                return response()->error('WAP_OFF_SHELF');
            }
            //版权到期 只可以阅读订购过的章节
            /*if (!$this->getOrderRecord($bid, $cid)) {
                return response()->error('WAP_OFF_SHELF');
            }*/
        }
        //获取章节信息
        $chapter = ChapterService::getChapterNameById($cid, $bid);
        if (!$chapter) {
            return response()->error('WAP_SYS_ERROR');
        }

        //阅读记录
        ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
            'cid' => $cid, 'chapter_name' => $chapter->name]);

        if ( $chapter->is_vip == 0) {
            $content = $this->getChapter($bid, $cid, $chapter, 0, 1);
            $content->is_had_subscribe = 0;
            return response()->item(new ChapterTransformer, $content);
        }
        //已经付费
        if ($this->getOrderRecord($bid, $cid)) {
            $content = $this->getChapter($bid, $cid, $chapter, 0, 0);
            $content->is_had_subscribe = 0;
            return response()->item(new ChapterTransformer, $content);
        }
        //以上都是不用付费的情况******************************************************
        //未付费
        $user_info = $this->_user_info;
        //获取价格
        $fee = $this->getPrice($book_info, $chapter);
        //返回的价格信息
        $data = [
            'book_id' => $oldbid,
            'book_name' => $book_info->book_name,
            'chapter_name' => $chapter->name,
            'chapter_id' => $cid,
            'pay_type' => $book_info->charge_type,
            'fee' => $fee,
            'user_balance' => $user_info->balance,
            'product_id' => $book_info->product_id,
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'is_discount' => 0,
            'discount_fee' => '',
            'discount' => ''

        ];
        //余额不足 弹窗提醒
        if ($user_info['balance'] < $fee) {
            if ($book_info->charge_type == 'BOOK') {
                return response()->error('WAP_BOOK_BALANCE_PAY', $data);
                //return response()->error('WAP_BOOK_SECOND_BALANCE_PAY',$data);
            } else if ($book_info->charge_type == 'CHAPTER') {
                //return response()->error('WAP_CHAPTER_INSUFFICIENT_BALANCE',$data);
                return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
            } else {
                return response()->error('WAP_SYS_ERROR');
            }
        }
        //余额充足 单本弹窗提醒，章节直接扣费
        if ($this->isOrderRemind($bid) && $book_info->charge_type == 'BOOK') {
            return response()->error('WAP_BOOK_BUY', $data);
        }
        //付费 不提醒
        if ($this->balancePay($book_info, $chapter, 0)) {
            $content = $this->getChapter($bid, $cid, $chapter, 0, 1);
            $content->is_had_subscribe = 1;
            return response()->item(new ChapterTransformer, $content);
        } else {
            //不需要提醒
            if ($book_info->charge_type == 'BOOK') {
                return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
            } elseif ($book_info->charge_type == 'CHAPTER') {
                return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
            } else {
                return response()->error('WAP_SYS_ERROR');
            }
        }
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 余额支付
     * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
     * @apiGroup Chapter
     * @apiName pay
     * @apiParam   (Int)         remind 提醒
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {Int}         data.chapter_id   章节id
     * @apiSuccess {String}      data.chapter_name   章节名称
     * @apiSuccess {Int}         data.chapter_sequence   序号
     * @apiSuccess {Int}         data.chapter_is_vip   是否vip
     * @apiSuccess {Int}         data.chapter_size   章节大小
     * @apiSuccess {Int}         data.prev_cid   上一章节id
     * @apiSuccess {Int}         data.next_cid   下一章节
     * @apiSuccess {String}      data.recent_update_at   更新时间
     * @apiSuccess {String}      data.chapter_content  章节内容
     * @apiSuccess {Int}         data.is_need_subscirbe  是否强制关注(删除)
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {
     *               chapter_id: 5,
     *               chapter_name: "第1240章 不是我",
     *               chapter_sequence: 1239,
     *               chapter_is_vip: 1,
     *               chapter_size: 2422,
     *               prev_cid: 0,
     *               next_cid: 0,
     *               recent_update_at: 2017-11-20 15:01:56,
     *               chapter_content: "叶妩被司行霈的阴阳怪气一吓，思路偏得太远了。 她张口结舌，忘记了自己要说什么。",
     *            }
     *       }
     */
    public function pay(Request $request, $t, $domain, $bid, $cid)
    {

        if (!$this->checkUid()) {
            return response()->error('NOT_LOGIN');
        }
        $remind = (int)$request->input('remind');
        $oldbid = $bid;
        $bid = Hashids::decode($bid)[0];
        $book_info = BookConfigService::getBookById($bid);;
        if (empty($book_info)) response()->error('WAP_SYS_ERROR');

        if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
            if (!$this->isBookOrdered($bid)) {
                response()->error('WAP_OFF_SHELF');
            }
        }
        //获取章节
        $chapter = ChapterService::getChapterNameById($cid, $bid);
        if (!$chapter) {
            return response()->error('WAP_SYS_ERROR');
        }


        if ($this->balancePay($book_info, $chapter, $remind)) {
            ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
                'cid' => $cid, 'chapter_name' => $chapter->name]);
            $content = $this->getChapter($bid, $cid, $chapter, 0, 1);
            $content->is_had_subscribe = 0;
            return response()->item(new ChapterTransformer, $content);
        } else {
            $fee = $this->getPrice($book_info, $chapter);
            $data = [
                'book_id' => $oldbid,
                'book_name' => $book_info->book_name,
                'chapter_name' => $chapter->name,
                'chapter_id' => $cid,
                'pay_type' => $book_info->charge_type,
                'fee' => $fee,
                'user_balance' => $this->_user_info['balance'],
                'product_id' => $book_info->product_id,
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'is_discount' => 0,
                'discount_fee' => '',
                'discount' => ''
            ];
            //不需要提醒
            if ($book_info->charge_type == 'BOOK') {
                return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
            } elseif ($book_info->charge_type == 'CHAPTER') {
                return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
            } else {
                return response()->error('WAP_SYS_ERROR');
            }
        }
    }


    /**
     * 余额支付
     * @param $book_info
     * @param $chapter_id
     * @param $chapter_size
     * @return bool
     */
    protected function balancePay($book_info, $chapter, $is_remind)
    {
        $fee = $this->getPrice($book_info, $chapter);
        if ($this->_user_info->is_new == 1) {
            //$fee = ceil($fee*0.3);
        }
        if ((int)$this->_user_info['balance'] >= $fee) {
            if ($this->bookOrderOrChapterOrder($book_info, $chapter->id, $fee, $chapter->name, $is_remind)) {
                try {
                    $day = date('Y--m-d');
                    Redis::hincrby('wap:chapterandbookorder:bid:' . $book_info->bid, $day, $fee);
                } catch (\Exception $e) {

                }
                return true;
            }
            return false;
        } else {
            return false;
        }

    }


    /**
     * 获取章节内容
     * @param $bid
     * @param $cid
     * @return bool|mixed
     */
    protected function getChapter($bid, $cid, $chapter, $is_show_subscribe_link = 0, $is_subscribe = 0)
    {
        //$this->readRecordOther($chapter);
        $chapter_content = ChapterService::getChapter($bid, $cid);
        if (!$chapter_content) return false;
        $chapter->content = str_replace($chapter_content->name, '', $chapter_content->content);

        $sign_status = $this->userSign($this->uid);
        $chapter->sign_status = $sign_status ? 1 : 0;
        $chapter->sign_reard = $sign_status;

        $chapter->is_show_subscribe_link = $is_show_subscribe_link;
        return $chapter;
    }

    /**
     * 签到
     * @param $uid
     * @return int
     */
    protected function userSign($uid)
    {

        if ($this->distribution_channel_id != 5) {
            //return 0;
        }

        $day = date('Y-m-d');
        $sign_stat = UserSignService::isSigni($uid, $day);
        if (!$sign_stat) {
            return 0;
        }
        if (UserSignService::sign($uid, $day, $sign_stat)) {
            $sign_key = 'leyuee:wap:usersigni';
            Redis::hset($sign_key, $uid, $sign_stat);
            return $sign_stat;
        }
        return 0;

    }

    /**
     * 添加订购记录
     * @param $book_info
     * @param $chapter_id
     * @param $fee
     * @return bool
     */
    protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
    {

        if ($book_info['charge_type'] == 'BOOK') {
            $data = [
                'uid' => $this->uid,
                'fee' => $fee,
                'u' => Cookie::get('send_order_id'),
                'distribution_channel_id' => $this->distribution_channel_id,
                'bid' => $book_info->bid,
                'book_name' => $book_info->book_name,
                'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
            ];
            return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
        } else {
            $data = [
                'uid' => $this->uid,
                'fee' => $fee,
                'cid' => $chapter_id,
                'bid' => $book_info->bid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'book_name' => $book_info->book_name,
                'chapter_name' => $chapter_name,
                'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
                'is_remind' => $is_remind
            ];
            //print_r($data);
            if ($is_remind) {
                $this->addOrderRemind($book_info->bid);
            }
            return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
        }
    }


    protected function addOrderRemind($bid)
    {
        if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
            return true;
        } else {
            ChapterReminderService::add($this->uid, $bid);
            return true;
        }
    }

    /**
     * 是否订购提醒
     * @param $chapter_id
     * @return bool
     */
    protected function isOrderRemind($bid)
    {
        $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
        return $is_no_reminder == 0;
    }



    /**
     * 获取订购记录
     * @param $book_info
     * @param $chapter_id
     * @return bool
     */
    protected function getOrderRecord($bid, $chapter_id)
    {
        //包年记录
        $uid = $this->uid;
        $res = YearOrderService::getRecord($uid);
        if ($res) return true;
        $res = null;

        //单本订购记录
        $res = BookOrderService::getRecordByuidBid($uid, $bid);
        if ($res) return true;
        $res = null;

        //章节订购记录
        $chapterOrder = new ChapterOrderService();

        if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;

        return false;
    }


    /**
     * 计算价格
     * @param $book_info
     * @param $chapter_size
     * @return float
     */
    protected function getPrice($book_info, $chapter)
    {
        if ($book_info->charge_type == 'BOOK') {
            $fee = $book_info->price * 100;
        } else {
            //$fee = ceil($chapter->size/100);
            //$channel_fee = BookService::getChapterPrice()
            $channel_fee = BookService::getChapterPrice($this->distribution_channel_id);
            if ($channel_fee) {
                $price_rate = $channel_fee / 100;
            } else {
                //$price_rate = 0.012;
                $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
            }
            //Log::info($price_rate);
            $fee = ceil($chapter->size * $price_rate);
        }
        //首个计费章节书币价格以35书币为基准，低于35提升至35，原价高于35书币，维持原价
        if (isset($chapter->sequence) && isset($book_info->vip_seq)) {
            if ($chapter->sequence == $book_info->vip_seq) {
                if ($fee < 35) {
                    $fee = 35;
                }
            }
        }
        return $fee;
    }


    /**
     * 判断访问的域名和数据库指定的域名是否一致，一致返回false ，不一致 true
     * @param $book_domain
     * @return bool
     */
    protected function isVisitDomainEqualBookDomain($book_domain)
    {
        return stripos(_domain(), $book_domain) === false;
    }



    protected function isBookOrdered($bid)
    {

        $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
        if ($chapter_order) return true;

        $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
        if ($res) return true;
        return false;
    }

    private function showChapterPrice($bid, $charge_type)
    {
        if ($charge_type == 'BOOK') {
            return false;
        }
        $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
        if ($show_chapter_price_bid != 'all') {
            $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
            if (!in_array($bid, $show_chapter_price_bid_array)) {
                return false;
            }
        }
        $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
        if (!$show_chapter_price_channel) {
            return false;
        }

        if ($show_chapter_price_channel == 'all') {
            return true;
        }
        $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
        if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
            return true;
        }
        return false;
    }
}
