xhGoodsCategoryService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\models\xhGoodsCategory;
  6. class xhGoodsCategoryService
  7. {
  8. /**
  9. * 根据分类id取商品列表
  10. */
  11. public static function getGoodsListByCId($cId)
  12. {
  13. $list = self::getGoodsIdByCId($cId);
  14. $goodsList = [];
  15. if (!empty($list)) {
  16. foreach ($list as $key => $val) {
  17. $goodsId = $val['gId'];
  18. $goodsInfo = xhGoodsService::getById($goodsId);
  19. //取出较小的封面图片 ssh 2019.8.3
  20. $url = $goodsInfo['cover'];
  21. $pos = strrpos($url, '.');
  22. $pre = substr($url, 0, $pos);
  23. $ext = substr($url, ($pos + 1));
  24. $goodsInfo['middleCover'] = $pre . '_200.' . $ext;
  25. $goodsList[] = $goodsInfo;
  26. }
  27. }
  28. return $goodsList;
  29. }
  30. /**
  31. * 根据分类id取商品id列表
  32. */
  33. public static function getGoodsIdByCId($cId)
  34. {
  35. $list = xhGoodsCategory::getAllByCondition(['cId' => $cId], 'inTurn desc');
  36. $ids = [];
  37. if (!empty($list)) {
  38. foreach ($list as $key => $val) {
  39. $ids[] = ['inTurn' => $val['inTurn'], 'gId' => $val['gId']];
  40. }
  41. }
  42. return $ids;
  43. }
  44. public static function refreshByCId($cId)
  45. {
  46. $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
  47. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  48. $list = xhGoodsCategory::getAllByCondition(['cId' => $cId]);
  49. $ids = [];
  50. if (!empty($list)) {
  51. foreach ($list as $key => $val) {
  52. $addList = [];
  53. $addList[] = $cacheKey;
  54. $addList[] = $val['id'];
  55. $addList[] = $val['gId'];
  56. Yii::$app->redis->executeCommand('ZADD', $addList);//有序合集
  57. }
  58. }
  59. }
  60. public static function add($data, $cId, $gId)
  61. {
  62. xhGoodsCategory::add($data);
  63. $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
  64. Yii::$app->redis->executeCommand('ZADD', [$cacheKey, 100, $gId]);//有序合集,参数中100 是排序默认值 对应表xhGoodsCategory的inTurn值
  65. }
  66. /**
  67. * 修改商品时,修改商品分类,删除原有分类
  68. */
  69. public static function delGId($cId, $gId)
  70. {
  71. xhGoodsCategory::deleteByCondition(['gId' => $gId, 'cId' => $cId]);
  72. $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
  73. Yii::$app->redis->executeCommand('ZREM', [$cacheKey, $gId]);//有序合集,参数中100 是排序默认值 对应表xhGoodsCategory的inTurn值
  74. }
  75. /**
  76. * 删除商品,清理想商品下的分类
  77. */
  78. public static function delGoodsCategoryId($goods)
  79. {
  80. $id = $goods['id'];
  81. xhGoodsCategory::deleteByCondition(['gId' => $id]);//先删除分类表里商品
  82. $categoryString = $goods['categoryId'];
  83. if (!empty($categoryString)) {
  84. $categoryList = explode(',', $categoryString);
  85. $categoryList = array_filter($categoryList);
  86. if (!empty($categoryList)) {
  87. foreach ($categoryList as $cId) {
  88. self::refreshByCId($cId);//再更新删除分类的缓存
  89. }
  90. }
  91. }
  92. }
  93. public static function updateByCId($cId, $data)
  94. {
  95. xhSlide::updateByCondition(['cId' => $cId], $data);
  96. self::refreshByCId($cId);
  97. }
  98. public static function getByCondition($condition)
  99. {
  100. return xhGoodsCategory::getByCondition($condition);
  101. }
  102. /**
  103. * 批量更新
  104. */
  105. public static function batch_update($goodsId, $catData)
  106. {
  107. xhGoodsCategory::deleteByCondition(['gId' => $goodsId]);
  108. xhGoodsCategory::batchAdd($catData);
  109. }
  110. }