package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.dto.ConceptDTO;
import com.diagbot.dto.MedicalDTO;
import com.diagbot.dto.TypeDTO;
import com.diagbot.entity.Concept;
import com.diagbot.entity.LibraryInfo;
import com.diagbot.entity.Medical;
import com.diagbot.service.impl.MedicalServiceImpl;
import com.diagbot.vo.AmendTermVo;
import com.diagbot.vo.ConceptVo;
import com.diagbot.vo.TermVo;
import com.diagbot.vo.TypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 术语临床信息查询业务层
 * @author: Weixuan Huang
 * @time: 2019/1/16 16:07
 */
@Component
public class MedicalFacade extends MedicalServiceImpl {

    @Autowired
    ConceptFacade conceptFacade;
    @Autowired
    LibraryInfoFacade libraryInfoFacade;


    /**
     * 获取术语医学知识
     *
     * @param termVo
     * @result ConceptDTO
     */
    public ConceptDTO getMedicalInfo(@RequestBody TermVo termVo) {
        List<LibraryInfo> libraryInfos;
        LibraryInfo libraryInfo;
        List<Medical> medicals;

        ConceptDTO conceptDTO = conceptFacade.getConcept(termVo);

        if (conceptDTO != null) {
            // 获取与医学术语的基本信息
            QueryWrapper<LibraryInfo> query = new QueryWrapper<>();
            query.eq("concept_id", conceptDTO.getId());
            query.eq("is_concept", 1);
            libraryInfos = libraryInfoFacade.list(query);

            if (libraryInfos != null) {
                libraryInfo = libraryInfos.get(0);
                conceptDTO.setName(libraryInfo.getName());

                // 获取与术语相关的医学知识
                QueryWrapper<Medical> querymed = new QueryWrapper<>();
                querymed.eq("concept_id", conceptDTO.getId());

                medicals = list(querymed);

                if (medicals != null) {
                    conceptDTO.setMedicalInfo(medicals);
                }
            }
        }

        return conceptDTO;
    }


    /**
     * 更新或添加术语医学信息
     *
     * @param amendTermVo
     * @return ConceptDTO
     */
    public ConceptDTO upsertMedicalInfo(AmendTermVo amendTermVo) {
        ConceptDTO conceptDTO = new ConceptDTO();

//        Map<String, Object> map;
        Medical medical;
        List<Medical> medicals;

        conceptDTO.setName(amendTermVo.getNewterm());
        QueryWrapper<LibraryInfo> query = new QueryWrapper<>();
        query.eq("name", amendTermVo.getNewterm());
        query.eq("type", amendTermVo.getNewtype());
        LibraryInfo libraryInfo = libraryInfoFacade.getOne(query);

        if (amendTermVo.getMedicalInfo() != null && libraryInfo != null) {

            conceptDTO.setLibId(libraryInfo.getId().intValue());
            conceptDTO.setId(libraryInfo.getConceptId());
            conceptDTO.setMedicalInfo(new ArrayList<>());

            for (int i=0; i<amendTermVo.getMedicalInfo().size(); i++) {
                medical = amendTermVo.getMedicalInfo().get(i);
//                medical = amendTermVo.getMedicalInfo();

//                map = new HashMap<>();
//                map.put("name", amendTermVo.getNewterm());
//                medicals = getMedicalInfo(map);

                QueryWrapper<Medical> medquery = new QueryWrapper<>();
                medquery.eq("name", amendTermVo.getNewterm());
                medicals = list(medquery);

                if (medicals.size() > 0) {
                    Medical exist_medical = medicals.get(0);
                    medical.setId(exist_medical.getId());
                }
                else {
                    medical.setConceptId(libraryInfo.getConceptId());
                }


                this.saveOrUpdate(medical);

//                amendTermVo.setMedicalInfo(medical);
//                amendTermVo.getMedicalInfo().set(i, medical);
                conceptDTO.getMedicalInfo().add(medical);
            }
        }

        return conceptDTO;
    }


    /**
     * 删除术语医学信息
     *
     * @param termVo
     * @return ConceptDTO
     */
    public Boolean removeMedicalInfo(TermVo termVo) {

        Boolean isdeleted = false;
        List<Medical> medicals;

        QueryWrapper<LibraryInfo> query = new QueryWrapper<>();
        query.eq("name", termVo.getTerm());
        query.eq("type", termVo.getType());
        LibraryInfo libraryInfo = libraryInfoFacade.getOne(query);

        if (libraryInfo != null) {
            QueryWrapper<Medical> medquery = new QueryWrapper<>();
            medquery.eq("name", termVo.getTerm());
            medquery.eq("concept_id", libraryInfo.getConceptId());
            medicals = list(medquery);

            if (medicals != null && medicals.size() > 0) {
                for (Medical medical:medicals) {
                    medical.setIs_deleted("Y");
                    saveOrUpdate(medical);
                }
                isdeleted = true;
            }
        }

        return isdeleted;
    }


    /**
     * 获取术语类型信息
     *
     * @param type
     * @return TypeDTO
     */
    public TypeDTO getTypeInfo(String type) {
        TypeDTO typeDTO = null;
        TypeVo typeVo = new TypeVo();

        try {
            typeVo.setType(type);
            List<TypeDTO> typelist = getTypeId(typeVo);

            if (typelist.size() > 0) {
                typeDTO = getTypeId(typeVo).get(0);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            return typeDTO;
        }
    }

    /**
     * 清空医学术语信息表
     *
     */
    public void deleteMedical() {
        truncateMedical();
    }
}
