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

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.RestController;

import com.wx.application.base.BaseController;
import com.wx.application.base.ResponseData;
import com.wx.application.nebula.graph.bean.NebulaEdge;
import com.wx.application.nebula.graph.bean.NebulaEdgeLine;
import com.wx.application.nebula.graph.bean.NebulaIndex;
import com.wx.application.nebula.graph.bean.NebulaSpace;
import com.wx.application.nebula.graph.bean.NebulaTag;
import com.wx.application.nebula.graph.bean.NebulaVertex;
import com.wx.application.nebula.graph.query.NebulaQo;
import com.wx.application.nebula.graph.service.NebulaOperateService;


@SuppressWarnings("rawtypes")
@RestController
@RequestMapping("/nebula_operate")
public class NebulaOperateController extends BaseController {

	@Autowired
	NebulaOperateService nebulaOperateService;
	
	/**
	 * 查看所有的图空间
	 */
	@GetMapping(value = "/showspace")
	public ResponseData showSpace() {
		return success(nebulaOperateService.findSpace());
	}
	
	/**
	 * 空间创建语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/createspace")
	public ResponseData createSpace(@RequestBody NebulaSpace space) {
		nebulaOperateService.createSpace(space);
		return success(space);
	}

	
	/**
	 * 删除莫个图空间
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/dropspace/{space}")
	public ResponseData dropSpace(@PathVariable("space") String space) {
		nebulaOperateService.dropSpace(space);
		return success();
	}
	
	/**
	 * 显示莫个图空间的详细信息
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/descspace/{space}")
	public ResponseData descSpace(@PathVariable("space") String space) {
		return success(nebulaOperateService.descSpace(space));
	}
	
	
	/**
	 * 标签创建语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/createtag/{space}")
	public ResponseData createTag(@PathVariable("space") String space, @RequestBody NebulaTag e) {
		nebulaOperateService.createTag(space, e);
		return success();
	}
	
	
	/**
	 * 标签添加字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alteraddtag/{space}")
	public ResponseData alterAddTag(@PathVariable("space") String space, @RequestBody NebulaTag e) {
		nebulaOperateService.alterAddTag(space, e);
		return success();
	}
	
	
	/**
	 * 标签删除字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alterdroptag/{space}")
	public ResponseData alterDropTag(@PathVariable("space") String space, @RequestBody NebulaTag e) {
		nebulaOperateService.alterDropTag(space, e);
		return success();
	}
	
	/**
	 * 标签修改字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alterchangetag/{space}")
	public ResponseData alterChangeTag(@PathVariable("space") String space, @RequestBody NebulaTag e) {
		nebulaOperateService.alterChangeTag(space, e);
		return success();
	}
	
	/**
	 * 标签删除
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/droptag/{space}/{tag}")
	public ResponseData dropTag(@PathVariable("space") String space, @PathVariable("tag") String tag) {
		nebulaOperateService.dropTag(space, tag);
		return success();
	}
	
	
	/**
	 * 查看所有标签
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/showtag/{space}")
	public ResponseData showTag(@PathVariable("space") String space) {
		return success(nebulaOperateService.showTag(space,true));
	}
	
	/**
	 * 查看标签的创建语句
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/showcreatetag/{space}/{tag}")
	public ResponseData showCreateTag(@PathVariable("space") String space,
			@PathVariable("tag") String tag) {
		return success(nebulaOperateService.showCreateTag(space, tag));
	}
	
	
	/**
	 * 标签查看详细信息
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/desctag/{space}/{tag}")
	public ResponseData descTag(@PathVariable("space") String space, @PathVariable("tag") String tag) {
		return success(nebulaOperateService.descTag(space, tag));
	}
	
	
	/**
	 * 边创建语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/createedge/{space}")
	public ResponseData createEdge(@PathVariable("space") String space, @RequestBody NebulaEdge e) {
		nebulaOperateService.createEdge(space, e);
		return success();
	}
	
	
	/**
	 * 边添加字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alteraddedge/{space}")
	public ResponseData alterAddEdge(@PathVariable("space") String space, @RequestBody NebulaEdge e) {
		nebulaOperateService.alterAddEdge(space, e);
		return success();
	}
	
	
	/**
	 * 边删除字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alterdropedge/{space}")
	public ResponseData alterDropEdge(@PathVariable("space") String space, @RequestBody NebulaEdge e) {
		nebulaOperateService.alterDropEdge(space, e);
		return success();
	}
	
	/**
	 * 边修改字段语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/alterchangeedge/{space}")
	public ResponseData alterChangeEdge(@PathVariable("space") String space, @RequestBody NebulaEdge e) {
		nebulaOperateService.alterChangeEdge(space, e);
		return success();
	}
	
	/**
	 * 边删除
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/dropedge/{space}/{edge}")
	public ResponseData dropEdge(@PathVariable("space") String space, @PathVariable("edge") String edge) {
		nebulaOperateService.dropEdge(space, edge);
		return success();
	}
	
	/**
	 * 查看标签的创建语句
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/showcreateedge/{space}/{edge}")
	public ResponseData showCreateEdge(@PathVariable("space") String space,
			@PathVariable("edge") String edge) {
		return success(nebulaOperateService.showCreateEdge(space, edge));
	}
	
	/**
	 * 查看所有边
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/showedge/{space}")
	public ResponseData showEdge(@PathVariable("space") String space) {
		return success(nebulaOperateService.showEdge(space,true));
	}
	
	/**
	 * 边查看详细信息
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/descedge/{space}/{edge}")
	public ResponseData descEdge(@PathVariable("space") String space, @PathVariable("edge") String edge) {
		return success(nebulaOperateService.descEdge(space, edge));
	}
	
	/**
	 * 索引创建语句
	 * @param e
	 * @return
	 */
	@PostMapping(value = "/createindex/{space}")
	public ResponseData createIndex(@PathVariable("space") String space, @RequestBody NebulaIndex e) {
		nebulaOperateService.createIndex(space, e);
		return success();
	}
	
	/**
	 * 查看边或者标签的索引
	 * @param e
	 * @return
	 */
	@GetMapping(value = "/showindex/{space}/{type}")
	public ResponseData showIndex(@PathVariable("space") String space, @PathVariable("type") String type) {
		return success(nebulaOperateService.showIndex(space, type));
	}
	
	/**
	 * 删除边或者标签的索引
	 * @param e
	 * @return
	 */
	@PostMapping(value = "/dropindex/{space}")
	public ResponseData dropIndex(@PathVariable("space") String space, @RequestBody NebulaIndex e) {
		nebulaOperateService.dropIndex(space, e);
		return success();
	}
	
	
	/**
	 * 删除点语句
	 */
	@GetMapping(value = "/deletevertex/{space}/{vid}")
	public ResponseData deleteVertex(@PathVariable("space") String space, @PathVariable("vid") String vid) {
		nebulaOperateService.deleteVertex(space, vid);
		return success();
	}
	
	/**
	 * 添加点语句
	 */
	@PostMapping(value = "/insertvertex/{space}/{tag}")
	public ResponseData insertVertex(@PathVariable("space") String space
			,@PathVariable("tag") String tag, @RequestBody NebulaVertex e) {
		nebulaOperateService.insertVertex(space, tag, e);
		return success(e);
	}
	
	
	
	/**
	 * 边数据删除语句
	 * @param space
	 * @return
	 */
	@PostMapping(value = "/deleteedgeline/{space}/{edge}")
	public ResponseData deleteEdgeData(@PathVariable("space") String space, 
			@PathVariable("edge") String edge, @RequestBody NebulaEdgeLine e) {
		nebulaOperateService.deleteEdgeLine(space, edge, e);
		return success();
	}
	
	/**
	 * 插入边数据
	 * @param e
	 * @return
	 */
	@PostMapping(value = "/insertedgeline/{space}/{edge}")
	public ResponseData insertEdgeData(@PathVariable("space") String space, 
			@PathVariable("edge") String edge, @RequestBody NebulaEdgeLine e) {
		nebulaOperateService.insertEdgeLine(space,edge, e);
		return success();
	}
	
	
	/**
	 * 根据id查找一个节点，不用使用tag的
	 */
	@GetMapping(value = "/findnodebyid/{space}/{vid}")
	public ResponseData findNodeById(@PathVariable("space") String space, 
			@PathVariable("vid") String vid) {
		return success(nebulaOperateService.findNodeById(space, vid));
	}
	
	
	/**
	 * 根据id查找一个节点，需要使用tag的
	 */
	@GetMapping(value = "/findnodebyid/{space}/{tag}/{vid}")
	public ResponseData findNodeById(@PathVariable("space") String space, 
			@PathVariable("tag") String tag,@PathVariable("vid") String vid) {
		return success(nebulaOperateService.findNodeById(space, tag, vid));
	}
	
	
	/**
	 * 根据查询条件查询tag下面的一个节点
	 */
	@PostMapping(value = "/findnodebykeyword/{space}/{tag}")
	public ResponseData findNodeByKeyword(@PathVariable("space") String space, 
			@PathVariable("tag") String tag,
			@RequestBody NebulaQo nQo) {
		return success(nebulaOperateService.findNodeByKeyword(space, tag, nQo));
	}
	
	
	/**
	 * 根据2个节点id获取一条边
	 */
	@GetMapping(value = "/findrelationbyid/{space}/{edge}/{srcVid}/{dstVid}")
	public ResponseData findRelationById(@PathVariable("space") String space, 
			@PathVariable("edge") String edge
			,@PathVariable("srcVid") String srcVid
			,@PathVariable("dstVid") String dstVid) {
		return success(nebulaOperateService.findRelationById(space, edge, srcVid, dstVid));
	}
	
	
	/**
	 * 查询一个节点的周边一层节点关系
	 * @param nQ
	 * @return
	 */
	@GetMapping(value = "/findonepathbyid/{space}/{vid}")
	public ResponseData findOnePathById(@PathVariable("space") String space, 
			@PathVariable("vid") String vid) {
		return success(nebulaOperateService.findOnePathById(space, vid));
	}
	
	/**
	 * 查询一个头节点和尾结点的关系
	 * @param nQ
	 * @return
	 */
	@GetMapping(value = "/findonepathbysrcidanddctid/{space}/{srcId}/{dctId}")
	public ResponseData findOnePathBySrcIdAndDctId(@PathVariable("space") String space, 
			@PathVariable("srcId") String srcId, @PathVariable("dctId") String dctId) {
		return success(nebulaOperateService.findOnePathBySrcIdAndDctId(space, srcId, dctId));
	}
	
	
	/**
	 * 根据图空间查询图谱
	 * 已知tag edge 查询（可没有）
	 */
	@PostMapping(value = "/findpathinspace/{space}")
	public ResponseData findPathInSpace(@PathVariable("space") String space, 
			@RequestBody NebulaQo nebulaQo) {
		return success(nebulaOperateService.findPathInSpace(space, nebulaQo));
	}
	
	/**
	 * 根据图空间查询图谱
	 * 已知tag 查询
	 */
	@PostMapping(value = "/findnodes/{space}")
	public ResponseData findNodes(@PathVariable("space") String space, 
			@RequestBody NebulaQo nebulaQo) {
		return success(nebulaOperateService.findNodes(space, nebulaQo));
	}
	
	/**
	 * 根据图空间查询图谱
	 * 已知edge 查询
	 */
	@PostMapping(value = "/findrelations/{space}")
	public ResponseData findrelations(@PathVariable("space") String space, 
			@RequestBody NebulaQo nebulaQo) {
		return success(nebulaOperateService.findRelations(space, nebulaQo));
	}
	
	
	/**
	 * 根据图空间进行统计
	 * @param space
	 * @return
	 */
	@GetMapping(value = "/censusgraphbyspace/{space}")
	public ResponseData censusgraphbyspace(@PathVariable("space") String space) {
		return success(nebulaOperateService.censusGraphBySpace(space));
	}

}
