<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 医疗产品
 * Class Users
 * @package app\admin\model
 */
class MedicalCareProduct extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'medical_care_product';

    public $timestamps = false;

    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/23
     */
    public static function getProductList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('product_name', 'like', '%' . $keywords . '%');
            })
            ->where('product_is_del',0)
            ->orderBy('product_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

        $count = static::when($keywords != '', function ($query) use ($keywords){
            $query->where('product_name', 'like', '%' . $keywords . '%');
        })->where('product_is_del',0)->count();
        return [$list, $count];
    }
  //修改器销量+虚拟销量
     public function getProductSalesAttribute($value)
    {
        return $value + $this->product_ficti . "(销量:".$value ." + 虚拟:".$this->product_ficti.")";
    }
    public function getProductCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getProductUpdateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    //审核时间
    public function getProductAutoTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function Attr(){
        return $this->hasMany(MedicalCareProductAttrValue::class,'attr_value_product_id','product_id')->where(['attr_value_is_delete'=>0]);
    }
    /**
     * Notes:产品sku关联模型
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: QJF
     * Date: 2022/10/10
     */
    public function sku()
    {
        return $this->hasMany(MedicalCareProductAttrValue::class, 'attr_value_product_id', 'product_id')->where(['attr_value_is_delete' => 0]);
    }

    /**
     * Notes:产品sku类别关联模型
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: QJF
     * Date: 2022/10/10
     */
    public function sku_type()
    {
        return $this->hasMany(MedicalCareProductAttr::class, 'attr_product_id', 'product_id');
    }
    //关联店铺
    public function Shop(){
        return $this->belongsTo(MerchantShop::class,'product_shop_id','shop_id');
    }
    //关联运费模板
    public function Postage(){
        return $this->belongsTo(MedicalCarePostage::class,'product_postage_id','postage_id');
    }
    //获取器修改产品标签
    public function getProductLabelsAttribute($value)
    {
        return explode('|', $value);
    }
    //获取器修改图片
    public function getProductSliderImageAttribute($value)
    {
        return explode(',', $value);
    }
}