package com.zcxk.util;

import com.zcxk.client.UserCenterClient;
import com.zcxk.entity.Org;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class OrgInfoUtil {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserCenterClient userCenterClient;
    public  String getOrgName(Integer id){

        String tenantId = UserUtil.getCurrentUser().getTenantId();
        if(tenantId==null){
            return "";
        }
        String key="orgInfo" + tenantId;
        String keysValue = (String) redisUtil.getHashValues(key,id+"");
        if(keysValue==null){
            Map<String,String>keysValues=updateOrgInfo(key);
            keysValue=keysValues.get(id+"");
        }

        return keysValue;
    };
    public Map<String,String>getAllOrgName(){
        String tenantId = UserUtil.getCurrentUser().getTenantId();
        if(tenantId==null){
            return new HashMap<>();
        }
        String key="orgInfo" + tenantId;
        Map<String, String> hashValue = redisUtil.getHashValue(key);
        if(hashValue.size()==0){
            hashValue=updateOrgInfo(key);
        }
        return hashValue;
    }
    private Map<String,String> updateOrgInfo(String key){

        Map<String,String>keysValues=new HashMap<>();

        List<Org> orgs = userCenterClient.getAllByTenantId();
        for (Org org1 : orgs) {
            keysValues.put(org1.getId()+"",org1.getOrgName());
        }
        redisUtil.putHashValues(key,keysValues);
        return keysValues;
    }
}
