xhRecommendService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\models\xhRecommend;
  6. use common\models\xhGoods;
  7. class xhRecommendService {
  8. public static function getByCondition($condition)
  9. {
  10. return xhRecommend::getByCondition($condition);
  11. }
  12. /**
  13. * 获取首页推荐图片明细列表
  14. */
  15. public static function getSlide($sjId)
  16. {
  17. $recommend = self::getBySjId($sjId);
  18. $slide = $recommend['slide'];
  19. $list = [];
  20. if(!empty($slide)){
  21. $list = json_decode($slide,true);
  22. }
  23. return $list;
  24. }
  25. /**
  26. * 获取推荐商品的明细列表
  27. */
  28. public static function getGoodsList($sjId)
  29. {
  30. $recommend = self::getBySjId($sjId);
  31. $goods = $recommend['goods'];
  32. $list = [];
  33. if(!empty($goods)){
  34. $arr = explode(',',trim($goods));
  35. foreach($arr as $id){
  36. $list[] = xhGoodsService::getById($id);
  37. }
  38. }
  39. return $list;
  40. }
  41. /**
  42. * 取推荐商品的id列表
  43. * @param unknown $sjId
  44. * @return multitype:
  45. */
  46. public static function getGoodsIds($sjId)
  47. {
  48. $recommend = self::getBySjId($sjId);
  49. $goods = $recommend['goods'];
  50. $arr = [];
  51. if(!empty($goods)){
  52. $arr = explode(',',trim($goods));
  53. }
  54. return $arr;
  55. }
  56. /**
  57. * 从推荐商品里去除某个商品
  58. */
  59. public static function wipeGoodsId($id,$sjId)
  60. {
  61. $recommend = xhRecommendService::getBySjId($sjId);
  62. $goods = $recommend['goods'];
  63. if(!empty($goods)){
  64. $arr = explode(',',$goods);
  65. $newGoods = array_diff($arr,[$id]);
  66. $string = implode(',',$newGoods);
  67. xhRecommendService::updateBySjId($sjId, ['goods' =>$string]);
  68. }
  69. }
  70. /**
  71. * 获取推荐信息
  72. */
  73. public static function getBySjId($sjId)
  74. {
  75. $recommend = xhRecommend::find()->where(['sjId' => $sjId])->asArray()->one();
  76. return $recommend;
  77. }
  78. public static function refreshRecommend($sjId)
  79. {
  80. $cacheKey = dict::getCacheKey('recommendGoods').$sjId;
  81. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  82. self::getBySjId($sjId);
  83. }
  84. public static function add($data,$sjId)
  85. {
  86. xhRecommend::add($data);
  87. self::refreshRecommend($sjId);
  88. }
  89. public static function updateBySjId($sjId,$data)
  90. {
  91. xhRecommend::updateByCondition(['sjId' => $sjId], $data);
  92. self::refreshRecommend($sjId);
  93. }
  94. }