package com.lantone.facade.med;

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.google.common.collect.Lists;
import com.lantone.dto.med.IndexDTO;
import com.lantone.dto.med.LisDetailDTO;
import com.lantone.dto.med.RetrievalConceptDTO;
import com.lantone.dto.med.RetrievalDTO;
import com.lantone.enums.med.LexiconEnum;
import com.lantone.vo.med.MedRetrievalVO;
import com.lantone.vo.med.RetrievalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2021/2/22 10:35
 */
@Component
public class MedRetrievalFacade {

    @Autowired
    private KlConceptFacade klConceptFacade;

    /**
     * 检索
     *
     * @param retrievalVO
     * @return
     */
    public RetrievalDTO index(RetrievalVO retrievalVO) {
        if (retrievalVO.getSize() == null) {
            retrievalVO.setSize(100);
        }
        if (retrievalVO.getDefaultList().equals(0)
                && StringUtil.isBlank(retrievalVO.getInputStr())) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "请输入检索内容");
        }
        RetrievalDTO retrievalDTO = new RetrievalDTO();
        MedRetrievalVO medRetrievalVO = new MedRetrievalVO();
        BeanUtil.copyProperties(retrievalVO, medRetrievalVO);
        medRetrievalVO.setTypeIds(new ArrayList<>());
        List<IndexDTO> indexList = Lists.newLinkedList();

        /**
         * 类型：1-化验大项、2-化验小项、3-辅检、4-诊断、5-药品、6-手术和操作、7-科室、8-输血、9-症状、10-量表、11-护理、12-中医诊断、13-中医证候、14-麻醉、15-药品剂型、16-给药途径、17-物理治疗
         */

        switch (retrievalVO.getType()) {
            case 1:
                medRetrievalVO.setTypeId(LexiconEnum.LisName.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.LisName.getKey());
                List<RetrievalConceptDTO> nameList = new ArrayList<>();
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    nameList = BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class);
                    retrievalDTO.setNameList(nameList);
                }
                break;
            case 2:
                medRetrievalVO.setTypeId(LexiconEnum.LisSubName.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.LisSubName.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    List<LisDetailDTO> lisDetails = Lists.newLinkedList();
                    indexList.forEach(item -> {
                        LisDetailDTO lisDetail = new LisDetailDTO();
                        BeanUtil.copyProperties(item, lisDetail);
                        lisDetail.setName(item.getPackName());
                        lisDetail.setUniqueName(item.getName());
                        //默认套餐名称
                        if (StringUtil.isBlank(lisDetail.getName())) {
                            lisDetail.setName(lisDetail.getUniqueName());
                        }
                        lisDetails.add(lisDetail);
                    });
                    retrievalDTO.setLisDetailNames(lisDetails);
                }
                break;
            case 3:
                medRetrievalVO.setTypeId(LexiconEnum.PacsName.getKey());
                medRetrievalVO.getTypeIds().addAll(Arrays.asList(LexiconEnum.PacsName.getKey(),
                        LexiconEnum.PacsSubName.getKey()));
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    List<RetrievalConceptDTO> pacsNames = new ArrayList<>();
                    pacsNames = BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class);
                    retrievalDTO.setNameList(pacsNames);
                }
                break;
            case 4:
                medRetrievalVO.setTypeId(LexiconEnum.Disease.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Disease.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 5:
                medRetrievalVO.setTypeId(LexiconEnum.Medicine.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Medicine.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 6:
                medRetrievalVO.setTypeId(LexiconEnum.Operation.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Operation.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 7:
                medRetrievalVO.setTypeId(LexiconEnum.Dept.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Dept.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    List<RetrievalConceptDTO> deptNames = new ArrayList<>();
                    deptNames = BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class);
                    retrievalDTO.setNameList(deptNames);
                }
                break;
            case 8:
                medRetrievalVO.setTypeId(LexiconEnum.Transfusion.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Transfusion.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    List<RetrievalConceptDTO> transfusionNames = new ArrayList<>();
                    transfusionNames = BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class);
                    retrievalDTO.setNameList(transfusionNames);
                }
                break;
            case 9:
                medRetrievalVO.setTypeId(LexiconEnum.Symptom.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Symptom.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    List<RetrievalConceptDTO> symptomNames = new ArrayList<>();
                    symptomNames = BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class);
                    retrievalDTO.setNameList(symptomNames);
                }
                break;
            case 10:
                medRetrievalVO.setTypeId(LexiconEnum.Scale.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Scale.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 11:
                medRetrievalVO.setTypeId(LexiconEnum.Nurse.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Nurse.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 12:
                medRetrievalVO.setTypeId(LexiconEnum.Tcmdisease.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Tcmdisease.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 13:
                medRetrievalVO.setTypeId(LexiconEnum.Tcmsyndrome.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Tcmsyndrome.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 14:
                medRetrievalVO.setTypeId(LexiconEnum.Anesthesia.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Anesthesia.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 15:
                medRetrievalVO.setTypeId(LexiconEnum.Form.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Form.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 16:
                medRetrievalVO.setTypeId(LexiconEnum.AdministrationRoute.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.AdministrationRoute.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
            case 17:
                medRetrievalVO.setTypeId(LexiconEnum.Physicotherapy.getKey());
                medRetrievalVO.getTypeIds().add(LexiconEnum.Physicotherapy.getKey());
                indexList = klConceptFacade.getBaseMapper().index(medRetrievalVO);
                if (ListUtil.isNotEmpty(indexList)) {
                    retrievalDTO.setNameList(BeanUtil.listCopyTo(indexList, RetrievalConceptDTO.class));
                }
                break;
        }
        return retrievalDTO;
    }

}