Order.php 1.32 KB
<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
	use HasDateTimeFormatter;

    protected $table = 'order';

    /** 未支付*/
    const UNPAID = 100;

    /** 用户取消*/
    const USER_CANCEL = 101;

    /** 超时取消*/
    const TIMEOUT_CANCEL = 102;

    /** 商户取消*/
    const MERCHANT_CANCEL = 103;

    /** 已支付*/  // 回调
    const PAID = 201;

    /** 已完成*/   // 回调并且业务逻辑(加天数、加销量)执行完毕
    const DONE = 204;

    /** 订单超时时间 30分钟 */
    const TIMEOUT = 1800;


    public function order_goods()
    {
        return $this->hasOne('App\Models\OrderGood','order_sn','order_sn');
    }

    /**
     * 获取SN唯一编号
     * @param string $prefix
     * @return string
     */
    static public function get_sn($prefix = '')
    {
        $chars = md5(uniqid(mt_rand(), true));

        $len = strlen($prefix);

        if ($len > 5) $prefix = substr($prefix, 0, 5);

        $uuid = $prefix . dechex(date('m')) . date('d') . substr(time(), -5 + $len) . '-'
            . substr(microtime(), 2, 4) . '-'
            . substr($chars, 12, 4) . '-'
            . substr($chars, 16, 4) . '-'
            . substr($chars, 20, 12);

        return strtoupper($uuid);
    }

}