$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); } }