package com.lantone.facade.med;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.StringUtil;
import com.lantone.client.StandConvertServiceClient;
import com.lantone.dto.med.StandConvertCrfDTO;
import com.lantone.dto.med.TermConceptDTO;
import com.lantone.entity.med.KlLibraryInfo;
import com.lantone.enums.med.LexiconEnum;
import com.lantone.enums.med.StandConvertCrfEnum;
import com.lantone.vo.med.StandConvertCrfVO;
import com.lantone.vo.med.TermMatchingListVO;
import com.lantone.vo.med.TermMatchingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author wangfeng
 * @Description:
 * @date 2021-06-10 19:48
 */
@Component
public class TermMatchingFacade {

    //    @Autowired
    //    private KlConceptFacade klConceptFacade;
    @Autowired
    private KlLibraryInfoFacade klLibraryInfoFacade;
    @Autowired
    private StandConvertServiceClient standConvertServiceClient;
/*
    public List<TermConceptDTO> getTermMatchingAll(TermMatchingVO termMatchingVO) {
        List<TermConceptDTO> ret = Lists.newArrayList();
        QueryWrapper<KlConcept> klConceptQueryWrapper = new QueryWrapper<>();
        klConceptQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        klConceptQueryWrapper.eq(termMatchingVO.getType() != null, "lib_type", termMatchingVO.getType());
        klConceptQueryWrapper.like(StringUtil.isNotBlank(termMatchingVO.getInputStr()), "lib_name", termMatchingVO.getInputStr());
        List<KlConcept> klConceptList = klConceptFacade.list(klConceptQueryWrapper);
        klConceptList.forEach(i -> {
            TermConceptDTO termConceptDTO = new TermConceptDTO();
            termConceptDTO.setId(i.getLibId());
            termConceptDTO.setName(i.getLibName());
            //            termConceptDTO.setSource(i.getIsConcept() == 1 ? 1 : 2);
            ret.add(termConceptDTO);
        });
        return ret;
    }*/

    public List<TermConceptDTO> getTermMatchingAll(TermMatchingVO termMatchingVO) {
        List<TermConceptDTO> list = new ArrayList<>();
        Integer type = convertType(termMatchingVO.getType());
        TermMatchingVO termMatchingVONew = new TermMatchingVO();
        BeanUtil.copyProperties(termMatchingVO, termMatchingVONew);
        termMatchingVONew.setType(type);
        if (type != null) {
            TermConceptDTO libraryInfo = klLibraryInfoFacade.getBaseMapper().getConcept(termMatchingVONew);
            if (null == libraryInfo && type.equals(LexiconEnum.PacsName.getKey())) {
                termMatchingVONew.setType(LexiconEnum.PacsSubName.getKey());
                libraryInfo = klLibraryInfoFacade.getBaseMapper().getConcept(termMatchingVONew);
            }
            if (null != libraryInfo) {
                TermConceptDTO termConceptDTO = new TermConceptDTO();
                termConceptDTO.setId(libraryInfo.getId());
                termConceptDTO.setName(libraryInfo.getName());
                termConceptDTO.setSource(libraryInfo.getSource() == 1 ? 1 : 2);
                termConceptDTO.setCode(libraryInfo.getCode());
                list.add(termConceptDTO);
            }
        }
        //没有找到标准词或同义词才找相似词
        if (ListUtil.isEmpty(list)) {
            //相似词
            StandConvertCrfVO standConvertCrfVO = new StandConvertCrfVO();
            // 类型，疾病: disease，症状: symptom，手术和操作:operation，药品: drug，实验室检查：lis，辅助检查：pacs， 辅助检查：vital
            String wordType = wordType(termMatchingVO.getType());
            if (StringUtil.isNotBlank(wordType)) {
                standConvertCrfVO.setWord_type(wordType);
                standConvertCrfVO.setWord(termMatchingVO.getInputStr());
                standConvertCrfVO.setNumber(5);
                StandConvertCrfDTO standConvertCrfDTO = new StandConvertCrfDTO();
                try {
                    standConvertCrfDTO = standConvertServiceClient.similarity(standConvertCrfVO);
                } catch (Exception e) {
                    throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "标准词转换【服务器】挂了!" + e.getMessage());
                }

                List<Map<String, String>> standard_words = standConvertCrfDTO.getStandard_words();
                List<String> placeToList = new ArrayList<>();
                if (ListUtil.isNotEmpty(standard_words)) {
                    for (Map<String, String> map : standard_words) {
                        String placeTo = map.get("standard_word");
                        placeToList.add(placeTo);
                    }
                    TermMatchingListVO termMatchingListVO = new TermMatchingListVO();
                    termMatchingListVO.setPlaceToList(placeToList);
                    termMatchingListVO.setType(type);
                    QueryWrapper<KlLibraryInfo> klLibraryInfo = new QueryWrapper<>();
                    klLibraryInfo.eq("is_deleted", IsDeleteEnum.N.getKey())
                            .eq("type_id", type)
                            .in("name", placeToList);
                    List<TermConceptDTO> libraryInfoList = klLibraryInfoFacade.getBaseMapper().getConceptList(termMatchingListVO);
                    if (ListUtil.isNotEmpty(libraryInfoList)) {
                        for (TermConceptDTO data : libraryInfoList) {
                            TermConceptDTO termConceptDTO = new TermConceptDTO();
                            termConceptDTO.setId(data.getId());
                            termConceptDTO.setName(data.getName());
                            termConceptDTO.setCode(data.getCode());
                            termConceptDTO.setSource(5);
                            list.add(termConceptDTO);
                        }
                    }
                }
            }
        }
        return list;
    }

    public Integer convertType(Integer type) {
        Integer retType = null;
        //type： 类型：1-化验大项、2-化验小项、3-辅检、4-诊断、5-药品、6-手术和操作、7-科室、8-输血、10-量表、11-护理、12-中医诊断、13-中医证候、14-麻醉、15-药品剂型、16-给药途径
        switch (type) {
            case 1:
                retType = LexiconEnum.LisName.getKey();
                break;
            case 2:
                retType = LexiconEnum.LisSubName.getKey();
                break;
            case 3:
                retType = LexiconEnum.PacsName.getKey();
                break;
            case 4:
                retType = LexiconEnum.Disease.getKey();
                break;
            case 5:
                retType = LexiconEnum.Medicine.getKey();
                break;
            case 6:
                retType = LexiconEnum.Operation.getKey();
                break;
            case 7:
                retType = LexiconEnum.Dept.getKey();
                break;
            case 8:
                retType = LexiconEnum.Transfusion.getKey();
                break;
            case 9:
                retType = LexiconEnum.Symptom.getKey();
                break;
            case 10:
                retType = LexiconEnum.Scale.getKey();
                break;
            case 11:
                retType = LexiconEnum.Nurse.getKey();
                break;
            case 12:
                retType = LexiconEnum.Tcmdisease.getKey();
                break;
            case 13:
                retType = LexiconEnum.Tcmsyndrome.getKey();
                break;
            case 14:
                retType = LexiconEnum.Anesthesia.getKey();
                break;
            case 15:
                retType = LexiconEnum.Form.getKey();
                break;
            case 16:
                retType = LexiconEnum.AdministrationRoute.getKey();
            default:
                break;
        }
        return retType;
    }

    public String wordType(Integer type) {
        String retType = null;
        //type： 类型：1-化验大项、2-化验小项、3-辅检、4-诊断、5-药品、6-手术和操作、7-科室、8-输血、10-量表、11-护理、12-中医诊断、13-中医证候
        //类型，疾病: disease，症状: symptom，手术和操作:operation，药品: drug，实验室检查：lis，辅助检查：pacs， 查体：vital
        switch (type) {
            case 1:
                retType = StandConvertCrfEnum.lis.getName();
                break;
            case 2:
                retType = StandConvertCrfEnum.lis.getName();
                break;
            case 3:
                retType = StandConvertCrfEnum.pacs.getName();
                break;
            case 4:
                retType = StandConvertCrfEnum.disease.getName();
                break;
            case 5:
                retType = StandConvertCrfEnum.drug.getName();
                break;
            case 6:
                retType = StandConvertCrfEnum.operation.getName();
                break;
            default:
                break;
        }
        return retType;
    }

}
