<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 角色模型
 * Class Users
 * @package app\admin\model
 */
class PackageOrder extends Model
{
    const PAY_STATUS_NO  = 1;
    const PAY_STATUS_YES = 2;
    const PAY_STATUS = [
        self::PAY_STATUS_NO => '待支付',
        self::PAY_STATUS_YES => '已支付'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'package_order';

    public $timestamps = false;

    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/20
     */
    public static function getPackageOrderList(int $page, int $limit ,$keywords)
    {
        $list = static::select('package_order.*','user.user_nickname','user.user_headimgurl')
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('order_dno', 'like', '%' . $keywords . '%');
            })
            ->with('package')
            ->join('user','package_order.order_user_id','=','user.user_id')
            ->orderBy('package_order.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 Package(){
        return $this->belongsTo(Package::class,'order_package_id','package_id');
    }
    public function getOrderPlaceTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getOrderEndTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getOrderPayTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getOrderCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getOrderUpdateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * Notes:插入主订单数据
     * @param array $data
     * @return false|int
     * User: yym
     * Date: 2022/9/27
     */
    public static function insertData(array $data)
    {
        if(empty($data))
        {
            return false;
        }
        return static::insertGetId($data);
    }
}
