<?php

namespace App\Modules\Trade\Pay;

use App\Modules\Book\Models\BookConfig;
use App\Modules\Subscribe\Models\BookOrder;
use App\Modules\Subscribe\Models\Order;


class BookOrderPaySuccess extends PaySuccessAbstract
{
    private $book_config;

    public function __construct(Order $order)
    {
        parent::__construct($order);
        $this->book_config = $this->getBookByProduct($order->product_id);
    }

    private function getBookByProduct($product_id)
    {
        return BookConfig::where('product_id', $product_id)->first();
    }

    protected function handlePayProcess()
    {
        $book_order = [];
        $book_order['bid'] = isset($this->book_config->bid) ? $this->book_config->bid : '';
        $book_order['book_name'] = isset($this->book_config->book_name) ? $this->book_config->book_name : '';
        $book_order['uid'] = $this->order->uid;
        $book_order['distribution_channel_id'] = $this->order->distribution_channel_id;
        $book_order['fee'] = $this->order->price;
        $book_order['send_order_id'] = $this->order->send_order_id;
        return BookOrder::firstOrCreate($book_order);
    }
}
