package org.diagbot.graphWeb.controller;

import com.alibaba.fastjson.JSONObject;
import org.diagbot.graph.jdbc.DriverManager;
import org.diagbot.graph.jdbc.Neo4jAPI;
import org.diagbot.graph.jdbc.Neo4jModel;
import org.diagbot.graphWeb.dao.GdbResponse;
import org.diagbot.graphWeb.dao.TestResponse;
import org.diagbot.pub.api.Response;
import org.diagbot.pub.web.BaseController;
import org.neo4j.driver.v1.Driver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Map;

@Controller
@RequestMapping("/graphdb")
public class gdbcontroller extends BaseController {
    private Driver drive = null;
    private Neo4jAPI n4api = null;

    /**
     * 诊断接口
     * @param request
     * @param bodyobj
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    @ResponseBody
    public Response<GdbResponse> condition(HttpServletRequest request, @RequestBody JSONObject bodyobj) throws Exception {
//        System.out.println(bodyobj.toJSONString());
        Response<GdbResponse> response = ProcessCondition(request, bodyobj);
        return response;
    }
    public Response<GdbResponse> ProcessCondition(HttpServletRequest request, JSONObject bodyobj) {
        Response<GdbResponse> response = new Response<>();
        GdbResponse gdbdata = new GdbResponse();
        String name = null;
        Map<String,String> RSet = null;
        String disease = null;
        try {
            if (drive == null) {
                drive = DriverManager.newDrive();
            }
            if (drive != null && drive.session() != null) {
                name = bodyobj.getString(Neo4jModel.name);
                String[] keys = name.split(",|，|、");
                n4api = new Neo4jAPI(drive);
                RSet = n4api.getCondition(keys);
            }
            gdbdata.setResult(RSet);
            response.setData(gdbdata);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return response;
        }
    }

    /**
     * 一个或多个疾病下的治疗接口
     * @param request
     * @param bodyobj
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/multipleDiseaseTreat", method = RequestMethod.POST)
    @ResponseBody
    public Response<GdbResponse> multipleDiseaseTreat(HttpServletRequest request, @RequestBody JSONObject bodyobj) throws Exception {
        Response<GdbResponse> response = processMulDiseaseTreat(request, bodyobj);
        return response;
    }

    public Response<GdbResponse> processMulDiseaseTreat(HttpServletRequest request, JSONObject bodyobj) {
        Response<GdbResponse> response = new Response<>();
        GdbResponse gdbdata = new GdbResponse();
        String diseases = null;
        String filds = null;
        ArrayList<String> strings = new ArrayList<>();
        Map<String,String> RSet = null;
        try {
            if (drive == null) {
                drive = DriverManager.newDrive();
            }
            if (drive != null && drive.session() != null) {
                diseases = bodyobj.getString("disease");//疾病组合
                filds = bodyobj.getString("filds");//其他组合(症状,体征结果,化验结果,检查结果,病史等)
                n4api = new Neo4jAPI(drive);
                RSet = n4api.getMulDiseaseTreat(diseases, filds);
            }
            gdbdata.setResult(RSet);
            response.setData(gdbdata);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return response;
        }
    }

    /**
     * 高危病接口
     * @param request
     * @param bodyobj
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/HighRisk", method = RequestMethod.POST)
    @ResponseBody
    public Response<GdbResponse> HighRisk(HttpServletRequest request, @RequestBody JSONObject bodyobj) throws Exception {
        Response<GdbResponse> response = processHighRisk(request, bodyobj);
        return response;
    }
    public Response<GdbResponse> processHighRisk(HttpServletRequest request, JSONObject bodyobj) {
        Response<GdbResponse> response = new Response<>();
        GdbResponse gdbdata = new GdbResponse();
        String diseases = null;
        Map<String,String> RSet = null;
        try {
            if (drive == null) {
                drive = DriverManager.newDrive();
            }
            if (drive != null && drive.session() != null) {
                diseases = bodyobj.getString("disease");//疾病
                n4api = new Neo4jAPI(drive);
                RSet = n4api.getHighRisk(diseases);
            }
            gdbdata.setResult(RSet);
            response.setData(gdbdata);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return response;
        }
    }

    /**
     * 自定义接口数据结构
     * @param request
     * @param bodyobj
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/HighRisk1", method = RequestMethod.POST)
    @ResponseBody
    public Response<TestResponse> HighRisk1(HttpServletRequest request, @RequestBody JSONObject bodyobj) throws Exception {
        Response<TestResponse> response = new Response<TestResponse>();
        TestResponse testResponse = new TestResponse();
        String diseases = bodyobj.getString("disease");//疾病
        JSONObject JS = new JSONObject();
        JS.put("name","jack");
        JS.put("hobby",new String[]{"swing","sing"});
        testResponse.setResult(JS);
        response.setData(testResponse);
        return response;
    }

}
