| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\models\xhGoodsCategory;
- class xhGoodsCategoryService
- {
-
- /**
- * 根据分类id取商品列表
- */
- public static function getGoodsListByCId($cId)
- {
- $list = self::getGoodsIdByCId($cId);
- $goodsList = [];
- if (!empty($list)) {
- foreach ($list as $key => $val) {
- $goodsId = $val['gId'];
- $goodsInfo = xhGoodsService::getById($goodsId);
-
- //取出较小的封面图片 ssh 2019.8.3
- $url = $goodsInfo['cover'];
- $pos = strrpos($url, '.');
- $pre = substr($url, 0, $pos);
- $ext = substr($url, ($pos + 1));
- $goodsInfo['middleCover'] = $pre . '_200.' . $ext;
-
- $goodsList[] = $goodsInfo;
- }
- }
- return $goodsList;
- }
-
- /**
- * 根据分类id取商品id列表
- */
- public static function getGoodsIdByCId($cId)
- {
- $list = xhGoodsCategory::getAllByCondition(['cId' => $cId], 'inTurn desc');
- $ids = [];
- if (!empty($list)) {
- foreach ($list as $key => $val) {
- $ids[] = ['inTurn' => $val['inTurn'], 'gId' => $val['gId']];
- }
- }
- return $ids;
- }
-
- public static function refreshByCId($cId)
- {
- $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- $list = xhGoodsCategory::getAllByCondition(['cId' => $cId]);
- $ids = [];
- if (!empty($list)) {
- foreach ($list as $key => $val) {
- $addList = [];
- $addList[] = $cacheKey;
- $addList[] = $val['id'];
- $addList[] = $val['gId'];
- Yii::$app->redis->executeCommand('ZADD', $addList);//有序合集
- }
- }
- }
-
- public static function add($data, $cId, $gId)
- {
- xhGoodsCategory::add($data);
- $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
- Yii::$app->redis->executeCommand('ZADD', [$cacheKey, 100, $gId]);//有序合集,参数中100 是排序默认值 对应表xhGoodsCategory的inTurn值
- }
-
- /**
- * 修改商品时,修改商品分类,删除原有分类
- */
- public static function delGId($cId, $gId)
- {
- xhGoodsCategory::deleteByCondition(['gId' => $gId, 'cId' => $cId]);
- $cacheKey = dict::getCacheKey('categoryGoodsList') . $cId;
- Yii::$app->redis->executeCommand('ZREM', [$cacheKey, $gId]);//有序合集,参数中100 是排序默认值 对应表xhGoodsCategory的inTurn值
- }
-
- /**
- * 删除商品,清理想商品下的分类
- */
- public static function delGoodsCategoryId($goods)
- {
- $id = $goods['id'];
- xhGoodsCategory::deleteByCondition(['gId' => $id]);//先删除分类表里商品
- $categoryString = $goods['categoryId'];
- if (!empty($categoryString)) {
- $categoryList = explode(',', $categoryString);
- $categoryList = array_filter($categoryList);
- if (!empty($categoryList)) {
- foreach ($categoryList as $cId) {
- self::refreshByCId($cId);//再更新删除分类的缓存
- }
- }
- }
- }
-
- public static function updateByCId($cId, $data)
- {
- xhSlide::updateByCondition(['cId' => $cId], $data);
- self::refreshByCId($cId);
- }
-
- public static function getByCondition($condition)
- {
- return xhGoodsCategory::getByCondition($condition);
- }
-
- /**
- * 批量更新
- */
- public static function batch_update($goodsId, $catData)
- {
- xhGoodsCategory::deleteByCondition(['gId' => $goodsId]);
- xhGoodsCategory::batchAdd($catData);
- }
- }
|