package com.ruoyi.base.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.base.domain.BaseCity;
import com.ruoyi.base.vo.BaseCategoryVO;
import com.ruoyi.base.vo.BaseCityVO;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.member.vo.MemberUniVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.mapper.BaseCategoryMapper;
import com.ruoyi.base.domain.BaseCategory;
import com.ruoyi.base.service.IBaseCategoryService;

/**
 * 分类树-各种分类属性结构Service业务层处理
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@Service
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategoryMapper,BaseCategory>  implements IBaseCategoryService
{
    @Autowired
    private BaseCategoryMapper baseCategoryMapper;

    @Override
    public IPage<BaseCategory> queryListPage(BaseCategory pageReqVO, Page<BaseCategory> page) {
        return baseCategoryMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 根据code查询字典
     *
     * @param code
     * @return java.util.List<com.ruoyi.base.domain.BaseCategory>
     * @author vinjor-M
     * @date 11:32 2025/3/18
     **/
    @Override
    public List<BaseCategory> selectByCode(String code) {
        return this.list(new LambdaQueryWrapper<BaseCategory>().eq(BaseCategory::getCode,code));
    }

    /**
     * 根据code查分类字典--列表
     *
     * @param code     编码
     * @param isSystem 是否系统级
     * @return java.util.List<com.ruoyi.base.domain.BaseCategory>
     * @author vinjor-M
     * @date 17:01 2025/3/25
     **/
    @Override
    public List<BaseCategory> listByParentCode(String code, Integer isSystem) {
        List<BaseCategory> list = this.selectByCode(code);
        if(list.isEmpty()){
            throw new ServiceException("未查询到该字典项!");
        }
        LambdaQueryWrapper<BaseCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategory::getPid,list.get(0).getId());
        if(null!=isSystem){
            queryWrapper.eq(BaseCategory::getIsSystem,isSystem);
        }
        queryWrapper.orderByAsc(BaseCategory::getSort);
        return list(queryWrapper);
    }

    /**
     * 查询列表
     *
     * @param code     编码
     * @param isSystem 是否系统级
     * @return java.util.List<com.ruoyi.member.vo.MemberUniVO>
     * @author PQZ
     * @date 14:43 2025/4/3
     **/
    @Override
    public List<MemberUniVO> uniListByParentCode(String code, Integer isSystem) {
        List<BaseCategory> list = listByParentCode(code, isSystem);
        return list.stream().map(item -> new MemberUniVO(item.getCode(),item.getTitle())).collect(Collectors.toList());
    }

    /**
     * 通用查询分类树方法
     *
     * @param code 分类code
     * @return java.util.List<com.ruoyi.base.vo.BaseCategoryVO>
     * @author PQZ
     * @date 10:50 2025/4/14
     **/
    @Override
    public List<BaseCategoryVO> treeData(String code) {
        LambdaQueryWrapper<BaseCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag,0);
        List<BaseCategory> list = list(lambdaQueryWrapper);
        List<BaseCategoryVO> treeData = buildCategoryTree(list);
        List<BaseCategoryVO> result = treeData.stream()
                .filter(node -> code.equals(node.getValue()))
                .findFirst()
                .map(BaseCategoryVO::getChildren)
                .orElseGet(ArrayList::new);
        return result;
    }
    /**
     * 生成树结构
     *
     * @param list List<BaseCity>
     * @return java.util.List<com.ruoyi.base.vo.BaseCityVO>
     * @author PQZ
     * @date 9:44 2025/4/3
     **/
    private List<BaseCategoryVO> buildCategoryTree(List<BaseCategory> list) {
        List<BaseCategoryVO> tree = new ArrayList<>();
        //创建一个Map来存储每个分类及子集
        Map<String, BaseCategoryVO> categoryMap = new HashMap<>();
        for (BaseCategory category : list) {
            categoryMap.put(category.getId(), new BaseCategoryVO(category.getCode(), category.getTitle()));
        }
        //组装类型VO列表
        for (BaseCategory category : list) {
            BaseCategoryVO categoryVO = categoryMap.get(category.getId());
            if ("0".equals(category.getPid())) {
                //跟类型添加到列表
                tree.add(categoryVO);
            } else {
                //非根类型添加到其父亲的孩子中
                BaseCategoryVO parentCategoryVO = categoryMap.get(category.getPid());
                if (parentCategoryVO != null) {
                    parentCategoryVO.getChildren().add(categoryVO);
                }
            }
        }
        return tree;
    }

}
