'未开始', self::STOP => '停用', self::NORMAL => '进行中' ]; //刷新库存状态: status 状态0未开始 1进行中 2已结束 public static function setUpdateStatus() { $count =0; $time = time(); $list = self::where('status', '<', self::STOP)->select(); foreach ($list as $item) { //未开始 if ( $item->start_time > $time) { $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NOTSTOP]); } //进行中 if ($time >= $item->start_time && $time < $item->end_time) { $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NORMAL]); } //已结束 if($time >= $item->end_time){ $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::STOP]); } } return $count; } //获取热销详情 public static function getProductInfo(int $product_id, int $ids, string $lan): object { $info = self::alias('a') ->join("product_list b", "a.product_id = b.id", "left") ->field('a.id,a.product_id,'.'b.'.$lan.'_name as name,b.images as img_url,price,cost_price,stock,num,start_time,end_time,b.details') ->where('a.product_id', $product_id) ->where('a.status', self::NORMAL) ->find(); if (empty($info)) { $info = self::alias('a') ->join("product_list b", "a.product_id = b.id", "left") ->field('a.id,a.product_id,'.'b.'.$lan.'_name as name,b.images as img_url,price,cost_price,stock,num,start_time,end_time,b.details') ->where('a.id', $ids) ->order('a.weigh desc') ->find(); } return $info; } //分类 public function productsList() { return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0); } protected static function init() { self::afterInsert(function ($row) { $pk = $row->getPk(); $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]); }); } public function getStartTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getEndTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getCreateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUpdateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } protected function setStartTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } protected function setEndTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } protected function setCreateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } protected function setUpdateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } }