package com.czg.service.system.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import com.czg.system.vo.SysCategoryInfoVO;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.system.entity.SysCategoryInfo;
import com.czg.system.service.SysCategoryInfoService;
import com.czg.service.system.mapper.SysCategoryInfoMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

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

/**
 * 类目信息表 服务层实现。
 *
 * @author ww
 * @since 2026-01-06
 */
@Service
public class SysCategoryInfoServiceImpl extends ServiceImpl<SysCategoryInfoMapper, SysCategoryInfo> implements SysCategoryInfoService {

    @Cacheable(value = "common:category", key = "'all'")
    @Override
    public List<SysCategoryInfoVO> categoryList() {
        List<SysCategoryInfo> list = list();
        if (CollUtil.isEmpty(list)) {
            return List.of();
        }
        List<SysCategoryInfoVO> result = new ArrayList<>();
        Map<String, List<SysCategoryInfo>> stringListMap = CollStreamUtil.groupByKey(list, SysCategoryInfo::getFirstCategory);
        stringListMap.forEach((k, v) -> {
            SysCategoryInfoVO vo = new SysCategoryInfoVO();
            vo.setFirstCategory(k);
            vo.setChild(v);
            result.add(vo);
        });
        return result;
    }
}
