GoodsLabel.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. use app\common\Enum\StatusEnum;
  5. use app\common\Enum\GoodsLabelEnum;
  6. class GoodsLabel extends Model
  7. {
  8. protected $name = 'shop_goods_label';
  9. protected $autoWriteTimestamp = 'int';
  10. protected $createTime = 'createtime';
  11. protected $updateTime = 'updatetime';
  12. protected $deleteTime = 'deletetime';
  13. protected $append = ['status_text', 'style_type_text'];
  14. public function getStatusList()
  15. {
  16. return StatusEnum::getMap();
  17. }
  18. public function getStyleTypeList()
  19. {
  20. return GoodsLabelEnum::getMap();
  21. }
  22. public function getStatusTextAttr($value, $data)
  23. {
  24. $value = $value ?: ($data['status'] ?? '');
  25. $list = $this->getStatusList();
  26. return $list[$value] ?? '';
  27. }
  28. public function getStyleTypeTextAttr($value, $data)
  29. {
  30. $value = $value ?: ($data['style_type'] ?? '');
  31. $list = $this->getStyleTypeList();
  32. return $list[$value] ?? '';
  33. }
  34. public function group()
  35. {
  36. return $this->belongsTo('GoodsLabelGroup', 'group_id', 'id');
  37. }
  38. public function getColorJsonAttr($value)
  39. {
  40. return is_string($value) ? (json_decode($value, true) ?: []) : [];
  41. }
  42. public function setColorJsonAttr($value)
  43. {
  44. return is_array($value) ? json_encode($value) : $value;
  45. }
  46. }