| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\models\xhRecommend;
- use common\models\xhGoods;
- class xhRecommendService {
- public static function getByCondition($condition)
- {
- return xhRecommend::getByCondition($condition);
- }
- /**
- * 获取首页推荐图片明细列表
- */
- public static function getSlide($sjId)
- {
- $recommend = self::getBySjId($sjId);
- $slide = $recommend['slide'];
- $list = [];
- if(!empty($slide)){
- $list = json_decode($slide,true);
- }
- return $list;
- }
- /**
- * 获取推荐商品的明细列表
- */
- public static function getGoodsList($sjId)
- {
- $recommend = self::getBySjId($sjId);
- $goods = $recommend['goods'];
- $list = [];
- if(!empty($goods)){
- $arr = explode(',',trim($goods));
- foreach($arr as $id){
- $list[] = xhGoodsService::getById($id);
- }
- }
- return $list;
- }
- /**
- * 取推荐商品的id列表
- * @param unknown $sjId
- * @return multitype:
- */
- public static function getGoodsIds($sjId)
- {
- $recommend = self::getBySjId($sjId);
- $goods = $recommend['goods'];
- $arr = [];
- if(!empty($goods)){
- $arr = explode(',',trim($goods));
- }
- return $arr;
- }
- /**
- * 从推荐商品里去除某个商品
- */
- public static function wipeGoodsId($id,$sjId)
- {
- $recommend = xhRecommendService::getBySjId($sjId);
- $goods = $recommend['goods'];
- if(!empty($goods)){
- $arr = explode(',',$goods);
- $newGoods = array_diff($arr,[$id]);
- $string = implode(',',$newGoods);
- xhRecommendService::updateBySjId($sjId, ['goods' =>$string]);
- }
- }
- /**
- * 获取推荐信息
- */
- public static function getBySjId($sjId)
- {
- $recommend = xhRecommend::find()->where(['sjId' => $sjId])->asArray()->one();
- return $recommend;
- }
- public static function refreshRecommend($sjId)
- {
- $cacheKey = dict::getCacheKey('recommendGoods').$sjId;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getBySjId($sjId);
- }
- public static function add($data,$sjId)
- {
- xhRecommend::add($data);
- self::refreshRecommend($sjId);
- }
- public static function updateBySjId($sjId,$data)
- {
- xhRecommend::updateByCondition(['sjId' => $sjId], $data);
- self::refreshRecommend($sjId);
- }
- }
|