model = new ShopOrderGoods(); } /** * 静态化入口 * @return static|null */ public static function make() { if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 用户购买过该商品的数量统计 * @param $uid 用户ID * @param int $goodsId 商品ID,0表示用户购买的所有商品数量统计 * @return array|mixed */ public function getCountByUserAndGoods($uid, $goodsSn='0') { $cacheKey = "caches:orders:goodsCount:{$uid}_{$goodsSn}"; $data = RedisCache::get($cacheKey); if($data){ return $data; } $where = ['og.uid'=>$uid]; if($goodsSn){ $where['g.goods_sn'] = $goodsSn; } $data = $this->model->alias('og') ->leftJoin('shop_order o', 'o.order_id = og.order_id') ->leftJoin('shop_goods g', 'og.goods_id = g.goods_id') ->where('o.status', 'in', [1, 2, 4]) ->where($where) ->count('og.og_id'); if($data){ RedisCache::set($cacheKey, $data, rand(3,5)); } return $data; } }