<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 健康订单
 * Class Users
 * @package app\admin\model
 */
class HealthyOrder extends Model
{
    /**
     *
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'healthy_order';

    public $timestamps = false;


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

        $count = static::when($keywords != '', function ($query) use ($keywords){
            $query->where('order_sn', 'like', '%' . $keywords . '%');
        })
        ->count();
        return [$list, $count];
    }
    
    //关联用户
    public function User(){
        return $this->belongsTo(User::class,'order_user_id','user_id');
    }

    //关联店铺
    public function Shop(){
        return $this->belongsTo(MerchantShop::class,'order_shop_id','shop_id');
    }

    //关联商品信息
    public function HealthyOrderProduct(){
        return $this->hasMany(HealthyOrderProduct::class,'order_product_order_id','order_id');
    }

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


}