| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace app\common\service;
- use app\common\model\GoodsAttensionModel;
- use app\common\model\ShopGoodsModel;
- use think\Exception;
- use think\facade\Db;
- use utils\RedisCache;
- /**
- * 商品服务 by wes
- * Class ShopGoodsService
- * @package app\common\service
- */
- class ShopGoodsService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ShopGoodsModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取列表
- * @param $map 分组
- * @param $pageSize 分页大小
- * @param $field 返回字段
- * @param $cache 是否缓存数据,默认是
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList($map, $pageSize=10, $field='', $cache=true)
- {
- $page = request()->post('page', 1);
- $cacheKey = "caches:goods:list_{$page}_{$pageSize}_".md5(json_encode($map, 256).$field);
- $list = RedisCache::get($cacheKey);
- if($list && $cache){
- return $list;
- }
- $where = ['on_sale'=> 1];
- $menuId = isset($map['menu_id'])? intval($map['menu_id']) : 0;
- if ($menuId>0){
- $where['menu_id'] = $menuId;
- }
- $goodsType = isset($map['goods_type'])? intval($map['goods_type']) : 0;
- if ($goodsType>0){
- $where['goods_type'] = $goodsType;
- }
- $keywords = isset($map['keywords'])? trim($map['keywords']) : '';
- if (!empty($keywords)) {
- $where['goods_name|hot_keywords'] = "%{$map['keywords']}%";
- }
- $field = $field? $field : 'sort,category,goods_sn,goods_name,goods_type,goods_img,min_original_price as original_price,min_price as price,rebate_score,sales_volume,inventory,attension_count,restrictions_num';
- $order = isset($map['sort']) && $map['sort']? $map['sort'] : 'sort desc,goods_id desc';
- $list = $this->model->where($where)
- ->where(function($query) use($map){
- $cls = isset($map['cls'])? trim($map['cls']) : 0;
- if (!empty($cls)) {
- $query->where('give_vip','>', 0);
- }
- })
- ->withAttr('goods_img',function($value){
- return getPreviewUrl($value);
- })
- ->field($field)
- ->order($order)
- ->paginate($pageSize);
- $list = $list? $list->toArray():[];
- if($list){
- RedisCache::set($cacheKey, $list, rand(10,20));
- }
- return $list;
- }
- /**
- * 获取随机商品
- * @param int $num
- * @return array|mixed
- */
- public function getRandomGoods($num=12)
- {
- $cacheKey = "caches:goods:random:{$num}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where('on_sale', 1)
- ->withAttr('box_pic',function ($value){
- return getPreviewUrl($value);
- })
- ->orderRaw("rand() , goods_id DESC")
- ->field('goods_id,box_pic,goods_sn')
- ->limit($num)
- ->select()
- ->toArray();
- if($data){
- RedisCache::set($cacheKey, $data, rand(5, 10));
- }
- return $data?$data : [];
- }
- /**
- * 获取开奖福袋商品
- * @param int $num
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getBoxMatchGoods($boxType, $canOpen=1, $num = 30)
- {
- $cacheKey = "caches:temp:boxGoods:{$boxType}_{$num}_{$canOpen}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where('box_type', $boxType)
- ->where('goods_type', 2)
- ->where('can_open', $canOpen)
- ->withAttr('goods_img', function($value){
- return getPreviewUrl($value);
- })
- ->field('goods_id,box_type,goods_img,price,goods_name')
- ->limit($num)
- ->select();
- $data = $data? $data->toArray() : [];
- if($data){
- RedisCache::set($cacheKey, $data, 86400);
- }
- return $data;
- }
- /**
- * 获取商品详情
- * @param $map
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getDetail($map)
- {
- $where = [];
- $goodsSn = isset($map['goods_sn'])? trim($map['goods_sn']) : 0;
- $goodsId = isset($map['goods_id'])? intval($map['goods_id']) : 0;
- if($goodsSn){
- $where['goods_sn'] = $goodsSn;
- }
- if($goodsId){
- $where['goods_id'] = $goodsId;
- }
- $isCache = false;
- $cacheKey = "caches:goods:detail_sn{$goodsSn}_g{$goodsId}";
- $info = RedisCache::get($cacheKey);
- if(empty($info)){
- $field = 'menu_id,spec_name,goods_id,category,goods_sn,goods_name,goods_img,goods_img_banner,goods_remark,min_original_price as original_price,min_price as price,note,inventory,rebate_score,sales_volume,restrictions,rush_buy,buynote_template as buyNote,post_template_id,restrictions_num';
- $info = $this->model->where($where)->field($field)
- ->withAttr('goods_img_banner', function ($value) {
- $banners = $value? json_decode($value, true) : [];
- if($banners){
- foreach ($banners as &$v){
- $v = getPreviewUrl($v);
- }
- }
- return $banners;
- })->withAttr('goods_img', function ($value) {
- return getPreviewUrl($value);
- })->withAttr('goods_remark', function ($value) {
- return getContent(htmlspecialchars_decode($value));
- })->withAttr('buyNote', function ($value) {
- return htmlspecialchars_decode($value);
- })->findOrEmpty()->toArray();
- }else{
- $isCache = true;
- }
- if($info){
- $specRelation = ShopGoodsSpecRelationService::make()->getDataByGoods($info['goods_id']);
- $goodsExpress = ExpressDeliveryService::make()->getDataByTemplate($info['post_template_id']);
- $goodsSpec = ShopGoodsSpecService::make()->getListByGoods($info['goods_id']);
- $info['expressNote'] = $goodsExpress['remake'] ?: '';
- $info['spec_relation'] = $specRelation;
- $info['goods_spec'] = $goodsSpec;
- $attensionCount = RedisCache::get("caches:goods:attension:{$info['goods_id']}");
- if($isCache && $attensionCount>0){
- $info['attension_count'] = $attensionCount;
- }
- RedisCache::set($cacheKey, $info, rand(3,5));
- }
- return $info;
- }
- /**
- * 更新商品浏览量
- * @param $goodsId
- * @return false
- */
- public function updateScanCount($goodsId)
- {
- if($goodsId && $this->model->where(['goods_id'=> $goodsId])->inc('scan_count',1)->update()){
- RedisCache::keyDel('caches:goods:list_*');
- return true;
- }
- return false;
- }
- /**
- * 商品收藏
- * @param $uid 用户
- * @param $goodsId
- * @return bool
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function goodsAttension($uid, $goodsId)
- {
- $info = $this->model->where(['goods_id'=> $goodsId])->field('goods_id,attension_count')->find();
- if(!$info){
- throw new Exception('商品不存在');
- }
- // 是否收藏过
- if(GoodsAttensionModel::where(['uid'=> $uid,'goods_id'=> $goodsId])->value('id')){
- return true;
- }else{
- $data = [
- 'uid'=> $uid,
- 'goods_id'=> $goodsId,
- 'create_time'=>sr_getcurtime(time())
- ];
- Db::startTrans();
- if(!GoodsAttensionModel::insertGetId($data)){
- Db::rollback();
- throw new Exception('收藏失败');
- }
- if(!$this->model->where(['goods_id'=> $goodsId])->inc('attension_count')->update()){
- Db::rollback();
- throw new Exception('收藏失败');
- }
- $attenSionCount = isset($info['attension_count'])? $info['attension_count'] : 0;
- RedisCache::set("caches:goods:attension:{$goodsId}", $attenSionCount+1, rand(5, 10));
- RedisCache::keyDel('caches:goods:list*');
- Db::commit();
- return true;
- }
- }
- /**
- * 取消收藏
- * @param $uid 用户
- * @param $goodsId 商品ID
- * @return bool
- */
- public function cancelAttension($uid, $goodsId)
- {
- if(!GoodsAttensionModel::where(['uid'=> $uid,'goods_id'=> $goodsId])->value('id')){
- return true;
- }
- Db::startTrans();
- if(GoodsAttensionModel::where(['uid'=> $uid,'goods_id'=> $goodsId])->delete()){
- RedisCache::keyDel('caches:goods:list_*');
- if(!$this->model->where(['goods_id'=> $goodsId])->dec('attension_count')->update()){
- Db::rollback();
- return false;
- }
- Db::commit();
- RedisCache::keyDel("caches:goods:attension:{$goodsId}");
- return true;
- }else{
- Db::rollback();
- return false;
- }
- }
- /**
- * 是否收藏过该商品
- * @param $uid 用户
- * @param $goodsId 商品
- * @return bool|mixed
- */
- public function checkAttension($uid, $goodsId)
- {
- $cacheKey = "caches:goods:attensionUser:u_{$uid}_g{$goodsId}";
- if(RedisCache::get($cacheKey)){
- return true;
- }
- $data = GoodsAttensionModel::where(['uid'=> $uid,'goods_id'=> $goodsId])->value('id');
- if($data){
- RedisCache::set($cacheKey, $data, rand(5, 10));
- }
- return $data? true : false;
- }
- /**
- * 获取商品缓存信息
- * @param $goodsSn
- * @param string $field
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getCacheInfo($goodsSn, $field='', $cache=true)
- {
- $cacheKey = "caches:goods:info_sn{$goodsSn}".($field? '_'.md5($field) : '');
- $info = RedisCache::get($cacheKey);
- if($info && $cache){
- return $info;
- }
- $field = $field? $field : 'a.goods_id,a.goods_sn,a.post_template_id,a.give_vip,a.goods_type,a.restrictions_num,a.rebate_score,a.goods_name,a.category,a.goods_img,a.supplier,s.name as supplier_name';
- $info = $this->model->alias('a')
- ->leftJoin('shop_supplier s','s.id = a.supplier')
- ->where(['a.goods_sn'=> $goodsSn])
- ->withAttr('goods_img', function ($value) {
- return getPreviewUrl($value);
- })
- ->field($field)
- ->find();
- $info = $info? $info->toArray() : [];
- if($info && $cache){
- RedisCache::set($cacheKey, $info, rand(50, 100));
- }
- return $info;
- }
- /**
- * 获取商品缓存信息
- * @param $goodsId
- * @param string $field
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getCacheInfoById($goodsId, $field='', $cache=true)
- {
- $cacheKey = "caches:goods:info_id{$goodsId}".($field? '_'.md5($field) : '');
- $info = RedisCache::get($cacheKey);
- if($info && $cache){
- return $info;
- }
- $field = $field? $field : 'a.goods_id,a.goods_sn,a.price,a.spec_name,a.post_template_id,a.give_vip,a.goods_type,a.restrictions_num,a.rebate_score,a.goods_name,a.category,a.goods_img,a.supplier,s.name as supplier_name';
- $info = $this->model->alias('a')
- ->leftJoin('shop_supplier s','s.id = a.supplier')
- ->where(['a.goods_id'=> $goodsId])
- ->withAttr('goods_img', function ($value) {
- return getPreviewUrl($value);
- })
- ->field($field)
- ->find();
- $info = $info? $info->toArray() : [];
- if($info && $cache){
- RedisCache::set($cacheKey, $info, rand(50, 100));
- }
- return $info;
- }
- /**
- * 按编号获取商品列表
- * @param $snArr 编号数组
- * @param string $field
- * @return ShopGoodsModel|array|mixed|\think\Model
- */
- public function getListBySn($snArr, $field='')
- {
- $cacheKey = "caches:goods:listSn:".md5(json_encode($snArr, 256).$field);
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $field = $field? $field : 'goods_type';
- $data = $this->model->whereIn('goods_sn', $snArr)->field($field)
- ->withAttr('goods_img', function ($value) {
- return getPreviewUrl($value);
- })->findOrEmpty();
- if($data){
- $data = $data->toArray();
- RedisCache::set($cacheKey, $data, rand(5, 10));
- }
- return $data;
- }
- /**
- * 福袋商品列表
- * @param $boxType 福袋类型,多个类型数组
- * @param int $pageSize 分页数
- * @param bool $cache
- * @return array|mixed
- * @throws \think\db\exception\DbException
- */
- public function getBoxGoodsListByType($boxType, $pageSize=10, $cache=true)
- {
- $key = is_array($boxType)? implode('_', $boxType) : $boxType;
- $cacheKey = "caches:boxGoods:box_{$key}";
- $data = RedisCache::get($cacheKey);
- if($data && $cache){
- return $data;
- }
- $data = $this->model->where(function($query) use($boxType){
- if($boxType){
- $boxType = is_array($boxType)? $boxType : [$boxType];
- $query->whereIn('box_type', $boxType);
- }else{
- $query->whereIn('box_type', [10,20,30,40]);
- }
- })
- ->withAttr('box_pic', function ($value) {
- return getPreviewUrl($value);
- })
- ->withAttr('goods_img', function ($value) {
- return getPreviewUrl($value);
- })
- ->field('goods_id,box_type,box_pic,goods_img,price,goods_name,goods_sn')
- ->order('box_type', 'desc')
- ->paginate($pageSize)
- ->toArray();
- if($data && $cache){
- RedisCache::set($cacheKey, $data, rand(5,10));
- }
- return $data;
- }
- }
|