package cn.sh.stc.sict.theme.hphy.service.impl;

import cn.sh.stc.sict.theme.hphy.dao.HphyDoctorMapper;
import cn.sh.stc.sict.theme.hphy.model.HphyDoctor;
import cn.sh.stc.sict.theme.hphy.model.HphyGuideMatch;
import cn.sh.stc.sict.theme.hphy.model.HphyGuideRecord;
import cn.sh.stc.sict.theme.hphy.service.HphyDoctorService;
import cn.sh.stc.sict.theme.hphy.service.HphyGuideMatchService;
import cn.sh.stc.sict.theme.hphy.vo.DoctorMatchVO;
import cn.sh.stc.sict.theme.hphy.vo.PythonResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("hphyDoctorService")
@AllArgsConstructor
public class HphyDoctorServiceImpl extends ServiceImpl<HphyDoctorMapper, HphyDoctor> implements HphyDoctorService {

    private final HphyDoctorMapper hphyDoctorMapper;
    private final HphyGuideMatchService hphyGuideMatchService;

    @Override
    public List<DoctorMatchVO> getRecommendList(HphyGuideRecord guide, List<PythonResult> pythonList) {
        List<DoctorMatchVO> result = new ArrayList<>();
        Map<String, Double> matchMap = pythonList.stream().collect(Collectors.toMap(PythonResult::getDoctorId, PythonResult::getMatchScore));
        List<String> doctorIdList = pythonList.stream().map(PythonResult::getDoctorId).collect(Collectors.toList());
        List<HphyDoctor> list = this.listByIds(doctorIdList);
        List<HphyGuideMatch> matchList = new ArrayList<>();
        list.forEach(doctor -> {
            DoctorMatchVO vo = new DoctorMatchVO();
            vo.setDoctor(doctor);
            vo.setMatchScore(matchMap.getOrDefault(doctor.getId(), 85.0));
            result.add(vo);
        });
        // 保存医生推荐记录
        // hphyGuideMatchService.asyncSaveBatch(matchList);

        return result.stream()
                .sorted(Comparator.comparing(DoctorMatchVO::getMatchScore).reversed())
                .collect(Collectors.toList());
    }
}
