123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace app\common\model;
- use app\common\library\Service;
- use think\Cache;
- use think\Db;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 商品模型
- */
- class Goods extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_goods';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'url'
- ];
- protected static $config = [];
- protected static $tagCount = 0;
- protected static function init()
- {
- $config = get_addon_config('shop');
- self::$config = $config;
- }
- public function getUrlAttr($value, $data)
- {
- return $this->buildUrl($value, $data);
- }
- public function getFullurlAttr($value, $data)
- {
- return $this->buildUrl($value, $data, true);
- }
- private function buildUrl($value, $data, $domain = false)
- {
- $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
- $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
- $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
- $time = $data['publishtime'] ?? time();
- $vars = [
- ':id' => $data['id'],
- ':diyname' => $diyname,
- ':category' => $cateid,
- ':catename' => $catename,
- ':cateid' => $cateid,
- ':year' => date("Y", $time),
- ':month' => date("m", $time),
- ':day' => date("d", $time),
- ];
- $suffix = static::$config['moduleurlsuffix']['goods'] ?? static::$config['urlsuffix'];
- return addon_url('shop/goods/index', $vars, $suffix, $domain);
- }
- public function getImageAttr($value, $data)
- {
- $value = $value ?: self::$config['default_goods_img'];
- return cdnurl($value, true);
- }
- public function getImagesAttr($value, $data)
- {
- $images = explode(',', $data['images'] ?? '');
- foreach ($images as $index => &$image) {
- $image && $image = cdnurl($image, true);
- }
- return array_filter($images);
- }
- public function setImagesAttr($value, $data)
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getContentAttr($value, $data)
- {
- //组装卡片信息
- return \addons\shop\library\Service::formatSourceTpl($value);
- }
- public static function getIndexGoodsList()
- {
- return self::where('status', 'normal')
- ->where("FIND_IN_SET('recommend',`flag`)")
- ->order('weigh desc')
- ->limit(12)
- ->cache(false)
- ->select();
- }
- /**
- * 获取SQL查询结果
- */
- public static function getQueryList($params)
- {
- $config = get_addon_config('shop');
- $sql = $params['sql'] ?? '';
- $bind = isset($params['bind']) ? explode(',', $params['bind']) : [];
- list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('sql', $params);
- $list = Cache::tag('shop')->get($cacheKey);
- if (!$list) {
- $list = Db::query($sql, $bind);
- Cache::tag('shop')->set($cacheKey, $list, $cacheExpire);
- }
- return $list;
- }
- public static function refreshStar($goods_id)
- {
- $goods = self::get($goods_id);
- if ($goods) {
- $one = Comment::where('goods_id', $goods_id)->field("COUNT(*) as nums,SUM(star) as amount")->find();
- if ($one) {
- $goods->star = floor($one['amount'] / $one['nums']);
- $goods->save();
- }
- }
- }
- /**
- * 获取商品列表
- * @param $params
- * @return array|false|\PDOStatement|string|\think\Collection
- */
- public static function getGoodsList($params)
- {
- $config = get_addon_config('shop');
- $type = empty($params['type']) ? '' : $params['type'];
- $category = !isset($params['category']) ? '' : $params['category'];
- $condition = empty($params['condition']) ? '' : $params['condition'];
- $field = empty($params['field']) ? '*' : $params['field'];
- $flag = empty($params['flag']) ? '' : $params['flag'];
- $row = empty($params['row']) ? 10 : (int)$params['row'];
- $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
- $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
- $limit = empty($params['limit']) ? $row : $params['limit'];
- $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
- $paginate = !isset($params['paginate']) ? false : $params['paginate'];
- $page = !isset($params['page']) ? 1 : (int)$params['page'];
- $with = !isset($params['with']) ? 'category' : $params['with'];
- list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('goodslist', $params);
- $where = ['status' => 'normal'];
- $where['deletetime'] = ['exp', Db::raw('IS NULL')];
- self::$tagCount++;
- $goodsModel = self::with($with)->alias('a');
- if ($category !== '') {
- if ($type === 'son') {
- $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
- //子级
- $where['category_id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
- } elseif ($type === 'sons') {
- //所有子级
- $where['category_id'] = ['in', Category::getCategoryChildrenIds($category)];
- } else {
- $where['category_id'] = ['in', $category];
- }
- }
- //如果有设置标志,则拆分标志信息并构造condition条件
- if ($flag !== '') {
- if (stripos($flag, '&') !== false) {
- $arr = [];
- foreach (explode('&', $flag) as $k => $v) {
- $arr[] = "FIND_IN_SET('{$v}', flag)";
- }
- if ($arr) {
- $condition .= "(" . implode(' AND ', $arr) . ")";
- }
- } else {
- $condition .= ($condition ? ' AND ' : '');
- $arr = [];
- foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
- $arr[] = "FIND_IN_SET('{$v}', flag)";
- }
- if ($arr) {
- $condition .= "(" . implode(' OR ', $arr) . ")";
- }
- }
- }
- $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
- $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
- $modelInfo = null;
- $prefix = config('database.prefix');
- $goodsModel
- ->where($where)
- ->where($condition)
- ->field($field, false, $prefix . "shop_goods", "a")
- ->orderRaw($order);
- if ($paginate) {
- $paginateArr = explode(',', $paginate);
- $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
- $config = [];
- $config['var_page'] = $paginateArr[2] ?? (isset($params['page']) ? 'page' : 'apage' . self::$tagCount);
- $config['path'] = $paginateArr[3] ?? '';
- $config['fragment'] = $paginateArr[4] ?? '';
- $config['query'] = request()->get();
- $config['page'] = $page;
- $list = $goodsModel->paginate($listRows, ($paginateArr[1] ?? false), $config);
- } else {
- $list = $goodsModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select();
- }
- return $list;
- }
- public function Category()
- {
- return $this->belongsTo('Category', 'category_id', 'id');
- }
- public function Sku()
- {
- return $this->hasMany('Sku', 'goods_id', 'id');
- }
- public function Comment()
- {
- return $this->hasMany('Comment', 'goods_id', 'id');
- }
- }
|