package org.diagbot.web;

import org.diagbot.vo.*;
import org.diagbot.entity.node.LIS;
import org.diagbot.entity.node.Disease;
import org.diagbot.entity.node.Symptom;
import org.diagbot.entity.node.base.BaseNode;
import org.diagbot.service.KnowledgeService;
import org.diagbot.vo.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("knowledge")
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;

    @RequestMapping("addDisease")
    public Disease addDisease(@Valid @RequestBody Disease disease) {
        return knowledgeService.addDisease(disease);
    }

    @RequestMapping("/findOneDisease/{id}")
    public Disease findOneDisease(@PathVariable("id")String id){
        return knowledgeService.findOneDisease(Long.parseLong(id));
    }

    @RequestMapping("/findDiseases/{name}")
    public List<Disease> findDiseases(@PathVariable("name")String name){
        return knowledgeService.findDisease(name);
    }

    @RequestMapping("/deleteOneDisease/{id}")
    public String deleteOneDisease(@PathVariable("id")String id){
        knowledgeService.deleteOneDisease(Long.parseLong(id));
        return "success";
    }

    @RequestMapping("/allDisease")
    public List<Disease> selectAllDisease(@Valid @RequestBody PageVo pageVo){
        return knowledgeService.selectAllDisease(pageVo);
    }

    @RequestMapping("/addSymptom")
    public Symptom addSymptom(@Valid @RequestBody Symptom symptom) {
        return knowledgeService.addSymptom(symptom);
    }

    @RequestMapping("/findOneSymptom/{id}")
    public Symptom findOneSymptom(@PathVariable("id")String id){
        return knowledgeService.findOneSymptom(Long.parseLong(id));
    }

    @RequestMapping("/findSymptom/{name}")
    public List<Symptom> findSymptom(@PathVariable("name")String name){
        return knowledgeService.findSymptoms(name);
    }

    @RequestMapping("/deleteOneSymptom/{id}")
    public String deleteOneSymptom(@PathVariable("id")String id){
        knowledgeService.deleteOneSymptom(Long.parseLong(id));
        return "success";
    }

    @RequestMapping("/allSymptom")
    public List<Symptom> selectAllSymptom(@Valid @RequestBody PageVo pageVo) {
        return knowledgeService.selectAllSymptom(pageVo);
    }

    @RequestMapping("addLIS")
    public LIS addLIS(@Valid @RequestBody LIS lis) {
        return knowledgeService.addLIS(lis);
    }

    @RequestMapping("/findOneLIS/{id}")
    public LIS findOneLIS(@PathVariable("id")String id){
        return knowledgeService.findOneLIS(Long.parseLong(id));
    }

    @RequestMapping("/findLIS/{name}")
    public List<LIS> findLIS(@PathVariable("name")String name){
        return knowledgeService.findLIS(name);
    }

    @RequestMapping("/deleteOneLIS/{id}")
    public String deleteOneLIS(@PathVariable("id")String id){
        knowledgeService.deleteOneLIS(Long.parseLong(id));
        return "success";
    }


    @RequestMapping("/allLIS")
    public List<LIS> selectAllLIS(@Valid @RequestBody PageVo pageVo) {
        return knowledgeService.selectAllLIS(pageVo);
    }


    @RequestMapping("/getRelationByNodes")
    public List<Map<String, Object>> getRelationByNodes(@Valid @RequestBody RelationVo relationVo){
        return knowledgeService.getRelationByNodes(relationVo);
    }

    @RequestMapping("/getRelationByEndNode")
    public List<Map<String, Object>> getRelationByEndNode(@Valid @RequestBody RelationVo relationVo) {
        return knowledgeService.getOneLevelRelationByEndNode(relationVo);
    }

    @RequestMapping("/getNodeByRelation")
    public Iterable<Map<String, Object>> getNodeByRelation(@Valid @RequestBody RelationVo relationVo) {
        return knowledgeService.getNodeByRelation(relationVo);
    }

    @RequestMapping("/updateNodeByName")
    public BaseNode updateNodeByName(@Valid @RequestBody AmendRelationVo amendRelationVo) {
        return knowledgeService.updateNodeByName(amendRelationVo);
    }

    @RequestMapping("/updateRelation")
    public List<Map<String, Object>> updateRelation(@Valid @RequestBody AmendRelationVo amendRelationVo) {
        return knowledgeService.updateRelation(amendRelationVo);
    }

    @RequestMapping("/getAllNode")
    public List<Map<String, Object>> getAllNode() {
        return knowledgeService.getAllNode();
    }

    /**
     * 获取高危疾病列表
     */
    @RequestMapping("/getHighRisk")
    public Response<GdbResponse> getHighRisk(@Valid @RequestBody SearchData searchData){
        Response<GdbResponse> resp = new Response<>();
        GdbResponse gdbResponse = new GdbResponse();
        gdbResponse.setResult(knowledgeService.getHighRiskDisease(searchData));
        resp.setData(gdbResponse);
        return resp;
    }

    /**
     * 推送LIS和PACS
     */
    @RequestMapping("/getLisPacs")
    public Response<ResponseData> getLisPacs(@Valid @RequestBody SearchData searchData){
        Response<ResponseData> kk = new Response<>();
        ResponseData responseData = new ResponseData();
        Map<String, List<FeatureRate>> lisPacs = knowledgeService.getLisPacs(searchData);
        List<FeatureRate> lisList = lisPacs.get("lisList");
        List<FeatureRate> pacsList = lisPacs.get("pacsList");
        List<FeatureRate> vitalResultList = lisPacs.get("vitalResultList");
        responseData.setLabs(lisList);
        responseData.setPacs(pacsList);
        responseData.setVitals(vitalResultList);
        kk.setData(responseData);
        return kk;
    }
    /**
     * 推送诊断(疑诊),治疗方案,不良反应,指标和量表
     */
    @RequestMapping("/getDisease")
    public ResponseData getDis(@Valid @RequestBody SearchData searchData){
        return knowledgeService.getDis(searchData);
    }
    /**
     * 给定疾病，获取推荐的化验或辅检项目
     */
    @RequestMapping("/getRecommendItem")
    public List<Map<String, Object>> getRecommendItem(@Valid @RequestBody QueryVo queryVo) {
        return knowledgeService.getRecommendItem(queryVo);
    }

    /**
     * 给定量表名称，推荐量表内容
     */
    @RequestMapping("/getScale")
    public Map<String, List<Map<String, Object>>> getScale(@Valid @RequestBody QueryVo queryVo) {
        return knowledgeService.getScale(queryVo);
    }

    /**
     * 给定诊断依据的id,删除图谱中对应的诊断依据
     * @param disease
     * @return
     */
    @RequestMapping("/deleteDisease")
    public RespDTO deleteDiseaseData(@Valid @RequestBody NeoParamVO disease){
        RespDTO respDTO = knowledgeService.deleteNeoDisease(disease);
        return respDTO;
    }

    /**
     * 给定诊断依据的id,更新图谱中对应的诊断依据
     * @param disease
     * @return
     */
    @RequestMapping("/updateDisease")
    public RespDTO updateDiseaseData(@Valid @RequestBody NeoParamVO disease){
        RespDTO respDTO = knowledgeService.updateNeoDisease(disease);
        return respDTO;
    }
    @RequestMapping("/init")
    public RespDTO batchInsert( ){
        RespDTO respDTO = knowledgeService.batchInsertDis();
        return respDTO;
    }

    /**
     * 图谱的bi指标统计
     * @return
     */
    @RequestMapping("/neoStatistics")
    public List<BiDetail> biCount(){
        List<BiDetail> biCount = knowledgeService.neoBi();
        return biCount;
    }
}
