package com.lantone.qc.dbanaly.lt.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.qc.dbanaly.lt.entity.Clinic;
import com.lantone.qc.dbanaly.lt.service.impl.ClinicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/7/17 11:30
 */
@Component
public class ClinicFacade extends ClinicServiceImpl {
    @Autowired
    ClinicFacade clinicFacade;
    @Autowired
    private LibraryInfoFacade libraryInfoFacade;

    public Map<Long, Clinic> getClinicMap() {
        QueryWrapper<Clinic> clinicQe = new QueryWrapper<>();
        clinicQe.eq("is_deleted", "N");
        clinicQe.isNotNull("concept_id");
        return list(clinicQe).stream().collect(Collectors.toMap(Clinic::getConceptId, i -> i));
    }

    public void getClinicInfo() {
        Map<Long, Map<String, Long>> conceptIdInfo = libraryInfoFacade.getConceptIdInfo();
        Map<String, Long> conceptIdMap = conceptIdInfo.get(1l);
        Map<Long, Clinic> clinicMap = getClinicMap();
        conceptIdMap.keySet().forEach(conceptName -> {
            clinicFacade.clinicCache(conceptName, clinicMap.get(conceptIdMap.get(conceptName)));
        });
    }

    @Cacheable(value = "clinicCache", key = "#clinicName", unless = "#result == null")
    public Clinic clinicCache(String clinicName, Clinic clinic) {
        return clinic;
    }

    @Cacheable(value = "clinicCache", key = "#clinicName", unless = "#result == null")
    public Clinic clinicCache(String clinicName) {
        QueryWrapper<Clinic> clinicQe = new QueryWrapper<>();
        clinicQe.eq("is_deleted", "N");
        clinicQe.isNotNull("concept_id");
        clinicQe.eq("name", clinicName);
        Clinic one = getOne(clinicQe, false);
        System.out.println(one);
        return one;
    }

}