add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.iotechn.unimall.admin.api.category;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.component.CacheComponent;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.biz.service.category.CategoryBizService;
import com.iotechn.unimall.data.constant.CacheConst;
import com.iotechn.unimall.data.domain.AdvertDO;
import com.iotechn.unimall.data.domain.CategoryDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.dto.CategoryDTO;
import com.iotechn.unimall.data.enums.AdvertUnionType;
import com.iotechn.unimall.data.enums.CategoryLevelType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.AdvertMapper;
import com.iotechn.unimall.data.mapper.CategoryMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * Created by rize on 2019/7/12.
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {
 
    @Autowired
    private CategoryMapper categoryMapper;
 
    @Autowired
    private AdvertMapper advertMapper;
 
    @Autowired
    private SpuMapper spuMapper;
 
    @Autowired
    private CategoryBizService categoryBizService;
 
    @Autowired
    private CacheComponent cacheComponent;
 
    /**
     * 获取两级类目树,用于添加类目时选择使用 商品、优惠券
     */
    @Override
    public List<CategoryDTO> categoryTree() throws ServiceException {
        return categoryBizService.categorySecondLevelTree();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CategoryDO create(String title, Long parentId, String picUrl, Long adminId) throws ServiceException {
        CategoryDO parent = null;
        CategoryDO categoryDO = new CategoryDO();
        if (!parentId.equals(0l)) {
            parent = categoryMapper.selectById(parentId);
            if (parent == null || parent.getLevel().intValue() > CategoryLevelType.TWO.getCode()) {
                throw new AdminServiceException(ExceptionDefinition.CATEGORY_PARENT_NODE_INFORMATION_ERROR);
            }
            categoryDO.setLevel(parent.getLevel() + 1);
            if (parent.getLevel().intValue() == CategoryLevelType.ONE.getCode().intValue()) {
                categoryDO.setFirstLevelId(parentId);
            } else if (parent.getLevel().intValue() == CategoryLevelType.ONE.getCode().intValue()) {
                categoryDO.setFirstLevelId(parent.getFirstLevelId());
            }
        } else {
            categoryDO.setLevel(CategoryLevelType.ONE.getCode());
        }
        categoryDO.setParentId(parentId);
        categoryDO.setPicUrl(picUrl);
        categoryDO.setTitle(title);
        Date now = new Date();
        categoryDO.setGmtCreate(now);
        categoryDO.setGmtUpdate(now);
        if (categoryMapper.insert(categoryDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        this.clearCache();
        return categoryDO;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        /**
         *  删除类目时 不允许类目有子类目
         *  删除类目时 不允许类目有商品
         *  删除类目时 不允许有广告关联类目
         */
        Integer countCategory = categoryMapper.selectCount(new QueryWrapper<CategoryDO>().eq("parent_id", id));
        Integer countSpu = spuMapper.selectCount(new QueryWrapper<SpuDO>().eq("category_id", id));
        Integer countAdvert = advertMapper.selectCount(
                new QueryWrapper<AdvertDO>()
                        .eq("union_type", AdvertUnionType.CATEGORY.getCode())
                        .eq("union_value", id.toString()));
        if (countCategory != 0 || countSpu != 0) {
            throw new AdminServiceException(ExceptionDefinition.CATEGORY_OUGHT_TO_EMPTY);
        }
        if (countAdvert != 0) {
            throw new AdminServiceException(ExceptionDefinition.CATEGORY_EXIST_ADVERT);
        }
        if (categoryMapper.deleteById(id) > 0) {
            this.clearCache();
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CategoryDTO update(Long id, String title, Long parentId, String iconUrl, String picUrl, Long adminId) throws ServiceException {
        CategoryDO categoryParent = null;
        if (parentId.longValue() != 0L) {
            categoryParent = categoryMapper.selectById(parentId);
        }
        CategoryDO category = categoryMapper.selectById(id);
 
        // 1. 检验类目是否存在,非一级类目的父类目是否存在
        if (category == null || (categoryParent == null && !parentId.equals(0L))) {
            throw new AdminServiceException(ExceptionDefinition.CATEGORY_NOT_FIND_PARENT_NODE_OR_NODE);
        }
 
        CategoryDO updateDO = new CategoryDO();
 
        // 如果是一级类目修改,就不会变更父类目;如果没有变更父类目,也不会变更对应的一二级长辈类目
        if (!category.getParentId().equals(0L) && !category.getParentId().equals(parentId)) {
            // 节点只能平级变更
            if (category.getLevel().intValue() != categoryParent.getLevel().intValue() + 1) {
                throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
            }
            if (categoryParent.getLevel().intValue() == CategoryLevelType.ONE.getCode().intValue()) {
                updateDO.setFirstLevelId(parentId);
            } else if (categoryParent.getLevel().intValue() == CategoryLevelType.TWO.getCode().intValue()) {
                updateDO.setFirstLevelId(categoryParent.getFirstLevelId());
            }
        }
        updateDO.setId(id);
        updateDO.setGmtUpdate(new Date());
        updateDO.setParentId(parentId);
        updateDO.setTitle(title);
        updateDO.setPicUrl(picUrl);
        if (categoryMapper.updateById(updateDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.CATEGORY_UPDATE_FAILURE);
        }
 
        // 如果变更了父节点,就需要变更当前节点的子孙节点的一二级长辈节点
        if (!category.getParentId().equals(parentId)) {
            // 只有二三级节点能变更父节点,其中只有二级节点变更后,会造成子节点的长辈节点变更。
            if (updateDO.getLevel().intValue() == 1) {
                QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
                wrapper.eq("parent_id", updateDO.getId());
                CategoryDO child = new CategoryDO();
                child.setFirstLevelId(updateDO.getFirstLevelId());
                categoryMapper.update(child, wrapper);
            }
        }
 
        CategoryDTO categoryDTO = new CategoryDTO();
        BeanUtils.copyProperties(updateDO, categoryDTO);
        List<CategoryDTO> list = categoryBizService.getCategoryList();
        for (CategoryDTO temp : list) {
            if (updateDO.getId().equals(temp.getId())) {
                BeanUtils.copyProperties(temp, categoryDTO);
                break;
            }
        }
        this.clearCache();
        return categoryDTO;
    }
 
    @Override
    public Page<CategoryDTO> list(Long id, String title, Integer level, Long parentId, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper wrapper = new QueryWrapper();
        if (id != null) {
            wrapper.eq("id", id);
        }
        if (title != null) {
            wrapper.like("title", title);
        }
        if (level != null) {
            wrapper.eq("level", level);
        }
        if (parentId != null) {
            List<Long> categorySelfAndChildren = categoryBizService.getCategorySelfAndChildren(parentId);
            if (!CollectionUtils.isEmpty(categorySelfAndChildren)) {
                wrapper.in("id", categorySelfAndChildren);
            }
        }
        wrapper.orderByAsc("level");
 
        Page selectPage = categoryMapper.selectPage(Page.div(page, limit, CategoryDO.class), wrapper);
        List<CategoryDO> categoryDOS = selectPage.getItems();
        List<CategoryDTO> totalCategory = categoryBizService.getCategoryList();
        List<CategoryDTO> list = categoryDOS.stream().map(item -> {
            CategoryDTO dto = new CategoryDTO();
            for (CategoryDTO temp : totalCategory) {
                if (temp.getId().equals(item.getId())) {
                    BeanUtils.copyProperties(temp, dto);
                    return dto;
                }
            }
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());
        return new Page<>(list, page, limit, selectPage.getCount());
    }
 
    /**
     * 抽取删除缓存公共代码
     */
    private void clearCache() {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.del(CacheConst.CATEGORY_ALL_LIST);
                cacheComponent.del(CacheConst.CATEGORY_SECOND_LEVEL_TREE);
                cacheComponent.del(CacheConst.CATEGORY_THREE_LEVEL_TREE);
            }
        });
    }
 
}