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.CacheConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.vertex.BdVertex;

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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 体系配置 服务实现类
 *
 * @author hzs
 * @since 2022-10-31
 */
@Service
public class BdVertexServiceImpl extends ServiceImpl<BdVertexMapper, BdVertex> implements IBdVertexService {

    @Autowired
    private RedisService redisService;

    /**
     * 根据id查询体系配置
     *
     * @param pkId
     * @return
     */
    @Override
    public BdVertex getVertex(Integer pkId) {
        String redisKey = CacheConstants.BD_VERTEX + pkId;
        BdVertex vertex = redisService.getCacheObject(redisKey);
        if (null == vertex) {
            vertex = baseMapper.selectById(pkId);
            if (null != vertex) {
                redisService.setCacheObject(redisKey, vertex);
            }
        }
        return vertex;
    }


    @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;
    }

    /**
     * 查询体系配置
     * @param vertexParam
     * @return
     */
    @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);
    }
}
