package com.lantone.security.facade.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.common.constant.AuthConstant;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.service.RedisService;
import com.lantone.common.util.StringUtil;
import com.lantone.security.entity.HospitalSet;
import com.lantone.security.service.impl.HospitalSetServiceImpl;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * <p>
 * 表名：sys_hospital_set 业务类
 * </p>
 */
@Component
public class HospitalSetFacade extends HospitalSetServiceImpl {

    @Autowired
    private RedisService redisService;
    @Autowired
    private HospitalSetFacade hospitalSetFacade;

    public Map<String, Map<String, Map<String, String>>> initHospitalSetMap() {
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<HospitalSet>()
                .eq("is_deleted", IsDeleteEnum.N.getKey());
        Map<String, Map<String, Map<String, String>>> hospitalSetMap = hospitalSetFacade.list(hospitalSetQueryWrapper)
                .stream()
                .collect(
                        Collectors.groupingBy(
                                i -> i.getHospitalId().toString(),
                                Collectors.groupingBy(
                                        HospitalSet::getCode,
                                        Collectors.toMap(
                                                HospitalSet::getName,
                                                HospitalSet::getVal
                                        )
                                )
                        )
                );
        redisService.del(AuthConstant.HOSPITAL_SET_MAP_KEY);
        redisService.set(AuthConstant.HOSPITAL_SET_MAP_KEY, hospitalSetMap);
        return hospitalSetMap;
    }
    /**
     * @param hospitalId 医院id
     * @param code  分组集合
     * @Description获取某医院code的内容
     * @Return java.util.Map<java.lang.String, java.util.Map < java.lang.String, java.lang.String>>
     */
    public Map<String, String> getHospitalCodeMap(String hospitalId, String code) {
        Map<String, Map<String, Map<String, String>>> hospitalSetMap = redisService.get(AuthConstant.HOSPITAL_SET_MAP_KEY);
        Map<String, Map<String, String>> codeMap = hospitalSetMap.get(hospitalId);
        if (MapUtils.isNotEmpty(codeMap) && StringUtil.isNotBlank(code)) {
            return codeMap.get(code);
        }
        return null;
    }
}