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


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


/**
 * ShopOrderGoods模型
 * Class ShopOrderGoods
 * @package app\common\model\shops
 */
class ShopOrderGoods extends BaseModel
{
    
    protected $name = 'shop_order_goods';
    protected $type = [
        'goods_category_ids' =>  'array',
        'goods_banners' => 'array',
    ];


    public function getSpecArrAttr($value,$data)
    {
        $value = [];
        if(in_array($data['specs_type'], [1,2])){
           $value = json_decode($data['specs'],true);
        }
        if($data['specs_type']== 3){
            $customAttributeItems = json_decode($data['custom_attribute_items'],true);
            $specs = json_decode($data['specs'],true);
            foreach ($specs as $k=>$v){
                $specStrArr = explode('-',$v);
                $val = [];
                foreach($customAttributeItems as $kk=>$vv){
                    if($specStrArr[0] == $vv['sign']){
                        $val['title'] = $vv['title'];
                        $val['sign'] = $vv['sign'];
                        foreach($vv['spec_items'] as $kkk=>$vvv){
                            if($specStrArr[1] == $vvv['spec_sign']){
                                $val += $vvv;
                            }
                        }
                        break;
                    }
                }
                $value[] = $val;
            }
        }
        return $value;
    }

    public function goodsSpecsInventory()
    {
        return $this->hasOne(ShopGoodSpecsInventory::class,'id','goods_specs_inventory_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']] ?? '';
    }
}