xiangpei
7 天以前 2226679bda25ddf977f12367a9a37fcf4e377249
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cn.lili.modules.goods.serviceimpl;
 
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.modules.goods.entity.dos.StoreGoodsLabel;
import cn.lili.modules.goods.entity.vos.StoreGoodsLabelVO;
import cn.lili.modules.goods.mapper.StoreGoodsLabelMapper;
import cn.lili.modules.goods.service.StoreGoodsLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
import java.util.Map;
 
/**
 * 店铺商品分类业务层实现
 *
 * @author Bulbasaur
 * @since 2020-03-07 16:18:56
 */
@Service
public class StoreGoodsLabelServiceImpl extends ServiceImpl<StoreGoodsLabelMapper, StoreGoodsLabel> implements StoreGoodsLabelService {
 
    /**
     * 缓存
     */
    @Autowired
    private Cache cache;
 
    @Override
    public List<StoreGoodsLabelVO> listByStoreId(String storeId) {
 
        //从缓存中获取店铺分类
        if (cache.hasKey(CachePrefix.STORE_CATEGORY.getPrefix() + storeId)) {
            return (List<StoreGoodsLabelVO>) cache.get(CachePrefix.STORE_CATEGORY.getPrefix() + storeId);
        }
 
        List<StoreGoodsLabel> list = list(storeId);
        List<StoreGoodsLabelVO> storeGoodsLabelVOList = new ArrayList<>();
 
        //循环列表判断是否为顶级,如果为顶级获取下级数据
        list.stream()
                .filter(storeGoodsLabel -> storeGoodsLabel.getLevel() == 0)
                .forEach(storeGoodsLabel -> {
                    StoreGoodsLabelVO storeGoodsLabelVO = new StoreGoodsLabelVO(storeGoodsLabel.getId(), storeGoodsLabel.getLabelName(), storeGoodsLabel.getLevel(), storeGoodsLabel.getSortOrder());
                    List<StoreGoodsLabelVO> storeGoodsLabelVOChildList = new ArrayList<>();
                    list.stream()
                            .filter(label -> label.getParentId() != null && label.getParentId().equals(storeGoodsLabel.getId()))
                            .forEach(storeGoodsLabelChild -> storeGoodsLabelVOChildList.add(new StoreGoodsLabelVO(storeGoodsLabelChild.getId(), storeGoodsLabelChild.getLabelName(), storeGoodsLabelChild.getLevel(), storeGoodsLabelChild.getSortOrder())));
                    storeGoodsLabelVOChildList.sort(Comparator.comparing(StoreGoodsLabelVO::getSortOrder));
                    storeGoodsLabelVO.setChildren(storeGoodsLabelVOChildList);
                    storeGoodsLabelVOList.add(storeGoodsLabelVO);
                });
 
        //调整店铺分类排序
        storeGoodsLabelVOList.sort(Comparator.comparing(StoreGoodsLabelVO::getSortOrder));
 
        if (!storeGoodsLabelVOList.isEmpty()) {
            cache.put(CachePrefix.CATEGORY.getPrefix() + storeId, storeGoodsLabelVOList);
        }
        return storeGoodsLabelVOList;
    }
 
    /**
     * 根据分类id集合获取所有店铺分类根据层级排序
     *
     * @param ids 商家ID
     * @return 店铺分类列表
     */
    @Override
    public List<StoreGoodsLabel> listByStoreIds(List<String> ids) {
        return this.list(new LambdaQueryWrapper<StoreGoodsLabel>().in(StoreGoodsLabel::getId, ids).orderByAsc(StoreGoodsLabel::getLevel));
    }
 
    @Override
    public List<Map<String, Object>> listMapsByStoreIds(List<String> ids, String columns) {
        QueryWrapper<StoreGoodsLabel> queryWrapper = new QueryWrapper<StoreGoodsLabel>().in("id", ids).orderByAsc("level");
        queryWrapper.select(columns);
        return this.listMaps(queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreGoodsLabel addStoreGoodsLabel(StoreGoodsLabel storeGoodsLabel) {
        //获取当前登录商家账号
        AuthUser tokenUser = UserContext.getCurrentUser();
        if (tokenUser == null || CharSequenceUtil.isEmpty(tokenUser.getStoreId())) {
            throw new ServiceException(ResultCode.USER_NOT_LOGIN);
        }
        storeGoodsLabel.setStoreId(tokenUser.getStoreId());
        //保存店铺分类
        this.save(storeGoodsLabel);
        //清除缓存
        removeCache(storeGoodsLabel.getStoreId());
        return storeGoodsLabel;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreGoodsLabel editStoreGoodsLabel(StoreGoodsLabel storeGoodsLabel) {
        //修改当前店铺的商品分类
        AuthUser tokenUser = UserContext.getCurrentUser();
        if (tokenUser == null || CharSequenceUtil.isEmpty(tokenUser.getStoreId())) {
            throw new ServiceException(ResultCode.USER_NOT_LOGIN);
        }
        LambdaUpdateWrapper<StoreGoodsLabel> 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) {
 
        AuthUser tokenUser = UserContext.getCurrentUser();
        if (tokenUser == null || CharSequenceUtil.isEmpty(tokenUser.getStoreId())) {
            throw new ServiceException(ResultCode.USER_NOT_LOGIN);
        }
        //删除店铺分类
        this.removeById(storeLabelId);
 
        //清除缓存
        removeCache(tokenUser.getStoreId());
    }
 
    /**
     * 获取店铺商品分类列表
     *
     * @param storeId 店铺ID
     * @return 店铺商品分类列表
     */
    private List<StoreGoodsLabel> list(String storeId) {
        LambdaQueryWrapper<StoreGoodsLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StoreGoodsLabel::getStoreId, storeId);
        queryWrapper.orderByDesc(StoreGoodsLabel::getSortOrder);
        return this.baseMapper.selectList(queryWrapper);
    }
 
    /**
     * 清除缓存
     */
    private void removeCache(String storeId) {
        cache.remove(CachePrefix.STORE_CATEGORY.getPrefix() + storeId);
    }
}