package com.diagbot.facade;

import com.diagbot.dto.SymptomSearchDTO;
import com.diagbot.entity.Symptom;
import com.diagbot.entity.SymptomWrapper;
import com.diagbot.enums.ShowTypeEnum;
import com.diagbot.service.impl.SymptomServiceImpl;
import com.diagbot.vo.SymptomSearchVO;
import com.diagbot.vo.SymptomUsualVO;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 症状
 * @Author: ztg
 * @Date: 2018/10/23 16:33
 */
@Component
public class SymptomFacade extends SymptomServiceImpl{
    /**
     *  获取常用症状
     *
     * @return
     */
    public List<Symptom> getUsualFac(SymptomUsualVO symptomUsualVO) {
        Map paramMap = new HashMap();
        paramMap.put("sexType", symptomUsualVO.getSexType());
        paramMap.put("age", symptomUsualVO.getAge());
        return this.getUsual(paramMap);
    }


    /**
     * 根据部位partIds获取症状信息
     *
     * @param map 参数
     * @return
     */
    public List<SymptomWrapper> getByPartIdsFac(Map map) {
        return this.getByPartIds(map);
    }


    /**
     * 症状检索
     *
     * @param symptomSearchVO
     * @return 检索的症状
     */
    public List<SymptomSearchDTO> searchFac(SymptomSearchVO symptomSearchVO) {
        List<SymptomSearchDTO> res = new ArrayList<>();
        Map paramMap = new HashMap();
        paramMap.put("name", symptomSearchVO.getName());
        paramMap.put("sexType", symptomSearchVO.getSexType());
        paramMap.put("age", symptomSearchVO.getAge());
        List<SymptomSearchDTO> symptomSearchDTOList = this.search(paramMap);
        List<Long> ids = new ArrayList<>();
        //获取所有的本体，添加返回结果，因为sql语句返回时同义词有可能排前面，而本体排后面
        for(SymptomSearchDTO bean : symptomSearchDTOList) {
            if(ShowTypeEnum.ITSELF.getKey() == bean.getShowType()) { //本体
                ids.add(bean.getId());
            }
        }
        for(SymptomSearchDTO bean : symptomSearchDTOList) {
            //如果是本体，直接返回
            if(ShowTypeEnum.ITSELF.getKey() == bean.getShowType()) {
                res.add(bean);
            } else {
                //如果是同义词,判断是否已添加，有多个同义词任意取一个
                if(!ids.contains(bean.getId())) {
                    ids.add(bean.getId());
                    res.add(bean);
                }
            }
        }
        return res;
    }

}
