package com.wx.application.nebula.graph.conrtroller;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wx.application.base.BaseController;
import com.wx.application.base.ResponseData;
import com.wx.application.nebula.graph.base.Generators;
import com.wx.application.nebula.graph.bean.ModelEdgeLine;
import com.wx.application.nebula.graph.bean.ModelVertex;
import com.wx.application.nebula.graph.bean.NebulaEdgeLine;
import com.wx.application.nebula.graph.enums.DataType;
import com.wx.application.nebula.graph.query.NebulaQo;
import com.wx.application.nebula.graph.service.ImportModelService;
import com.wx.application.nebula.graph.service.NebulaModelService;

/**
 * 本体
 * @author zj
 *
 */
@RestController
@RequestMapping("/nebula_model")
public class NebulaModelController extends BaseController {
	
	@Autowired
	NebulaModelService nebulaModelService;
	
	@Autowired
	ImportModelService importModelService;
	
	/**
	 * 插入模型点
	 * @param vertex
	 */
	@PostMapping(value = "/insertmodelvertex")
	public ResponseData insertModelVertex(@RequestBody ModelVertex mx) {
		if(StringUtils.isBlank(mx.getVid())) {
			String vid = Generators.vid(mx.getLabel());
			vid = Generators.ontologyVid(mx.getOntologyId(), vid);
			mx.setVid(vid);
		}
		return success(nebulaModelService.insertModelVertex(mx));
	}
	
	/**
	 * 删除一个本体模型点
	 */
	@GetMapping(value = "/deletemodelvertex/{vid}")
	public ResponseData deleteModelVertex(@PathVariable("vid") String vid) {
		nebulaModelService.deleteModelVertex(vid);
		return success();
	}
	
	
	/**
	 * 创建一个模型关系
	 */
	@PostMapping(value = "/insertmodeledge")
	public ResponseData insertModelEdge(@RequestBody ModelEdgeLine edge) {
		return success(nebulaModelService.insertModelEdge(edge));
	}
	
	
	/**
	 * 删除一个模型关系
	 */
	@PostMapping(value = "/deletemodeledge")
	public ResponseData deleteModelEdge(@RequestBody NebulaEdgeLine edge) {
		nebulaModelService.deleteModelEdge(edge);
		return success();
	}
	
	
	/**
     * 根据OntologyId删除所有的本体和关系
     */
	@GetMapping(value = "/deleteallbyontologyid/{ontologyId}")
    public ResponseData deleteAllByOntologyId(@PathVariable("ontologyId") Long ontologyId) {
		nebulaModelService.deleteAllByOntologyId(ontologyId);
    	return success();
    }

    /**
     * 根据OntologyId查询的本体和关系，用于图谱展示
     */
	@PostMapping(value = "/findrelationbyontologyid/{ontologyId}")
    public ResponseData findRelationByOntologyId(@PathVariable("ontologyId") Long ontologyId, 
    		@RequestBody NebulaQo nQ) {
    	return success(nebulaModelService.findRelationByOntologyId(ontologyId, nQ));
    }
    
	
	/**
     * 根据OntologyId查询的本体和关系,查询所有的,目前标注用
     */
	@PostMapping(value = "/findallgraphbyontologyid/{ontologyId}")
    public ResponseData findAllGraphByOntologyId(@PathVariable("ontologyId") Long ontologyId) {
    	return success(nebulaModelService.findAllGraphByOntologyId(ontologyId));
    }
	
    
    /**
     * 复制图模型
     */
	@GetMapping(value = "/copybyontologyid/{srcOntologyId}/{dictOntologyId}")
    public ResponseData copyByOntologyId(@PathVariable("srcOntologyId")Long srcOntologyId,
    		@PathVariable("dictOntologyId")Long dictOntologyId) {
		nebulaModelService.copyByOntologyId(srcOntologyId, dictOntologyId);
    	return success();
    }
    
    /**
	 * 根据本体模型创建图空间
	 */
	@GetMapping(value = "/initgraphdatabase/{ontologyId}/{space}")
	public ResponseData initGraphDatabase(@PathVariable("ontologyId") Long ontologyId, 
			@PathVariable("space") String space) {
		nebulaModelService.initGraphDatabase(ontologyId, space);
		return success();
	}
	
	
	/**
	 * 根据模板excel导入模型
	 * @throws IOException 
	 */
	@PostMapping(value = "/importmodel/{ontologyId}")
    public ResponseData importModel(@PathVariable("ontologyId") Long ontologyId,
    		@RequestParam("file") MultipartFile file) throws IOException {
		return success(importModelService.analysisExcel(ontologyId,file.getInputStream()));
    }


	/**
	 * 本体空间统计点
	 */
	@GetMapping(value = "/countvertexsbyontologyid/{ontologyId}")
    public ResponseData countVertexsbyontologyid(@PathVariable("ontologyId") Long ontologyId) {
    	return success(nebulaModelService.countVertexsByOntologyid(ontologyId));
    }
	/**
	 * 本体空间统计边
	 */
	@GetMapping(value = "/countedgesbyontologyid/{ontologyId}")
    public ResponseData countEdgesByOntologyid(@PathVariable("ontologyId") Long ontologyId) {
    	return success(nebulaModelService.countEdgesByOntologyid(ontologyId));
    }
	
	/**
	 * 根据关键统计开始节点和 结束节点的数量
	 */
	@GetMapping(value = "/groupcountvexbyedgebyontologyid/{ontologyId}")
    public ResponseData groupCountVexByEdgeByOntologyid(@PathVariable("ontologyId") Long ontologyId) {
    	return success(nebulaModelService.groupCountVexByEdgeByOntologyid(ontologyId));
    }
	

	/**
	 * 图空间的值属性数量分组聚合
	 */
	@GetMapping(value = "/groupfieldbyontologyid/{ontologyId}")
    public ResponseData groupFieldByOntologyId(@PathVariable("ontologyId") Long ontologyId) {
    	return success(nebulaModelService.groupFieldByOntologyId(ontologyId));
    }
	
	
	/**
	 * 模型空间根据概念id查找path
	 */
	@GetMapping(value = "/findpathbyid/{ontologyId}/{vid}")
	public ResponseData findPathById(@PathVariable("ontologyId") Long ontologyId, 
			@PathVariable("vid") String vid) {
		return success(nebulaModelService.findPathById(ontologyId, vid));
	}
	
	/**
	 * 模型空间根据id查找节点
	 */
	@GetMapping(value = "/findnodebyid/{ontologyId}/{vid}")
	public ResponseData findNodeById(@PathVariable("ontologyId") Long ontologyId, 
			@PathVariable("vid") String vid) {
		return success(nebulaModelService.findNodeById(vid));
	}
	
	/**
	 * 通过对象属性名称查询所有的path
	 */
	@GetMapping(value = "/findpathbyedgelabel/{ontologyId}/{label}")
	public ResponseData findPathByEdgeLabel(@PathVariable("ontologyId") Long ontologyId, 
			@PathVariable("label") String label) {
		return success(nebulaModelService.findPathByEdgeLabel(ontologyId,label));
	}
	 
	/**
	 * 通过值属性名称查询所有概念
	 */
	@GetMapping(value = "/findnodesbyfield/{ontologyId}/{field}")
	public ResponseData findNodesbyField(@PathVariable("ontologyId") Long ontologyId,
			@PathVariable("field") String field) {
		return success(nebulaModelService.findNodesbyField(ontologyId, field));
	}
	
	
	/**
	 * 获得值域列表
	 */
	@GetMapping(value = "/getdatatype")
	public ResponseData getDataType() {
		return success(DataType.values());
	}
	
	/**
	 * 模型空间根据概念id对象属性列表
	 */
	@GetMapping(value = "/findlistpathbyid/{ontologyId}/{vid}")
	public ResponseData findListPathById(@PathVariable("ontologyId") Long ontologyId, 
			@PathVariable("vid") String vid) {
		return success(nebulaModelService.findListPathById(vid));
	}
	
	
	/**
	 * 递归
	 * 查询当前节点的值属性，包含继承的值属性
	 */
	@GetMapping(value = "/findnebulafieldwithparents/{vid}")
	public ResponseData findNebulaFieldWithParents(@PathVariable("vid") String vid) {
		return success(nebulaModelService.findNebulaFieldWithParents(vid));
	}
	
}
