zhujie
9 天以前 19428a49b4c07b14097615d48a7a72dbf941c4e7
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
package com.mzl.flower.service.customer;
 
import com.mzl.flower.domain.CategoryConfig;
import com.mzl.flower.domain.CategoryConfigDO;
import com.mzl.flower.domain.CategoryConfigSync;
import com.mzl.flower.mapper.CategoryConfigMapper;
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.List;
 
@Service
public class CategoryConfigServiceImpl implements CategoryConfigService {
 
    @Autowired
    private CategoryConfigMapper categoryConfigMapper;
 
    @Override
    @Transactional
    public void saveOrUpdateCategoryConfig(CategoryConfigSync categoryConfigSync) {
        if (categoryConfigSync == null || categoryConfigSync.getUserId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
 
        // 先删除该用户之前的所有分类配置
        categoryConfigMapper.deleteByUserId(categoryConfigSync.getUserId());
 
        // 然后插入新的分类配置
        if (categoryConfigSync.getCategories() != null && !categoryConfigSync.getCategories().isEmpty()) {
            for (CategoryConfig clientCategory : categoryConfigSync.getCategories()) {
                CategoryConfigDO dbCategory = new CategoryConfigDO();
                dbCategory.setUserId(categoryConfigSync.getUserId());
                dbCategory.setCategoryId(clientCategory.getId());
                dbCategory.setName(clientCategory.getName());
                dbCategory.setOrder(clientCategory.getOrder());
                dbCategory.setIsEnabled(clientCategory.getIsEnabled());
                categoryConfigMapper.insert(dbCategory);
            }
        }
    }
 
    @Override
    public List<CategoryConfig> getCategoryConfigsByUserId(String userId) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
 
        List<CategoryConfigDO> dbCategories = categoryConfigMapper.findByUserId(userId);
        if (dbCategories == null || dbCategories.isEmpty()) {
            return new ArrayList<>();
        }
 
        List<CategoryConfig> clientCategories = new ArrayList<>();
        for (CategoryConfigDO dbCategory : dbCategories) {
            CategoryConfig clientCategory = new CategoryConfig();
            clientCategory.setId(dbCategory.getCategoryId());
            clientCategory.setName(dbCategory.getName());
            clientCategory.setOrder(dbCategory.getOrder());
            clientCategory.setIsEnabled(dbCategory.getIsEnabled());
            clientCategories.add(clientCategory);
        }
 
        return clientCategories;
    }
}