'未开始', self::Stop => '停用', self::Normal => '进行中' ]; //类型 const TypeSingle = 1; //单品 const TypeGroup = 2; //组合 //刷新库存状态: 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+init_num) as num,start_time,end_time,b.is_area,b.video,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+init_num) as num,start_time,end_time,b.is_area,b.video,b.details') ->where('a.id', $ids) ->order('a.weigh desc') ->find(); } return $info; } //获取当前抢购产品 public static function getPopularByTime( $productId, string $lan, $tim) { return self::alias('a') ->where('a.start_time', '<=', $tim) ->where('a.end_time', '>=', $tim) ->join('product_list b', "a.product_id = b.id", "left") ->field('a.*,b.is_area,'.$lan.'_name as name') ->where('a.product_id', $productId)->find(); } //分类 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); } }