xin
2025-05-30 b394df082b875856884d6d02cce2a43c49ad6704
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
package com.oying.modules.pc.store.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.exception.EntityNotFoundException;
import com.oying.modules.pc.common.exception.LevelExceededException;
import com.oying.modules.pc.store.domain.StoreCategory;
import com.oying.modules.pc.store.domain.dto.StoreCategoryQueryCriteria;
import com.oying.modules.pc.store.mapper.StoreCategoryMapper;
import com.oying.modules.pc.store.service.StoreCategoryService;
import com.oying.utils.FileUtil;
import com.oying.utils.PageResult;
import com.oying.utils.PageUtil;
import com.oying.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
 
/**
 * @author lzp
 * @date 2025-04-24
 **/
@Service
@RequiredArgsConstructor
public class StoreCategoryServiceImpl extends ServiceImpl<StoreCategoryMapper, StoreCategory> implements StoreCategoryService {
 
    private static final int MAX_LEVEL = 2;
 
    private final StoreCategoryMapper storeCategoryMapper;
 
    @Override
    public PageResult<StoreCategory> queryAll(StoreCategoryQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(storeCategoryMapper.findAll(criteria, page));
    }
 
    @Override
    public List<StoreCategory> queryAll(StoreCategoryQueryCriteria criteria) {
        return storeCategoryMapper.findAll(criteria);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(StoreCategory resources) {
        // 重名
        this.calculateAndSetLevel(resources);
        resources.setCreateBy(SecurityUtils.getCurrentUserId());
        storeCategoryMapper.insert(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(StoreCategory resources) {
        StoreCategory storeCategory = getById(resources.getCategoryId());
        storeCategory.copy(resources);
        resources.setUpdateBy(SecurityUtils.getCurrentUserId());
        storeCategoryMapper.updateById(storeCategory);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        storeCategoryMapper.deleteBatchIds(ids);
    }
 
    /**
     * 计算并设置类目的层级
     */
    private void calculateAndSetLevel(StoreCategory category) {
 
        if (category == null) {
            throw new IllegalArgumentException("对象不能为null");
        }
 
        Long parentCategoryId = category.getParentId();
        if (parentCategoryId == null) {
            category.setParentId(0L);
            category.setLevel(1);
            return;
        }
 
        StoreCategory parentCategory = Optional.ofNullable(getById(parentCategoryId))
                .orElseThrow(() -> new EntityNotFoundException(StoreCategory.class, "parentId", parentCategoryId.toString()));
 
        int currentLevel = parentCategory.getLevel() + 1;
 
        if (currentLevel > MAX_LEVEL) {
            throw new LevelExceededException(category.getLevel(), MAX_LEVEL);
        }
 
        category.setLevel(currentLevel);
    }
}