<?php
namespace app\common\model\shops;


use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\facade\Cache;


/**
 * ShopGoods模型
 * Class ShopGoods
 * @package app\common\model\shops
 */
class ShopGoods extends BaseModel
{
    
    protected $name = 'shop_goods';

    protected $type = [
        'goods_category_ids' =>  'array',
        'goods_banners' => 'array',
        'custom_attribute_items' => 'array'
    ];

    public function goodsCategory()
    {
        return $this->hasOne(ShopCategory::class, 'id', 'goods_category_id')
            ->field('id,name,picture');
    }

    public function goodSpecsInventory()
    {
        return $this->hasMany(ShopGoodSpecsInventory::class, 'shop_goods_id', 'id');

    }

    public function getDeliveryTypeTextAttr($value,$data):string
    {
        $default = Cache::get('DELIVERY_TYPE');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'delivery_type')->column('name','value');
            Cache::set('DELIVERY_TYPE', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
        return $status[$data['delivery_type']] ?? '';
    }

    public function getShopGoodsTypeTextAttr($value,$data):string
    {
        $default = Cache::get('SHOP_GOODS_TYPE');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'shop_goods_type')->column('name','value');
            Cache::set('SHOP_GOODS_TYPE', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
        return $status[$data['shop_goods_type']] ?? '';
    }

}