package com.wx.application.core.service;


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

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.OntologyRelation;
import com.wx.application.core.mapper.OntologyRelationMapper;
import com.wx.application.nebula.graph.base.Generators;
import com.wx.application.nebula.graph.bean.ModelEdgeLine;
import com.wx.application.nebula.graph.bean.NebulaEdgeLine;
import com.wx.application.nebula.graph.query.NebulaRelation;
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("coreOntologyRelationService")
@Transactional
public class OntologyRelationService extends BaseService<OntologyRelation, OntologyRelationMapper> {

	@Autowired
	OntologyRelationMapper baseMapper;
	
	
	@Autowired
	NebulaModelService nebulaModelService;
	
	public OntologyRelation createOntologyRelation(OntologyRelation ontologyRelation) {
		/**
		 * 验证label
		 */
		OntologyRelation has = baseMapper.findByOntologyIdAndLabel(ontologyRelation.getOntologyId(), 
				ontologyRelation.getLabel());
		
		if(has != null && has.getId() > 0) {
			return has;
		}
		
		return create(ontologyRelation);
	}
	
	/**
	 * 新增对象属性
	 */
	public Boolean createRelations(OntologyRelation ontologyRelation) {
		
		try {
			
			Long id = ontologyRelation.getId();
			List<String> srcIds = ontologyRelation.getSrcIds();
			List<String> dstIds = ontologyRelation.getDctIds();
			if(id == null) {
				return false;
			}
			
			OntologyRelation orl = getOne(id);
			
			if(orl == null) {
				return false;
			}
			
			/**
		     * 根据label标签查询关系,目前库里已经存在的
		     */
			List<NebulaRelation> relations = nebulaModelService
					.findRelationsByLabel(orl.getLabel());
			
			
			List<NebulaRelation> newRelations = new ArrayList<>();
			
			if(srcIds != null && dstIds != null) {
				srcIds.forEach(srcId->{
					dstIds.forEach(dstId->{
						NebulaRelation ln = new NebulaRelation();
						ln.setSrcId(srcId);
						ln.setDstId(dstId);
						newRelations.add(ln);
					});
				});
			}
			
			
			relations.forEach(v-> {
				if(!newRelations.contains(v)) {
					NebulaEdgeLine el = new NebulaEdgeLine();
					el.setSrcId(v.getSrcId());
					el.setDstId(v.getDstId());
					nebulaModelService.deleteModelEdge(el);
				} else {
					newRelations.remove(v);
				}
			});
			
			newRelations.forEach(v-> {
				ModelEdgeLine el = new ModelEdgeLine();
				el.setSrcId(v.getSrcId());
				el.setDstId(v.getDstId());
				el.setLabel(orl.getLabel());
				el.setEdgeName(Generators.edgename(orl.getLabel()));
				el.setOntologyId(orl.getOntologyId());
				nebulaModelService.insertModelEdge(el);
			});
			
			
		} catch (Exception e) {
			log.info("{}",e);
			return false;
		}
		return true;
	}
	
	public Boolean modifyOntologyRelation(OntologyRelation ontologyRelation) {
		
		Long id = ontologyRelation.getId();
		
		OntologyRelation orl = getOne(id);
		String oldname = orl.getLabel();
		
		Boolean flag = false;
		if(flag = modify(ontologyRelation)) {
			
			/**
		     * 根据label标签查询关系
		     */
			List<NebulaRelation> relations = nebulaModelService
					.findRelationsByLabel(oldname);
			
			for(NebulaRelation relation : relations) {
				JSONObject properties = relation.getProperties();
				ModelEdgeLine el = properties.toBean(ModelEdgeLine.class);
				el.setSrcId(relation.getSrcId());
				el.setDstId(relation.getDstId());
				el.setLabel(ontologyRelation.getLabel());
				el.setEdgeName(Generators.edgename(ontologyRelation.getLabel()));
				nebulaModelService.insertModelEdge(el);
			}
			
		}
		
		return flag;
	}
	
	/**
	 * 递归删除
	 */
	public Boolean deleteOntologyRelation(Long id) {
		
		try {
			/**
			 * 根据id 查询所有的子项和自己
			 */
			QueryWrapper<OntologyRelation> wrapper = new QueryWrapper();
			wrapper.eq("parent_id", id).or().eq("id", id);
			
			List<OntologyRelation> ors = baseMapper.selectList(wrapper);
			baseMapper.delete(wrapper);
			
			List<String> names = ors.stream().map(OntologyRelation::getLabel).collect(Collectors.toList());
			
			names.forEach(v-> {
				/**
			     * 根据label标签查询关系
			     */
				List<NebulaRelation> relations = nebulaModelService
						.findRelationsByLabel(v);
				
				NebulaEdgeLine edge = null;
				for(NebulaRelation r : relations) {
					edge = new NebulaEdgeLine();
					edge.setSrcId(r.getSrcId());
					edge.setDstId(r.getDstId());
					nebulaModelService.deleteModelEdge(edge);
				}
			});
			
			if(ors != null) {
				for(OntologyRelation ot : ors) {
					if(ot.getId() == id) {
						continue;
					}
					deleteOntologyRelation(ot.getId());
				}
			}
		} catch (Exception e) {
			log.info("{}",e);
			return false;
		}
		return true;
	}
	
}
