package com.wx.application.core.service;


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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.application.base.BaseService;
import com.wx.application.core.entity.OntologyConcept;
import com.wx.application.core.mapper.OntologyConceptMapper;
import com.wx.application.core.mapper.OntologyFieldMapper;
import com.wx.application.core.mapper.OntologyRelationMapper;
import com.wx.application.nebula.graph.base.Generators;
import com.wx.application.nebula.graph.bean.ModelVertex;
import com.wx.application.nebula.graph.service.NebulaModelService;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author zj
 * @since 2022-09-25
 */
@Slf4j
@Service("coreOntologyConceptService")
@Transactional
public class OntologyConceptService extends BaseService<OntologyConcept, OntologyConceptMapper> {

	@Autowired
	OntologyConceptMapper baseMapper;
	
	@Autowired
	OntologyFieldMapper ontologyFieldMapper;
	
	@Autowired
	OntologyRelationMapper ontologyRelationMapper;
	
	@Autowired
	NebulaModelService nebulaModelService;
	
	/**
	 * 新增概念
	 */
	public OntologyConcept createOntologyConcept(OntologyConcept ontologyConcept) {
		
		String ownId = ontologyConcept.getOwnId();
		
		/**
		 * 验证label
		 */
		OntologyConcept has = baseMapper.findByOntologyIdAndLabel(ontologyConcept.getOntologyId(), 
				ontologyConcept.getLabel());
		
		if(has != null && has.getId() > 0) {
			return has;
		}

		if(StringUtils.isBlank(ontologyConcept.getOwnId())) {
			ownId = Generators.vid(ontologyConcept.getLabel());
			ownId = Generators.ontologyVid(ontologyConcept.getOntologyId() , ownId);
			ontologyConcept.setOwnId(ownId);
		}
		
		create(ontologyConcept);
		/**
		 * 将概念加入图谱中
		 */
		ModelVertex mx = new ModelVertex();
		mx.setOntologyId(ontologyConcept.getOntologyId());
		mx.setLabel(ontologyConcept.getLabel());
		mx.setTagName(Generators.tagname(mx.getLabel()));
		mx.setVid(ownId);
		nebulaModelService.insertModelVertex(mx);
		
		return ontologyConcept;
	}
	
	public Boolean modifyOntologyConcept(OntologyConcept ontologyConcept) {
		
		if(StringUtils.isBlank(ontologyConcept.getOwnId()) 
				|| StringUtils.isBlank(ontologyConcept.getLabel())) {
			return false;
		}
		
		QueryWrapper<OntologyConcept> wrapper = new QueryWrapper();
		wrapper.eq("own_id", ontologyConcept.getOwnId());
		OntologyConcept oct = baseMapper.selectOne(wrapper);
		
		oct.setLabel(ontologyConcept.getLabel());
		
		/**
		 * 将概念加入图谱中
		 */
		Boolean flag = false;
		if(flag = modify(oct)) {
			ModelVertex mx = new ModelVertex();
			mx.setOntologyId(oct.getOntologyId());
			mx.setLabel(oct.getLabel());
			mx.setTagName(Generators.tagname(mx.getLabel()));
			mx.setVid(oct.getOwnId());
			nebulaModelService.insertModelVertex(mx);
		}
		
		return flag;
	}
	
	/**
	 * 递归删除
	 */
	public Boolean deleteOntologyConcept(Long id) {
		
		try {
			
			if(id == null) {
				return false;
			}
			
			OntologyConcept oct = getOne(id);
			QueryWrapper<OntologyConcept> wrapper = new QueryWrapper();
			wrapper.eq("parent_id", oct.getOwnId()).or().eq("id", id);
			List<OntologyConcept> childs = baseMapper.selectList(wrapper);
			baseMapper.delete(wrapper);
			
			List<String> vids = childs.stream().map(OntologyConcept::getOwnId).collect(Collectors.toList());
			
			/**
			 * 删除图谱中的点
			 */
			nebulaModelService.deleteModelVertexs(vids);
			
			if(childs != null) {
				for(OntologyConcept ot : childs) {
					deleteOntologyConcept(ot.getId());
				}
			}
		} catch (Exception e) {
			log.info("{}",e);
			return false;
		}
		return true;
	}
	
	/**
	 * 根据本体id统计
	 * @param ontologyId
	 * @return
	 */
	public JSONObject countAllByOntologyId(Long ontologyId) {
		
		int conceptc =  baseMapper.countByOntologyId(ontologyId);
		int fieldc =  ontologyFieldMapper.countByOntologyId(ontologyId);
		int relationc =  ontologyRelationMapper.countByOntologyId(ontologyId);
		
		JSONObject result = new JSONObject();
		result.set("conceptc", conceptc);
		result.set("fieldc", fieldc);
		result.set("relationc", relationc);
		
		return result;
	}
	
	
	public Integer countChildbyid(Long id) {
		
		if(id == null) {
			return 0;
		}
		
		OntologyConcept oct = getOne(id);
		
		if(oct == null) {
			return 0;
		}
		
		Integer c = baseMapper.countChildbyid(oct.getOwnId());
		
		return c;
	}
	
	
	public List<OntologyConcept> findWithParents(String ownId) {
		QueryWrapper<OntologyConcept> wrapper = new QueryWrapper();
		wrapper.eq("own_id", ownId);
		OntologyConcept own = baseMapper.selectOne(wrapper);
		List<OntologyConcept> concepts = null;
		if(own != null) {
			concepts = new ArrayList<>();
			findWithParents(own, concepts);
		}
		return concepts;
	}
	
	/**
	 * 递归
	 * 查询当前节点的值属性，包含继承的值属性
	 */
	public void findWithParents(OntologyConcept own, List<OntologyConcept> concepts) {
		QueryWrapper<OntologyConcept> wrapper = new QueryWrapper();
		wrapper.eq("own_id", own.getParentId());
		OntologyConcept pa = baseMapper.selectOne(wrapper);
		if(pa != null) {
			concepts.add(pa);
			findWithParents(pa, concepts);
		}
	}
	
}
