<?php

namespace App\Modules\Trade\Pay;

use App\Modules\Subscribe\Models\Order;
use App\Modules\Subscribe\Models\YearOrder;


class ForeverOrderPaySuccess extends PaySuccessAbstract
{
    private $forever_order;

    public function __construct(Order $order)
    {
        parent::__construct($order);
        $this->forever_order = [];
        $this->forever_order['uid'] = $order->uid;
        $this->forever_order['distribution_channel_id'] = $order->distribution_channel_id;
        $this->forever_order['fee'] = $order->price;
        $this->forever_order['send_order_id'] = $order->send_order_id;
    }

    protected function handlePayProcess()
    {
        $this->forever_order['begin_time'] = date('Y-m-d H:i:s');
        $this->forever_order['end_time'] = date('Y-m-d H:i:s', time() + 86400 * 365 * 80);
        $old = YearOrder::where('uid', $this->forever_order['uid'])->select('id', 'uid', 'u', 'begin_time', 'end_time', 'fee')->first();
        if ($old) {
            $old->end_time = date('Y-m-d H:i:s', time() + 86400 * 365 * 80);
            $old->fee = $old->fee + $this->forever_order['fee'];
            $old->save();
            return $old;
        } else {
            return YearOrder::firstOrCreate($this->forever_order);
        }
    }
}
