<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 套餐模型
 * Class Users
 * @package app\admin\model
 */
class LifePackage extends Model
{
    const ROLE_STATUS = 1;

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const GOODS_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_package';

    public $timestamps = false;

    /**
     * Notes:获取套餐列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/18
     */
    public static function getPackageList(int $page, int $limit, string $keywords,$package_type)
    {
        $list = static::select('*')
            ->where(['package_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('package_name', 'like', '%' . $keywords . '%');
            })
            ->when($package_type != '', function ($query) use ($package_type){
                $query->where('package_type',$package_type);
            })
            ->orderBy('package_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

        foreach ($list as &$v) {
            $v['package_tags'] = explode(",",$v['package_tags']);
        }
        $count = static::where(['package_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('package_name', 'like', '%' . $keywords . '%');
            })
            ->when($package_type != '', function ($query) use ($package_type){
                $query->where('package_type',$package_type);
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取套餐列表
     * @return array
     * User: YCP
     * Date: 2022/10/18
     */
    public static function getPackageAll()
    {
        $list = static::select(['package_id','package_name'])
            ->where(['package_is_del'=>static::IS_DEL_NO])
            ->orderBy('package_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取套餐名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: YCP
     * Date: 2022/10/18
     */
    public static function packageMation($admin_package)
    {
        $where = [];
        $where['package_is_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_package != '', function ($query) use ($admin_package){
                $query->whereIn('package_id', $admin_package);
            })
            ->get('package_name');
        return $mation;
    }

    /**
     * Notes:检测套餐名是否存在
     * @param $doctor_name
     * @param $doctor_mobile
     * @return bool
     * User: YCP
     * Date: 2022/10/18
     * 验证条件：套餐名存在的情况下
     */
    public static function checkPackageName($package_name)
    {
        return static::where(['package_name' => $package_name, 'package_is_del' => static::IS_DEL_NO])->exists();
    }

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



}
