package com.dgt.graphdemo.controller;

import com.dgt.graphdemo.entity.GraphData;
import com.dgt.graphdemo.entity.GraphQuery;
import com.dgt.graphdemo.entity.MyNode;
import com.dgt.graphdemo.exception.CommonBizException;
import com.dgt.graphdemo.exception.ExpCodeEnum;
import com.dgt.graphdemo.result.Result;
import com.dgt.graphdemo.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author lugela
 * @Title: GraphController
 * @ProjectName graphdemo
 * @Description: TODO
 * @date 2019/6/27 9:37
 */
@RestController
@RequestMapping("/dgt/graph")
public class GraphController {
    @Autowired
    GraphService graphService;

    @RequestMapping("/nodeAndRep.do")
    public Result<GraphData> getGraph(@RequestBody(required = false)GraphQuery graphQuery){
        if (graphQuery==null){
            CommonBizException commonBizException = new CommonBizException(ExpCodeEnum.PARAM_NULL);
            return Result.newFailureResult(commonBizException);
        }


        List<String> weaverIdList =null;
        List<String> graphNames = graphQuery.getGraphNameList();
        List<String>  graphNameList = null;
        if (graphNames!=null && !graphNames.isEmpty()){
            graphNameList=  graphNames.stream().map(x->"'"+x+"'").collect(Collectors.toList());
        }
         weaverIdList = graphQuery.getWeaverIdList();
        if (weaverIdList==null || weaverIdList.isEmpty()){
            weaverIdList = new ArrayList<>();
        }
        GraphData graphData = graphService.deatilGraphRep(graphNameList,weaverIdList);
        return Result.successResult(graphData);
    }


    @RequestMapping("/nodeExtend.do")
    public Result<GraphData> getExtendGraph(@RequestBody(required = false)GraphQuery graphQuery){
        if (graphQuery==null){
            CommonBizException commonBizException = new CommonBizException(ExpCodeEnum.PARAM_NULL);
            return Result.newFailureResult(commonBizException);
        }


        List<String> weaverIdList =graphQuery.getWeaverIdList();
        MyNode node = graphQuery.getNode();
        String id = node.getId();
        String type = node.getType();
        String filterType = graphQuery.getFilterType();
        GraphData graphData = graphService.deatilGraphExtend(filterType, type, id, weaverIdList);
        return Result.successResult(graphData);
    }

}
