// +---------------------------------------------------------------------- namespace app\rob\model; use think\Model; use think\model\concern\SoftDelete; class ProductModel extends Model { use SoftDelete; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $json = ['image_list']; protected $jsonAssoc = true; /** * 销售状态 */ public function getOnSaleTextAttr($value, $data) { $status = ['', '上架', '下架']; return $status[$data['on_sale']]; } /** * 状态 */ public function getStatusAttr($value, $data) { $start_time = strtotime($data['start_time']); $end_time = strtotime($data['end_time']); $now = time(); if ($start_time < $now && $end_time > $now) { return 2; } if ($start_time > $now) { return 1; } return 3; } public function getStatusTextAttr($value, $data) { $start_time = strtotime($data['start_time']); $end_time = strtotime($data['end_time']); $now = time(); if ($start_time < $now && $end_time > $now) { return '进行中'; } if ($start_time > $now) { return '未开始'; } return '已结束'; } /** * 类型 */ public function getTypeTextAttr($value, $data) { $status = ['', '实体券', '虚拟券']; return $status[$data['type']]; } public function productCode() { return $this->hasMany(ProductCodeModel::class, 'product_id', 'id'); } }