<?php

namespace app\admin\model;

use support\Db;
use support\Model;

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

    public $timestamps = false;


    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/25
     */
    public static function getOrderList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->with(['User','LifeFarmLandOrderDetail'])
            ->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 User(){
        return $this->belongsTo(User::class,'order_user_id','user_id');
    }

    //关联订单详情
    public function LifeFarmLandOrderDetail(){
        return $this->hasMany(LifeFarmLandOrderDetail::class,'detail_order_id','order_id');
    }

    //时间格式
    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);
        }
        
    }


}