package com.fjhx.victoriatourist.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
import com.fjhx.tenant.mapper.dict.DictTenantDataMapper;
import com.fjhx.tenant.service.dict.DictTenantDataService;
import com.ruoyi.framework.mybatis.holder.TenantHolder;

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

public class DictUtilsByOpen {




    private static final DictTenantDataService dictTenantDataService = SpringUtil.getBean(DictTenantDataService.class);
    private static final DictTenantDataMapper dictTenantDataMapper = SpringUtil.getBean(DictTenantDataMapper.class);


    public static Map<String, String> getDictMap(String code) {
        List<DictTenantDataVo> dictTenantDataServiceList = getDictList(code);
        Map<String, String> dictMap = new HashMap<>();
        if (ObjectUtil.isNotEmpty(dictTenantDataServiceList)) {
            dictMap = dictTenantDataServiceList.stream().collect(Collectors.toMap(DictTenantDataVo::getDictKey, DictTenantDataVo::getDictValue));
        }
        return dictMap;
    }

    /**
     * 获取字典map
     */
    public static List<DictTenantDataVo> getDictList(String code) {
        TenantHolder.setIgnore(true);

        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        DictTenantDataSelectDto dto = new DictTenantDataSelectDto();
        dto.setDictCode(code);
        dto.setTenantId("wdly");
        List<DictTenantDataVo> afterSalesType = dictTenantDataMapper.getList(dto);
        DynamicDataSourceContextHolder.poll();
        //代码块
        TenantHolder.clear();
        return  afterSalesType;
    }
}
