package com.qianhe.zqbf.service.impl;

import com.qianhe.common.core.domain.entity.SysDictData;
import com.qianhe.system.mapper.SysDictDataMapper;
import com.qianhe.zqbf.domain.*;
import com.qianhe.zqbf.mapper.ZqbfXmzBfxmMapper;
import com.qianhe.zqbf.service.ZqbfXmzBfxmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ZqbfXmzBfxmServiceImpl implements ZqbfXmzBfxmService {
    @Autowired
    private ZqbfXmzBfxmMapper zqbfXmzBfxmMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;


    /**
     * 条件查询
     */
    @Override
    public List<ZqbfXmzBfxmVo> list(ZqbfXmzBfxmQuery zqbfXmzBfxmQuery) {
        List<ZqbfXmzBfxmVo> dataList = zqbfXmzBfxmMapper.list(zqbfXmzBfxmQuery);
        return dataList;
    }

    /**
     * 查询详情
     */
    @Override
    public ZqbfXmzBfxmVo info(Long id) {
        ZqbfXmzBfxmVo info = zqbfXmzBfxmMapper.info(id);
        return info;
    }

    /**
     * 新增
     */
    @Override
    public int add(ZqbfXmzBfxm zqbfXmzBfxm, String username) {
        zqbfXmzBfxm.setCjr(username);
        zqbfXmzBfxm.setGxr(username);
        zqbfXmzBfxm.setCjsj(new Date());
        return zqbfXmzBfxmMapper.add(zqbfXmzBfxm);
    }

    /**
     * 修改
     */
    @Override
    public int edit(ZqbfXmzBfxm zqbfXmzBfxm, String username) {
        zqbfXmzBfxm.setGxr(username);
        return zqbfXmzBfxmMapper.edit(zqbfXmzBfxm);
    }

    /**
     * 删除
     */
    @Override
    public int delete(Long[] ids) {
        return zqbfXmzBfxmMapper.delete(ids);
    }

    @Override
    public List<TreeSelectT> tree() {
        // 1. 获取源数据并快速返回
        List<ZqbfXmzBfxmVo> sourceList = zqbfXmzBfxmMapper.list(ZqbfXmzBfxmQuery.builder().build());
        if (sourceList == null || sourceList.isEmpty()) {
            return Collections.emptyList();
        }

        // 2. 获取字典数据
        List<SysDictData> dictList = dictDataMapper.selectDictDataByType("zqbf_xmz_zjzj");
        if (dictList == null || dictList.isEmpty()) {
            return Collections.emptyList();
        }

        // 3. 构建项目分组 Map（按 xmzb 分组），避免每次 stream 过滤
        Map<String, List<ZqbfXmzBfxmVo>> groupMap = sourceList.stream()
                .filter(item -> item.getXmzb() != null)
                .collect(Collectors.groupingBy(ZqbfXmzBfxmVo::getXmzb));

        // 4. 构建树形结构
        List<TreeSelectT> dataList = new ArrayList<>(dictList.size());
        for (SysDictData row : dictList) {
            String dictValue = row.getDictValue();
            if (dictValue == null) {
                continue; // 跳过无效的字典值
            }

            TreeSelectT treeSelectT = new TreeSelectT();
            treeSelectT.setLabel(row.getDictLabel());
            treeSelectT.setValue(dictValue);

            List<TreeSelectT> children = new ArrayList<>();
            List<ZqbfXmzBfxmVo> childrenList = groupMap.getOrDefault(dictValue, Collections.emptyList());

            for (ZqbfXmzBfxmVo item : childrenList) {
                TreeSelectT child = new TreeSelectT();
                child.setLabel(item.getXmmc());
                child.setValue(String.valueOf(item.getXmid()));
                children.add(child);
            }

            treeSelectT.setChildren(children);
            dataList.add(treeSelectT);
        }

        return dataList;
    }
}
