Administrator
5 days ago b2aa119a3e476cbb43f23a82745159d8071925f7
src/main/java/cc/mrbird/febs/ai/service/impl/AiProductCategoryServiceImpl.java
@@ -2,6 +2,7 @@
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;
@@ -9,6 +10,7 @@
import cc.mrbird.febs.common.entity.QueryRequest;
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;
@@ -20,6 +22,7 @@
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
/**
 * AI产品类别 Service实现类
@@ -43,9 +46,42 @@
    public List<AiProductCategory> getList() {
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        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;
    }
@@ -74,6 +110,25 @@
        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;
    }
@@ -110,6 +165,8 @@
        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("操作成功");
    }
@@ -152,4 +209,13 @@
    public List<AiProductCategory> categoryTree() {
        return this.getList() ;
    }
    @Override
    public List<AiProductCategory> parent() {
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        query.select(AiProductCategory::getId,AiProductCategory::getName);
        query.eq(AiProductCategory::getLevel, 1);
        query.ne(AiProductCategory::getState, 2);
        return this.getBaseMapper().selectList(query);
    }
}