| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace app\common\service;
- use app\common\model\ShopGoodsSpec;
- use utils\RedisCache;
- /**
- * 商品规格服务 by wes
- * Class ShopGoodsSpecService
- * @package app\common\service
- */
- class ShopGoodsSpecService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->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;
- }
- }
|