123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace app\admin\model\shop;
- use think\Model;
- use traits\model\SoftDelete;
- class Goods extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_goods';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- // 'status_text',
- 'scheduled_online_time_text',
- 'scheduled_offline_time_text',
- ];
- // 规格类型字段映射 - 实际数据库字段就是spec_type
- // 这些方法已经不需要了,因为字段名是一致的
- // public function getUrlAttr($value, $data)
- // {
- // return $this->buildUrl($value, $data);
- // }
- // private function buildUrl($value, $data, $domain = false)
- // {
- // $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
- // $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
- // $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
- // $time = $data['publishtime'] ?? time();
- // $vars = [
- // ':id' => $data['id'],
- // ':diyname' => $diyname,
- // ':category' => $cateid,
- // ':catename' => $catename,
- // ':cateid' => $cateid,
- // ':year' => date("Y", $time),
- // ':month' => date("m", $time),
- // ':day' => date("d", $time),
- // ];
- // $config = get_addon_config('shop');
- // $suffix = $config['moduleurlsuffix']['goods'] ?? $config['urlsuffix'];
- // return addon_url('shop/goods/index', $vars, $suffix, $domain);
- // }
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- self::beforeWrite(function ($row) {
-
- });
- }
- // 计划上架时间的设置器:将前端datetime转为时间戳
- public function setScheduledOnlineTimeAttr($value)
- {
- if (empty($value)) {
- return 0;
- }
-
- // 如果已经是时间戳,直接返回
- if (is_numeric($value) && (int)$value == $value) {
- return $value;
- }
-
- // 如果是日期时间字符串,转换为时间戳
- return strtotime($value);
- }
-
- // 计划上架时间的获取器:将时间戳转为datetime格式
- public function getScheduledOnlineTimeAttr($value)
- {
- if (empty($value) || $value == 0) {
- return '';
- }
-
- return date('Y-m-d H:i:s', $value);
- }
-
- // 计划上架时间的文本属性
- public function getScheduledOnlineTimeTextAttr($value, $data)
- {
- $time = $data['scheduled_online_time'] ?? 0;
- if (empty($time) || $time == 0) {
- return '';
- }
-
- return date('Y-m-d H:i:s', $time);
- }
-
- // 计划下架时间的设置器:将前端datetime转为时间戳
- public function setScheduledOfflineTimeAttr($value)
- {
- if (empty($value)) {
- return 0;
- }
-
- // 如果已经是时间戳,直接返回
- if (is_numeric($value) && (int)$value == $value) {
- return $value;
- }
-
- // 如果是日期时间字符串,转换为时间戳
- return strtotime($value);
- }
-
- // 计划下架时间的获取器:将时间戳转为datetime格式
- public function getScheduledOfflineTimeAttr($value)
- {
- if (empty($value) || $value == 0) {
- return '';
- }
-
- return date('Y-m-d H:i:s', $value);
- }
-
- // 计划下架时间的文本属性
- public function getScheduledOfflineTimeTextAttr($value, $data)
- {
- $time = $data['scheduled_offline_time'] ?? 0;
- if (empty($time) || $time == 0) {
- return '';
- }
-
- return date('Y-m-d H:i:s', $time);
- }
- protected function setAttributeIdsAttr($value)
- {
- $ids = [];
- foreach ($value as $item) {
- foreach ($item as $id) {
- if ($id) {
- $ids[] = $id;
- }
- }
- }
- return implode(',', $ids);
- }
- public function Freight()
- {
- return $this->belongsTo('Freight', 'freight_id', 'id', [], 'LEFT');
- }
- public function Brand()
- {
- return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT');
- }
- public function GoodsSku()
- {
- return $this->belongsTo('GoodsSku', 'id', 'goods_id', [], 'LEFT');
- }
- public function Category()
- {
- return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT');
- }
- }
|