<?php

namespace App\Modules\Trade\Pay;

use App\Modules\Subscribe\Models\Order;
use App\Modules\User\Models\QappPackage;

abstract class OrderArousePayAbstract
{
    /**
     * 唤起支付
     */
    abstract public function arouse(array $data);


    protected $uid;

    public function __construct(int $uid)
    {
        $this->uid = $uid;
    }

    public function handle(array $data)
    {
        $this->createOrder($data);
        return $this->arouse($data);
    }

    public function createOrder(array $data)
    {
        $params = [
            'uid'                     => $this->uid,
            'product_id'              => $data['product_id'],
            'distribution_channel_id' => $data['distribution_channel_id'],
            'trade_no'                => $data['trade_no'],
            'pay_merchant_id'         => isset($data['pay_merchant_id']) ? $data['pay_merchant_id'] : 0,
            'order_type'              => $data['order_type'],
            'create_ip'               => $data['create_ip'],
            'send_order_id'           => $data['send_order_id'],
            'from_type'               => $data['from_type'],
            'price'                   => $data['price'] / 100,
            'status'                  => 'UNPAID',
            'transaction_id'          => '',
            'pay_end_at'              => '0000-00-00 00:00:00',
            'pay_merchant_source'     => '',
            'send_order_name'         => isset($data['send_order_name']) ? $data['send_order_name'] : '',
            'pay_type'                => isset($data['pay_type']) ? $data['pay_type'] : 1,
            'from_bid'                => isset($data['from_bid']) ? $data['from_bid'] : 0,
            'activity_id'             => isset($data['activity_id']) ? $data['activity_id'] : 0,
            'inner_send_order_id'     => isset($data['inner_send_order_id']) ? $data['inner_send_order_id'] : '',
        ];
        Order::firstOrCreate($params);
    }

    protected function getSubjectName(int $channel_id)
    {
        $package = QappPackage::where('channel_id', $channel_id)->select('name')->first();
        $name    = $package ? $package->name : '';
        return "搜索快应用（{$name}）继续阅读，投诉电话：0571-56680189";
    }

    protected function getSubjectNameByPackage(string $package)
    {
        $package = QappPackage::where('package', $package)->select('name')->first();
        $name    = $package ? $package->name : '';
        return "搜索快应用（{$name}）继续阅读，投诉电话：0571-56680189";
    }
}
