<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 悦活套餐订单
 * Class Users
 * @package app\api\model
 */
class TravelOrder extends Model
{
    /**
     *
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'travel_order';

    public $timestamps = false;


    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/19
     */
    public static function getOrderList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->with(['Line'])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('order_dno', 'like', '%' . $keywords . '%');
            })
            ->orderBy('order_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

        $count = static::when($keywords != '', function ($query) use ($keywords){
            $query->where('order_dno', 'like', '%' . $keywords . '%');
        })
        ->count();
        return [$list, $count];
    }

    
    //时间格式
    public function getOrderCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getOrderPayTimeAttribute($value)
    {
        if($value == 0 || $value == ''){
            return 0;
        }else{
            return date('Y-m-d H:i:s', $value);
        }
        
    }

    //关联用户
    public function User(){
        return $this->belongsTo(User::class,'order_user_id','user_id');
    }
    
    public function Line(){
        return $this->belongsTo(TravelLine::class,'order_line_id','line_id');
    }
}