| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\common\model;
- use think\Model;
- use app\common\library\Auth;
- class ProductLists extends Model
- {
- // 表名
- protected $table = 'product_list';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'create_time_text',
- 'update_time_text'
- ];
-
- const Stop = 0;
- const Normal = 1;
- //状态
- public $status_list = [
- '-1' => '全部',
- self::Stop => '停用',
- self::Normal => '正常'
- ];
- //获取产品信息
- public static function getBySynthesisProduct(string $product_id, int $type_id, $lan = 'zh')
- {
- if(empty($product_id)) return [];
- $info = self::whereIn('id', $product_id);
- if($type_id == ProductPledges::Combin) $info = $info->orderRaw('field(id,'. $product_id.')');
- $info = $info->field('id,thum,'.$lan.'_name as name')->select();
- return $info ;
- }
- //地区
- public function productarea()
- {
- return $this->hasMany('ProductArea', 'product_id', 'id')->field('id,product_id');
- }
- //已出售地区
- public function productareastop()
- {
- return $this->hasMany('ProductArea', 'product_id', 'id')->where('status', 0)->field('id,product_id');
- }
- //hold_num
- public function productorder()
- {
- return $this->hasMany('ProductOrder', 'product_id', 'id')->where('status', 'in', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->field('id,product_id');
- }
- //分类
- public function products()
- {
- return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
- }
- //抢购 ProductPopular
- public function productpopular()
- {
- return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
- }
- //寄售
- public function producttransfer()
- {
- $map = [];
- $transfer = input('post.transfer_id/d', 0);
- if($transfer > 0) $map['id'] = $transfer;
- $order = 'id desc,price ASC';
- if(input('post.sort') == 1) $order = 'price desc';
- if(input('post.sort') == 2) $order = 'price asc';
- return $this->hasMany('ProductTransfer', 'product_id', 'id', [], 'LEFT')->where($map)->where('status', self::Normal)->order($order);
- }
-
- //收藏
- public function collect()
- {
-
- return $this->hasOne('UserCollect', 'product_id', 'id', [], 'LEFT')->where('user_id', Auth::instance()->getUser()['id']);
- }
-
-
- //获取产品分类
- public static function getProductTypeById(int $productId)
- {
- return self::where('id', $productId)->value('type_id');
- }
- //获取状态
- public function getStatusTextAttr($value, $data){
- $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
- }
-
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- }
-
- 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 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);
- }
- }
|