CanOrder.php 4.1 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * trait :对象可以被访问并生成访问日志trait
+-----------------------------------------------------------------------------------------------------------------------
 *
 * @copyright Copyright
 * @author    Richer <yangzi1028@163.com>
 * @package   App\Models\Traits
 * @version   2019年11月12日,17:23:28
 * @link
 */
namespace App\Models\Traits;

use App\Factories\OrderFactory;
use App\Models\Order\Order;
use Illuminate\Database\Eloquent\Model;

/**
 * Trait CanOrder.
 */
trait CanOrder
{

    /**
     * User order  given targets.
     *
     * @param Model $target 订单下单对象
     * @return mixed
     */
    public function placeOrder($target)
    {
        $factory = app(OrderFactory::class);
        return $factory->generateOrder($target, $this);
    }

    /**
     * Cancel a order for given targets.
     *
     * @param Order $order 订单对象
     *
     * @return bool
     */
    public function cancelOrder($order)
    {
        if (!$order) {
            return true;
        }
        // 实例化订单工厂类
        $factory = app(OrderFactory::class);
        if (!$result = $factory->cancel($order, $this)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }

    /**
     * Pay order
     *
     * @param Order $order 订单对象
     * @param string $type 支付方式
     * @param string $notify_url 回调地址
     * @return bool
     */
    public function payOrder($order, $type, $notify_url = '')
    {
        // 实例化订单工厂类
        $factory = app(OrderFactory::class);
        if (!$result = $factory->pay($this, $order, $type, $notify_url)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }
    /**
     * @param $recharge
     * @param $user
     * @param $type
     * @return bool
     */
    public function pay($recharge, $user, $type)
    {
        $factory = app(OrderFactory::class);

        if (!$result = $factory->pay($recharge, $user, $type)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }

    /**
     * Refund order
     *
     * @param Order $order 订单对象
     *
     * @return bool
     */
    public function refundOrder($order)
    {
        // 实例化订单工厂类
        $factory = app(OrderFactory::class);
        if (!$result = $factory->refund($order, $this)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }

    /**
     * Receive order
     *
     * @param Order $order 订单对象
     *
     * @return bool
     */
    public function receiveOrder($order)
    {
        // 实例化订单工厂类
        $factory = app(OrderFactory::class);
        if (!$result = $factory->receive($order, $this)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }


    /**
     * Evaluate order
     *
     * @param Order $order 订单对象
     *
     * @return bool
     */
    public function evaluateOrder($order)
    {
        // 实例化订单工厂类
        $factory = app(OrderFactory::class);
        if (!$result = $factory->evaluate($order, $this)) {
            $this->message = $factory->getMessage();
            return false;
        }
        return $result;
    }

    /**
     * Return orders.
     *
     * @param string $orderable_type 对象类型
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function orders($orderable_type = null)
    {
        // 不使用多态关联,因为订单的数据需要保留历史
        //return $this->morphedByMany($orderable_type , 'orderable','orders');
        return $this->hasMany(Order::class, 'user_id', 'id');
    }

    /**
     * 获取错误提示
     *
     * @return string
     */
    public function getOrderMsg()
    {
        return $this->message;
    }
}