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.LibraryDetailDTO;
import com.diagbot.entity.Concept;
import com.diagbot.entity.LibraryDetail;
import com.diagbot.entity.LibraryInfo;
import com.diagbot.service.impl.LibraryDetailServiceImpl;
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 java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 术语基本信息查询业务层
 * @author: Weixuan Huang
 * @time: 2019/1/16 14:17
 */
@Component
public class LibraryDetailFacade extends LibraryDetailServiceImpl {

    @Autowired
    private ConceptFacade conceptFacade;

    @Autowired
    private LibraryInfoFacade libraryInfoFacade;


    /**
     * 获取所有术语静态知识列表
     *
     * @param conceptVo
     * @return IPage<LibraryDetailDTO>
     */
    public IPage<LibraryDetailDTO> getAllLibraryDetail(ConceptVo conceptVo) {

        return getAllKnowledge(conceptVo);
    }




    /**
     * 获取医学术语静态信息
     *
     * @param termvo
     * @return ConceptDTO
     */
    public ConceptDTO getConceptKnowledge(@RequestBody TermVo termvo) {

        List<LibraryInfo> libraryInfos;
        LibraryInfo libraryInfo;
        List<LibraryDetail> libraryDetails;

        ConceptDTO conceptDTO = conceptFacade.getConcept(termvo);

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

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

            // 获取与术语相关的静态知识
            QueryWrapper<LibraryDetail> querydetail = new QueryWrapper<>();
            querydetail.eq("concept_id", conceptDTO.getId());
            querydetail.eq("is_deleted", "N");

            libraryDetails = list(querydetail);

            if (libraryDetails != null) {
                conceptDTO.setInformation(libraryDetails);
            }

        }

        return conceptDTO;
    }


    /**
     * 更新或添加医学术语静态信息
     *
     * @param amendTermVo
     * @return ConceptDTO
     */
    public ConceptDTO upsertConceptKnowledge(AmendTermVo amendTermVo) {

        ConceptDTO conceptDTO = new ConceptDTO();

        LibraryDetail libraryDetail;
        List<LibraryDetail> libraryDetails;

        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.getInformation() != null && libraryInfo != null) {

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

            for (int i=0; i<amendTermVo.getInformation().size(); i++) {
                libraryDetail = amendTermVo.getInformation().get(i);

                QueryWrapper<LibraryDetail> detquery = new QueryWrapper<>();
                detquery.eq("concept_id", libraryInfo.getConceptId());
                libraryDetails = list(detquery);

                if (libraryDetails.size() > 0) {
                    LibraryDetail exist_medical = libraryDetails.get(0);
                    libraryDetail.setId(exist_medical.getId());
                }
                else {
                    libraryDetail.setConceptId(libraryInfo.getConceptId().longValue());
                }

                this.saveOrUpdate(libraryDetail);

                conceptDTO.getInformation().add(libraryDetail);
            }
        }
        else {
            conceptDTO.setInformation(null);
        }

        return conceptDTO;
    }

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

        Boolean isdeleted = false;
        List<LibraryDetail> libraryDetails;

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

        if (libraryInfo != null) {
            QueryWrapper<LibraryDetail> detquery = new QueryWrapper<>();
            detquery.eq("concept_id", libraryInfo.getConceptId());
            libraryDetails = list(detquery);

            if (libraryDetails != null && libraryDetails.size() > 0) {
                for (LibraryDetail libraryDetail:libraryDetails) {
                    libraryDetail.setIsDeleted('Y');
                    saveOrUpdate(libraryDetail);
                }
                isdeleted = true;
            }
        }

        return isdeleted;
    }

    /**
     * 清空医学术语知识表
     *
     */
    public void deleteDetail() {
        truncateDetail();
    }
}
