package com.wzj.soopin.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.content.convert.ArticleCategoryConvert;
import com.wzj.soopin.content.domain.po.ArticleCategory;
import com.wzj.soopin.content.domain.vo.ArticleCategoryVO;
import com.wzj.soopin.content.mapper.ArticleCategoryMapper;
import com.wzj.soopin.content.service.IArticleCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 店铺封禁
 *
 * @author zcc
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategory> implements IArticleCategoryService {

    private final ArticleCategoryConvert convert;
    @Override
    public List<ArticleCategoryVO> tree(Wrapper<ArticleCategory> wrapper) {

        List<ArticleCategory> list = this.list(wrapper);
        return list.stream().filter(item -> item.getParentId() == 0).map(item -> {
            ArticleCategoryVO articleCategoryVO = convert.toVO(item);
            articleCategoryVO.setChildren(
                list.stream().filter(child -> Objects.equals(child.getParentId(), item.getId()))
                    .map(convert::toVO)
                    .collect(Collectors.toList())
            );

            return articleCategoryVO;
        }).toList();
    }
}
