package com.cmvr.ti.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.ti.mapper.TiVehicleConfigMapper;
import com.cmvr.ti.model.domain.TiVehicleConfig;
import com.cmvr.ti.model.vo.TiVehicleConfigTreeVO;
import com.cmvr.ti.service.ITiVehicleConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 车辆配置Service业务层处理
 */
@Service
@RequiredArgsConstructor
public class TiVehicleConfigServiceImpl
        extends ServiceImpl<TiVehicleConfigMapper, TiVehicleConfig>
        implements ITiVehicleConfigService {

    @Override
    public TiVehicleConfig selectTiVehicleConfigById(Long id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 返回树结构：OEM → Model → Version
     */
    @Override
    public List<TiVehicleConfigTreeVO> selectTiVehicleConfigList() {

        // 1. 获取全部节点
        List<TiVehicleConfig> list = this.list();

        // 2. 转为 VO
        List<TiVehicleConfigTreeVO> treeNodes = list.stream().map(cfg -> {
            TiVehicleConfigTreeVO vo = new TiVehicleConfigTreeVO();
            vo.setId(cfg.getId());
            vo.setParentId(cfg.getParentId());
            vo.setLabel(cfg.getName());
            vo.setType(cfg.getType());
            vo.setRemark(cfg.getRemark());
            return vo;
        }).collect(Collectors.toList());

        // 3. 组装树
        return buildTree(treeNodes);
    }

    /**
     * 标准树构建
     */
    private List<TiVehicleConfigTreeVO> buildTree(List<TiVehicleConfigTreeVO> nodes) {
        Map<Long, TiVehicleConfigTreeVO> map = nodes.stream()
                .collect(Collectors.toMap(TiVehicleConfigTreeVO::getId, n -> n));

        List<TiVehicleConfigTreeVO> root = new ArrayList<>();

        for (TiVehicleConfigTreeVO node : nodes) {
            if (node.getParentId() == 0) {
                root.add(node);
            } else {
                TiVehicleConfigTreeVO parent = map.get(node.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(node);
                }
            }
        }

        return root;
    }

    @Override
    public int insertTiVehicleConfig(TiVehicleConfig tiVehicleConfig) {
        return this.baseMapper.insert(tiVehicleConfig);
    }

    @Override
    public int updateTiVehicleConfig(TiVehicleConfig tiVehicleConfig) {
        return this.baseMapper.updateById(tiVehicleConfig);
    }

    @Override
    public int deleteTiVehicleConfigByIds(Long[] ids) {
        for (Long id : ids) {
            long childCount = this.lambdaQuery()
                    .eq(TiVehicleConfig::getParentId, id)
                    .count();
            if (childCount > 0) {
                throw new GlobalException("节点下存在子节点，无法批量删除！");
            }
        }
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }
    @Override
    public int deleteTiVehicleConfigById(Long id) {

        long childCount = this.lambdaQuery()
                .eq(TiVehicleConfig::getParentId, id)
                .count();

        if (childCount > 0) {
            throw new GlobalException("当前节点存在子节点，无法删除，请先删除其子节点！");
        }

        return this.baseMapper.deleteById(id);
    }

}
