Goods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\model\goods;
  3. use app\admin\model\box\Detail;
  4. use app\admin\model\setting\Setting;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class Goods extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'goods';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'create_time';
  16. protected $updateTime = 'update_time';
  17. protected $deleteTime = 'delete_time';
  18. // 追加属性
  19. protected $append = [
  20. 'tag_text',
  21. 'status_text',
  22. 'rmb_price',
  23. 'create_time_text',
  24. 'update_time_text',
  25. 'delete_time_text'
  26. ];
  27. protected static function init()
  28. {
  29. Goods::afterDelete(function ($goods) {
  30. // 删除时同步删除盲盒中的商品
  31. $boxDetails = Detail::field('id')->where('goods_id', $goods->id)->select();
  32. foreach ($boxDetails as $item) {
  33. $item->delete();
  34. }
  35. return true;
  36. });
  37. Goods::afterUpdate(function ($goods) {
  38. if (!empty($goods->status) && 'offline' == $goods->status) {
  39. // 下架商品同步删除盲盒中的商品
  40. $boxDetails = Detail::field('id')->where('goods_id', $goods->id)->select();
  41. foreach ($boxDetails as $item) {
  42. $item->delete();
  43. }
  44. }
  45. return true;
  46. });
  47. }
  48. public function getTagList()
  49. {
  50. return ['normal' => __('Tag normal'), 'rare' => __('Tag rare'), 'supreme' => __('Tag supreme'), 'legend' => __('Tag legend')];
  51. }
  52. public function getStatusList()
  53. {
  54. return ['online' => __('Status online'), 'offline' => __('Status offline')];
  55. }
  56. public function getTagTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : (isset($data['tag']) ? $data['tag'] : '');
  59. $list = $this->getTagList();
  60. return isset($list[$value]) ? $list[$value] : '';
  61. }
  62. public function getStatusTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  65. $list = $this->getStatusList();
  66. return isset($list[$value]) ? $list[$value] : '';
  67. }
  68. public function getCreateTimeTextAttr($value, $data)
  69. {
  70. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  71. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  72. }
  73. public function getUpdateTimeTextAttr($value, $data)
  74. {
  75. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  76. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  77. }
  78. public function getDeleteTimeTextAttr($value, $data)
  79. {
  80. $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
  81. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  82. }
  83. public function getRmbPriceAttr($value, $data)
  84. {
  85. if (empty($data['coin_price'])) {
  86. return 0;
  87. }
  88. return round(Setting::getRmbFromCoin($data['coin_price'] ?: 0), 2);
  89. }
  90. protected function setCreateTimeAttr($value)
  91. {
  92. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  93. }
  94. protected function setUpdateTimeAttr($value)
  95. {
  96. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  97. }
  98. protected function setDeleteTimeAttr($value)
  99. {
  100. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  101. }
  102. }