package org.diagbot.graphWeb.work;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.diagbot.common.work.FeatureRate;
import org.diagbot.common.work.Filnlly;
import org.diagbot.common.work.ResponseData;
import org.diagbot.common.work.SearchData;
import org.diagbot.graph.jdbc.DriverManager;
import org.diagbot.graph.jdbc.Neo4jAPI;

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

public class GraphCalculate {
    public ResponseData calculate(HttpServletRequest request, SearchData searchData) throws Exception {
        ResponseData responseData = new ResponseData();
        ParamsDataProxy paramsDataProxy = new ParamsDataProxy();
        paramsDataProxy.createSearchData(request,searchData);
        List<String> inputList = new ArrayList<>();
        Map<String, Map<String, String>> inputs = searchData.getInputs();
        Set<String> ss = inputs.keySet();
        inputList.addAll(ss);
        Neo4jAPI neo4jAPI = new Neo4jAPI(DriverManager.newDrive());
        //处理化验数据
        if(searchData.getLisArr().size()!=0){
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(searchData.getLisArr()));
            List<String> lisResult = neo4jAPI.AnalysisLISResult(jsonArray);
            inputList.addAll(lisResult);
        }
        //计算提出的词,是否在图谱中找到
       List<String> grapWords = neo4jAPI.getGrapWords((String[]) inputList.toArray(new String[inputList.size()]));
        List<FeatureRate> graphWordfeatureRates = new ArrayList<>();
        if(grapWords.size()>0){
            for (String word: grapWords ) {
                FeatureRate featureRate = new FeatureRate();
                featureRate.setFeatureName(word.replaceAll("\"",""));
                featureRate.setExtraProperty("");
                featureRate.setDesc("");
                featureRate.setRate("");
                graphWordfeatureRates.add(featureRate);
            }
        }
        //计算诊断
        Map<String, String> condition = neo4jAPI.getCondition((String[]) inputList.toArray(new String[inputList.size()]));
        List<FeatureRate> featureRates = new ArrayList<>();
        for (Map.Entry<String,String> d:condition.entrySet()
                ) {
            FeatureRate featureRate = new FeatureRate();
            featureRate.setFeatureName(d.getKey());
            featureRate.setExtraProperty("");
            featureRate.setDesc(d.getValue());
            featureRate.setRate("");
            featureRates.add(featureRate);
        }

        //走治疗
        if(searchData.getDiag().trim()!=null){
            String diag = searchData.getDiag();
            Map<String, Filnlly> mulDiseaseTreat = neo4jAPI.getMulDiseaseTreat_2(diag, String.join(",", inputList));
            responseData.setTreat(mulDiseaseTreat);
        }
        responseData.setGraphWords(graphWordfeatureRates);
        responseData.setDis(featureRates);
//        response.setData(responseData);
        responseData.setInputs(searchData.getInputs());
        return responseData;
    }
}
