package com.persagy.dmp.rwd.define.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.define.entity.RelationDefine;
import com.persagy.dmp.rwd.define.dao.RelationDefineMapper;
import com.persagy.dmp.rwd.define.service.IRelationDefineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 关系定义 实现类
 * @author Charlie Yu
 * @date 2021-06-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RelationDefineServiceImpl  extends ServiceImpl<RelationDefineMapper, RelationDefine> implements IRelationDefineService {

    @Resource
    private RelationDefineMapper dao;

    @Override
    public List<RelationDefine> queryByCondition(Wrapper<RelationDefine> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    @Override
    public Integer queryCount(Wrapper<RelationDefine> queryWrapper) {
        return dao.selectCount(queryWrapper);
    }

    @Override
    public Page queryByCondition(Page page, Wrapper<RelationDefine> queryWrapper) {
        return dao.selectPage(page, queryWrapper);
    }

    @Override
    public RelationDefine insert(RelationDefine vo) {
        dao.insert(vo);
        return vo;
    }

    @Override
    public RelationDefine update(RelationDefine vo) {
        dao.updateById(vo);
        return vo;
    }

    @Override
    public void delete(String id) {
        RelationDefine vo = dao.selectById(id);
        vo.setValid(ValidEnum.FALSE.getType());
        dao.updateById(vo);
    }
    /***
     * Description: 根据图类型编码和边类型编码查询边类型对象映射
     * @param graphCodes : 图类型编码集合
     * @param relCodes : 边类型编码集合
     * @return : java.util.Map<java.lang.String,com.persagy.dmp.rwd.basic.constant.DigitalRelCode>
     * @author : lijie
     * @date :2021/9/14 21:21
     * Update By lijie 2021/9/14 21:21
     */
    @Override
    public Map<String, RelationDefine> queryRelMapByCodes(Set<String> graphCodes, Set<String> relCodes) {
        if (CollUtil.isEmpty(graphCodes)
                || CollUtil.isEmpty(relCodes)){
            return new HashMap<>(16);
        }
        LambdaQueryWrapper<RelationDefine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RelationDefine::getGraphCode,graphCodes);
        queryWrapper.in(RelationDefine::getCode,relCodes);
        queryWrapper.eq(RelationDefine::getValid,true);
        List<RelationDefine> relationDefines = this.list(queryWrapper);
        if (CollUtil.isEmpty(relationDefines)){
            return new HashMap<>(16);
        }
        return relationDefines.stream().collect(Collectors.toMap(relationDefine -> relationDefine.getGraphCode()
                + StrUtil.UNDERLINE+relationDefine.getCode(),relationDefine -> relationDefine,(k1,k2)->k1));
    }
}
