package com.yihu.figure.service;

import com.yihu.figure.dao.patient.PatientInfoDao;
import com.yihu.figure.dao.SuggestDao;
import com.yihu.figure.dao.patient.portrait.UserPortraitDao;
import com.yihu.figure.model.patient.PatientInfo;
import com.yihu.figure.model.patient.portrait.UserPortrait;
import com.yihu.figure.model.suggest.Suggest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by yeshijie on 2017/3/6.
 */
@Service
public class PatientInfoService extends BaseService {
    @Autowired
    private PatientInfoDao patientInfoDao;
    @Autowired
    private UserPortraitDao userPortraitDao;
    @Autowired
    private SuggestDao suggesdao;

    public PatientInfo getPatientInfo(String patientCode) {
        PatientInfo patient = patientInfoDao.findByCode(patientCode);
        //得到用户标签
        List<UserPortrait> userPortraits = userPortraitDao.findByPatientCode(patientCode);
        patient.setUserPortraits(userPortraits);
        //根据用户的标签得到建议
        Map<String, List<Suggest>> map = new HashMap<>();
        userPortraits.stream().forEach(u -> {
            List<Suggest> ss = suggesdao.findByKeyword("%" + u.getValue() + "%");
            ss.stream().forEach(s -> {
                if (map.containsKey(s.getTypeName())) {
                    map.get(s.getTypeName()).add(s);
                } else {
                    List<Suggest> suggests=new ArrayList<Suggest>();
                    suggests.add(s);
                    map.put(s.getTypeName(), suggests);
                }
            });
        });
        patient.setSuggests(map);
        return patient;
    }

    public List<PatientInfo> getPatientList() {
        List<PatientInfo>  pas=patientInfoDao.findByHasCode();
        return pas;
    }
}
