| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace app\common\model;
- use fast\Arr;
- use Google\Service\Dfareporting\ObaIcon;
- use think\Model;
- class ProductPopular extends Model
- {
-
- // 表名
- protected $table = 'product_popular';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'start_time_text',
- 'end_time_text',
- 'create_time_text',
- 'update_time_text'
- ];
-
- //0未开始 1进行中 2已结束 Not started
- const Notstop = 0;
- const Normal = 1;
- const Stop = 2;
- //状态
- public $status_list = [
- self::Notstop => '未开始',
- 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);
- }
- }
|