package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.ConceptDTO;
import com.diagbot.entity.LibraryInfo;
import com.diagbot.entity.Medical;
import com.diagbot.service.impl.LibraryInfoServiceImpl;
import com.diagbot.vo.AmendTermVo;
import com.diagbot.vo.TermVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 LibraryInfoFacade extends LibraryInfoServiceImpl {

    private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");


    /**
     * 获取医学术语基本信息
     *
     * @param params
     * @result LibraryInfo
     */
    public LibraryInfo getLibraryInfor(Map<String, Object> params) {
        QueryWrapper<LibraryInfo> wrapper = new QueryWrapper();
        LibraryInfo libraryinfor = null;

        for (String key:params.keySet()) {
            wrapper.eq(key, params.get(key));
        }

        libraryinfor = this.getOne(wrapper);

        return libraryinfor;
    }


    /**
     * 更新或添加医学术语基本信息
     *
     * @param amendTermVo
     * @return LibraryInfo
     */
    public LibraryInfo upsertLibraryInfo(AmendTermVo amendTermVo) {

        String oldterm = amendTermVo.getOldterm().trim();
        String oldtype = amendTermVo.getOldtype().trim();
        String newterm = amendTermVo.getNewterm().trim();
        String newtype = amendTermVo.getNewtype().trim();
        String concept = amendTermVo.getConcept().trim();
        int concept_id = amendTermVo.getConcept_id();

        QueryWrapper<LibraryInfo> queryWrapper;
        LibraryInfo libraryInfo = null;

        // 原先的术语名称不为空，则修改原先的术语
        if (oldterm.length() > 0 && oldtype.length() > 0) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", oldterm);
            queryWrapper.eq("type", oldtype);

            libraryInfo = getOne(queryWrapper);

            if (libraryInfo != null) {
                libraryInfo.setName(newterm);
                libraryInfo.setType(newtype);

                if (libraryInfo.getIsDeleted() == "Y") {
                    libraryInfo.setIsDeleted("N");
                }

                saveOrUpdate(libraryInfo);
            }
        }
        else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", newterm);
            queryWrapper.eq("type", newtype);

            libraryInfo = getOne(queryWrapper);

            // 新的术语不存在
            if (libraryInfo == null) {
                libraryInfo = new LibraryInfo();
                libraryInfo.setName(newterm);
                libraryInfo.setType(newtype);

                saveOrUpdate(libraryInfo);
            }
            // 新的术语存在，但处于删除状态
            else if (libraryInfo.getIsDeleted() == "Y") {
                libraryInfo.setIsDeleted("N");
                saveOrUpdate(libraryInfo);
            }

            libraryInfo = getOne(queryWrapper);
        }

        return libraryInfo;
    }


}
