123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace app\common\model;
- use app\common\library\Service;
- use think\Cache;
- use think\Db;
- use think\Model;
- use traits\model\SoftDelete;
- use app\common\Service\ShopConfigService;
- /**
- * 商品模型
- */
- class Goods extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_goods';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- ];
- protected static $config = [];
- protected static $tagCount = 0;
- protected static function init()
- {
- $config = ShopConfigService::getConfigs('shop.goods',false);
- self::$config = $config;
- }
- 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 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)
- {
- $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)
- {
- $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 !== '') {
- $categoryIds = [];
- if ($type === 'son') {
- $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql();
- //子级
- $categoryIds = Db::query($subQuery);
- $categoryIds = array_column($categoryIds, 'id');
- } elseif ($type === 'sons') {
- //所有子级
- $categoryIds = Category::getCategoryChildrenIds($category);
- } else {
- $categoryIds = is_array($category) ? $category : explode(',', $category);
- }
-
- if (!empty($categoryIds)) {
- $condition .= ($condition ? ' AND ' : '') . '(';
- $conditionParts = [];
- foreach ($categoryIds as $categoryId) {
- $conditionParts[] = "FIND_IN_SET('{$categoryId}', category_ids)";
- }
- $condition .= implode(' OR ', $conditionParts) . ')';
- }
- }
- //如果有设置标志,则拆分标志信息并构造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()
- // {
- // // 由于现在使用category_ids字段,这里返回主分类(第一个分类)
- // return $this->belongsTo('Category', 'category_id', 'id');
- // }
-
- public function Categories()
- {
- // 新增:获取商品的所有分类
- $categoryIds = explode(',', $this->category_ids ?: '');
- return Category::where('id', 'in', array_filter($categoryIds))->select();
- }
- public function Sku()
- {
- return $this->hasMany('Sku', 'goods_id', 'id');
- }
- public function Comment()
- {
- return $this->hasMany('Comment', 'goods_id', 'id');
- }
- public function Brand()
- {
- return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT');
- }
-
-
- }
|