Goods.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. class Goods extends Model
  6. {
  7. use SoftDelete;
  8. // 表名
  9. protected $name = 'shop_goods';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = 'deletetime';
  16. // 追加属性
  17. protected $append = [
  18. // 'status_text',
  19. 'scheduled_online_time_text',
  20. 'scheduled_offline_time_text',
  21. ];
  22. // 规格类型字段映射 - 实际数据库字段就是spec_type
  23. // 这些方法已经不需要了,因为字段名是一致的
  24. // public function getUrlAttr($value, $data)
  25. // {
  26. // return $this->buildUrl($value, $data);
  27. // }
  28. // private function buildUrl($value, $data, $domain = false)
  29. // {
  30. // $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  31. // $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
  32. // $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
  33. // $time = $data['publishtime'] ?? time();
  34. // $vars = [
  35. // ':id' => $data['id'],
  36. // ':diyname' => $diyname,
  37. // ':category' => $cateid,
  38. // ':catename' => $catename,
  39. // ':cateid' => $cateid,
  40. // ':year' => date("Y", $time),
  41. // ':month' => date("m", $time),
  42. // ':day' => date("d", $time),
  43. // ];
  44. // $config = get_addon_config('shop');
  45. // $suffix = $config['moduleurlsuffix']['goods'] ?? $config['urlsuffix'];
  46. // return addon_url('shop/goods/index', $vars, $suffix, $domain);
  47. // }
  48. protected static function init()
  49. {
  50. self::afterInsert(function ($row) {
  51. $pk = $row->getPk();
  52. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  53. });
  54. self::beforeWrite(function ($row) {
  55. });
  56. }
  57. // 计划上架时间的设置器:将前端datetime转为时间戳
  58. public function setScheduledOnlineTimeAttr($value)
  59. {
  60. if (empty($value)) {
  61. return 0;
  62. }
  63. // 如果已经是时间戳,直接返回
  64. if (is_numeric($value) && (int)$value == $value) {
  65. return $value;
  66. }
  67. // 如果是日期时间字符串,转换为时间戳
  68. return strtotime($value);
  69. }
  70. // 计划上架时间的获取器:将时间戳转为datetime格式
  71. public function getScheduledOnlineTimeAttr($value)
  72. {
  73. if (empty($value) || $value == 0) {
  74. return '';
  75. }
  76. return date('Y-m-d H:i:s', $value);
  77. }
  78. // 计划上架时间的文本属性
  79. public function getScheduledOnlineTimeTextAttr($value, $data)
  80. {
  81. $time = $data['scheduled_online_time'] ?? 0;
  82. if (empty($time) || $time == 0) {
  83. return '';
  84. }
  85. return date('Y-m-d H:i:s', $time);
  86. }
  87. // 计划下架时间的设置器:将前端datetime转为时间戳
  88. public function setScheduledOfflineTimeAttr($value)
  89. {
  90. if (empty($value)) {
  91. return 0;
  92. }
  93. // 如果已经是时间戳,直接返回
  94. if (is_numeric($value) && (int)$value == $value) {
  95. return $value;
  96. }
  97. // 如果是日期时间字符串,转换为时间戳
  98. return strtotime($value);
  99. }
  100. // 计划下架时间的获取器:将时间戳转为datetime格式
  101. public function getScheduledOfflineTimeAttr($value)
  102. {
  103. if (empty($value) || $value == 0) {
  104. return '';
  105. }
  106. return date('Y-m-d H:i:s', $value);
  107. }
  108. // 计划下架时间的文本属性
  109. public function getScheduledOfflineTimeTextAttr($value, $data)
  110. {
  111. $time = $data['scheduled_offline_time'] ?? 0;
  112. if (empty($time) || $time == 0) {
  113. return '';
  114. }
  115. return date('Y-m-d H:i:s', $time);
  116. }
  117. protected function setAttributeIdsAttr($value)
  118. {
  119. $ids = [];
  120. foreach ($value as $item) {
  121. foreach ($item as $id) {
  122. if ($id) {
  123. $ids[] = $id;
  124. }
  125. }
  126. }
  127. return implode(',', $ids);
  128. }
  129. public function Freight()
  130. {
  131. return $this->belongsTo('Freight', 'freight_id', 'id', [], 'LEFT');
  132. }
  133. public function Brand()
  134. {
  135. return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT');
  136. }
  137. public function GoodsSku()
  138. {
  139. return $this->belongsTo('GoodsSku', 'id', 'goods_id', [], 'LEFT');
  140. }
  141. public function Category()
  142. {
  143. return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT');
  144. }
  145. }