package com.backendsys.modules.b2c.good.service.impl;

import cn.hutool.core.util.StrUtil;
import com.backendsys.modules.b2c.good.dao.GoodBrandDao;
import com.backendsys.modules.b2c.good.entity.GoodBrand;
import com.backendsys.modules.b2c.good.entity.GoodUnit;
import com.backendsys.modules.b2c.good.service.GoodBrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodBrandServiceImpl implements GoodBrandService {

    @Autowired
    private GoodBrandDao goodBrandDao;

    // 获取商品单位
    @Override
    public List<GoodBrand> selectGoodBrandList(GoodBrand goodBrand) {
        List<GoodBrand> goodBrandList = goodBrandDao.selectGoodBrandList(goodBrand);
        return buildTree(goodBrandList, -1L);
    }

    /**
     * 递归构建树形结构
     * @param all_brands 所有品牌数据
     * @param parent_id  当前父级ID
     * @return 树形结构列表
     */
    private List<GoodBrand> buildTree(List<GoodBrand> all_brands, Long parent_id) {
        List<GoodBrand> treeList = new ArrayList<>();

        for (GoodBrand brand : all_brands) {
            if (parent_id.equals(brand.getParent_id())) {
                // 递归查询子节点
                List<GoodBrand> children = buildTree(all_brands, brand.getId());
                if (!children.isEmpty()) {
                    brand.setChildren(children);
                }
                treeList.add(brand);
            }
        }

        return treeList;
    }

}
