ShopGoods.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\common\model\shops;
  3. use app\common\model\BaseModel;
  4. use app\common\model\dict\DictData;
  5. use think\facade\Cache;
  6. /**
  7. * ShopGoods模型
  8. * Class ShopGoods
  9. * @package app\common\model\shops
  10. */
  11. class ShopGoods extends BaseModel
  12. {
  13. protected $name = 'shop_goods';
  14. protected $type = [
  15. 'goods_category_ids' => 'array',
  16. 'goods_banners' => 'array',
  17. 'custom_attribute_items' => 'array'
  18. ];
  19. public function goodsCategory()
  20. {
  21. return $this->hasOne(ShopCategory::class, 'id', 'goods_category_id')
  22. ->field('id,name,picture');
  23. }
  24. public function goodSpecsInventory()
  25. {
  26. return $this->hasMany(ShopGoodSpecsInventory::class, 'shop_goods_id', 'id');
  27. }
  28. public function getDeliveryTypeTextAttr($value,$data):string
  29. {
  30. $default = Cache::get('DELIVERY_TYPE');
  31. if (!$default) {
  32. $status = DictData::whereIn('type_value', 'delivery_type')->column('name','value');
  33. Cache::set('DELIVERY_TYPE', json_encode($status,true),5);
  34. } else {
  35. $status = json_decode($default,true);
  36. }
  37. return $status[$data['delivery_type']] ?? '';
  38. }
  39. public function getShopGoodsTypeTextAttr($value,$data):string
  40. {
  41. $default = Cache::get('SHOP_GOODS_TYPE');
  42. if (!$default) {
  43. $status = DictData::whereIn('type_value', 'shop_goods_type')->column('name','value');
  44. Cache::set('SHOP_GOODS_TYPE', json_encode($status,true),5);
  45. } else {
  46. $status = json_decode($default,true);
  47. }
  48. return $status[$data['shop_goods_type']] ?? '';
  49. }
  50. }