<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 悦活套餐订单
 * Class Users
 * @package app\admin\model
 */
class LifePackageOrder extends Model
{
    /**
     *
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_package_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,$order_package_type)
    {
        $list = static::select('*')
            ->with(['Shop','User','Package','PackageGoods'])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('order_dno', 'like', '%' . $keywords . '%');
            })
            ->when($order_package_type != '', function ($query) use ($order_package_type){
                $query->where('order_package_type',$order_package_type);
            })
            ->orderBy('order_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

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

    
    //关联店铺
    public function Shop(){
        return $this->belongsTo(MerchantShop::class,'order_shop_id','shop_id');
    }
    //关联用户
    public function User(){
        return $this->belongsTo(User::class,'order_user_id','user_id');
    }
    //关联套餐
    public function Package(){
        return $this->belongsTo(LifePackage::class,'order_package_id','package_id');
    }
    //关联套餐内项目
    public function PackageGoods(){
        return $this->hasMany(LifePackageGoods::class,'package_id','order_package_id')->where(['goods_is_del'=>0]);
    }
    //时间格式
    public function getOrderCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

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