package org.jeecg.modules.oroqen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.oroqen.entity.OroqenCultureCategory;
import org.jeecg.modules.oroqen.mapper.OroqenCultureCategoryMapper;
import org.jeecg.modules.oroqen.service.IOroqenCultureCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @Description: 文化内容分类
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Service
public class OroqenCultureCategoryServiceImpl extends ServiceImpl<OroqenCultureCategoryMapper, OroqenCultureCategory> implements IOroqenCultureCategoryService {

    @Override
    public List<OroqenCultureCategory> getTreeList() {
        QueryWrapper<OroqenCultureCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort_order", "create_time");
        List<OroqenCultureCategory> allCategories = this.list(queryWrapper);
        
        return buildTree(allCategories, null);
    }

    @Override
    public List<OroqenCultureCategory> getChildrenByParentId(String parentId) {
        QueryWrapper<OroqenCultureCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (StringUtils.hasText(parentId)) {
            queryWrapper.eq("parent_id", parentId);
        } else {
            queryWrapper.isNull("parent_id").or().eq("parent_id", "");
        }
        queryWrapper.orderByAsc("sort_order", "create_time");
        return this.list(queryWrapper);
    }

    @Override
    public boolean existsByCategoryCode(String categoryCode, String excludeId) {
        QueryWrapper<OroqenCultureCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_code", categoryCode);
        if (StringUtils.hasText(excludeId)) {
            queryWrapper.ne("id", excludeId);
        }
        return this.count(queryWrapper) > 0;
    }

    /**
     * 构建树形结构
     */
    private List<OroqenCultureCategory> buildTree(List<OroqenCultureCategory> allCategories, String parentId) {
        List<OroqenCultureCategory> result = new ArrayList<>();
        
        List<OroqenCultureCategory> children = allCategories.stream()
                .filter(category -> {
                    if (parentId == null) {
                        return category.getParentId() == null || "".equals(category.getParentId());
                    } else {
                        return parentId.equals(category.getParentId());
                    }
                })
                .collect(Collectors.toList());
        
        for (OroqenCultureCategory category : children) {
            List<OroqenCultureCategory> subChildren = buildTree(allCategories, category.getId());
            category.setChildren(subChildren);
            result.add(category);
        }
        
        return result;
    }
}