package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.api.ImportExcel;
import com.diagbot.dto.ConceptDTO;
import com.diagbot.dto.LibraryInfoDTO;
import com.diagbot.entity.*;
import com.diagbot.service.impl.ConceptServiceImpl;
import com.diagbot.vo.AmendTermVo;
import com.diagbot.vo.ConceptVo;
import com.diagbot.vo.TermVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;


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


/**
 * @Description: 术语查询业务层
 * @author: Weixuan Huang
 * @time: 2019/1/14 16:17
 */
@Component
public class ConceptFacade extends ConceptServiceImpl {

    @Autowired
    private LibraryInfoFacade libraryinfoFacade;
    @Autowired
    private LibraryDetailFacade libraryDetailFacade;
    @Autowired
    private MedicalFacade medicalFacade;
    @Autowired
    private RelationFacade relationFacade;


    /**
     * 获取医学术语基本信息
     *
     * @param termvo
     * @return List<ConceptDTO>
     */
    public List<ConceptDTO> getConceptInfo(@RequestBody TermVo termvo) {

        List<ConceptDTO> conceptDTOs = new ArrayList<>();
        ConceptDTO conceptDTO;
        List<LibraryInfo> libraryinfos;

        // 获取医学术语的基本信息
        QueryWrapper<LibraryInfo> query = new QueryWrapper<>();
        query.like("name", termvo.getTerm());
        query.eq("is_concept", 1);
        List<LibraryInfo> libraryInfos = libraryinfoFacade.list(query);

        for (LibraryInfo libraryInfo:libraryInfos) {
            libraryinfos = new ArrayList<>();

            conceptDTO = new ConceptDTO();
            conceptDTO.setId(libraryInfo.getConceptId());
            conceptDTO.setLibId(libraryInfo.getId().intValue());
            conceptDTO.setName(libraryInfo.getName());

            libraryinfos.add(libraryInfo);
            conceptDTO.setLibraryInfo(libraryinfos);

            conceptDTOs.add(conceptDTO);
        }

        return conceptDTOs;
    }


    /**
     * 获取标准术语名称列表
     *
     * @param conceptVo
     * @return IPage<LibraryDTO>
     */
    public IPage<LibraryInfoDTO> getAllConcepts(ConceptVo conceptVo) {

        return libraryinfoFacade.getAllConcept(conceptVo);
    }

    /**
     * 获取标准术语信息列表
     *
     * @param conceptVo
     * @return IPage<LibraryDTO>
     */
    public IPage<LibraryInfoDTO> getAllInformation(ConceptVo conceptVo) {

        return libraryinfoFacade.getAllInfor(conceptVo);
    }


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

        TermVo termVo = new TermVo();
        termVo.setTerm(amendTermVo.getNewterm());
        termVo.setType(amendTermVo.getNewtype());

        LibraryInfo libraryInfo = libraryinfoFacade.upsertLibraryInfo(amendTermVo);

        QueryWrapper<Concept> query = new QueryWrapper<>();
        query.eq("lib_id", libraryInfo.getId());
        Concept concept = getOne(query);

        if (concept == null) {
            concept = new Concept();
            concept.setLibId(libraryInfo.getId().intValue());
            saveOrUpdate(concept);

            concept = getOne(query);
        }

        libraryInfo.setConceptId(concept.getId());
        libraryInfo.setIsConcept(1);
        libraryinfoFacade.saveOrUpdate(libraryInfo);

        if (amendTermVo.getLibraryInfo() != null && amendTermVo.getLibraryInfo().size() > 0) {
            List<LibraryInfo> libraryInfos = amendTermVo.getLibraryInfo();
            for (LibraryInfo libraryinfo:libraryInfos) {
                libraryinfo.setConceptId(concept.getId());
                libraryinfo.setIsConcept(0);
                libraryinfo.setIsDeleted("N");
                libraryinfoFacade.saveOrUpdate(libraryinfo);
            }
        }


        query = new QueryWrapper<>();
        query.eq("name", amendTermVo.getNewterm());
        query.eq("type", amendTermVo.getNewtype());
        concept = getOne(query);

        BeanUtils.copyProperties(concept, conceptDTO);

        QueryWrapper<LibraryInfo> conceptquery = new QueryWrapper<>();
        conceptquery = new QueryWrapper<>();
        conceptquery.eq("concept_id", concept.getId());
        conceptquery.isNull("is_concept");
        List<LibraryInfo> libraryInfos = libraryinfoFacade.list(conceptquery);

        conceptDTO.setLibraryInfo(libraryInfos);

        return conceptDTO;
    }

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

        ConceptDTO conceptDTO = getConcept(termVo);

        if (conceptDTO == null) {
            conceptDTO = new ConceptDTO();
            String msg = "概念\"" + termVo.getTerm() + "\"不存在!";
            conceptDTO.setMessage(new ArrayList<String>());
            conceptDTO.getMessage().add(msg);

            return conceptDTO;
        }
        else {
            conceptDTO = libraryDetailFacade.getConceptKnowledge(termVo);
            if (conceptDTO.getInformation() != null && conceptDTO.getInformation().size() > 0) {
                String msg = "概念\"" + termVo.getTerm() + "\"的静态知识还存在!";
                conceptDTO.setMessage(new ArrayList<String>());
                conceptDTO.getMessage().add(msg);

                return conceptDTO;
            }

        }

        return conceptDTO;
    }




    /**
     * 获取概念Id
     *
     * @param term
     * @return Integer
     */
    public int getConceptId(String term, String type) {
        int id = 0;
        TermVo termVo = new TermVo();

        try {
            termVo.setTerm(term);
            termVo.setType(type);
            ConceptDTO conceptDTO = getConcept(termVo);
            if (conceptDTO != null) {
                id = conceptDTO.getId();
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            return id;
        }
    }

    /**
     * 从文件批量导入术语信息
     *
     * @param file
     * @return ConceptDTO
     */
    public ConceptDTO importBatch(MultipartFile file) {
        ImportExcel.setConceptFacade(this);
        ImportExcel.setLibraryinfoFacade(libraryinfoFacade);
        ImportExcel.setLibraryDetailFacade(libraryDetailFacade);
        ImportExcel.setMedicalFacade(medicalFacade);
        ImportExcel.setRelationFacade(relationFacade);

        ConceptDTO conceptDTO = ImportExcel.importBatch(file);

        return conceptDTO;
    }


}
