model = new ShopGoodsSpec(); } /** * 静态化入口 * @return static|null */ public static function make() { if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 获取规格列表数据 * @param $goodsId 商品ID * @param string $field 返回字段 * @param string $cache 是否缓存 * @return array|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getListByGoods($goodsId, $field='', $cache=false) { if($goodsId<=0){ return false; } $cacheKey = "caches:goodsSpec:list_{$goodsId}".($field? '_'.md5($field):''); $data = RedisCache::get($cacheKey); if($data && $cache){ return $data; } $where = ['goods_id'=> $goodsId]; $field = $field? $field : 'spec_ids,spec_text,original_price,price,picture,stock'; $data = $this->model->where($where)->field($field)->select(); $data = $data? $data->toArray():[]; if($data && $cache){ RedisCache::set($cacheKey, $data, rand(3, 5)); } return $data; } /** * 获取商品规格库存 * @param $goodsSn 商品编号 * @param $specIds * @return array|mixed */ public function getStock($goodsSn, $specIds) { $cacheKey = "caches:goodsSpec:stock:{$goodsSn}_".md5($specIds); $data = RedisCache::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['goods_sn'=> $goodsSn,'spec_ids'=> $specIds])->value('stock'); if($data){ RedisCache::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 获取商品规格数据 * @param $goodsId * @param $specIds * @param string $field * @param bool $cache * @return array|false|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getDataByGoods($goodsId, $specIds, $field='', $cache=true) { if($goodsId<=0){ return false; } $cacheKey = "caches:goodsSpec:list_{$goodsId}".($specIds? '_'.md5(json_encode($specIds, 256)):''); $data = RedisCache::get($cacheKey); if($data && $cache){ return $data; } $where = ['goods_id'=> $goodsId]; $field = $field? $field : 'goods_spec_id,spec_ids,spec_text,original_price,price,rebate_score,weight,picture,stock'; $data = $this->model->where($where)->field($field)->find(); $data = $data? $data->toArray():[]; if($data && $cache){ RedisCache::set($cacheKey, $data, rand(3, 5)); } return $data; } }