<?php


namespace App\Models\Order;
use Illuminate\Database\Eloquent\Model;
use DB;


class Order extends Model
{
    protected $table = 'orders';

    protected $fillable = [
        'uid', 'product_id', 'price', 'status', 'pay_num', 'trade_no','tiktok_order_id','pay_url', 'send_order_id', 'send_order_name',
        'transaction_id', 'distribution_channel_id', 'pay_end_at', 'create_ip',  'pay_merchant_source', 'pay_merchant_id', 'from_bid', 'order_type',
        'activity_id','from_type'
    ];

	static function getByTradeNo($trade_no)
	{
	    return self::select('*')->where('trade_no',$trade_no)->first();
	}

    static function getByTikTokOrderId($orderId)
    {
        return self::where('tiktok_order_id',$orderId)->first();
    }

	public static  function  getOrderList($uid,$page_size){
	    return self::select('id','price','created_at','status','trade_no')->where('uid',$uid)->orderBy('id','desc')->paginate($page_size);
    }


    static function create_order($data){

        return self::firstOrCreate($data);
    }


    public static function getUserChargeTimes($uid){
        return self::where('uid',$uid)->where('status','PAID')->count('id');
    }
}
