Administrator
56 mins ago 33fb9a2fdb0e6cd68a987bbdcbe22ef587fa5813
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cc.mrbird.febs.ai.service.impl;
 
import cc.mrbird.febs.ai.entity.AiMemberRole;
import cc.mrbird.febs.ai.entity.AiProductCategory;
import cc.mrbird.febs.ai.enums.ProductCategoryLevelEnum;
import cc.mrbird.febs.ai.mapper.AiProductCategoryMapper;
import cc.mrbird.febs.ai.service.AiProductCategoryService;
import cc.mrbird.febs.ai.util.UUID;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * AI产品类别 Service实现类
 *
 * @author yourname
 * @date 2025-07-29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AiProductCategoryServiceImpl extends ServiceImpl<AiProductCategoryMapper, AiProductCategory> implements AiProductCategoryService {
 
    private final AiProductCategoryMapper aiProductCategoryMapper;
 
    @Override
    public AiProductCategory getById(String id) {
        return aiProductCategoryMapper.selectById(id);
    }
 
    @Override
    public List<AiProductCategory> getList(String companyId) {
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        if (StrUtil.isNotEmpty(companyId)){
            query.eq(AiProductCategory::getCompanyId, companyId);
        }
        query.ne(AiProductCategory::getState, 2);
        query.eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_ONE.getLevel());
        query.orderByDesc(AiProductCategory::getHotState);
        query.orderByAsc(AiProductCategory::getSort);
        List<AiProductCategory> aiProductCategories = aiProductCategoryMapper.selectList(query);
        if (CollUtil.isNotEmpty(aiProductCategories)){
            Set<String> parentIds = aiProductCategories.stream().map(AiProductCategory::getId).collect(Collectors.toSet());
            if (CollUtil.isNotEmpty(parentIds)){
                List<AiProductCategory> aiProductCategoriesChild = aiProductCategoryMapper.selectList(
                        Wrappers.lambdaQuery(AiProductCategory.class)
                                .in(AiProductCategory::getParentId, parentIds)
                                .ne(AiProductCategory::getState, 2)
                                .eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_TWO.getLevel())
                                .orderByAsc(AiProductCategory::getSort)
                );
 
                //Stream流操作aiProductCategoriesChild,返回一个Map<ParentId,List<AiProductCategory>>,List<AiProductCategory>按照sort排序
                Map<String, List<AiProductCategory>> resultMap = aiProductCategoriesChild.stream()
                        .collect(Collectors.groupingBy(
                                AiProductCategory::getParentId,
                                Collectors.collectingAndThen(
                                        Collectors.toList(),
                                        list -> list.stream()
                                                .sorted(Comparator.comparing(AiProductCategory::getSort))
                                                .collect(Collectors.toList())
                                )
                        ));
                if (CollUtil.isNotEmpty(resultMap)){
                    aiProductCategories.forEach(aiProductCategory -> {
                        List<AiProductCategory> collect = resultMap.get(aiProductCategory.getId());
                        if (CollUtil.isNotEmpty(collect)){
                            aiProductCategory.setChild(collect);
                        }
                    });
                }
            }
        }
        return aiProductCategories;
    }
 
    @Override
    public Map<String, AiProductCategory> selectMapByIds(Set<String> collect) {
        Map<String,AiProductCategory> map = new HashMap<>();
        if(CollUtil.isNotEmpty( collect)){
            List<AiProductCategory> aiProductCategories = aiProductCategoryMapper.selectList(
                    Wrappers.lambdaQuery(AiProductCategory.class)
                    .in(AiProductCategory::getId,collect)
            );
            if(CollUtil.isNotEmpty(aiProductCategories)){
                aiProductCategories.forEach(aiProductCategory -> map.put(aiProductCategory.getId(),aiProductCategory));
            }
        }
        return map;
    }
 
    @Override
    public IPage<AiProductCategory> listInPage(AiProductCategory dto, QueryRequest request) {
 
 
        Page<AiProductCategory> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        if (StrUtil.isNotEmpty(dto.getCompanyId())){
            query.eq(AiProductCategory::getCompanyId, dto.getCompanyId());
        }
        query.ne(AiProductCategory::getState, 2);
        query.orderByDesc(AiProductCategory::getHotState);
        query.orderByAsc(AiProductCategory::getSort);
        Page<AiProductCategory> pages = aiProductCategoryMapper.selectPage(page, query);
        List<AiProductCategory> records = pages.getRecords();
        if (CollUtil.isNotEmpty( records)){
            //stream流获取全部的parentId
            Set<String> parentIds = records.stream().map(AiProductCategory::getParentId).collect(Collectors.toSet());
            if (CollUtil.isNotEmpty(parentIds)){
                List<AiProductCategory> aiProductCategories = aiProductCategoryMapper.selectList(
                        Wrappers.lambdaUpdate(AiProductCategory.class)
                                .in(AiProductCategory::getId, parentIds)
                );
                Map<String, AiProductCategory> map = aiProductCategories.stream().collect(
                        Collectors.toMap(AiProductCategory::getId, aiProductCategory -> aiProductCategory));
                records.forEach(aiProductCategory -> {
                    AiProductCategory orDefault = map.getOrDefault(aiProductCategory.getParentId(), null);
                    if(ObjectUtil.isNotNull(orDefault)){
                        aiProductCategory.setParentName(orDefault.getName());
                    }
                });
            }
        }
        return pages;
    }
 
    @Override
    public FebsResponse changeState(String id, Integer type, Integer state) {
        AiProductCategory entity = this.getById(id);
        if(ObjectUtil.isNotNull(entity)){
            if(1 == type){
                aiProductCategoryMapper.update(null,
                        Wrappers.lambdaUpdate(AiProductCategory.class)
                        .set(AiProductCategory::getState, state)
                        .eq(AiProductCategory::getId, id)
                        );
            }
            if(2 == type){
                aiProductCategoryMapper.update(null,
                        Wrappers.lambdaUpdate(AiProductCategory.class)
                        .set(AiProductCategory::getHotState, state)
                        .eq(AiProductCategory::getId, id)
                        );
            }
        }
        return new FebsResponse().success().message("操作成功");
    }
 
    @Override
    public FebsResponse add(AiProductCategory dto) {
        AiProductCategory entity = new AiProductCategory();
        entity.setId(UUID.getSimpleUUIDString());
        entity.setCompanyId(dto.getCompanyId());
        entity.setCode(dto.getCode());
        entity.setName(dto.getName());
        entity.setBackImg(dto.getBackImg());
        entity.setIconImg(dto.getIconImg());
        entity.setSort(dto.getSort());
        entity.setCreatedTime(new Date());
        entity.setParentId(dto.getParentId());
        entity.setLevel(StrUtil.isNotEmpty(dto.getParentId()) ? 2 : 1);
        this.save(entity);
        return new FebsResponse().success().message("操作成功");
    }
 
    @Override
    public FebsResponse update(AiProductCategory dto) {
        String id = dto.getId();
        AiProductCategory entity = this.getById(id);
        dto.setLevel(ProductCategoryLevelEnum.LEVEL_ONE.getLevel());
        String parentId = dto.getParentId();
        if (parentId.equals( id)){
            throw new FebsException("父类不能选择自己");
        }
        if (StrUtil.isNotEmpty(parentId)){
            AiProductCategory parent = this.getById(parentId);
            if (
                    ObjectUtil.isNotNull(parent)
                            && parent.getLevel() < ProductCategoryLevelEnum.LEVEL_TWO.getLevel()
            ){
                dto.setLevel(parent.getLevel() +1);
                dto.setParentId(parentId);
            }
        }
        if (ObjectUtil.isNotNull( entity)){
            this.update(null,
                    Wrappers.lambdaUpdate(AiProductCategory.class)
                            .set(AiProductCategory::getCode, dto.getCode())
                            .set(AiProductCategory::getName, dto.getName())
                            .set(AiProductCategory::getBackImg, dto.getBackImg())
                            .set(AiProductCategory::getIconImg, dto.getIconImg())
                            .set(AiProductCategory::getSort, dto.getSort())
                            .set(AiProductCategory::getUpdatedTime, new Date())
                            .set(AiProductCategory::getParentId, dto.getParentId())
                            .set(AiProductCategory::getLevel, dto.getLevel())
                            .eq(AiProductCategory::getId, id)
            );
        }
        return new FebsResponse().success().message("操作成功");
    }
 
    @Override
    public FebsResponse delete(String id) {
 
        AiProductCategory entity = this.getById(id);
        if(ObjectUtil.isNotNull( entity)){
            this.update(null,
                    Wrappers.lambdaUpdate(AiProductCategory.class)
                            .set(AiProductCategory::getState, 2)
                            .set(AiProductCategory::getUpdatedTime, new Date())
                            .eq(AiProductCategory::getId, id));
 
        }
        return new FebsResponse().success().message("操作成功");
    }
 
    @Override
    public List<AiProductCategory> categoryTree(String companyId) {
        return this.getList(companyId) ;
    }
 
    @Override
    public List<AiProductCategory> parent(String companyId) {
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        query.select(AiProductCategory::getId,AiProductCategory::getName);
        if (StrUtil.isNotEmpty(companyId)){
            query.eq(AiProductCategory::getCompanyId, companyId);
        }
        query.eq(AiProductCategory::getLevel, 1);
        query.ne(AiProductCategory::getState, 2);
        return this.getBaseMapper().selectList(query);
    }
}