diff --git a/framework/src/main/java/cn/lili/common/cache/CachePrefix.java b/framework/src/main/java/cn/lili/common/cache/CachePrefix.java index 49d4fc13..e2734e35 100644 --- a/framework/src/main/java/cn/lili/common/cache/CachePrefix.java +++ b/framework/src/main/java/cn/lili/common/cache/CachePrefix.java @@ -177,7 +177,7 @@ public enum CachePrefix { /** * 店铺管理员角色权限对照表 */ - SHOP_URL_ROLE, + STORE_URL_ROLE, /** * 手机验证标识 @@ -449,7 +449,12 @@ public enum CachePrefix { /** * 文章 */ - ARTICLE_CACHE + ARTICLE_CACHE, + + /** + * 店铺分类 + */ + STORE_CATEGORY ; diff --git a/framework/src/main/java/cn/lili/modules/store/service/StoreGoodsLabelService.java b/framework/src/main/java/cn/lili/modules/store/service/StoreGoodsLabelService.java index 7ba4c0f9..2a886deb 100644 --- a/framework/src/main/java/cn/lili/modules/store/service/StoreGoodsLabelService.java +++ b/framework/src/main/java/cn/lili/modules/store/service/StoreGoodsLabelService.java @@ -22,13 +22,6 @@ public interface StoreGoodsLabelService extends IService { */ List listByStoreId(String storeId); - /** - * 获取当前店铺的店铺分类列表 - * - * @return 店铺分类列表 - */ - List listByStore(); - /** * 添加商品分类 * diff --git a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreGoodsLabelServiceImpl.java b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreGoodsLabelServiceImpl.java index 38f69fee..72b1513e 100644 --- a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreGoodsLabelServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreGoodsLabelServiceImpl.java @@ -1,7 +1,10 @@ package cn.lili.modules.store.serviceimpl; +import cn.lili.common.cache.Cache; +import cn.lili.common.cache.CachePrefix; import cn.lili.common.security.AuthUser; import cn.lili.common.security.context.UserContext; +import cn.lili.modules.goods.entity.vos.CategoryVO; import cn.lili.modules.store.entity.dos.StoreGoodsLabel; import cn.lili.modules.store.entity.vos.StoreGoodsLabelVO; import cn.lili.modules.store.mapper.StoreGoodsLabelMapper; @@ -10,10 +13,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; /** @@ -26,11 +31,21 @@ import java.util.List; @Transactional(rollbackFor = Exception.class) public class StoreGoodsLabelServiceImpl extends ServiceImpl implements StoreGoodsLabelService { + //缓存 + @Autowired + private Cache cache; + @Override public List listByStoreId(String storeId) { - //TODO 从缓存获取店铺商品分类列表 + + //从缓存中获取店铺分类 + if (cache.hasKey(CachePrefix.STORE_CATEGORY.getPrefix() + storeId + "tree")) { + return (List) cache.get(CachePrefix.CATEGORY.getPrefix() + "tree"); + } + List list = list(storeId); List storeGoodsLabelVOList = new ArrayList<>(); + //循环列表判断是否为顶级,如果为顶级获取下级数据 list.stream() .filter(storeGoodsLabel -> storeGoodsLabel.getLevel() == 0) @@ -43,15 +58,55 @@ public class StoreGoodsLabelServiceImpl extends ServiceImpl() { + @Override + public int compare(StoreGoodsLabelVO o1, StoreGoodsLabelVO o2) { + return o1.getSortOrder().compareTo(o2.getSortOrder()); + } + }); + + if (storeGoodsLabelVOList.size() != 0) { + cache.put(CachePrefix.CATEGORY.getPrefix() + storeId + "tree", storeGoodsLabelVOList); + } return storeGoodsLabelVOList; } @Override - public List listByStore() { + public StoreGoodsLabel addStoreGoodsLabel(StoreGoodsLabel storeGoodsLabel) { //获取当前登录商家账号 AuthUser tokenUser = UserContext.getCurrentUser(); - //返回列表 - return list(tokenUser.getId()); + storeGoodsLabel.setStoreId(tokenUser.getStoreId()); + //保存店铺分类 + this.save(storeGoodsLabel); + //清除缓存 + removeCache(storeGoodsLabel.getStoreId()); + return storeGoodsLabel; + } + + @Override + public StoreGoodsLabel editStoreGoodsLabel(StoreGoodsLabel storeGoodsLabel) { + //修改当前店铺的商品分类 + AuthUser tokenUser = UserContext.getCurrentUser(); + LambdaUpdateWrapper lambdaUpdateWrapper = Wrappers.lambdaUpdate(); + lambdaUpdateWrapper.eq(StoreGoodsLabel::getStoreId, tokenUser.getStoreId()); + lambdaUpdateWrapper.eq(StoreGoodsLabel::getId, storeGoodsLabel.getId()); + //修改店铺分类 + this.update(storeGoodsLabel, lambdaUpdateWrapper); + //清除缓存 + removeCache(storeGoodsLabel.getStoreId()); + return storeGoodsLabel; + } + + @Override + public void removeStoreGoodsLabel(String storeLabelId) { + + //删除店铺分类 + this.removeById(storeLabelId); + + //清除缓存 + removeCache(UserContext.getCurrentUser().getStoreId()); } /** @@ -66,28 +121,10 @@ public class StoreGoodsLabelServiceImpl extends ServiceImpl lambdaUpdateWrapper = Wrappers.lambdaUpdate(); - lambdaUpdateWrapper.eq(StoreGoodsLabel::getStoreId, tokenUser.getStoreId()); - lambdaUpdateWrapper.eq(StoreGoodsLabel::getId, storeGoodsLabel.getId()); - this.update(storeGoodsLabel, lambdaUpdateWrapper); - return storeGoodsLabel; - } - - @Override - public void removeStoreGoodsLabel(String storeLabelId) { - this.removeById(storeLabelId); + /** + * 清除缓存 + */ + private void removeCache(String storeId) { + cache.remove(CachePrefix.CATEGORY.getPrefix() + storeId + "tree"); } }