<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 绿色蔬菜模型
 * Class Users
 * @packagegoods app\admin\model
 */
class LifeFarmGoods 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_farm_goods';

    public $timestamps = false;

    /**
     * Notes:获取绿色蔬菜列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/11/4
     */
    public static function getFarmGoodsList(int $page, int $limit, string $keywords)
    {
        $list = static::select('*')
            ->where(['goods_is_del'=>static::IS_DEL_NO])
            ->with(['Shop','Category','MedicalCarePostage'])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('goods_name', 'like', '%' . $keywords . '%');
            })
            ->orderBy('goods_sort','DESC')
            ->forPage($page, $limit)
            ->get();

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

    //关联店铺
    public function Shop(){
        return $this->belongsTo(MerchantShop::class,'goods_shop_id','shop_id');
    }
    //关联分类
    public function Category(){
        return $this->belongsTo(Category::class,'goods_category_id','category_id');
    }
    //关联运费模板
    public function MedicalCarePostage(){
        return $this->belongsTo(MedicalCarePostage::class,'goods_postage_id','postage_id');
    }


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



}
