package com.hzs.system.config.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.MemberConstants;
import com.hzs.common.domain.vertex.BdVertex;
import com.hzs.common.domain.vertex.ext.BdVertexExt;
import com.hzs.system.config.mapper.BdVertexMapper;
import com.hzs.system.config.param.VertexParam;
import com.hzs.system.config.service.IBdVertexService;
import com.hzs.system.config.vo.VertexVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体系配置 服务实现类
 */
@Service
public class BdVertexServiceImpl extends ServiceImpl<BdVertexMapper, BdVertex> implements IBdVertexService {

    @Override
    public BdVertex getVertex(Integer pkId) {
        return baseMapper.selectById(pkId);
    }

    @Override
    public Map<Integer, BdVertex> getAllVertexMap() {
        Map<Integer, BdVertex> resultMap = new HashMap<>();
        List<BdVertex> vertexList = baseMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(vertexList)) {
            for (BdVertex bdVertex : vertexList) {
                resultMap.put(bdVertex.getPkId(), bdVertex);
            }
        }
        return resultMap;
    }

    @Override
    public List<VertexVo> selectByList(VertexParam vertexParam) {
        return baseMapper.selectByList(vertexParam);
    }

    @Override
    public List<BdVertex> queryListByUserId(Long userId) {
        return baseMapper.queryListByUserId(userId);
    }

    @Override
    public void updateBdVertexById(Integer pkVertex) {
        BdVertex vertex = getVertex(pkVertex);
        // 先把原来的更新为null
        baseMapper.updateBdVertexNullById(pkVertex);
        baseMapper.updateBdVertexById(vertex.getPkMember(), pkVertex);
    }

    @Override
    public List<BdVertexExt> queryBdVertexChild(Integer pkCountry) {
        return baseMapper.queryBdVertexChild(pkCountry);
    }

    @Override
    public BdVertexExt queryVertexAndMember(Long pkId) {
        return baseMapper.queryVertexAndMember(pkId);
    }

    @Override
    public void flushAllVertex(Integer pkCountry) {
        List<BdVertex> list = baseMapper.queryFlushVertexList(MemberConstants.GJ30720200CODE, pkCountry);
        for (BdVertex vertex : list) {
            baseMapper.updateVertexNew(vertex.getPkMember(), vertex.getPkId(), pkCountry);
        }
    }

}
