xhCategoryService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\models\xhCategory;
  6. class xhCategoryService
  7. {
  8. /**
  9. * 更新分类菜单缓存
  10. */
  11. public static function refreshMenu($sjId)
  12. {
  13. $preKey = dict::getCacheKey('categoryNavigationBar');
  14. $cacheKey = $preKey . $sjId;
  15. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  16. $list = self::getAll($sjId);
  17. Yii::$app->redis->executeCommand('SET', [$cacheKey, json_encode($list)]);
  18. return $list;
  19. }
  20. /**
  21. * 取出所有分类
  22. */
  23. public static function getAll($sjId, $classify = null, $flag = null)
  24. {
  25. $list = self::getByCateogryId(0, $sjId, $classify, $flag);
  26. return $list;
  27. }
  28. /**
  29. * 根据分类id取子集分类 $flag false 默认不带键名 true 返回带键名的
  30. */
  31. public static function getByCateogryId($id, $sjId, $classify = null, $flag = null)
  32. {
  33. $condition = ['parentId' => $id, 'sjId' => $sjId];
  34. if (isset($classify)) {
  35. $condition['classify'] = $classify;
  36. }
  37. $xhCategory = xhCategory::getAllByCondition($condition, 'inTurn asc');
  38. $data = array();
  39. if ($xhCategory) {
  40. foreach ($xhCategory as $k => $v) {
  41. $id = $v['id'];
  42. $name = $v['categoryName'];
  43. $arr = array(
  44. 'id' => $id,
  45. 'categoryName' => $name,
  46. 'classify' => $v['classify'],
  47. 'cover' => $v['cover'],
  48. 'description' => $v['description'],
  49. 'isShow' => $v['isShow'],
  50. 'child' => self::getByCateogryId($id, $sjId, $classify, $flag),
  51. );
  52. if ($flag) {
  53. $data[$id] = $arr;
  54. } else {
  55. $data[] = $arr;
  56. }
  57. }
  58. }
  59. return $data;
  60. }
  61. /**
  62. * 取分类列表,没有层级关系
  63. */
  64. public static function getList($sjId)
  65. {
  66. $preKey = dict::getCacheKey('categoryList');
  67. $cacheKey = $preKey . $sjId;
  68. $string = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  69. if (!empty($string)) {
  70. $arr = json_decode($string, true);
  71. return $arr;
  72. }
  73. $category = xhCategory::getAllByCondition(['sjId' => $sjId]);
  74. $list = [];
  75. if (!empty($category)) {
  76. foreach ($category as $key => $val) {
  77. $list[$val['id']] = $val['categoryName'];
  78. }
  79. }
  80. $string = json_encode($list);
  81. Yii::$app->redis->executeCommand('SET', [$cacheKey, $string]);
  82. return $list;
  83. }
  84. /**
  85. * 添加分类
  86. */
  87. public static function addCategory($data, $parentId, $sjId)
  88. {
  89. $c = xhCategory::getByCondition(['sjId' => $sjId, 'parentId' => $parentId], 'inTurn desc');
  90. $inTurn = !empty($c) ? $c['inTurn'] + 1 : 0;//排到最后面
  91. $data['inTurn'] = $inTurn;
  92. $data['showName'] = $data['categoryName'];
  93. $data['sjId'] = $sjId;
  94. $category = xhCategory::add($data);
  95. $id = $category['id'];
  96. $cacheKey = dict::getCacheKey('category') . $id;
  97. $params = [$cacheKey];
  98. $params[] = 'info_already_get_by_sql';
  99. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  100. foreach ($category as $key => $val) {
  101. $params[] = $key;
  102. $params[] = $val;
  103. }
  104. Yii::$app->redis->executeCommand('HMSET', $params);//添加到缓存
  105. self::refreshMenu($sjId);//更新菜单
  106. return $category;
  107. }
  108. /**
  109. * 修改分类
  110. * @param $id
  111. * @param $data
  112. * @param $sjId
  113. */
  114. public static function updateById($id, $data, $sjId)
  115. {
  116. xhCategory::updateById($id, $data);
  117. $cacheKey = dict::getCacheKey('category') . $id;
  118. $params = [$cacheKey];
  119. foreach ($data as $key => $val) {
  120. $params[] = $key;
  121. $params[] = $val;
  122. }
  123. Yii::$app->redis->executeCommand('HMSET', $params);//更新缓存
  124. self::refreshMenu($sjId);//更新菜单
  125. }
  126. public static function deleteById($id, $sjId)
  127. {
  128. xhCategory::deleteById($id);
  129. $cacheKey = dict::getCacheKey('category') . $id;
  130. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除缓存
  131. $cacheKey = dict::getCacheKey('categoryGoodsList') . $id;
  132. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除 分类下的商品 的缓存
  133. self::refreshMenu($sjId);//刷新菜单缓存
  134. }
  135. public static function getById($id)
  136. {
  137. $category = xhCategory::find()->where(['id' => $id])->asArray()->one();
  138. return $category;
  139. }
  140. //取出要显示的菜单分类
  141. public static function getShowList($sjId)
  142. {
  143. $data = xhCategory::find()->where(['sjId' => $sjId, 'isShow' => 1])->limit(8)->orderBy('inTurn desc')->asArray()->all();
  144. return $data;
  145. }
  146. }